forked from Sage-Bionetworks/synapseusagereports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport_data_query.R
executable file
·63 lines (52 loc) · 2.42 KB
/
report_data_query.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
#!/usr/bin/env Rscript
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(readr))
suppressPackageStartupMessages(library(RMySQL))
suppressPackageStartupMessages(library(yaml))
suppressPackageStartupMessages(library(testthat))
suppressPackageStartupMessages(library(optparse))
suppressPackageStartupMessages(library(synapseusagereports))
option_list <- list(
make_option(c("--project_id"), type = "character",
help = "Synapse Project ID.",
dest = "project_id",
metavar = "synapseid"),
make_option(c("--query_type"), type = "character",
help = "Type of query to perform. One of: download, pageview, filedownloadrecord, all",
dest = "query_type"),
make_option(c("--start_date"), type = "character",
help = "Date at UTC (YYYY-MM-DD format)",
dest = "start_date"),
make_option(c("--end_date"), type = "character",
help = "Date at UTC (YYYY-MM-DD format)",
dest = "end_date"),
make_option(c("--config_file"), type = "character",
help = "YAML database configuration file.",
dest = "config_file",
default = "~/datawarehouse_config.yml")
)
opts <- parse_args(OptionParser(option_list = option_list))
start_date <- lubridate::as_date(opts$start_date)
end_date <- lubridate::as_date(opts$end_date)
date_interval <- lubridate::interval(start_date, lubridate::today("UTC"))
n_months_from_today <- date_interval / lubridate::period(1, "months")
if (n_months_from_today > 6) {
message("Your start date is more than 6 months ago. The results of your queries may be incorrect.")
}
config <- yaml.load_file(opts$config_file)
con <- RMySQL::dbConnect(RMySQL::MySQL(),
user = config$username,
password = config$password,
host = config$host,
dbname = config$db)
if (opts$query_type == "all") {
queryData <- report_data_query_all(con, project_id = opts$project_id,
start_date = start_date,
end_date = end_date)
} else {
queryData <- report_data_query(con, project_id = opts$project_id,
query_type = opts$query_type,
start_date = start_date,
end_date = end_date)
}
cat(readr::format_csv(queryData))