Skip to content

Commit

Permalink
Api/st storage (#216)
Browse files Browse the repository at this point in the history
* readClusterDesc minor fix + add details to error message of api_put

* .importSTStorage() is now compatible API with readInputTS()

* .getInputOptionsAPI() add areas with st-storage when setSimulationPathAPI() is called

* .importSTStorage indenting code

* .getInputOptionsAPI fix for add option cluster with st-storage only with version >=860
  • Loading branch information
berthetclement authored Aug 30, 2023
1 parent 46c8267 commit 78cd297
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 32 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ BUGFIXES :
* `setSimulationPathAPI` generate new global parameter `sleep` to add timer to API request
* Correction of `.importOutput()` to use `readAntares()` with `parallel == TRUE` in shiny application
* `setSimulationPathAPI()` delete a redundant API request
* `readClusterDesc()` minor fix in API mode + fix if no cluster exists => return specific error message
* `readIniAPI()` read well file `generaldata` for sections "playlist" and "variables selection"
* `readClusterDesc()` minor fix in API mode


DATA :

Expand Down
12 changes: 9 additions & 3 deletions R/API-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ api_get <- function(opts, endpoint, ..., default_endpoint = "v1/studies") {
if(condition_status_check){
mess_error <- content(result)
if(!is.null(names(mess_error)))
mess_error <- paste0("\n[description] : ", mess_error$description,
"\n[IniReaderError] : ", mess_error$exception)
mess_error <- paste0("\n[Description] : ", mess_error$description,
"\n[Exception] : ", mess_error$exception)
else
mess_error <- NULL
stop_for_status(result, task = mess_error)
Expand Down Expand Up @@ -99,7 +99,13 @@ api_post <- function(opts, endpoint, ..., default_endpoint = "v1/studies") {
config = config,
...
)
stop_for_status(result)
api_content <- content(result)
if(!is.null(names(api_content)))
api_content <- paste0("\n[Description] : ", api_content$description,
"\n[Exception] : ", api_content$exception)
else
api_content <- NULL
stop_for_status(result, task = api_content)
content(result)
}

Expand Down
64 changes: 49 additions & 15 deletions R/importInput.R
Original file line number Diff line number Diff line change
Expand Up @@ -435,24 +435,50 @@
return(NULL)

if(!"api" %in% opts$typeLoad){
clusters <- list.files(file.path(opts$inputPath, "st-storage/series", area))
clusters <- list.files(
file.path(opts$inputPath,
"st-storage/series",
area)
)

# "st-storage" have 5 txt files output for each cluster
list_names_txt_files <- unique(
list.files(
file.path(opts$inputPath,
"st-storage/series",
area,
clusters)
)
)

list_names_less_txt <- sub(pattern = ".txt",
replacement = "",
x = list_names_txt_files)

} else {
clusters <- names(read_secure_json(file.path(opts$inputPath, "st-storage/series", area),
token = opts$token, timeout = opts$timeout, config = opts$httr_config))
list_info_clusters <- read_secure_json(
file.path(opts$inputPath,
"st-storage/series",
area),
token = opts$token,
timeout = opts$timeout,
config = opts$httr_config
)

clusters <- names(list_info_clusters)

files_names <- names(list_info_clusters[[1]])

list_names_txt_files <- paste0(files_names, ".txt")
list_names_less_txt <- files_names
}

# "st-storage" have 5 txt files output for each cluster
list_names_txt_files <- unique(
list.files(file.path(opts$inputPath,
"st-storage/series", area, clusters))
)

list_names_less_txt <- sub(pattern = ".txt", replacement = "", x = list_names_txt_files)

# read TS for every cluster
ldply(clusters, function(cl) {
pattern <- paste0("%s/%s/%%s/", list_names_txt_files)
filePatterns <- sprintf(pattern, "st-storage/series", area)
pattern <- paste0("%s/%s/%%s/",
list_names_txt_files)
filePatterns <- sprintf(pattern,
"st-storage/series", area)

res <- lapply(filePatterns,
function(.x){
Expand All @@ -477,8 +503,16 @@
res$area <- area
res$cluster <- cl

setcolorder(res, c("area", "cluster", "timeId", setdiff(names(res),
c("area", "cluster", "timeId"))))
setcolorder(res,
c("area",
"cluster",
"timeId",
setdiff(
names(res),
c("area",
"cluster",
"timeId")))
)
})

# added a column "name_file" to tag the file name
Expand Down
3 changes: 3 additions & 0 deletions R/readClusterDesc.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ readClusterSTDesc <- function(opts = simOptions()) {
clusters[, .SD, .SDcols = order(names(clusters))]
},jsoncld, names(jsoncld), SIMPLIFY = FALSE), fill = TRUE)

if(length(res) == 0)
stop("Cannot find cluster description.", call. = FALSE)

res <- res[, .SD, .SDcols = c("area", "name", "group", names(res)[!names(res) %in%c("area", "name", "group")])]

}else{
Expand Down
51 changes: 38 additions & 13 deletions R/utils_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,44 @@ read_secure_json <- function(url, token = NULL, timeout = 60, config = list()) {
})
}
}


list(
mode = "Input",
antaresVersion = antaresVersion,
areaList = areaList,
districtList = districtList,
linkList = as.character(linksDef$link),
linksDef = linksDef,
areasWithClusters = areaList[areaHasClusters],
areasWithResClusters = areaList[areaHasResClusters],
parameters = params
)

# Areas with st-storage (>=860)
if(paths$version>=860){
clusterSTList <- read_secure_json(file.path(inputPath, "st-storage", "clusters", "&depth=4"), ...)
areaHasSTClusters <- vapply(areaList, FUN.VALUE = logical(1), function(a) {
TF <- FALSE
try({
f <- clusterSTList[[a]]$list
if(!is.null(f))return(TRUE)
})
return(TF)
})

# return
list(
mode = "Input",
antaresVersion = antaresVersion,
areaList = areaList,
districtList = districtList,
linkList = as.character(linksDef$link),
linksDef = linksDef,
areasWithClusters = areaList[areaHasClusters],
areasWithResClusters = areaList[areaHasResClusters],
areasWithSTClusters = areaList[areaHasSTClusters],
parameters = params
)
}else
list(
mode = "Input",
antaresVersion = antaresVersion,
areaList = areaList,
districtList = districtList,
linkList = as.character(linksDef$link),
linksDef = linksDef,
areasWithClusters = areaList[areaHasClusters],
areasWithResClusters = areaList[areaHasResClusters],
parameters = params
)
}

# valid_url <- function(url_in, t = 2){
Expand Down

0 comments on commit 78cd297

Please sign in to comment.