-
Notifications
You must be signed in to change notification settings - Fork 1
/
transload
195 lines (174 loc) · 5.57 KB
/
transload
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env ruby
require_relative 'lib/transloader'
# Adjust the logging level based on the "LOG_LEVEL" environment
# variable.
LOG_LEVELS = {
"ERROR" => :error,
"WARN" => :warn,
"INFO" => :info,
"DEBUG" => :debug,
"TRACE" => :trace
}
env_log_level = ENV["LOG_LEVEL"].nil? ? nil : ENV["LOG_LEVEL"].upcase
level = LOG_LEVELS.fetch(env_log_level, :info)
SemanticLogger.default_level = level
SemanticLogger.add_appender(io: $stdout)
$logger = SemanticLogger['Transload']
def get_station(options, http_client)
$logger.debug "Creating station instance for #{options.provider}"
case options.provider
when "environment_canada"
Transloader::EnvironmentCanadaStation.new(
database_url: options.database_url,
http_client: http_client,
id: options.station_id
)
when "data_garrison"
Transloader::DataGarrisonStation.new(
database_url: options.database_url,
http_client: http_client,
id: options.station_id,
properties: { user_id: options.user_id }
)
when "campbell_scientific"
Transloader::CampbellScientificStation.new(
database_url: options.database_url,
http_client: http_client,
id: options.station_id,
properties: { data_urls: options.data_urls }
)
when "klrs_h_energy"
Transloader::KLRSHistoricalEnergyStation.new(
database_url: options.database_url,
http_client: http_client,
id: options.station_id,
properties: { data_paths: options.data_paths }
)
when "klrs_h_weather"
Transloader::KLRSHistoricalWeatherStation.new(
database_url: options.database_url,
http_client: http_client,
id: options.station_id,
properties: { data_paths: options.data_paths }
)
end
end
def get_metadata(options, http_client)
$logger.debug "Getting metadata"
station = get_station(options, http_client)
if (!options.overwrite && station.metadata != {})
$logger.warn "Existing metadata found, will not overwrite."
return false
else
station.download_metadata()
end
end
def put_metadata(options, http_client)
$logger.debug "Putting metadata"
station = get_station(options, http_client)
station.upload_metadata(options.destination,
allowed: options.allowed,
blocked: options.blocked)
end
# TODO: Use Station to do this so that other store types can be used
def set_metadata(options)
$logger.debug "Editing metadata"
# Data Garrison requires additional namespacing as both user id and
# station id are needed.
if options.provider == Transloader::DataGarrisonStation::PROVIDER_NAME
station_id = "#{options.user_id}-#{options.station_id}"
else
station_id = options.station_id
end
# Convert value to float, if possible. Otherwise leave as a string.
begin
value = Float(options.value)
rescue
value = options.value
end
store = Transloader::StationStore.new({
provider: options.provider,
station: station_id,
database_url: options.database_url
})
# Work in reverse to convert an array of keys to a set of nested
# hashes
new_metadata = options.keys.reverse.reduce(nil) do |memo, key|
if memo.nil?
memo = { key.to_sym => value }
else
memo = { key.to_sym => memo }
end
memo
end
store.merge_metadata(new_metadata)
puts JSON.pretty_generate(store.metadata)
end
def show_metadata(options)
$logger.debug "Showing metadata"
# Data Garrison requires additional namespacing as both user id and
# station id are needed.
if options.provider == Transloader::DataGarrisonStation::PROVIDER_NAME
station_id = "#{options.user_id}-#{options.station_id}"
else
station_id = options.station_id
end
store = Transloader::StationStore.new({
provider: options.provider,
station: station_id,
database_url: options.database_url
})
value = store.metadata.get(options.keys.first.to_sym)
puts JSON.pretty_generate({ options.keys.first => value })
end
def get_observations(options, http_client)
$logger.debug "Getting observations"
station = get_station(options, http_client)
station.download_observations(options.date)
end
def put_observations(options, http_client)
$logger.debug "Putting observations"
station = get_station(options, http_client)
station.upload_observations(options.destination, options.date,
allowed: options.allowed,
blocked: options.blocked)
end
# Parse Args
parser = Transloader::CommandLineOptionParser.new
args = parser.parse(ARGV)
verb = args[0]
noun = args[1]
options = args[2]
# Create re-usable HTTP client with pre-set options
http_client = Transloader::HTTP.new(
auth: options.http_auth,
headers: options.http_headers
)
begin
# Determine action
if verb == :get && noun == :metadata
get_metadata(options, http_client)
elsif verb == :put && noun == :metadata
put_metadata(options, http_client)
elsif verb == :set && noun == :metadata
set_metadata(options)
elsif verb == :show && noun == :metadata
show_metadata(options)
elsif verb == :get && noun == :observations
get_observations(options, http_client)
elsif verb == :put && noun == :observations
put_observations(options, http_client)
end
rescue Transloader::HTTPError, SensorThings::HTTPError => e
$logger.fatal "#{e.message}"
$logger.fatal "#{e.response.uri}"
$logger.fatal "#{e.response.code} #{e.response.message}"
e.response.each_header do |header, value|
$logger.fatal "#{header}: #{value}"
end
$logger.fatal "#{e.response.body}"
exit 1
rescue Transloader::Error, SensorThings::Error => e
$logger.fatal "#{e.message}"
exit 2
end