-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_targets.R
More file actions
126 lines (117 loc) · 3.37 KB
/
Copy path_targets.R
File metadata and controls
126 lines (117 loc) · 3.37 KB
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
# packages needed for targets
library(targets)
library(tarchetypes)
# this has helper functions
source("R/utilities.r")
# functions for the actual workflow
source("R/functions.r")
options(tidyverse.quiet = TRUE)
# magrittr will be available for every step
tar_option_set(packages = c("magrittr"))
# If the token is older than this long refresh it
token_lifespan <- as.difftime(60, units='mins')
token_target_path <- tar_path(access_token)
bucket_name <- Sys.getenv('BUCKET_NAME')
folder_name <- Sys.getenv('FOLDER_NAME')
if(!exists('process_date'))
{
cat('No need to date to run\n')
process_date <- NULL
} else{
cat(process_date)
}
# if we want to hardcode a data, but there has to be a better way
# start_now <- as.Date('2021-01-03')
list(
tar_force(
access_token,
get_token(api_key=Sys.getenv('ECOBEE_API_KEY'), refresh_token=Sys.getenv('ECOBEE_REFRESH_TOKEN')),
# if the token is too old or doesn't exist, run
if_not_na(Sys.time() - file.mtime(token_target_path), token_lifespan) >= token_lifespan
)
, tar_target(
thermostat_info,
get_thermostat_info(access_token)
)
, tar_target(
thermostat_ids,
get_thermostat_ids(thermostat_info)
)
, tar_target(
start_date,
# if we want to hardcode a data, but there has to be a better way
# start_now
compute_start_date(thermostat_info, override=process_date)
)
, tar_target(
end_date,
# if we want to hardcode a data, but there has to be a better way
# start_now + 1
compute_end_date(thermostat_info, override=process_date)
)
, tar_target(
start_interval,
compute_start_interval(thermostat_info)
)
, tar_target(
end_interval,
compute_end_interval(start_interval)
)
, tar_target(
report,
get_report(
startDate=start_date, endDate=end_date,
startInterval=start_interval, endInterval=end_interval,
thermostats=thermostat_ids,
access_token=access_token
)
)
, tar_target(
thermostat_names,
get_thermostat_names(thermostat_info)
)
, tar_target(
central_thermostat_info,
extract_thermostat_info(report)
)
, tar_target(
sensor_info,
extract_sensor_info(report)
)
, tar_target(
all_info,
combine_thermostat_sensors(central_thermostat_info, sensor_info, thermostat_names)
)
, tar_target(
data_date,
# if we want to hardcode a data, but there has to be a better way
# start_now
get_local_date(thermostat_info, override=process_date)
)
, tar_target(
filename,
paste(data_date, 'csv', sep='.')
)
, tar_target(
filepath,
# tempfile(fileext='.csv')
here::here('data', filename)
)
, tar_target(
write_data,
write_file(all_info, file=filename),
format='file'
)
, tar_target(
put_to_bucket,
write_to_bucket(file=write_data, folder_name=folder_name, filename=filename, bucket_name=bucket_name)
# aws.s3::put_object(
# file=write_data, object=sprintf('%s/%s', folder_name, filename), bucket=bucket_name
# )
)
, tar_change(
delete_file,
delete_file_if_exists(write_data),
change=put_to_bucket
)
)