From ac75ab08c2d1e3e8ce27135a8244b9037f88cf47 Mon Sep 17 00:00:00 2001 From: madeliri Date: Mon, 20 Apr 2026 18:50:40 +0300 Subject: [PATCH] feat: bring config file back --- .Rprofile | 5 ++- .gitignore | 1 + app.R | 41 ++++++++++++++++--------- config.yml | 17 ++++++++++ configs/{ => enabled}/enabled_schemes.R | 1 - modules/data_validation.R | 10 ++---- modules/db.R | 8 ++++- modules/global_options.R | 36 +++++++++++++++++----- 8 files changed, 84 insertions(+), 35 deletions(-) create mode 100644 config.yml rename configs/{ => enabled}/enabled_schemes.R (67%) diff --git a/.Rprofile b/.Rprofile index 5f9e93d..d6098c7 100644 --- a/.Rprofile +++ b/.Rprofile @@ -4,9 +4,8 @@ source("renv/activate.R") (function() { paths <- c( - "FORM_AUTH_ENABLED", - "FORM_VERSION", - "FORM_TITLE" + "R_CONFIG_ACTIVE", + "AUTH_DB_KEY" ) lines <- paths[Sys.getenv(paths) == ""] diff --git a/.gitignore b/.gitignore index f4f1c25..1a38f03 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ scheme.rds configs/schemas/d2tra_t.xlsx configs/schemas/antifib.xlsx +/_devel .Renviron .DS_Store .lintr diff --git a/app.R b/app.R index 21a2448..1efe5c4 100644 --- a/app.R +++ b/app.R @@ -9,12 +9,9 @@ suppressPackageStartupMessages({ library(shinymanager) }) -# КАК ЗАПРЯТЯАТЬ ID - # SOURCE FILES ============================ box::purge_cache() box::use( - configs/enabled_schemes[enabled_schemes], modules/utils, modules/global_options, modules/db, @@ -22,17 +19,25 @@ box::use( modules/scheme_generator[scheme_R6] ) -# SETTINGS ================================ -FILE_SCHEME <- fs::path("configs/schemas", "schema.xlsx") -AUTH_ENABLED <- Sys.getenv("FORM_AUTH_ENABLED", FALSE) -HEADER_TEXT <- sprintf("%s (%s)", Sys.getenv("FORM_TITLE", "NA"), Sys.getenv("FORM_VERSION", "NA")) - global_options$set_global_options( shiny.host = "0.0.0.0" - # enabled_schemes = "example_of_scheme" ) + global_options$check_and_init_scheme() +# global vars +box::use( + modules/global_options[AUTH_ENABLED] +) + +# SETTINGS ================================ +HEADER_TEXT <- sprintf("%s (%s)", config::get("form_name"), config::get("form_app_version")) + +# sadasdasdasdasdas +options(box.path = config::get("form_configs_path")) +box::use(enabled/enabled_schemes[enabled_schemes]) + + # CHECK FOR PANDOC # TEMP ! NEED TO HANDLE rmarkdown::find_pandoc(dir = "/opt/homebrew/bin/") @@ -45,7 +50,14 @@ schms <- readRDS("scheme.rds") # UI ======================= ui <- page_sidebar( - title = HEADER_TEXT, + # title = HEADER_TEXT, + title = tagList( + h4(HEADER_TEXT, style = "margin-top: .5rem"), + tooltip( + span("about", icon("circle-info", lib = "font-awesome")), + "Данный функционал позволяет обрабатывать информацию о пациентах на комиссию, используя Excel (обновление данных возможно через окно формирования списка пациентов)" + ) + ), theme = bs_theme(version = 5, preset = "bootstrap"), sidebar = sidebar( actionButton("add_new_main_key_button", "Добавить новую запись", icon("plus", lib = "font-awesome")), @@ -59,12 +71,9 @@ ui <- page_sidebar( position = "left", open = list(mobile = "always") ), - as_fill_carrier(uiOutput("main_ui_navset")) + as_fill_carrier(uiOutput("main_ui_navset")), ) -# MODALS ======================== -# окно для подвтерждения очищения данных - # init auth ======================= if (AUTH_ENABLED) { # shinymanager::set_labels("en", "Please authenticate" = "scheme()") @@ -898,6 +907,8 @@ server <- function(input, output, session) { "Пользователь" , ifelse(AUTH_ENABLED, res_auth$user, "anonymous"), "Название базы" , names(enabled_schemes)[enabled_schemes == scheme()], "id базы" , scheme(), + "id формы" , config::get("form_id"), + "ver формы" , config::get("form_app_version"), "Время выгрузки" , format(Sys.time(), "%d.%m.%Y %H:%M:%S"), ) @@ -1196,6 +1207,8 @@ server <- function(input, output, session) { action_row <- tibble( date = Sys.time(), user = ifelse(AUTH_ENABLED, res_auth$user, "anonymous"), + app_id = config::get("form_id"), + app_ver = config::get("form_app_version"), remote_addr = session$request$REMOTE_ADDR, key = key, action = action, diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..7ec1182 --- /dev/null +++ b/config.yml @@ -0,0 +1,17 @@ +default: + form_app_version: 0.15.0 + +prod: + form_db_path: db + form_configs_path: configs + form_auth_enabled: true + form_id: new_form + form_name: NEW FORMY + +devel: + form_db_path: _devel/db + form_configs_path: _devel/configs + form_auth_enabled: false + form_id: new_form + form_name: NEW FORMY + form_app_version: 0.15.0 dev \ No newline at end of file diff --git a/configs/enabled_schemes.R b/configs/enabled/enabled_schemes.R similarity index 67% rename from configs/enabled_schemes.R rename to configs/enabled/enabled_schemes.R index aad923e..568ef30 100644 --- a/configs/enabled_schemes.R +++ b/configs/enabled/enabled_schemes.R @@ -1,5 +1,4 @@ #' @export enabled_schemes <- c( `Тестовая база данных` = "example_of_scheme" - # `D2TRA (для отладки)` = "d2tra_t" ) \ No newline at end of file diff --git a/modules/data_validation.R b/modules/data_validation.R index ab1a22b..3314c78 100644 --- a/modules/data_validation.R +++ b/modules/data_validation.R @@ -70,10 +70,7 @@ init_val = function(scheme, ns) { val_is_a_number = function(x) { # exit if empty - if (is_this_empty_value(x)) { - return(NULL) - } - + if (is_this_empty_value(x)) return(NULL) # хак для пропуска значений if (x == "NA") return(NULL) @@ -87,10 +84,7 @@ val_is_a_number = function(x) { val_number_within_a_range = function(x, ranges) { # exit if empty - if (is_this_empty_value(x)) { - return(NULL) - } - + if (is_this_empty_value(x)) return(NULL) if (x == "NA") return(NULL) # замена разделителя десятичных цифр diff --git a/modules/db.R b/modules/db.R index 68e95dd..9be7a8e 100644 --- a/modules/db.R +++ b/modules/db.R @@ -4,7 +4,11 @@ #' @param where text mark to distingiush calss make_db_connection = function(scheme, where = "") { if (getOption("APP.DEBUG", FALSE)) message("=== DB CONNECT ", where) - DBI::dbConnect(RSQLite::SQLite(), fs::path("db", scheme, ext = "sqlite")) + DBI::dbConnect(RSQLite::SQLite(), fs::path( + config::get("form_db_path"), + scheme, + ext = "sqlite" + )) } #' @export @@ -12,12 +16,14 @@ make_db_connection = function(scheme, where = "") { #' Function to close connection to db, disigned to easy dubugging and #' hide warnings. close_db_connection = function(con, where = "") { + tryCatch( expr = DBI::dbDisconnect(con), error = function(e) print(e), warning = function(w) if (getOption("APP.DEBUG", FALSE)) message("=!= ALREADY DISCONNECTED ", where), finally = if (getOption("APP.DEBUG", FALSE)) message("=/= DB DISCONNECT ", where) ) + } #' @export diff --git a/modules/global_options.R b/modules/global_options.R index eefb0f9..1455b76 100644 --- a/modules/global_options.R +++ b/modules/global_options.R @@ -3,13 +3,28 @@ set_global_options = function( SYMBOL_DELIM = "; ", APP.DEBUG = FALSE, - # APP.FILE_DB = fs::path("data.sqlite"), shiny.host = "127.0.0.1", shiny.port = 1338, ... ) { + + config_params_to_check <- c( + "form_app_version", + "form_db_path", + "form_configs_path", + "form_auth_enabled", + "form_id", + "form_name" + ) + + expected_params_in_config <- config_params_to_check %in% names(config::get()) + if (!all(expected_params_in_config)) { + cli::cli_abort(c("ну так не пойдет:", paste("-", config_params_to_check[!expected_params_in_config]))) + } + options( SYMBOL_DELIM = SYMBOL_DELIM, + # form.db_path = config::get("form_db_path"), APP.DEBUG = APP.DEBUG, # APP.FILE_DB = APP.FILE_DB, shiny.host = shiny.host, @@ -18,27 +33,30 @@ set_global_options = function( ) } +#' @export +AUTH_ENABLED <- Sys.getenv("FORM_AUTH_ENABLED", FALSE) + #' @export check_and_init_scheme = function() { cli::cli_inform(c("*" = "проверка схемы...")) - options(box.path = here::here()) - box::use(configs/enabled_schemes[enabled_schemes]) + options(box.path = config::get("form_configs_path")) + box::use(enabled/enabled_schemes[enabled_schemes]) files_to_watch <- c( - "configs/enabled_schemes.R", + fs::path(config::get("form_configs_path"), "enabled", "enabled_schemes.R"), "modules/scheme_generator.R", "modules/utils.R" ) scheme_names <- enabled_schemes - scheme_file <- paste0("configs/schemas/", scheme_names, ".xlsx") + scheme_file <- paste0(config::get("form_configs_path"), "/schemas/", scheme_names, ".xlsx") scheme_file <- stats::setNames(scheme_file, scheme_names) if (!all(file.exists(scheme_file))) cli::cli_abort(c("Отсутствуют файлы схем для следующих наименований:", paste("-", names(scheme_file)[!file.exists(scheme_file)]))) - db_files <- paste0("db/", scheme_names, ".sqlite") + db_files <- paste0(config::get("form_db_path"), "/", scheme_names, ".sqlite") hash_file <- "temp/schema_hash.rds" @@ -52,8 +70,10 @@ check_and_init_scheme = function() { # в ином случае - проверяем кэш } else { - + saved_hash <- readRDS(hash_file) + print(exist_hash) + print(saved_hash) # если данные были изменены проводим реинициализацию таблицы и схемы if (!all(exist_hash == saved_hash)) { @@ -79,7 +99,7 @@ init_scheme = function(scheme_file) { modules/scheme_generator[scheme_R6] ) - if (!dir.exists("db")) dir.create("db") + if (!dir.exists(config::get("form_db_path"))) dir.create(config::get("form_db_path")) cli::cli_h1("Инициализация схемы") schms <- purrr::map2(