-
Notifications
You must be signed in to change notification settings - Fork 0
/
shadow_to_prod_stresscams.R
94 lines (71 loc) · 1.81 KB
/
shadow_to_prod_stresscams.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
message(Sys.time(), "\n\n")
x <- na.omit(stringr::str_match(commandArgs(), "--file=(.+)")[,2])
if (length(x)) setwd(dirname(x))
decolonize <- function(s) {
s <- stringr::str_replace_all(s, "[[:punct:]]", "_")
stringr::str_replace_all(s, "[[:space:]]", "__")
}
suppressPackageStartupMessages(
library(loggit)
)
set_logfile(
glue::glue(
"{getwd()}/log/shadow_to_prod_stresscams_{decolonize(Sys.time())}.log"
)
)
set_timestamp_format("%Y-%m-%dT%H:%M:%OS4%z")
message("Execution start")
real_time_rq <- httr::RETRY("GET", "example.com")
real_time <- httr::parse_http_date(httr::headers(real_time_rq)$date)
off <- Sys.time() - real_time
loggit(
"INFO",
glue::glue("Offset of this server and real time is {format(off)}"),
offset_s = as.double(off, units = "secs")
)
source("secret.R")
source("utils/initializers.R")
source("utils/sql_constructors.R")
suppressPackageStartupMessages({
library(DBI)
library(dplyr)
library(dbplyr)
library(RSQLite)
})
message("Connecting to Shadow DB")
con_sh_st <- etl_connect_shadow("stresscams")
rawuids_to_push <- tbl(con_sh_st, "from_raw") %>%
filter(pushed_to_prod == 0) %>%
collect() %>%
pull(rawuid) %>%
head(1500) %>%
unique()
loggit(
"INFO",
"Found stresscam records to push",
rows = length(rawuids_to_push)
)
if (length(rawuids_to_push) == 0) {
stop("Ending script execution.", call. = FALSE)
set_logfile(logfile = NULL)
}
rows_aff <- etl_upsert_stresscams(
"stresscam_ratings",
"stresscam_ratings",
rawuids_to_push,
unicity = "stresscam_device_id_timestamp_utc_key"
)
loggit(
"INFO",
"stresscam_ratings",
rows = rows_aff
)
rows_aff <- etl_mark_pushed(con_sh_st, "from_raw", rawuids_to_push)
loggit(
"INFO",
"from_raw",
rows = rows_aff
)
dbDisconnect(con_sh_st)
message("Execution end")
set_logfile(logfile = NULL)