From 9e23fe06b42bf92663b16fc312a9f137e861bf18 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Mon, 11 Dec 2023 17:52:36 +0100 Subject: [PATCH 1/6] Fix getJobs to delete duplicated lines --- R/API.R | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/R/API.R b/R/API.R index ea041342..a6ac8a17 100644 --- a/R/API.R +++ b/R/API.R @@ -220,9 +220,28 @@ getJobs <- function(job_id = NULL, opts = antaresRead::simOptions()) { stop("getJobs can only be used with Antares API.", call. = FALSE) if (is.null(job_id)) { jobs <- api_get(opts = opts, "launcher/jobs", default_endpoint = "v1") - suppressWarnings(data.table::rbindlist(jobs, fill = TRUE)) + + # fix duplicated raw cause multiple elements for "owner" + jobs <- lapply(jobs, + rename_floor_list, + target_name = "owner") + + jobs <- suppressWarnings(data.table::rbindlist(jobs, fill = TRUE)) + + add_col_names <- c("owner_id", "owner_name") + if(all(add_col_names %in% names(jobs))){ + names_to_keep = setdiff(names(jobs), + add_col_names) + names_to_keep <- append(names_to_keep, add_col_names) + setcolorder(jobs, names_to_keep) + }else + suppressWarnings(data.table::rbindlist(jobs, fill = TRUE)) } else { jobs <- api_get(opts = opts, paste0("launcher/jobs/", job_id), default_endpoint = "v1") + + # fix duplicated raw cause multiple elements for "owner" + jobs <- rename_floor_list(target_name = "owner", + list_to_reforge = jobs) data.table::as.data.table(jobs) } } From 2e8ed4a0ef1cdae6bc0b401420424ab8968e11d2 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Mon, 11 Dec 2023 17:53:20 +0100 Subject: [PATCH 2/6] add private function "rename_floor_list()" to "utils.R" --- R/utils.R | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index ef021765..e16ced8c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -86,5 +86,28 @@ update_opts <- function(opts) { } } - +# reorder a list into a simple named list +# target a named list within the list, then move items up one step in the main list +rename_floor_list <- function(target_name, list_to_reforge){ + assertthat::assert_that(inherits(target_name, "character")) + assertthat::assert_that(inherits(list_to_reforge, "list")) + + if(target_name %in% names(list_to_reforge)){ + if(class(list_to_reforge[[target_name]]) %in% "list"){ + target_elements <- list_to_reforge[[target_name]] + names(target_elements) <- paste(target_name, names(target_elements), sep = "_") + + # overwrite list + list_to_reforge[[target_name]] <- NULL + list_to_reforge <- append(list_to_reforge, target_elements) + + return(list_to_reforge) + }else{ + list_to_reforge[[target_name]] <- NULL + return(list_to_reforge) + } + + }else + return(list_to_reforge) +} From 0f3588f378a31da4210b6e257242e7945934233f Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Tue, 12 Dec 2023 15:05:28 +0100 Subject: [PATCH 3/6] writeHydroValues() rewrite documentation latex syntax ... --- R/writeHydroValues.R | 12 ++++++------ man/writeHydroValues.Rd | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/R/writeHydroValues.R b/R/writeHydroValues.R index 6d24678f..d39b79ff 100644 --- a/R/writeHydroValues.R +++ b/R/writeHydroValues.R @@ -9,16 +9,16 @@ #' @param type Type of hydro file, it can be "waterValues", "reservoir", "maxpower", "inflowPattern" or "creditmodulations". #' @param data The data must have specific dimension depending on the type of file : #' \itemize{ -#' \item{waterValues}{: a 365x101 numeric matrix: +#' \item \code{waterValues}: a 365x101 numeric matrix: #' marginal values for the stored energy based on date (365 days) #' and on the reservoir level (101 round percentage values ranging from #' 0% to 100%). OR a 3-column matrix with 365x101 rows. In this latter case the 3 columns must #' be 'date', 'level' and 'value' (in this order), and the rows must be sorted by: -#' ascending day, ascending level.} -#' \item{reservoir}{: a 365x3 numeric matrix. The columns contains respectively the levels min, avg and max.} -#' \item{maxpower}{: a 365x4 numeric matrix.} -#' \item{inflowPattern}{: a 365x1 numeric matrix.} -#' \item{creditmodulations}{: a 2x101 numeric matrix.} +#' ascending day, ascending level. +#' \item \code{reservoir}: a 365x3 numeric matrix. The columns contains respectively the levels min, avg and max. +#' \item \code{maxpower}: a 365x4 numeric matrix. +#' \item \code{inflowPattern}: a 365x1 numeric matrix. +#' \item \code{creditmodulations}: a 2x101 numeric matrix. #' } #' #' @param overwrite Logical. Overwrite the values if a file already exists. diff --git a/man/writeHydroValues.Rd b/man/writeHydroValues.Rd index 2ffb0615..e2cc7b0d 100644 --- a/man/writeHydroValues.Rd +++ b/man/writeHydroValues.Rd @@ -19,16 +19,16 @@ writeHydroValues( \item{data}{The data must have specific dimension depending on the type of file : \itemize{ -\item{waterValues}{: a 365x101 numeric matrix: +\item \code{waterValues}: a 365x101 numeric matrix: marginal values for the stored energy based on date (365 days) and on the reservoir level (101 round percentage values ranging from 0\% to 100\%). OR a 3-column matrix with 365x101 rows. In this latter case the 3 columns must be 'date', 'level' and 'value' (in this order), and the rows must be sorted by: -ascending day, ascending level.} -\item{reservoir}{: a 365x3 numeric matrix. The columns contains respectively the levels min, avg and max.} -\item{maxpower}{: a 365x4 numeric matrix.} -\item{inflowPattern}{: a 365x1 numeric matrix.} -\item{creditmodulations}{: a 2x101 numeric matrix.} +ascending day, ascending level. +\item \code{reservoir}: a 365x3 numeric matrix. The columns contains respectively the levels min, avg and max. +\item \code{maxpower}: a 365x4 numeric matrix. +\item \code{inflowPattern}: a 365x1 numeric matrix. +\item \code{creditmodulations}: a 2x101 numeric matrix. }} \item{overwrite}{Logical. Overwrite the values if a file already exists.} From ee0afa3c8dd84cfcbc3418b6531aea3ec1cba1f6 Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Fri, 23 Feb 2024 18:06:45 +0100 Subject: [PATCH 4/6] reforge to use api_get with new parameters to parse in text --- DESCRIPTION | 5 ++++- R/API.R | 56 ++++++++++++++++++++++++++------------------------ man/getJobs.Rd | 2 +- 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5ec1caad..5bc22d83 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -27,7 +27,8 @@ BugReports: https://github.com/rte-antares-rpackage/antaresEditObject/issues Encoding: UTF-8 RoxygenNote: 7.2.2 Roxygen: list(markdown = TRUE) -Depends: antaresRead (>= 2.4.2) +Depends: + antaresRead (>= 2.4.2) Imports: assertthat, cli, @@ -51,3 +52,5 @@ Suggests: knitr, rmarkdown VignetteBuilder: knitr +Remotes: + rte-antares-rpackage/antaresRead@Ant946/getjobs diff --git a/R/API.R b/R/API.R index a6ac8a17..78d1a55f 100644 --- a/R/API.R +++ b/R/API.R @@ -202,7 +202,7 @@ useVariant <- function(name, variant_id = NULL, opts = antaresRead::simOptions() #' @param job_id The job identifier, if `NULL` (default), retrieve all jobs. #' @template opts-arg #' -#' @return A `data.table` with information about jobs. +#' @return A `data.frame` with information about jobs. #' @export #' #' @importFrom data.table rbindlist @@ -218,32 +218,34 @@ getJobs <- function(job_id = NULL, opts = antaresRead::simOptions()) { assertthat::assert_that(inherits(opts, "simOptions")) if (!is_api_study(opts)) stop("getJobs can only be used with Antares API.", call. = FALSE) - if (is.null(job_id)) { - jobs <- api_get(opts = opts, "launcher/jobs", default_endpoint = "v1") - - # fix duplicated raw cause multiple elements for "owner" - jobs <- lapply(jobs, - rename_floor_list, - target_name = "owner") - - jobs <- suppressWarnings(data.table::rbindlist(jobs, fill = TRUE)) - - add_col_names <- c("owner_id", "owner_name") - if(all(add_col_names %in% names(jobs))){ - names_to_keep = setdiff(names(jobs), - add_col_names) - names_to_keep <- append(names_to_keep, add_col_names) - setcolorder(jobs, names_to_keep) - }else - suppressWarnings(data.table::rbindlist(jobs, fill = TRUE)) - } else { - jobs <- api_get(opts = opts, paste0("launcher/jobs/", job_id), default_endpoint = "v1") - - # fix duplicated raw cause multiple elements for "owner" - jobs <- rename_floor_list(target_name = "owner", - list_to_reforge = jobs) - data.table::as.data.table(jobs) - } + if (is.null(job_id)) + .getjobs(opts = opts) + else + .getjobs(opts = opts, + id_job = job_id) +} + +.getjobs <- function(opts, id_job=NULL){ + # make endpoint with id or not + if(is.null(id_job)) + custom_endpoint <- file.path("launcher", "jobs") + else + custom_endpoint <- file.path("launcher", "jobs", id_job) + # api call with text content + jobs <- api_get(opts = opts, + endpoint = custom_endpoint, + default_endpoint = "v1", + parse_result = "text") + # reformat + if(!is.null(id_job)) + jobs <- paste0("[",jobs,"]") + # as DT to reformat names + jobs <- as.data.table( + jsonlite::fromJSON(jobs)) + names(jobs) <- sub(pattern = "\\.", + replacement = "_", + x = names(jobs)) + return(jobs) } diff --git a/man/getJobs.Rd b/man/getJobs.Rd index 74d6c2be..dc6d0656 100644 --- a/man/getJobs.Rd +++ b/man/getJobs.Rd @@ -13,7 +13,7 @@ getJobs(job_id = NULL, opts = antaresRead::simOptions()) \code{\link[antaresRead:setSimulationPath]{antaresRead::setSimulationPath()}}} } \value{ -A \code{data.table} with information about jobs. +A \code{data.frame} with information about jobs. } \description{ Retrieve API jobs From a19bdff9f93a15ea6ded86ae41f7bb20fead330a Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Mon, 26 Feb 2024 11:35:35 +0100 Subject: [PATCH 5/6] update new.md + doc --- NEWS.md | 1 + R/API.R | 2 +- man/getJobs.Rd | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index c13cdf67..3c7d021c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -16,6 +16,7 @@ BUGFIXES : * Fix `setPlaylist()` works in API and local mode with weights. * Fix `getPlaylist()` works in API and local mode with weights. * Fix `createDSR()` in API mode : daily binding constraint takes 366 rows +* `getJobs()` no longer returns duplicates and displays the two new columns `owner_id` and `owner_name`. # antaresEditObject 0.6.1 diff --git a/R/API.R b/R/API.R index 78d1a55f..75664693 100644 --- a/R/API.R +++ b/R/API.R @@ -202,7 +202,7 @@ useVariant <- function(name, variant_id = NULL, opts = antaresRead::simOptions() #' @param job_id The job identifier, if `NULL` (default), retrieve all jobs. #' @template opts-arg #' -#' @return A `data.frame` with information about jobs. +#' @return A `data.table` with information about jobs. #' @export #' #' @importFrom data.table rbindlist diff --git a/man/getJobs.Rd b/man/getJobs.Rd index dc6d0656..74d6c2be 100644 --- a/man/getJobs.Rd +++ b/man/getJobs.Rd @@ -13,7 +13,7 @@ getJobs(job_id = NULL, opts = antaresRead::simOptions()) \code{\link[antaresRead:setSimulationPath]{antaresRead::setSimulationPath()}}} } \value{ -A \code{data.frame} with information about jobs. +A \code{data.table} with information about jobs. } \description{ Retrieve API jobs From d86760695061c232707aaabcd0dd49280182855e Mon Sep 17 00:00:00 2001 From: "BERTHET Clement (Externe)" Date: Tue, 27 Feb 2024 11:02:57 +0100 Subject: [PATCH 6/6] delete reference dev branch ci/cd --- DESCRIPTION | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5bc22d83..79acffbf 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -52,5 +52,4 @@ Suggests: knitr, rmarkdown VignetteBuilder: knitr -Remotes: - rte-antares-rpackage/antaresRead@Ant946/getjobs +