alexdum commited on
Commit
d56db11
·
1 Parent(s): 6362620

feat: Implement caching for enriched station data loading and update map tooltip variable.

Browse files
Files changed (2) hide show
  1. fun/map_helpers.R +1 -1
  2. global.R +23 -5
fun/map_helpers.R CHANGED
@@ -30,6 +30,6 @@ highlight_selected_station <- function(proxy, lng, lat, label_html, move_map = T
30
  circle_stroke_color = "red",
31
  circle_stroke_width = 3,
32
  circle_opacity = 0.4,
33
- tooltip = label_html
34
  )
35
  }
 
30
  circle_stroke_color = "red",
31
  circle_stroke_width = 3,
32
  circle_opacity = 0.4,
33
+ tooltip = "popup_content"
34
  )
35
  }
global.R CHANGED
@@ -307,6 +307,26 @@ get_dwd_stations <- function(resolution = "hourly") {
307
  #' @param resolution "hourly", "daily", or "monthly"
308
  #' @return Data frame of enriched stations
309
  load_enriched_stations <- function(resolution = "daily") {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310
  # 1. Get base station list (name, lat, lon, state, etc.)
311
  st_df <- get_dwd_stations(resolution)
312
  if (is.null(st_df) || nrow(st_df) == 0) {
@@ -482,13 +502,11 @@ load_enriched_stations <- function(resolution = "daily") {
482
  available_params = ifelse(is.na(available_params) | available_params == "", "Unknown", available_params)
483
  )
484
 
485
- as.data.frame(st_df)
 
 
486
  }
487
 
488
- # --- Initial Load (Daily Default) ---
489
- # Load enriched stations from pre-generated cache
490
- stations <- load_enriched_stations("daily")
491
-
492
  # --- App Defaults ---
493
  # Default to last year of available data (usually today)
494
  max_year_data <- year(Sys.Date())
 
307
  #' @param resolution "hourly", "daily", or "monthly"
308
  #' @return Data frame of enriched stations
309
  load_enriched_stations <- function(resolution = "daily") {
310
+ # Check for Enriched Cache
311
+ suffix <- if (resolution == "daily") "_daily" else if (resolution == "monthly") "_monthly" else ""
312
+ cache_file <- paste0("www/tabs/dwd_stations_enriched", suffix, ".rds")
313
+
314
+ if (file.exists(cache_file)) {
315
+ info <- file.info(cache_file)
316
+ if (difftime(Sys.time(), info$mtime, units = "days") <= cache_ttl_days) {
317
+ # Verify it's a valid data frame
318
+ tryCatch(
319
+ {
320
+ res <- readRDS(cache_file)
321
+ if (is.data.frame(res) && nrow(res) > 0) {
322
+ return(res)
323
+ }
324
+ },
325
+ error = function(e) message("Enriched cache corrupted, rebuilding...")
326
+ )
327
+ }
328
+ }
329
+
330
  # 1. Get base station list (name, lat, lon, state, etc.)
331
  st_df <- get_dwd_stations(resolution)
332
  if (is.null(st_df) || nrow(st_df) == 0) {
 
502
  available_params = ifelse(is.na(available_params) | available_params == "", "Unknown", available_params)
503
  )
504
 
505
+ res <- as.data.frame(st_df)
506
+ saveRDS(res, cache_file)
507
+ res
508
  }
509
 
 
 
 
 
510
  # --- App Defaults ---
511
  # Default to last year of available data (usually today)
512
  max_year_data <- year(Sys.Date())