Skip to content

Commit

Permalink
update editClusterST() with new endpoint (path + post)
Browse files Browse the repository at this point in the history
  • Loading branch information
berthetclement committed Sep 23, 2024
1 parent 18d8750 commit 9a5bb89
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 230 deletions.
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
* `removeArea()` : send a warning instead of a stop if an area is referenced in a binding constraint coefficient
* `removeLink()` : send a warning instead of a stop if a link is referenced in a binding constraint coefficient
* `removeCluster()` : send a warning instead of a stop if a cluster is referenced in a binding constraint coefficient
* `createClusterST()` : updated with new endpoint API
* `createClusterST()` : updated with new endpoint API (POST + PUT)
* `editClusterST()` : updated with new endpoint API (PATCH + PUT)

NEW FEATURES (Antares v8.8) :

* `updateOptimizationSettings()` allows the user to update solver.log property
* `createClusterST()` / `editClusterST()` use new parameters and default values
* Add new function `api_patch()` to put PATCH (httr) request to API


BUGFIXES :
Expand Down
119 changes: 66 additions & 53 deletions R/editClusterST.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,65 +91,78 @@ editClusterST <- function(area,
# format name for API
cluster_name <- transform_name_to_id(cluster_name)

# /!\ temporary solution /!\
# as the endpoint does not return an error if the cluster does not exist
if(!is_api_mocked(opts)){
exists <- FALSE
suppressWarnings(
clusters <- readClusterSTDesc(opts = opts)
)
if (nrow(clusters) > 0) {
clusters_filtered <- clusters[clusters$area == tolower(area) &
clusters$cluster == cluster_name,]
exists <- nrow(clusters_filtered) > 0
}
assertthat::assert_that(exists,
msg = paste0("Cluster '",
cluster_name,
"' does not exist. It can not be edited."))
}
##
# PATCH for properties
##
# adapt parameter names
list_properties <- list("group" = params_cluster[["group"]],
"name" = cluster_name,
"injectionNominalCapacity" = params_cluster[["injectionnominalcapacity"]],
"withdrawalNominalCapacity" = params_cluster[["withdrawalnominalcapacity"]],
"reservoirCapacity" = params_cluster[["reservoircapacity"]],
"efficiency" = params_cluster[["efficiency"]],
"initialLevel" = params_cluster[["initiallevel"]],
"initialLevelOptim" = params_cluster[["initialleveloptim"]],
"enabled" = params_cluster[["enabled"]])

# update parameters if something else than name
if (length(params_cluster) > 1) {
currPath <- "input/st-storage/clusters/%s/list/%s"
writeIni(
listData = params_cluster,
pathIni = sprintf(currPath, area, cluster_name),
opts = opts
)
list_properties <- dropNulls(list_properties)

if(length(list_properties)>1){
# make json file
body <- jsonlite::toJSON(list_properties,
auto_unbox = TRUE)

# send request (without coeffs/term)
result <- api_patch(opts = opts,
endpoint = file.path(opts$study_id,
"areas",
area,
"storages",
cluster_name),
body = body,
encode = "raw")

cli::cli_alert_success("Endpoint {.emph {'Edit ST-storage (properties)'}} {.emph
{.strong {cluster_name}}} success")
}

# update data
names_data_params <- c("PMAX_injection",
"PMAX_withdrawal",
"inflows",
"lower_rule_curve",
"upper_rule_curve")
##
# PUT for TS values
##
# adapt list name TS
list_value_ts <- list(pmax_injection = PMAX_injection,
pmax_withdrawal = PMAX_withdrawal,
inflows = inflows,
lower_rule_curve = lower_rule_curve,
upper_rule_curve = upper_rule_curve)

for (i in names_data_params){
if (!is.null(get(i))) {
# format name for API
data_param_name <- transform_name_to_id(i, id_dash = TRUE)
list_value_ts <- dropNulls(list_value_ts)

if(length(list_value_ts)!=0){
lapply(names(list_value_ts), function(x){
body = jsonlite::toJSON(list(data=list_value_ts[[x]],
index=0,
columns=0),
auto_unbox = FALSE)

endpoint <- file.path(opts$study_id,
"areas",
area,
"storages",
cluster_name,
"series",
x)

currPath <- paste0("input/st-storage/series/%s/%s/",data_param_name)
cmd <- api_command_generate(
action = "replace_matrix",
target = sprintf(currPath, area, cluster_name),
matrix = get(i)
)
api_command_register(cmd, opts = opts)
`if`(
should_command_be_executed(opts),
api_command_execute(cmd,
opts = opts,
text_alert = paste0("Update ",
i,
" cluster's series: {msg_api}")),
cli_command_registered("replace_matrix")
)
}
# update
api_put(opts = opts,
endpoint = endpoint,
body = body,
encode = "raw")

cli::cli_alert_success("Endpoint {.emph {'Edit ST-storage (TS value)'}} {.emph
{.strong {x}}} success")
})
}

return(invisible(opts))
}
#####-
Expand Down
89 changes: 0 additions & 89 deletions tests/testthat/test-createClusterST.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,95 +172,6 @@ test_that("Create short-term storage cluster (new feature v8.6)",{

})


# API ----

# test_that("API Command test for createClusterST", {
# # Simulation parameters for api code
# opts_mock <- mockSimulationAPI(force = TRUE,
# antares_version = "860")
#
# # create complete cluster st-storage
#
# area_name <- "area01"
# cluster_name <- "ClusTER01"
#
# # no casse sensitiv
# createClusterST(area = area_name,
# cluster_name = cluster_name,
# group = "Other",
# storage_parameters = storage_values_default(),
# PMAX_injection = matrix(1,8760),
# PMAX_withdrawal = matrix(0.5,8760),
# inflows = matrix(0.25,8760),
# lower_rule_curve = matrix(0.2,8760),
# upper_rule_curve = matrix(0.9,8760))
#
# # use getVariantCommands to catch information
# # here (specific st-storage : `list` with 1 group (parameters) + 5 data parameters)
# res_list <- getVariantCommands(last = 6)
#
# ## test first group of list for ini parameters
# action_api_1 <- res_list[[1]]
#
# # name of api instruction/action
# testthat::expect_equal(action_api_1$action, "create_st_storage")
# # check names and values parameters
# names_st_paramas <- names(storage_values_default())
# names_vector_parameters <-setdiff(names(action_api_1$args$parameters),
# c("name", "group"))
# # check if all parameters are present
# testthat::expect_true(all(names_st_paramas
# %in% names_vector_parameters))
# # check casse name cluster
# name_ori <- paste0(area_name, "_", cluster_name)
#
# testthat::expect_equal(tolower(name_ori),
# action_api_1$args$parameters$name)
#
# ## test other group for data
# # search "replace_matrix" action
# index_data <- lapply(res_list, `[[`, 1) %in%
# "replace_matrix"
#
# data_list <- res_list[index_data]
#
# # test for every floor in "args" :
# # "target" (path of txt file)
# # "matrix" (data)
# data_path_files <- lapply(data_list, function(x){
# x$args$target
# })
#
# # test for every path, the path destination + name of txt file
# # name txt files corresponding data parameters of function `createClusterST()`
# full_root_path_name <- file.path("input", "st-storage", "series", area_name,
# tolower(name_ori))
#
# # from code
# # these names ares approved with antares desktop but not with API
# names_file_list <- c("PMAX-injection", "PMAX-withdrawal", "inflows",
# "lower-rule-curve", "upper-rule-curve")
#
# # reformat API
# names_file_list <- transform_name_to_id(names_file_list, id_dash = TRUE)
#
# # check root path for every file
# is_good_path <- lapply(data_path_files, function(x){
# grepl(pattern = full_root_path_name, x = x)
# })
#
# testthat::expect_true(all(unlist(is_good_path)))
#
# # check names of files
# names_file_api <- lapply(data_path_files, function(x){
# regmatches(x,regexpr("([^\\/]+$)",x))
# })
#
# testthat::expect_true(all(unlist(names_file_api) %in%
# names_file_list))
# })

# >=880 ----

test_that("Create short-term storage cluster (new feature v8.8.0)",{
Expand Down
87 changes: 0 additions & 87 deletions tests/testthat/test-editClusterST.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,93 +150,6 @@ test_that("edit st-storage clusters (only for study >= v8.6.0" , {
unlink(opts_test$studyPath, recursive = TRUE)
})



# API ----

test_that("API Command test for editClusterST", {
# Simulation parameters for api code
opts_mock <- mockSimulationAPI(force = TRUE,
antares_version = "860")

# create complete cluster st-storage
area_name <- "area01"
cluster_name <- "ClusTER01"

# create complete cluster st-storage
editClusterST(area = area_name,
cluster_name = cluster_name,
group = "Other1",
storage_parameters = storage_values_default(),
PMAX_injection = matrix(1,8760),
PMAX_withdrawal = matrix(0.5,8760),
inflows = matrix(0.25,8760),
lower_rule_curve = matrix(0.2,8760),
upper_rule_curve = matrix(0.9,8760))

# use getVariantCommands to catch information
# here (specific st-storage : list with 8 group (parameters) + 5 data parameters)
res_list <- getVariantCommands(last = 13)

## test first group of list for ini parameters
action_api_1 <- res_list[[1]]

# name of api instruction/action
testthat::expect_equal(action_api_1$action, "update_config")
# check "args" name parameters (just for one parameter/one action)
param_target <- res_list[[3]]$args$target
param_target <- regmatches(param_target, regexpr("([^\\/]+$)",param_target))
testthat::expect_equal(param_target, "efficiency")

# check "data" (value of parameter)
testthat::expect_equal("1.000000", res_list[[3]]$args$data)

## test other group for data
# search "replace_matrix" action
index_data <- lapply(res_list, `[[`, 1) %in%
"replace_matrix"

data_list <- res_list[index_data]

# test for every floor in "args" :
# "target" (path of txt file)
# "matrix" (data)
data_path_files <- lapply(data_list, function(x){
x$args$target
})

# test for every path, the path destination + name of txt file
# name txt files corresponding data parameters of function `editClusterST()`
# check casse of name cluster name in every path
name_ori <- paste0(area_name, "_", cluster_name)

full_root_path_name <- file.path("input", "st-storage", "series", area_name,
tolower(name_ori))

# from code
# these names are approved with antares desktop but not with API
names_file_list <- c("PMAX-injection", "PMAX-withdrawal", "inflows",
"lower-rule-curve", "upper-rule-curve")

# reformat API
names_file_list <- transform_name_to_id(names_file_list, id_dash = TRUE)

# check root path for every file
is_good_path <- lapply(data_path_files, function(x){
grepl(pattern = full_root_path_name, x = x)
})

testthat::expect_true(all(unlist(is_good_path)))

# check names of files
names_file_api <- lapply(data_path_files, function(x){
regmatches(x,regexpr("([^\\/]+$)",x))
})

testthat::expect_true(all(unlist(names_file_api) %in%
names_file_list))
})

# v880 ----
test_that("Edit short-term storage cluster (new feature v8.8.0)",{
## basics errors cases ----
Expand Down

0 comments on commit 9a5bb89

Please sign in to comment.