Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/cloud deploy #9

Merged
merged 16 commits into from
Feb 20, 2024
5 changes: 5 additions & 0 deletions .Rprofile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ if (file.exists("renv")) {

# Allow absolute module imports (relative to the app root).
options(box.path = getwd())

Sys.setenv(TRISK_INPUT_PATH = file.path("app", "data", "st_inputs"))
Sys.setenv(TRISK_OUTPUT_PATH = file.path("app", "data", "backend_db"))
# 'prod' or 'dev'
Sys.setenv(CRISPY_APP_ENV = "dev")
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2685,3 +2685,4 @@ scripts/trisk_knative/st_inputs/prewrangled_capacity_factors.csv
scripts/trisk_knative/st_inputs/prewrangled_financial_data_stress_test.csv
scripts/trisk_knative/st_inputs/price_data_long.csv
scripts/trisk_knative/st_inputs/Scenarios_AnalysisInput.csv
scripts/creds.R
3 changes: 2 additions & 1 deletion app/logic/cloud_logic.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
trigger_trisk_api_computation <- function(trisk_run_params, api_endpoint) {
# Define the URL
# trisk_api_service <- "trisk-api-service"

Check warning on line 3 in app/logic/cloud_logic.R

View workflow job for this annotation

GitHub Actions / Run linters and tests

file=app/logic/cloud_logic.R,line=3,col=5,[commented_code_linter] Commented code should be removed.
trisk_api_service <- Sys.getenv("TRISK_API_SERVICE")
url <- paste0("http://", trisk_api_service, ":80/compute_trisk")
# url <- "http://164.90.241.52:80/compute_trisk/"

Check warning on line 6 in app/logic/cloud_logic.R

View workflow job for this annotation

GitHub Actions / Run linters and tests

file=app/logic/cloud_logic.R,line=6,col=5,[commented_code_linter] Commented code should be removed.

# Define the body of the request
body <- list(
Expand All @@ -14,7 +14,8 @@

# Define the headers, including Host
headers <- c(
`Host` = paste0("trisk-api.default.",trisk_api_service),

`Host` = paste0("trisk-api.default.", trisk_api_service),
`Content-Type` = "application/json"
)

Expand Down
5 changes: 2 additions & 3 deletions app/logic/constant.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

# ENV CONSTANTS ====================================

trisk_input_path <- file.path("app", "data", "st_inputs")
backend_trisk_run_folder <- file.path("app", "data", "backend_db")
trisk_input_path <- Sys.getenv("TRISK_INPUT_PATH")
backend_trisk_run_folder <- Sys.getenv("TRISK_OUTPUT_PATH")
api_endpoint <- Sys.getenv("TRISK_API_ENDPOINT")


# PROJECT CONSTANTS ====================================

# Must be ordered from "less granular" to "more granular"
max_trisk_granularity <- list(
"ald_sector" = 1,
"ald_business_unit" = 2
# "company_id" = 3

Check warning on line 15 in app/logic/constant.R

View workflow job for this annotation

GitHub Actions / Run linters and tests

file=app/logic/constant.R,line=15,col=5,[commented_code_linter] Commented code should be removed.
)


Expand Down
11 changes: 8 additions & 3 deletions app/logic/data_load.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
box::use(
app / logic / cloud_logic[

app/logic/cloud_logic[
get_data_from_postgres
]
)
Expand All @@ -8,8 +9,12 @@ base_data_load <- function(table_name, run_id = NULL, backend_trisk_run_folder =
if (Sys.getenv("CRISPY_APP_ENV") == "dev") {
table_data_path <- fs::path(backend_trisk_run_folder, table_name, ext = "parquet")
if (file.exists(table_data_path)) {
table_data <- arrow::read_parquet(table_data_path) |>
dplyr::filter(.data$run_id == run_id)
if (!is.null(run_id)) {
table_data <- arrow::read_parquet(table_data_path) |>
dplyr::filter(.data$run_id == .env$run_id)
} else {
table_data <- arrow::read_parquet(table_data_path)
}
} else {
table_data <- default_tibble
}
Expand Down
8 changes: 4 additions & 4 deletions app/logic/data_write.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ append_st_results_to_backend_data <- function(

# If the file exists, read the existing data, otherwise set the existing data to NULL
if (file.exists(fpath)) {
persistent_data <- read_parquet(fpath)

persistent_data <- arrow::read_parquet(fpath)
} else {
persistent_data <- NULL
}

# Append the new data to the existing data
persistent_data <- bind_rows(persistent_data, new_data)
persistent_data <- dplyr::bind_rows(persistent_data, new_data)

# Write the updated data back to the file
write_parquet(persistent_data, fpath)
arrow::write_parquet(persistent_data, fpath)
}
return(run_id)
}
2 changes: 1 addition & 1 deletion app/logic/plots/exposure_change_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
geom_col(aes(y = !!rlang::sym(y_exposure_var)), width = bar_width, fill = plot_bar_color) +
geom_tile(
aes(
y = !!rlang::sym(y_exposure_var) + (!!rlang::sym(y_value_loss_var) / 2),
y = !!rlang::sym(y_exposure_var) + (!!rlang::sym(y_value_loss_var)/2),

Check warning on line 58 in app/logic/plots/exposure_change_plot.R

View workflow job for this annotation

GitHub Actions / Run linters and tests

file=app/logic/plots/exposure_change_plot.R,line=58,col=75,[infix_spaces_linter] Put spaces around all infix operators.
height = abs(!!rlang::sym(y_value_loss_var)),
fill = dplyr::if_else(!!rlang::sym(y_value_loss_var) < 0, "Loss", "Gain")
),
Expand Down
14 changes: 9 additions & 5 deletions app/logic/trisk_button_logic.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
box::use(
app / logic / trisk_mgmt[
run_trisk_with_params
app/logic/trisk_mgmt[
run_trisk_with_params,
format_error_message
],
app / logic / cloud_logic[
app/logic/cloud_logic[
trigger_trisk_api_computation,
],
app / logic / data_load[
app/logic/data_load[
load_backend_trisk_run_metadata
],
app / logic / data_write[
app/logic/data_write[
append_st_results_to_backend_data
]
)
Expand Down Expand Up @@ -70,6 +71,9 @@ trisk_generator <- function(
max_trisk_granularity
)
run_id <- check_if_run_exists(trisk_run_params, backend_trisk_run_folder)

} else {
run_id <- NULL
}
} else if (Sys.getenv("CRISPY_APP_ENV") == "prod") {
run_id <- trigger_trisk_api_computation(trisk_run_params)
Expand Down
24 changes: 1 addition & 23 deletions app/logic/trisk_mgmt.R
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
# Import necessary libraries and functions
box::use(
arrow[read_parquet, write_parquet],
dplyr[bind_rows],
r2dii.climate.stress.test[run_trisk],
app / logic / data_load[load_backend_crispy_data, load_backend_trajectories_data, load_backend_trisk_run_metadata]
)


box::use(
app / logic / trisk_mgmt[
run_trisk_with_params,
format_error_message
],
app / logic / data_load[
load_backend_trajectories_data,
load_backend_crispy_data
]
)



# Function to run the trisk model with given parameters and input path
# Returns the wrangled and checked results
run_trisk_with_params <- function(trisk_run_params, trisk_input_path) {
# Run the trisk model with the provided parameters and input path
# The results are returned and stored in st_results_wrangled_and_checked
st_results_wrangled_and_checked <- do.call(
run_trisk,
r2dii.climate.stress.test::run_trisk,
c(
trisk_run_params,
list(
Expand Down
10 changes: 5 additions & 5 deletions app/main.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ box::use(
# Load required modules and logic files
box::use(
# modules
app / view / sidebar_parameters,
app / view / homepage,
app / view / crispy_equities,
app / view / crispy_loans,
app/view/sidebar_parameters,
app/view/homepage,
app/view/crispy_equities,
app/view/crispy_loans,
# logic
app / logic / constant[
app/logic/constant[
trisk_input_path,
backend_trisk_run_folder,
max_trisk_granularity,
Expand Down
4 changes: 2 additions & 2 deletions app/view/modules/plots_equity_change.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ box::use(
)

box::use(
app / logic / plots / exposure_change_plot[pipeline_exposure_change_plot],
app / logic / plots / crispy_npv_change_plot[pipeline_crispy_npv_change_plot]
app/logic/plots/exposure_change_plot[pipeline_exposure_change_plot],
app/logic/plots/crispy_npv_change_plot[pipeline_crispy_npv_change_plot]
)


Expand Down
2 changes: 1 addition & 1 deletion app/view/modules/plots_trajectories.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ box::use(
)

box::use(
app / logic / plots / scenario_time_plot[pipeline_scenario_time_plot]
app/logic/plots/scenario_time_plot[pipeline_scenario_time_plot]
)


Expand Down
2 changes: 1 addition & 1 deletion app/view/modules/trisk_button.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ server <- function(
# open the model dialog
shinyjs::runjs(
paste0(
"$('#", session$ns("mymodal"), "').modal({closable: true}).modal('show');"
"$('#", session$ns("mymodal"), "').modal({closable: false}).modal('show');"
)
)

Expand Down
2 changes: 1 addition & 1 deletion app/view/params/params_dimensions.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ box::use(
)

box::use(
app / logic / renamings[rename_string_vector]
app/logic/renamings[rename_string_vector]
)


Expand Down
2 changes: 1 addition & 1 deletion app/view/params/params_scenarios.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ box::use(
)

box::use(
app / logic / renamings[rename_string_vector]
app/logic/renamings[rename_string_vector]
)

ui <- function(id) {
Expand Down
2 changes: 1 addition & 1 deletion app/view/params/params_trisk.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ box::use(
)

box::use(
app / logic / renamings[rename_string_vector]
app/logic/renamings[rename_string_vector]
)


Expand Down
6 changes: 3 additions & 3 deletions app/view/portfolio/portfolio_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ box::use(
)

box::use(
app / view / portfolio / portfolio_edition,
app / logic / constant[max_trisk_granularity],
app / logic / renamings[rename_tibble_columns]
app/view/portfolio/portfolio_edition,
app/logic/constant[max_trisk_granularity],
app/logic/renamings[rename_tibble_columns]
)


Expand Down
2 changes: 1 addition & 1 deletion app/view/portfolio/portfolio_edition.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ box::use(
)

box::use(
app / view / portfolio / simple_search_dropdown
app/view/portfolio/simple_search_dropdown
)


Expand Down
10 changes: 4 additions & 6 deletions app/view/sidebar_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ box::use(
)

box::use(
app / view / params / params_scenarios,
app / view / params / params_dimensions,
app / view / params / params_trisk,
app / logic / renamings[rename_string_vector]
app/view/params/params_scenarios,
app/view/params/params_dimensions,
app/view/params/params_trisk,
app/logic/renamings[rename_string_vector]
)


Expand Down Expand Up @@ -76,8 +76,6 @@ server <- function(id, backend_trisk_run_folder,
market_passthrough = trisk_config_r()$market_passthrough
)
})
# prevent the UI modal from updating too often (ie. blinking)
# trisk_run_params_r <- shiny::throttle(trisk_run_params_r, millis = 200)

perimeter <- list(
"trisk_granularity_r" = trisk_granularity_r,
Expand Down
5 changes: 0 additions & 5 deletions scripts/creds.R

This file was deleted.

Loading
Loading