Spaces:
Running
Running
fix: improve midnight date rollover logic to handle stale container sessions and URL initialization
Browse files
server.R
CHANGED
|
@@ -37,33 +37,38 @@ server <- function(input, output, session) {
|
|
| 37 |
)
|
| 38 |
|
| 39 |
# --- Midnight Rollover: keep end date current across days ---
|
| 40 |
-
#
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
# Poll every hour
|
| 44 |
day_tick <- reactiveTimer(3600000)
|
| 45 |
|
| 46 |
observe({
|
| 47 |
day_tick() # re-fire every hour
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
last_applied_today(today)
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
current_end <- input$date_range[2]
|
| 57 |
if (is.null(current_end) || is.na(current_end)) return()
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 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",
|
|
|
|
| 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)
|