-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.R
38 lines (30 loc) · 1.41 KB
/
global.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
library(mongolite)
library(leaflet)
library(dplyr)
library(dotenv)
load_dot_env(file = ".env.development")
Sys.setenv(TZ="America/Chicago") # TODO: hardcoded for now. However, this should depend on the location of the device.
if (Sys.getenv("MONGODB_PASSWORD") == '') {
connUrl = paste0("mongodb://",
Sys.getenv("MONGODB_ACCOUNT"), Sys.getenv("MONGODB_BASE_URL"), ":", Sys.getenv("MONGODB_PORT"), "/",
Sys.getenv("MONGODB_DBNAME"))
} else {
connUrl = paste0("mongodb://",
Sys.getenv("MONGODB_ACCOUNT"), ":", Sys.getenv("MONGODB_PASSWORD"), "@",
Sys.getenv("MONGODB_ACCOUNT"), Sys.getenv("MONGODB_BASE_URL"), ":", Sys.getenv("MONGODB_PORT"), "/",
Sys.getenv("MONGODB_DBNAME"), "?ssl=true")
}
db = mongo(url = connUrl, collection = "deviceevents")
db_data = collect(db$find('{}')) # TODO: optimize collection
db_data <- jsonlite::flatten(db_data)
matches <- stringr::str_match(names(db_data), "Classes.(.+)")
db_class_ids_idx <- which(!is.na(matches[, 2]))
db_class_ids <- matches[db_class_ids_idx, 2]
db_data$DeviceName <- NULL
db_data$DateTimeT <- as.POSIXct(db_data$DateTime, tz=getOption("tz"))
# Generate random set of locations
devicesDB <- function() {
db_deviceinfos = mongo(url = connUrl, collection = "deviceinfos")
return (collect(db_deviceinfos$find('{}')))
}
db_devices <- devicesDB()