diff --git a/R/rain_db.R b/R/rain_db.R index 686c995..8a261b7 100644 --- a/R/rain_db.R +++ b/R/rain_db.R @@ -1439,6 +1439,26 @@ compute_plot_limits <- function(values) { } +get_metric_plot_y_label <- function( + metric_id, + view = c("raw", "daily", "weekly", "monthly") +) { + view <- match.arg(view) + metric <- get_weather_metric(metric_id) + + if (!identical(metric_id, "rain_6m") || identical(view, "raw")) { + return(sprintf("%s (%s)", metric$label, metric$unit)) + } + + switch( + view, + daily = "Daily rain total (mm)", + weekly = "Weekly rain total (mm)", + monthly = "Monthly rain total (mm)" + ) +} + + plot_cached_metric <- function( metric_data, metric_id, @@ -1457,7 +1477,7 @@ plot_cached_metric <- function( names(colours) <- station_names y_limits <- compute_plot_limits(metric_data$value_num) - y_label <- sprintf("%s (%s)", metric$label, metric$unit) + y_label <- get_metric_plot_y_label(metric_id = metric_id, view = view) if (view != "raw") { metric_data$observed_day <- as.Date(metric_data$observed_day) diff --git a/app.R b/app.R index 72f54cc..9c950d9 100644 --- a/app.R +++ b/app.R @@ -142,41 +142,6 @@ get_preferred_window_view <- function(window_unit = "days") { } -get_summary_aggregate <- function(window_unit = "days") { - switch( - window_unit, - days = "daily", - months = "weekly", - years = "monthly", - "daily" - ) -} - - -format_aggregate_label <- function(aggregate = c("raw", "daily", "weekly", "monthly")) { - aggregate <- match.arg(aggregate) - - switch( - aggregate, - raw = "Raw", - daily = "Daily", - weekly = "Weekly", - monthly = "Monthly" - ) -} - - -format_aggregate_period_label <- function(aggregate = c("daily", "weekly", "monthly")) { - aggregate <- match.arg(aggregate) - - switch( - aggregate, - daily = "Day", - weekly = "Week", - monthly = "Month" - ) -} - ui <- fluidPage( tags$head( tags$style(HTML(" @@ -280,9 +245,7 @@ ui <- fluidPage( div( class = "panel-card", h3(class = "panel-title", "Rain"), - withSpinner(plotOutput("rainPlot", height = "420px")), - h4(textOutput("summaryTitle", container = span)), - tableOutput("dailySummary") + withSpinner(plotOutput("rainPlot", height = "420px")) ) ), column( @@ -379,10 +342,6 @@ server <- function(input, output, session) { as.integer(Sys.Date() - selected_start_date()) + 1L }) - selected_summary_aggregate <- reactive({ - get_summary_aggregate(input$window_unit) - }) - cached_rain <- reactive({ data_version() @@ -408,18 +367,6 @@ server <- function(input, output, session) { ) }) - daily_summary <- reactive({ - data_version() - - query_cached_rainfall( - location_id = input$location_id, - start_date = selected_start_date(), - end_date = Sys.Date(), - aggregate = selected_summary_aggregate(), - db_path = db_path - ) - }) - metric_latest <- reactive({ data_version() @@ -434,13 +381,6 @@ server <- function(input, output, session) { selected_metric()$label }) - output$summaryTitle <- renderText({ - sprintf( - "%s rain totals", - format_aggregate_label(selected_summary_aggregate()) - ) - }) - output$syncControls <- renderUI({ if (is.null(api_headers)) { return( @@ -599,17 +539,6 @@ server <- function(input, output, session) { ) }) - output$dailySummary <- renderTable({ - summary_data <- daily_summary() - if (!nrow(summary_data)) { - return(NULL) - } - - period_label <- format_aggregate_period_label(selected_summary_aggregate()) - names(summary_data) <- c("Station ID", "Station", period_label, "Metric", "Label", "Unit", "Rain") - summary_data[, c("Station", period_label, "Rain")] - }, striped = TRUE, spacing = "s", digits = 2) - output$metricLatest <- renderTable({ latest_data <- metric_latest() if (!nrow(latest_data)) { diff --git a/tests/testthat/test-rain-db.R b/tests/testthat/test-rain-db.R index 1ab5422..8e4e88e 100644 --- a/tests/testthat/test-rain-db.R +++ b/tests/testthat/test-rain-db.R @@ -227,3 +227,12 @@ test_that("weekly and monthly rainfall aggregates collapse long windows", { expect_equal(as.character(monthly_rain$observed_day), c("2024-01-01", "2024-02-01")) expect_equal(monthly_rain$rain_mm, c(3, 3)) }) + + +test_that("rain plot labels reflect grouped totals", { + expect_equal(get_metric_plot_y_label("rain_6m", "raw"), "Rain (mm / 6 min)") + expect_equal(get_metric_plot_y_label("rain_6m", "daily"), "Daily rain total (mm)") + expect_equal(get_metric_plot_y_label("rain_6m", "weekly"), "Weekly rain total (mm)") + expect_equal(get_metric_plot_y_label("rain_6m", "monthly"), "Monthly rain total (mm)") + expect_equal(get_metric_plot_y_label("air_temperature", "weekly"), "Temperature (C)") +})