Skip to content

Commit 524bb35

Browse files
committed
refactoring
1 parent c70f649 commit 524bb35

File tree

1 file changed

+60
-52
lines changed

1 file changed

+60
-52
lines changed

R/runTimeSeries.R

+60-52
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
getTimeSeries <- function(
18-
connection = NULL,
19-
tempEmulationSchema = NULL,
20-
cdmDatabaseSchema,
21-
cohortDatabaseSchema = cdmDatabaseSchema,
22-
cohortTable = "cohort",
23-
runCohortTimeSeries = TRUE,
24-
runDataSourceTimeSeries = FALSE,
25-
timeSeriesMinDate = as.Date("1980-01-01"),
26-
timeSeriesMaxDate = as.Date(Sys.Date()),
27-
stratifyByGender = TRUE,
28-
stratifyByAgeGroup = TRUE,
29-
cohortIds = NULL) {
30-
31-
if (all(!runCohortTimeSeries, !runDataSourceTimeSeries)) {
32-
warning(
33-
" - Both Cohort Time Series and Data Source Time Series are set to FALSE. Exiting time series diagnostics."
34-
)
35-
return(NULL)
36-
}
37-
start <- Sys.time()
38-
39-
ParallelLogger::logTrace(" - Creating Andromeda object to collect results")
40-
resultsInAndromeda <- Andromeda::andromeda()
41-
42-
if (runCohortTimeSeries) {
43-
sqlCount <-
44-
" SELECT cohort_definition_id, COUNT(*) count
45-
FROM @cohort_database_schema.@cohort_table
46-
{@cohort_ids != ''} ? { where cohort_definition_id IN (@cohort_ids)}
47-
GROUP BY cohort_definition_id;"
48-
resultsInAndromeda$cohortCount <- renderTranslateQuerySql(
49-
connection = connection,
50-
sql = sqlCount,
51-
cohort_database_schema = cohortDatabaseSchema,
52-
cohort_ids = cohortIds,
53-
cohort_table = cohortTable
54-
)
55-
if (resultsInAndromeda$cohortCount %>%
56-
dplyr::summarise(n = dplyr::n()) %>%
57-
dplyr::pull(.data$n) == 0) {
58-
warning("Please check if cohorts are instantiated. Exiting cohort time series.")
59-
return(NULL)
60-
}
61-
}
62-
## Calendar period----
17+
createCalendarPeriodsTable <- function(connection, tempEmulationSchema, timeSeriesMinDate, timeSeriesMaxDate) {
6318
ParallelLogger::logTrace(" - Preparing calendar table for time series computation.")
6419
# note calendar span is created based on all dates in observation period table,
6520
# with 1980 cut off/left censor (arbitrary choice)
@@ -70,7 +25,7 @@ getTimeSeries <- function(
7025
) %>% as.integer())
7126
maxYear <-
7227
clock::get_year(timeSeriesMaxDate) %>% as.integer()
73-
28+
7429
calendarQuarter <-
7530
dplyr::tibble(
7631
periodBegin = clock::date_seq(
@@ -81,7 +36,7 @@ getTimeSeries <- function(
8136
) %>%
8237
dplyr::mutate(periodEnd = clock::add_months(x = .data$periodBegin, n = 3) - 1) %>%
8338
dplyr::mutate(calendarInterval = "q")
84-
39+
8540
calendarMonth <-
8641
dplyr::tibble(
8742
periodBegin = clock::date_seq(
@@ -92,7 +47,7 @@ getTimeSeries <- function(
9247
) %>%
9348
dplyr::mutate(periodEnd = clock::add_months(x = .data$periodBegin, n = 1) - 1) %>%
9449
dplyr::mutate(calendarInterval = "m")
95-
50+
9651
calendarYear <-
9752
dplyr::tibble(
9853
periodBegin = clock::date_seq(
@@ -103,13 +58,13 @@ getTimeSeries <- function(
10358
) %>%
10459
dplyr::mutate(periodEnd = clock::add_years(x = .data$periodBegin, n = 1) - 1) %>%
10560
dplyr::mutate(calendarInterval = "y")
106-
61+
10762
timeSeriesDateRange <- dplyr::tibble(
10863
periodBegin = timeSeriesMinDate,
10964
periodEnd = timeSeriesMaxDate,
11065
calendarInterval = "c"
11166
)
112-
67+
11368
calendarPeriods <-
11469
dplyr::bind_rows(
11570
calendarMonth,
@@ -120,7 +75,7 @@ getTimeSeries <- function(
12075
dplyr::distinct() %>%
12176
dplyr::arrange(.data$periodBegin, .data$periodEnd, .data$calendarInterval) %>%
12277
dplyr::mutate(timeId = dplyr::row_number())
123-
78+
12479
ParallelLogger::logTrace(" - Inserting calendar periods")
12580
DatabaseConnector::insertTable(
12681
connection = connection,
@@ -133,7 +88,60 @@ getTimeSeries <- function(
13388
tempEmulationSchema = tempEmulationSchema,
13489
camelCaseToSnakeCase = TRUE
13590
)
91+
}
92+
93+
getTimeSeries <- function(
94+
connection = NULL,
95+
tempEmulationSchema = NULL,
96+
cdmDatabaseSchema,
97+
cohortDatabaseSchema = cdmDatabaseSchema,
98+
cohortTable = "cohort",
99+
runCohortTimeSeries = TRUE,
100+
runDataSourceTimeSeries = FALSE,
101+
timeSeriesMinDate = as.Date("1980-01-01"),
102+
timeSeriesMaxDate = as.Date(Sys.Date()),
103+
stratifyByGender = TRUE,
104+
stratifyByAgeGroup = TRUE,
105+
cohortIds = NULL) {
106+
107+
if (all(!runCohortTimeSeries, !runDataSourceTimeSeries)) {
108+
warning(
109+
" - Both Cohort Time Series and Data Source Time Series are set to FALSE. Exiting time series diagnostics."
110+
)
111+
return(NULL)
112+
}
113+
start <- Sys.time()
114+
115+
ParallelLogger::logTrace(" - Creating Andromeda object to collect results")
116+
resultsInAndromeda <- Andromeda::andromeda()
136117

118+
if (runCohortTimeSeries) {
119+
sqlCount <-
120+
" SELECT cohort_definition_id, COUNT(*) count
121+
FROM @cohort_database_schema.@cohort_table
122+
{@cohort_ids != ''} ? { where cohort_definition_id IN (@cohort_ids)}
123+
GROUP BY cohort_definition_id;"
124+
resultsInAndromeda$cohortCount <- renderTranslateQuerySql(
125+
connection = connection,
126+
sql = sqlCount,
127+
cohort_database_schema = cohortDatabaseSchema,
128+
cohort_ids = cohortIds,
129+
cohort_table = cohortTable
130+
)
131+
if (resultsInAndromeda$cohortCount %>%
132+
dplyr::summarise(n = dplyr::n()) %>%
133+
dplyr::pull(.data$n) == 0) {
134+
warning("Please check if cohorts are instantiated. Exiting cohort time series.")
135+
return(NULL)
136+
}
137+
}
138+
139+
## Create calendar periods table
140+
createCalendarPeriodsTable(connection,
141+
tempEmulationSchema,
142+
timeSeriesMinDate,
143+
timeSeriesMaxDate)
144+
137145
tsSetUpSql <- "-- #time_series
138146
DROP TABLE IF EXISTS #time_series;
139147
DROP TABLE IF EXISTS #c_time_series1;

0 commit comments

Comments
 (0)