From e873cf0e97ccb78e8426ca8f8148b41367c66d95 Mon Sep 17 00:00:00 2001 From: KKamel67 <58913912+KKamel67@users.noreply.github.com> Date: Wed, 17 Jan 2024 17:36:43 +0100 Subject: [PATCH] setPlaylist() in API mode optimization (#121) * Add new contributor * Add changelog * Add unit test for setPlaylist() * Update setPlaylist() to minimize the number of queries in API mode * Breaking changes in the right section * Add elements for the changelog * Refresh opts object at the end of the function * Add unit test to check that opts is refreshed --------- Co-authored-by: kemihak --- DESCRIPTION | 1 + NEWS.md | 7 +++++- R/playlist.R | 41 +++++++++++++++++++++++++++------- tests/testthat/test-playlist.R | 34 ++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 9 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5ea9ef4e..5ec1caad 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -15,6 +15,7 @@ Authors@R: c( person("Assil", "Mansouri", role = "ctb"), person("Abdallah", "Mahoudi", role = "ctb"), person("Clement", "Berthet", role = "ctb"), + person("Kamel", "Kemiha", role = "ctb"), person("Nicolas", "Boitard", role = "ctb"), person("RTE", role = "cph")) Description: Edit an 'Antares' simulation before running it : create new areas, links, thermal diff --git a/NEWS.md b/NEWS.md index aa3e25d0..ad1f2857 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,12 +4,17 @@ NEW FEATURES : * Complete function `deleteStudy()` with new parameter `simulation` to delete a simulation in an Antares study +### Breaking changes + +* `setPlaylist()` optimized for the API mode + - returned an updated list of simulation parameters returned by the function `setSimulationPath()` and `setSimulationPathAPI()` + # antaresEditObject 0.6.1 -### Breaking changes * `writeInputTS()` allows the user to set a link with the separator ' - ' (ex: 'area1 - area2') + BUGFIXES : * Error CRAN CHECKS (fix issue #115) diff --git a/R/playlist.R b/R/playlist.R index c7283b12..f58d6489 100644 --- a/R/playlist.R +++ b/R/playlist.R @@ -151,6 +151,8 @@ setPlaylist <- function(playlist, weights = NULL, opts = antaresRead::simOptions()) { + api_study <- is_api_study(opts) + version_study <- substr(opts$antaresVersion, 1, 1) version_study <- as.numeric(version_study) @@ -181,9 +183,11 @@ setPlaylist <- function(playlist, if (length(playlist) == length(mc_years)) { # update line to disable the playlist generaldata$general$`user-playlist` <- FALSE - # write updated file - writeIni(listData = generaldata, pathIni = "settings/generaldata", overwrite = TRUE, opts = opts) - + if (api_study) { + # To minimize the number of queries, we reduce the list to the updated items + generaldata <- generaldata[which(names(generaldata) == "general")] + generaldata$general[which(names(generaldata$general) != "user-playlist")] <- NULL + } } else { # otherwise, set the playlist # update line to enable the playlist generaldata$general$`user-playlist` <- TRUE @@ -193,7 +197,7 @@ setPlaylist <- function(playlist, # create new playlist (+ patch double to integer) new_playlist <- setNames(as.list(sort(as.integer(playlist - 1))), rep("playlist_year +", length(playlist))) - if (opts$typeLoad == "api"){ + if (api_study){ new_playlist$sep <- ", " new_playlist <- list("playlist_year +" = paste0("[", do.call(paste, new_playlist), "]")) } @@ -215,11 +219,32 @@ setPlaylist <- function(playlist, ), rep("playlist_year_weight", length(weights))) ) } - + if (api_study) { + # To minimize the number of queries, we reduce the list to the updated items + generaldata <- generaldata[which(names(generaldata) == "general")] + generaldata$general[which(names(generaldata$general) != "user-playlist")] <- NULL + } # add new playlist to the parameters description generaldata$playlist <- new_playlist - - # write updated file - writeIni(listData = generaldata, pathIni = "settings/generaldata", overwrite = TRUE, opts = opts) } + + # write updated file + writeIni(listData = generaldata, pathIni = "settings/generaldata", overwrite = TRUE, opts = opts) + + # Update simulation options object + if(api_study){ + suppressWarnings( + res <- antaresRead::setSimulationPathAPI(host = opts$host, + study_id = opts$study_id, + token = opts$token, + simulation = "input") + ) + }else{ + suppressWarnings( + res <- antaresRead::setSimulationPath(path = opts$studyPath, + simulation = "input") + ) + } + + return(invisible(res)) } diff --git a/tests/testthat/test-playlist.R b/tests/testthat/test-playlist.R index 066c9fbd..6e250b39 100644 --- a/tests/testthat/test-playlist.R +++ b/tests/testthat/test-playlist.R @@ -28,3 +28,37 @@ sapply(studies, function(study) { unlink(x = file.path(pathstd, "test_case"), recursive = TRUE) }) + + +test_that("Check if setPlaylist() is disabled when playlist = mcYear and enabled when playlist is customized", { + + ant_version <- "8.2.0" + st_test <- paste0("my_study_820_", paste0(sample(letters,5),collapse = "")) + suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version)) + + # Default behaviour + expect_false(opts$parameters$general$`user-playlist`) + + # Set parameters + updateGeneralSettings(nbyears = 10) + suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input")) + nbyears <- opts$parameters$general$nbyears + mc_years <- seq(nbyears) + + # With custom playlist - user-playlist is enabled + playlist_to_write <- sort(sample(mc_years, size = nbyears/2, replace = FALSE)) + opts <- setPlaylist(playlist = playlist_to_write, opts = opts) + playlist_in_file <- getPlaylist(opts = opts) + + expect_true(opts$parameters$general$`user-playlist`) + playlist_opts <- opts$parameters$playlist + playlist_opts <- playlist_opts[which(names(playlist_opts) == "playlist_year +")] + expect_true(all(unlist(playlist_opts) %in% (playlist_to_write - 1))) + expect_equal(playlist_to_write, playlist_in_file) + + # No playlist if length(playlist) == length(mcYear) - user-playlist is disabled + setPlaylist(playlist = mc_years, opts = opts) + suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input")) + + expect_false(opts$parameters$general$`user-playlist`) +})