alexdum commited on
Commit
175a7fe
·
1 Parent(s): e90b2ff

fix: improve midnight date rollover logic to handle stale container sessions and URL initialization

Browse files
Files changed (1) hide show
  1. server.R +28 -14
server.R CHANGED
@@ -37,33 +37,38 @@ server <- function(input, output, session) {
37
  )
38
 
39
  # --- Midnight Rollover: keep end date current across days ---
40
- # Track the last "today" that was applied so we only update once per day
41
- last_applied_today <- reactiveVal(default_end_date)
 
 
42
 
43
- # Poll every hour; cheap – only a date comparison
44
  day_tick <- reactiveTimer(3600000)
45
 
46
  observe({
47
  day_tick() # re-fire every hour
48
- today <- Sys.Date()
49
- if (today <= last_applied_today()) return() # still the same day
50
 
51
- # Day has rolled over
52
- last_applied_today(today)
53
 
54
- # Only auto-advance if the user's current end date matches the previous
55
- # "today" (i.e. they haven't manually picked a custom end date)
56
- current_end <- input$date_range[2]
57
  if (is.null(current_end) || is.na(current_end)) return()
58
- previous_today <- today - 1L
59
- if (current_end != previous_today) return() # user chose a custom date
 
60
 
61
  # Maintain the same window width (start -> end distance)
62
- current_start <- input$date_range[1]
63
  window_days <- as.numeric(difftime(current_end, current_start, units = "days"))
64
  new_end <- today
65
  new_start <- new_end - window_days
66
 
 
 
 
 
67
  updateDateRangeInput(session, "date_range",
68
  start = new_start, end = new_end,
69
  max = today
@@ -195,13 +200,22 @@ server <- function(input, output, session) {
195
  }
196
 
197
  # Apply date range
 
198
  if (!is.null(params$start) && !is.null(params$end)) {
199
  start_date <- tryCatch(as.Date(params$start), error = function(e) NULL)
200
  end_date <- tryCatch(as.Date(params$end), error = function(e) NULL)
201
  if (!is.null(start_date) && !is.null(end_date)) {
202
- updateDateRangeInput(session, "date_range", start = start_date, end = end_date)
 
203
  prev_dates(c(start_date, end_date))
204
  }
 
 
 
 
 
 
 
205
  }
206
 
207
  # Apply station selection (deferred to allow inputs to update)
 
37
  )
38
 
39
  # --- Midnight Rollover: keep end date current across days ---
40
+ # The default_end_date in global.R is frozen at container startup.
41
+ # This handles TWO scenarios:
42
+ # A) New session opened days after container start → fix on session init
43
+ # B) Long-lived session staying open past midnight → fix via hourly poll
44
 
45
+ # Poll every hour for scenario B
46
  day_tick <- reactiveTimer(3600000)
47
 
48
  observe({
49
  day_tick() # re-fire every hour
50
+ # Also depend on url_initialized so we fire once on session start
51
+ req(url_initialized())
52
 
53
+ today <- Sys.Date()
 
54
 
55
+ # Read current end date from the widget, coerce explicitly to Date
56
+ current_end <- tryCatch(as.Date(input$date_range[2]), error = function(e) NA)
 
57
  if (is.null(current_end) || is.na(current_end)) return()
58
+
59
+ # Only auto-advance if the end date is in the past (stale)
60
+ if (current_end >= today) return()
61
 
62
  # Maintain the same window width (start -> end distance)
63
+ current_start <- tryCatch(as.Date(input$date_range[1]), error = function(e) today - 366)
64
  window_days <- as.numeric(difftime(current_end, current_start, units = "days"))
65
  new_end <- today
66
  new_start <- new_end - window_days
67
 
68
+ message("[midnight-rollover] Updating date range: ",
69
+ current_end, " -> ", new_end,
70
+ " (start: ", current_start, " -> ", new_start, ")")
71
+
72
  updateDateRangeInput(session, "date_range",
73
  start = new_start, end = new_end,
74
  max = today
 
200
  }
201
 
202
  # Apply date range
203
+ today <- Sys.Date()
204
  if (!is.null(params$start) && !is.null(params$end)) {
205
  start_date <- tryCatch(as.Date(params$start), error = function(e) NULL)
206
  end_date <- tryCatch(as.Date(params$end), error = function(e) NULL)
207
  if (!is.null(start_date) && !is.null(end_date)) {
208
+ updateDateRangeInput(session, "date_range",
209
+ start = start_date, end = end_date, max = today)
210
  prev_dates(c(start_date, end_date))
211
  }
212
+ } else {
213
+ # No URL dates — use live Sys.Date() (default_end_date may be stale)
214
+ new_end <- today
215
+ new_start <- new_end - 366
216
+ updateDateRangeInput(session, "date_range",
217
+ start = new_start, end = new_end, max = today)
218
+ prev_dates(c(new_start, new_end))
219
  }
220
 
221
  # Apply station selection (deferred to allow inputs to update)