-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.R
158 lines (133 loc) · 4.73 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
library(ggridges)
library(ggplot2)
library(viridis)
library(hrbrthemes)
library(DBI)
library(aws.s3)
library(httr)
library(jsonlite)
library(svglite)
monthConversion <- c(
"January" = "Januar",
"February" = "Februar",
"March" = "März",
"April" = "April",
"May" = "Mai",
"June" = "Juni",
"July" = "Juli",
"August" = "August",
"September" = "September",
"October" = "Oktober",
"November" = "November",
"December" = "Dezember"
)
convertMonth <- function(m) {
return(monthConversion[m])
}
retrieveDataFrameForLakeId <- function(con, lakeId) {
query <- paste0("SELECT * FROM lake_data WHERE lake_id='", lakeId, "';")
dtab <- dbGetQuery(con, query)
data_frame <- as.data.frame(dtab)
data_frame$CST <- with(data_frame, as.Date(timestamp))
data_frame$Month <- months(as.Date(data_frame$CST))
data_frame$Month <- mapply(convertMonth, data_frame$Month)
data_frame$Month <- factor(data_frame$Month, levels = rev(list('Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember')))
return(data_frame)
}
createPlot <- function(data_frame, title, subtitle, filename) {
ggplot(data_frame, aes(x = `temperature`, y = `Month`, fill = after_stat(x))) +
stat_density_ridges(
geom = "density_ridges_gradient", calc_ecdf = TRUE,
quantiles = 3, quantile_lines = TRUE
) +
scale_fill_viridis(name = "Temp. [F]", option = "C") +
labs(title = title, subtitle = subtitle) +
xlab("Temperatur") +
ylab("Month") +
theme(
legend.position = "none",
panel.spacing = unit(0.1, "lines"),
strip.text.x = element_text(size = 8)
)
ggsave(filename)
}
createLinePlot <- function(df, title, subtitle, filename) {
ggplot(df, aes(y = `temperature`, x = `timestamp`)) +
geom_line() +
scale_fill_viridis(name = "Temp. [F]", option = "C") +
labs(title = title, subtitle = subtitle) +
xlab("Monat") +
ylab("Temperatur") +
theme(
legend.position = "none",
panel.spacing = unit(0.1, "lines"),
strip.text.x = element_text(size = 8)
)
ggsave(filename)
}
apiUrl <- function() {
Sys.getenv("API_URL", unset = "http://backend:8080")
}
getLakeIds <- function() {
url <- modify_url(apiUrl(), path = "lake")
response <- GET(url)
fromJSON(content(response, "text"), simplifyVector = FALSE)
}
lakeFromApi <- function(lakeId) {
url <- modify_url(apiUrl(), path = paste0("lake/", lakeId))
response <- GET(url)
if (response$status_code == 200) {
fromJSON(content(response, "text"), simplifyVector = FALSE)
} else {
NULL
}
}
bucketName <- Sys.getenv("BUCKET_NAME", unset = "wooglife")
# Connect to a specific postgres database i.e. Heroku
con <- dbConnect(RPostgres::Postgres(), dbname = Sys.getenv("POSTGRES_DB"),
host = Sys.getenv("POSTGRES_HOSTNAME"),
port = 5432,
user = Sys.getenv("POSTGRES_USER"),
password = Sys.getenv("POSTGRES_PASSWORD"))
lakes <- getLakeIds()$lakes
for (i in seq_along(lakes)) {
lake <- lakes[[i]]
data_frame <- retrieveDataFrameForLakeId(con, lake$id)
apiLake <- lakeFromApi(lake$id)
if (is.null(apiLake)) {
print(paste0("failed to retrieve data for '", lake$id, "'"))
next
}
dateFormat <- "%d.%m.%Y"
firstDate <- format(head(data_frame$CST[[1]]), format = dateFormat)
lastDate <- format(tail(data_frame$CST)[[1]], format = dateFormat)
subtitle <- paste(firstDate, "-", lastDate)
title <- paste0(lake$name, " (", nrow(data_frame), " Datenpunkte)")
filename <- paste0(lake$id, ".svg")
filename_line <- paste0(lake$id, "_line.svg")
createPlot(data_frame, title, subtitle, filename)
# we only care about a shorter timeframe with the linegraph
df <- tail(data_frame, 1440)
firstDate <- format(head(df$CST[[1]]), format = dateFormat)
lastDate <- format(tail(df$CST)[[1]], format = dateFormat)
subtitle <- paste(firstDate, "-", lastDate)
title <- paste0(lake$name, " (", nrow(df), " Datenpunkte)")
createLinePlot(df, title, subtitle, filename_line)
# `region` must be empty, the s3 library automatically transforms the url to this: `{region}.{endpoint}`
# this doesn't work well with the exoscale endpoint since it's `sos-{region}.exo.io`
tryCatch(
put_object(file = filename, object = filename, bucket = bucketName, region = "", acl = "public-read"),
error = function(err) {
print(paste0("failed to put '", filename, "' into '", bucketName, "' bucket:"))
print(err)
}
)
tryCatch(
put_object(file = filename_line, object = filename_line, bucket = bucketName, region = "", acl = "public-read"),
error = function(err) {
print(paste0("failed to put '", filename_line, "' into '", bucketName, "' bucket:"))
print(err)
}
)
}
dbDisconnect(con)