Skip to content

Commit

Permalink
Ant721/delete simulation results with api (#119)
Browse files Browse the repository at this point in the history
* Add removeOutput() to delete a simulation

* Merge removeOutput() and deleteStudy()

* deleteStudy() : add API condition

* deleteStudy() : no opts output
  • Loading branch information
boitardn authored Jan 16, 2024
1 parent 6e4020b commit a416eaf
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 26 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: antaresEditObject
Type: Package
Title: Edit an 'Antares' Simulation
Version: 0.6.1
Version: 0.6.2
Authors@R: c(
person("Tatiana", "Vargas", email = "tatiana.vargas@rte-france.com", role = c("aut", "cre")),
person("Frederic", "Breant", role = "aut"),
Expand All @@ -15,6 +15,7 @@ Authors@R: c(
person("Assil", "Mansouri", role = "ctb"),
person("Abdallah", "Mahoudi", role = "ctb"),
person("Clement", "Berthet", role = "ctb"),
person("Nicolas", "Boitard", role = "ctb"),
person("RTE", role = "cph"))
Description: Edit an 'Antares' simulation before running it : create new areas, links, thermal
clusters or binding constraints or edit existing ones. Update 'Antares' general & optimization settings.
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# antaresEditObject 0.6.2

NEW FEATURES :

* Complete function `deleteStudy()` with new parameter `simulation` to delete a simulation in an Antares study

# antaresEditObject 0.6.1

### Breaking changes
Expand Down
71 changes: 49 additions & 22 deletions R/createStudy.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,32 +112,59 @@ createStudyAPI <- function(host, token = NULL, study_name = "my_study", antares_
invisible(opts)
}


#' @title Delete a study
#' @title Delete a study or a simulation
#'
#' @param opts List. study options
#' @param prompt_validation `logical` to put validation message to delete study (default `FALSE`)
#'
#' @param simulation simulation to be deleted (default `NULL`)
#' @importFrom antaresRead api_delete
#' @export
deleteStudy <- function(opts = simOptions(), prompt_validation= FALSE){
deleteStudy <- function(opts = simOptions(), prompt_validation = FALSE, simulation = NULL){

delete_simulation <- !is.null(simulation)
is_api_study <- is_api_study(opts)

if(!is_api_study && !file.exists(opts$studyPath))
stop("Study not found.")

if(prompt_validation){
prompt_question <- sprintf("Are you sure you want to delete the study : %s (%s)?",
ifelse(opts$typeLoad == "api",
opts$study_id,
opts$studyPath),
opts$studyName)
prompt_answer <- menu(c("Yes", "No"),
title=prompt_question)
if (prompt_answer == 2)
return()
if(delete_simulation){
prompt_question <- sprintf("Are you sure you want to delete the simulation : %s ?",
simulation)
} else {
prompt_question <- sprintf("Are you sure you want to delete the study : %s (%s)?",
ifelse(opts$typeLoad == "api",
opts$study_id,
opts$studyPath),
opts$studyName)
}

prompt_answer <- menu(c("Yes", "No"),title=prompt_question)
if(prompt_answer == 2)
return(NULL)
}
if(opts$typeLoad == "api")
api_delete(opts = opts, endpoint = opts$study_id)
else{
if(file.exists(opts$studyPath))
unlink(opts$studyPath, recursive = TRUE)
else
stop("Study not found.")
}
cat("\nStudy successfully deleted")

if(is_api_study){
if(delete_simulation){
study_path <- gsub(pattern = "\\/raw\\?path=",
replacement = "",
x = opts$studyPath)
url <- I(file.path(study_path,"outputs",simulation))
} else {
url <- opts$study_id
}
api_delete(opts = opts, endpoint = url)

} else {
path <- opts$studyPath
if(delete_simulation)
path <- file.path(path,"output",simulation)
unlink(path, recursive = TRUE)
}

cat(sprintf("\n%s successfully deleted",
ifelse(delete_simulation,
"Simulation",
"Study")))
}

8 changes: 5 additions & 3 deletions man/deleteStudy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/testthat/test-createStudy.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,21 @@ test_that("delete v8.1.0 study", {
properties <- antaresRead:::readIniFile(file.path(path, "study.antares"))
expect_identical(properties$antares$version, 810L)
expect_true(is_active_RES(opts))
testthat::expect_true(file.exists(opts$studyPath))
deleteStudy(opts = simOptions())
testthat::expect_true(!file.exists(opts$studyPath))
})


test_that("delete v8.6.0 simulation", {
setup_study_860(sourcedir860)
suppressWarnings(
opts_test <- setSimulationPath(study_temp_path)
)
split_simPath <- strsplit(opts_test$simPath,"/")[[1]]
simulation <- split_simPath[length(split_simPath)]
testthat::expect_true(file.exists(opts_test$simPath))
deleteStudy(opts = opts_test,simulation = simulation)
testthat::expect_true(!file.exists(opts_test$simPath))
})

0 comments on commit a416eaf

Please sign in to comment.