Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Imports:
R6,
ResultModelManager,
yaml,
stringr
stringr,
ParallelLogger
Remotes:
javier-gracia-tabuenca-tuni/DatabaseConnector@bigquery-DBI-2
Suggests:
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ export(createPlotFromResults)
export(createReport)
export(createStratifiedCodeCountsTable)
export(create_api)
export(fct_setUpLogger)
export(getCDMSource)
export(getCodeCounts)
export(getCodeCounts_memoise)
export(getConceptsWithCodeCounts)
export(getConceptsWithCodeCounts_memoise)
export(getLogs)
export(helper_FinnGen_getDatabaseFile)
export(helper_FinnGen_getDatabaseFileCounts)
export(helper_createSqliteDatabaseFromDatabase)
export(pruneLevelsFromResults)
export(runApiServer)
export(sendFeedback)
importFrom(DatabaseConnector,connect)
importFrom(DatabaseConnector,createConnectionDetails)
importFrom(DatabaseConnector,createDbiConnectionDetails)
Expand Down
59 changes: 59 additions & 0 deletions R/fct_logs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

#' Set Up Logger
#'
#' Sets up a logger with console and file appenders for logging.
#'
#' @param sToken The session token to be added to log entries
#'
#' @return A logger object.
#'
#' @export
fct_setUpLogger <- function(sToken=""){

timestamp <- timestamp <- as.character(as.numeric(format(Sys.time(), "%d%m%Y%H%M%OS2"))*100)
folderWithLog <- file.path(tempdir(), paste0("logs", timestamp))
dir.create(folderWithLog, showWarnings = FALSE)
fileName <- file.path(folderWithLog, "log.txt")
options(parallelLoggerTempFile = fileName)

logger <- ParallelLogger::createLogger(
threshold = "TRACE",
appenders = list(
# to console for tracking
.createConsoleAppenderForSandboxLogging(sToken=sToken),
# to file for showing in app
ParallelLogger::createFileAppender(
fileName = fileName,
layout = ParallelLogger::layoutTimestamp
)
)
)
ParallelLogger::clearLoggers()
#addDefaultFileLogger(file.path(folderWithLog, "log2.txt"))
ParallelLogger::registerLogger(logger)

}


