Mise en place API
This commit is contained in:
+39
-23
@@ -41,39 +41,55 @@ maj_ecow_for_all <- function() {
|
||||
}
|
||||
|
||||
mafonctiondetest <- function(cheptel){
|
||||
source("R/common/ws_client.R", local = TRUE)
|
||||
server_path <- "http://localhost:8080/HbcSchedulerAndServices/"
|
||||
url_active_by_chep <- paste0(server_path, "webresources/animals/findAnimalEcowByActiveCheptel/")
|
||||
maliste <- http_get(url = paste0(url_active_by_chep, cheptel))
|
||||
return(lengths(maliste))
|
||||
source(here::here("R/common/ws_client.R"))
|
||||
cheptel_ecow <- get_cheptel_ecow(cheptel)
|
||||
vaches <- cheptel_ecow$vaches
|
||||
return(length(vaches))
|
||||
}
|
||||
|
||||
cheptels <- list("FR71499477", "FR71499477635", "FR71499477")
|
||||
|
||||
#' Mise à jour de l'indicateur Ecow pour une liste de cheptels et gestion des erreurs
|
||||
#' @param list_cheptels list. Liste de numéros de cheptels avec le FR devant
|
||||
maj_ecow_for_list_cheptels <- function(list_cheptels){
|
||||
res <- lapply(cheptels, function(num_chep) {
|
||||
tryCatch(
|
||||
{
|
||||
message(sprintf("→ Traitement cheptel %s ...", num_chep))
|
||||
out <- mafonctiondetest(num_chep) #' TODO MAJ avec la fonction de calcul globale
|
||||
# out <- calcul_ecow_by_chep(num_chep) #' Appel de la fonction de calcul globale
|
||||
message(sprintf("✓ OK cheptel %s : %s lignes", num_chep, ifelse(is.data.frame(out), nrow(out), NA_integer_)))
|
||||
out
|
||||
},
|
||||
error = function(e) {
|
||||
# Log + continue
|
||||
message(sprintf("✗ ERREUR cheptel %s : %s", num_chep, conditionMessage(e)))
|
||||
NULL
|
||||
},
|
||||
total <- length(list_cheptels)
|
||||
start_time <- Sys.time()
|
||||
message(sprintf("🚀 Début traitement - %s cheptels à traiter", total))
|
||||
|
||||
res <- lapply(seq_along(list_cheptels), function(i) {
|
||||
num_chep <- list_cheptels[[i]]
|
||||
withCallingHandlers(
|
||||
tryCatch(
|
||||
{
|
||||
message(sprintf("[%s/%s] Traitement cheptel %s ...", i, total, num_chep))
|
||||
out <- calcul_ecow_by_chep(num_chep, 1) # Pour l'instant on s'arrête aux vaches
|
||||
message(sprintf("✓ OK cheptel %s", num_chep))
|
||||
out
|
||||
},
|
||||
error = function(e) {
|
||||
message(sprintf("✗ ERREUR cheptel %s : %s", num_chep, conditionMessage(e)))
|
||||
NULL
|
||||
}
|
||||
),
|
||||
warning = function(w) {
|
||||
# Tu peux décider de logger les warnings sans interrompre
|
||||
message(sprintf("! AVERTISSEMENT cheptel %s : %s", num_chep, conditionMessage(w)))
|
||||
invokeRestart("muffleWarning") # évite d’imprimer plusieurs fois le warning
|
||||
invokeRestart("muffleWarning")
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
end_time <- Sys.time()
|
||||
duration_sec <- round(as.numeric(difftime(end_time, start_time, units = "secs")), 2)
|
||||
duration_fmt <- format_duration(duration_sec)
|
||||
message(sprintf("🏁 Fin traitement - %s cheptels traités en %s", total, duration_fmt))
|
||||
|
||||
return(res)
|
||||
}
|
||||
|
||||
|
||||
format_duration <- function(seconds) {
|
||||
h <- seconds %/% 3600
|
||||
m <- (seconds %% 3600) %/% 60
|
||||
s <- seconds %% 60
|
||||
sprintf("%02d:%02d:%02d", h, m, s)
|
||||
}
|
||||
|
||||
save_data_vaches <- function(vaches){
|
||||
|
||||
Reference in New Issue
Block a user