Skip to content

Commit

Permalink
async server updated
Browse files Browse the repository at this point in the history
  • Loading branch information
aminadibi committed Oct 14, 2021
1 parent b602dde commit 82c0792
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 70 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: covidseirPrism
Title: Peer Models Network PRISM Bridge for the covidseir
Version: 0.2.1
Version: 0.2.2
Authors@R: c(
person("Amin", "Adibi", email = "[email protected]", role = c("aut", "cph")),
person("Mohsen", "Sadatsafavi", email = "[email protected]", role = c("aut", "cre")))
Expand All @@ -16,4 +16,4 @@ Imports:
ggplot2 (>= 3.3.0),
covidseir (>= 0.0.0.9006)
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.0
RoxygenNote: 7.1.2
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Generated by roxygen2: do not edit by hand

export(gateway)
export(gatewayasync)
export(prism_get_output_structure)
export(prism_model_run)
import(covidseir)
import(ggplot2)
Expand Down
73 changes: 30 additions & 43 deletions R/prism_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,39 @@ thisSession$LONG_RUN_STATUS_DONE<-1
thisSession$LONG_RUN_STATUS_ERROR<- -1

thisSession$redis_connection_status <- 0 #0:not connected; 1:connected
thisSession$REDIS_ADDRESS = "captain.cp.prism-ubc.linaralabs.com"
thisSession$REDIS_PORT <- 3001
thisSession$REDIS_ADDRESS = "srv-captain--persistent-redis"
thisSession$REDIS_PORT <- 6379

thisSession$MODEL_DESCRIPTION<-paste0("This is ",get_my_name()," - PRISM enabled!")
thisSession$MODEL_VERSION<-paste(packageVersion(get_my_name()))


#FOR ONE SHOT JSON CALL!
#Can authenticate the user either by API key or by a session_id.
#Can authenticate the user either by API key or by a session_id.
#' @export
gateway<-function(...)
{
arguments=list(...)
func<-arguments$func

session_id<-arguments$session_id

if(is.null(session_id)) session_id=""

session_id<<-session_id

arguments$func<-NULL
arguments$api_key<-NULL
arguments$session_id<-NULL


if(length(arguments)==0) {
out<-eval(parse(text=paste(func,"()")))
}
else {
out<-do.call(func, args = arguments)
}

return(jsonlite::toJSON(out))
}

Expand All @@ -59,33 +59,33 @@ gateway<-function(...)
gatewayasync<-function(...)
{
token <- generate_token()

arguments=list(...)
func<-arguments$func

session_id<-arguments$session_id

if(is.null(session_id)) session_id=""

session_id<<-session_id

arguments$func<-NULL
arguments$api_key<-NULL
arguments$session_id<-NULL

token <- generate_token()
redisConnect(host = thisSession$REDIS_ADDRESS, port = thisSession$REDIS_PORT, password = "H1Uf5o6326n6C2276m727cU82O")

redisConnect(host = thisSession$REDIS_ADDRESS, port = thisSession$REDIS_PORT, password = Sys.getenv("REDIS_PASSWORD"))
redisSet(paste0("AS:status:",token),"[READY]")
redisSet(paste0("AS:status_time:",token), Sys.time())
redisSet(paste0("AS:status_data:",token),
list(model_name=get_my_name(),
func=func,
arguments=arguments
)
)
)
)
redisClose()

out <- list(token=token, error_code=0)

return(jsonlite::toJSON(out))
Expand All @@ -94,7 +94,6 @@ gatewayasync<-function(...)




#' @export
prism_model_run<-function(model_input=NULL)
{
Expand All @@ -107,14 +106,14 @@ connect_to_model<-function(api_key="")
{
model_name<-environmentName(environment(connect_to_model))
out<-list(error_code=0,session_id="",version="",description="")


if(thisSession$MODE_REQUIRE_SESSION)
{
session_id<-generate_session_id()
out$session_id<-session_id
}

out$version<-thisSession$MODEL_VERSION
out$description<-thisSession$MODEL_DESCRIPTION
return(out)
Expand All @@ -140,18 +139,7 @@ generate_token<-function()
#' @export
prism_get_output_structure<-function()
{
out<-list(
n_agents=prism_output(source="$n_agents", type = "numeric/scalar", group = "", title = "Number of simulated individuals", description = ""),
cumul_time=prism_output(source="$cumul_time",type = "numeric/scalar", title = "cumulative time"),
n_deaths=prism_output(source="$n_deaths",type = "numeric/scalar", title = "number of deaths"),
n_COPD=prism_output(source="$n_COPD",type = "numeric/scalar", title = "Number of patients with COPD"),
total_exac=prism_output(source="$total_exac",type = "numeric/vector", title = "Total number of exacerbations by severity"),
total_exac_time=prism_output(source="$total_exac_time",type = "numeric/vector", title = "total_exac_time"),
total_pack_years=prism_output(source="$total_pack_years",type = "numeric/scalar", title = "Total pack-years"),
total_doctor_visit=prism_output(source="$total_doctor_visit",type = "numeric/vector", title = "Total doctor visits"),
total_cost=prism_output(source="$total_cost",type = "numeric/scalar", title = "Total costs"),
total_qaly=prism_output(source="$total_qaly",type = "numeric/scalar", title = "Total QALY")
)
out<-list()
return(out)
}

Expand All @@ -178,19 +166,18 @@ prism_get_async_results <- function(token=NULL)
stop("Token was not provided.")
return()
}
redisConnect(host = thisSession$REDIS_ADDRESS, port = thisSession$REDIS_PORT, password = "H1Uf5o6326n6C2276m727cU82O")

redisConnect(host = thisSession$REDIS_ADDRESS, port = thisSession$REDIS_PORT, password = Sys.getenv("REDIS_PASSWORD"))

status <- redisGet(paste0("AS:status:",token))
if(is.null(status))
{
stop("Job with this token not found.")
}
status_data <- redisGet(paste0("AS:status_data:",token))

redisClose()

return(list(status=status,status_data=status_data))
}


25 changes: 0 additions & 25 deletions man/set_var.Rd

This file was deleted.

0 comments on commit 82c0792

Please sign in to comment.