Skip to content

Commit ea2beda

Browse files
authored
Merge pull request #665 from The-Strategy-Unit/more_v5_fixes
improves status logic
2 parents 9cc5502 + 5185d10 commit ea2beda

1 file changed

Lines changed: 69 additions & 56 deletions

File tree

R/mod_run_model_api_calls.R

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -109,69 +109,75 @@ mod_run_model_check_container_status <- function(
109109
promises::then(
110110
\(response) {
111111
res <- httr2::resp_body_json(response)
112+
if (is.null(res$status)) {
113+
res$status <- "unknown"
114+
}
112115

113-
state <- res$state %||% "unknown"
114-
if (state == "Terminated") {
115-
if ((res$detail_status %||% "Completed") == "Completed") {
116-
cat("model run success: ", id, "\n", sep = "")
117-
status("Success")
118-
} else {
119-
cat("model run error: ", id, "\n", res$error, "\n", sep = "")
120-
status(glue::glue("Error running the model ({id}): {res$error}"))
121-
}
116+
if (res$status == "complete") {
117+
cat("model run success: ", id, "\n", sep = "")
118+
status("Success")
122119
return(NULL)
123-
} else if (state %in% c("Creating", "unknown")) {
124-
# no need to change status
125-
} else {
120+
} else if (res$status == "error") {
121+
cat("model run error: ", id, "\n", res$error, "\n", sep = "")
122+
status(glue::glue("Error running the model ({id}): {res$error}"))
123+
return(NULL)
124+
} else if (res$status == "submitted") {
125+
# do not do anything just yet
126+
} else if (res$status == "running") {
126127
progress <- res$complete %||%
127128
list(Inpatients = 0, Outpatients = 0, AaE = 0)
128129
model_runs <- res$model_runs
129130

130-
if (is.null(progress)) {
131-
cat(
132-
"model run id: ",
133-
id,
134-
", stage: saving results\n",
135-
sep = ""
136-
)
137-
status("Model running [saving results]")
131+
if (progress[["Inpatients"]] < model_runs) {
132+
stage <- "Inpatients"
133+
complete <- progress[["Inpatients"]]
134+
} else if (progress[["Outpatients"]] < model_runs) {
135+
stage <- "Outpatients"
136+
complete <- progress[["Outpatients"]]
137+
} else if (progress[["AaE"]] < model_runs) {
138+
stage <- "A&E"
139+
complete <- progress[["AaE"]]
138140
} else {
139-
if (
140-
progress[["AaE"]] > 0 || progress[["Outpatients"]] >= model_runs
141-
) {
142-
stage <- "A&E"
143-
complete <- progress[["AaE"]]
144-
} else if (
145-
progress[["Outpatients"]] > 0 ||
146-
progress[["Inpatients"]] >= model_runs
147-
) {
148-
stage <- "Outpatients"
149-
complete <- progress[["Outpatients"]]
150-
} else {
151-
stage <- "Inpatients"
152-
complete <- progress[["Inpatients"]]
153-
}
154-
pcnt <- scales::percent(complete / model_runs, 0.1)
155-
156-
cat(
157-
"model run id: ",
158-
id,
159-
", stage: ",
160-
stage,
161-
" progress: ",
162-
complete,
163-
"/",
164-
model_runs,
165-
" (",
166-
pcnt,
167-
")\n",
168-
sep = ""
169-
)
170-
171-
status(glue::glue(
172-
"Model Running [{stage}: {complete}/{model_runs} ({pcnt})]"
173-
))
141+
stage <- "Saving Results"
142+
complete <- 0
174143
}
144+
145+
pcnt <- scales::percent(complete / model_runs, 0.1)
146+
147+
cat(
148+
"model run id: ",
149+
id,
150+
", stage: ",
151+
stage,
152+
" progress: ",
153+
complete,
154+
"/",
155+
model_runs,
156+
" (",
157+
pcnt,
158+
")\n",
159+
sep = ""
160+
)
161+
162+
status(glue::glue(
163+
"Model Running [{stage}: {complete}/{model_runs} ({pcnt})]"
164+
))
165+
} else {
166+
cat(
167+
"unknown status for model run id: ",
168+
id,
169+
" - ",
170+
res$status,
171+
"\n",
172+
sep = ""
173+
)
174+
# recursive call, but reduce error counter since this is unexpected
175+
return(mod_run_model_check_container_status(
176+
dataset,
177+
model_run_id,
178+
status,
179+
error_counter - 1
180+
))
175181
}
176182

177183
# recursive call
@@ -180,7 +186,14 @@ mod_run_model_check_container_status <- function(
180186
) |>
181187
promises::catch(
182188
\(error) {
183-
cat("error:", error$message, "\n")
189+
cat(
190+
"error: ",
191+
error$message,
192+
" [error counter: ",
193+
error_counter,
194+
"]\n",
195+
sep = ""
196+
)
184197
# recursive call
185198
mod_run_model_check_container_status(
186199
dataset,

0 commit comments

Comments
 (0)