#' Create Console Appender for Sandbox Logging
#'
#' Creates a console appender for sandbox logging with a specified layout.
#'
#' @param layout A layout function for the logger. Defaults to ParallelLogger::layoutParallel.
#' @param sToken The session token to be added to log entries
#'
#' @return An appender object for logging.
#'
.createConsoleAppenderForSandboxLogging <- function(layout = ParallelLogger::layoutParallel,sToken) {
appendFunction <- function(this, level, message, echoToConsole) {
# Avoid note in check:
# Should add session id (session$token) to help group logs by session in central log database.
# paste0("[sandbox-co2-log] session:", session$token, message)
missing(this)
message <- paste0("[sandbox-co2-log] --session:",sToken,"-- ",message)
writeLines(message, con = stderr())
}
appender <- list(appendFunction = appendFunction, layout = layout)
class(appender) <- "Appender"
return(appender)
}
2 changes: 1 addition & 1 deletion R/getCDMSource.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#'
getCDMSource <- function(
CDMdbHandler) {
message("getCDMSource")
ParallelLogger::logInfo("getCDMSource: Getting CDM source information")
#
# VALIDATE
#
Expand Down
2 changes: 1 addition & 1 deletion R/getCodeCounts.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ getCodeCounts <- function(
CDMdbHandler,
conceptId,
codeCountsTable = "code_counts") {
message("getCodeCounts: ", conceptId)
ParallelLogger::logInfo("getCodeCounts: Getting code counts for conceptId: ", conceptId)
#
# VALIDATE
#
Expand Down
2 changes: 2 additions & 0 deletions R/getConceptsWithCodeCounts.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ getConceptsWithCodeCounts <- function(
vocabularyDatabaseSchema <- CDMdbHandler$vocabularyDatabaseSchema
resultsDatabaseSchema <- CDMdbHandler$resultsDatabaseSchema

ParallelLogger::logInfo("getConceptsWithCodeCounts: Getting concepts with code counts")

#
# FUNCTION
#
Expand Down
17 changes: 17 additions & 0 deletions R/getLogs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#' Get logs from ParallelLogger temp file
#'
#' @description
#' Retrieves log messages from the ParallelLogger temporary log file.
#' This may help track internal API events, errors, and debug information.
#'
#' @return A character vector with each log line as a string.
#'
#' @export
getLogs <- function() {
ParallelLogger::logInfo("getLogs: Getting logs")
#
fileName <- getOption("parallelLoggerTempFile")
logs <- readLines(fileName)
return(logs)
}
6 changes: 5 additions & 1 deletion R/runApiServer.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ runApiServer <- function(
# VALIDATE
#

fct_setUpLogger()
ParallelLogger::logInfo("Starting ROMOPAPI API server")

if (is.null(cohortTableHandlerConfig)) {
message("No path to database config provided. Using the test counts only database.")
ParallelLogger::logInfo("No path to database config provided. Using the test counts only database.")
# if not provided, use the test counts only database
test_databasesConfig <- HadesExtras_readAndParseYaml(
pathToYalmFile = system.file("testdata", "config", "onlyCounts_databasesConfig.yml", package = "ROMOPAPI"),
Expand All @@ -58,6 +61,7 @@ runApiServer <- function(
}

if (buildCountsTable == TRUE) {
ParallelLogger::logInfo("Building code counts tables")
createCodeCountsTables(CDMdbHandler, codeCountsTable = "code_counts")
}

Expand Down
21 changes: 21 additions & 0 deletions R/sendFeedback.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@



#' Send feedback to the API server
#'
#' @param feedback A character string containing the feedback to send.
#'
#' @return NULL
#'
#' @export
sendFeedback <- function(feedback) {

#
# VALIDATE
#
feedback |> checkmate::assertCharacter()
#
# FUNCTION
#
ParallelLogger::logInfo("FEEDBACK: ", feedback)
}
16 changes: 16 additions & 0 deletions inst/plumber/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,19 @@ function(res) {
# Read and return the file content
readChar(file_path, file.info(file_path)$size)
}

#* Get the logs
#* @get /getLogs
function() {
logs <- getLogs()
return(logs)
}

#* Send feedback to the API server
#* @post /sendFeedback
function(res, feedback = "") {
feedback <- as.character(feedback)
sendFeedback(feedback)
res$status <- 200
return(list(message = "Feedback sent"))
}
4 changes: 2 additions & 2 deletions inst/testdata/config/atlasDev_databasesConfig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ BQ5k:
bigint: integer64
tempEmulationSchema: atlas-development-270609.sandbox
cdm:
cdmDatabaseSchema: atlas-development-270609.finngen_omop_r13_v3
vocabularyDatabaseSchema: atlas-development-270609.finngen_omop_r13_v3
cdmDatabaseSchema: atlas-development-270609.finngen_omop_r13_v3_5k
vocabularyDatabaseSchema: atlas-development-270609.finngen_omop_r13_v3_5k
resultsDatabaseSchema: atlas-development-270609.sandbox
cohortTable:
cohortDatabaseSchema: atlas-development-270609.sandbox
Expand Down
Binary file modified inst/testdata/data/FinnGenR13_countsOnly.sqlite
Binary file not shown.
4 changes: 3 additions & 1 deletion inst/testdata/data/createTestingData.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ conceptIds <- c(
317009, # Snomed: Asthma
45596282, # ICD10: Asthma
21601855, # ATC level 4: C10AA (Statins)
2010001615 # test endpoint
#2010001615, # test endpoint
320136 # Big graph, parent of Asthma snomed concept (Disorders of the respiratory system)
)

CDMdbHandler <- HadesExtras_createCDMdbHandlerFromList(test_cohortTableHandlerConfig, loadConnectionChecksLevel = "basicChecks")
Expand Down Expand Up @@ -45,3 +46,4 @@ dplyr::tbl(connection, "stratified_code_counts") |>
dplyr::count() |>
dplyr::pull(n) |>
expect_gt(0)

22 changes: 22 additions & 0 deletions man/dot-createConsoleAppenderForSandboxLogging.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/fct_setUpLogger.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions man/getLogs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/sendFeedback.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testmanual/manualtest-runApiServer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


databaseConfig <- yaml::read_yaml("inst/testdata/config/onlyCounts_databasesConfig.yml")

ROMOPAPI::runApiServer(
cohortTableHandlerConfig = databaseConfig$cohortTableHandler,
buildCountsTable = FALSE
)

# http://127.0.0.1:8585/__docs__/