-
Notifications
You must be signed in to change notification settings - Fork 5
/
storage_timescaledb_sql.go
356 lines (341 loc) · 11.2 KB
/
storage_timescaledb_sql.go
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package main
const createTableSQL = `
CREATE TABLE IF NOT EXISTS weather (
time timestamp WITH TIME ZONE NOT NULL,
stationname text NULL,
barometer float4 NULL,
intemp float4 NULL,
inhumidity float4 NULL,
outtemp float4 NULL,
windspeed float4 NULL,
windspeed10 float4 NULL,
winddir float4 NULL,
windchill float4 NULL,
heatindex float4 NULL,
extratemp1 float4 NULL,
extratemp2 float4 NULL,
extratemp3 float4 NULL,
extratemp4 float4 NULL,
extratemp5 float4 NULL,
extratemp6 float4 NULL,
extratemp7 float4 NULL,
soiltemp1 float4 NULL,
soiltemp2 float4 NULL,
soiltemp3 float4 NULL,
soiltemp4 float4 NULL,
leaftemp1 float4 NULL,
leaftemp2 float4 NULL,
leaftemp3 float4 NULL,
leaftemp4 float4 NULL,
outhumidity float4 NULL,
extrahumidity1 float4 NULL,
extrahumidity2 float4 NULL,
extrahumidity3 float4 NULL,
extrahumidity4 float4 NULL,
extrahumidity5 float4 NULL,
extrahumidity6 float4 NULL,
extrahumidity7 float4 NULL,
rainrate float4 NULL,
rainincremental float4 NULL,
uv float4 NULL,
solarjoules float4 NULL,
solarwatts float4 NULL,
radiation float4 NULL,
stormrain float4 NULL,
stormstart timestamp WITH TIME ZONE NULL,
dayrain float4 NULL,
monthrain float4 NULL,
yearrain float4 NULL,
dayet float4 NULL,
monthet float4 NULL,
yearet float4 NULL,
soilmoisture1 float4 NULL,
soilmoisture2 float4 NULL,
soilmoisture3 float4 NULL,
soilmoisture4 float4 NULL,
leafwetness1 float4 NULL,
leafwetness2 float4 NULL,
leafwetness3 float4 NULL,
leafwetness4 float4 NULL,
insidealarm int NULL,
rainalarm int NULL,
outsidealarm1 int NULL,
outsidealarm2 int NULL,
extraalarm1 int NULL,
extraalarm2 int NULL,
extraalarm3 int NULL,
extraalarm4 int NULL,
extraalarm5 int NULL,
extraalarm6 int NULL,
extraalarm7 int NULL,
extraalarm8 int NULL,
soilleafalarm1 int NULL,
soilleafalarm2 int NULL,
soilleafalarm3 int NULL,
soilleafalarm4 int NULL,
txbatterystatus int NULL,
consbatteryvoltage float4 NULL,
stationbatteryvoltage float4 NULL,
forecasticon int NULL,
forecastrule int NULL,
sunrise TIMESTAMP WITH TIME ZONE NULL,
sunset TIMESTAMP WITH TIME ZONE NULL
);`
const createExtensionSQL = `CREATE EXTENSION IF NOT EXISTS timescaledb;`
const createHypertableSQL = `SELECT create_hypertable('weather', 'time', if_not_exists => true);`
const createCircAvgStateTypeSQL = `CREATE TYPE circular_avg_state AS (
sin_sum real,
cos_sum real,
accum real
);
`
const createCircAvgStateFunctionSQL = `CREATE OR REPLACE FUNCTION circular_avg_state_accumulator(state circular_avg_state, reading real)
RETURNS circular_avg_state
STRICT
IMMUTABLE
LANGUAGE plpgsql
AS $$
DECLARE
sin_sum real;
cos_sum real;
BEGIN
sin_sum := state.sin_sum + SIND(reading);
cos_sum := state.cos_sum + COSD(reading);
RETURN ROW(sin_sum, cos_sum, state.accum + 1)::circular_avg_state;
END;
$$;
`
const createCircAvgCombinerFunctionSQL = `CREATE OR REPLACE FUNCTION circular_avg_state_combiner(state1 circular_avg_state, state2 circular_avg_state)
RETURNS circular_avg_state
STRICT
IMMUTABLE
LANGUAGE plpgsql
AS $$
DECLARE
sin_sum real;
cos_sum real;
accum_sum real;
BEGIN
sin_sum := state1.sin_sum + state2.sin_sum;
cos_sum := state1.cos_sum + state2.cos_sum;
accum_sum := state1.accum + state2.accum;
RETURN ROW(sin_sum, cos_sum, accum_sum)::circular_avg_state;
END;
$$;`
const createCircAvgFinalizerFunctionSQL = `CREATE OR REPLACE FUNCTION circular_avg_final(state circular_avg_state)
RETURNS real
STRICT
IMMUTABLE
LANGUAGE plpgsql
AS $$
DECLARE
sin_avg real;
cos_avg real;
atan2_result real;
final_result real;
BEGIN
sin_avg := state.sin_sum / state.accum;
cos_avg := state.cos_sum / state.accum;
atan2_result := ATAN2D(sin_avg, cos_avg);
if atan2_result < 0 THEN
final_result := atan2_result + 360;
ELSE
final_result := atan2_result;
END IF;
RETURN final_result;
END;
$$;
`
const createCircAvgAggregateFunctionSQL = `CREATE OR REPLACE AGGREGATE circular_avg (real)
(
SFUNC = circular_avg_state_accumulator,
STYPE = circular_avg_state,
COMBINEFUNC = circular_avg_state_combiner,
FINALFUNC = circular_avg_final,
INITCOND = '(0,0,0)',
PARALLEL = SAFE
);`
const create1mViewSQL = `CREATE MATERIALIZED VIEW IF NOT EXISTS weather_1m
WITH (timescaledb.continuous, timescaledb.materialized_only = false)
AS
SELECT
time_bucket('1 minute', time) as bucket,
stationname,
avg(barometer) as barometer,
max(barometer) as max_barometer,
min(barometer) as min_barometer,
avg(intemp) as intemp,
max(intemp) as max_intemp,
min(intemp) as min_intemp,
avg(extratemp1) as extratemp1,
max(extratemp1) as max_extratemp1,
min(extratemp1) as min_extratemp1,
avg(inhumidity) as inhumidity,
max(inhumidity) as max_inhumidity,
min(inhumidity) as min_inhumidity,
avg(outtemp) as outtemp,
max(outtemp) as max_outtemp,
min(outtemp) as min_outtemp,
avg(outhumidity) as outhumidity,
max(outhumidity) as max_outhumidity,
min(outhumidity) as min_outhumidity,
avg(solarwatts) as solarwatts,
avg(solarjoules) as solarjoules,
circular_avg(winddir) as winddir,
avg(windspeed) as windspeed,
max(windspeed) as max_windspeed,
avg(windchill) as windchill,
min(windchill) as min_windchill,
avg(heatindex) as heatindex,
max(heatindex) as max_heatindex,
sum(rainincremental) as period_rain,
avg(rainrate) as rainrate,
max(rainrate) as max_rainrate,
max(dayrain) as dayrain,
max(monthrain) as monthrain,
max(yearrain) as yearrain,
avg(consbatteryvoltage) as consbatteryvoltage,
avg(stationbatteryvoltage) as stationbatteryvoltage
FROM
weather
GROUP BY bucket, stationname;`
const create5mViewSQL = `CREATE MATERIALIZED VIEW IF NOT EXISTS weather_5m
WITH (timescaledb.continuous, timescaledb.materialized_only = false)
AS
SELECT
time_bucket('5 minutes', time) as bucket,
stationname,
avg(barometer) as barometer,
max(barometer) as max_barometer,
min(barometer) as min_barometer,
avg(intemp) as intemp,
max(intemp) as max_intemp,
min(intemp) as min_intemp,
avg(extratemp1) as extratemp1,
max(extratemp1) as max_extratemp1,
min(extratemp1) as min_extratemp1,
avg(inhumidity) as inhumidity,
max(inhumidity) as max_inhumidity,
min(inhumidity) as min_inhumidity,
avg(outtemp) as outtemp,
max(outtemp) as max_outtemp,
min(outtemp) as min_outtemp,
avg(outhumidity) as outhumidity,
max(outhumidity) as max_outhumidity,
min(outhumidity) as min_outhumidity,
avg(solarwatts) as solarwatts,
avg(solarjoules) as solarjoules,
circular_avg(winddir) as winddir,
avg(windspeed) as windspeed,
max(windspeed) as max_windspeed,
avg(windchill) as windchill,
min(windchill) as min_windchill,
avg(heatindex) as heatindex,
max(heatindex) as max_heatindex,
sum(rainincremental) as period_rain,
avg(rainrate) as rainrate,
max(rainrate) as max_rainrate,
max(dayrain) as dayrain,
max(monthrain) as monthrain,
max(yearrain) as yearrain,
avg(consbatteryvoltage) as consbatteryvoltage,
avg(stationbatteryvoltage) as stationbatteryvoltage
FROM
weather
GROUP BY bucket, stationname;`
const create1hViewSQL = `CREATE MATERIALIZED VIEW IF NOT EXISTS weather_1h
WITH (timescaledb.continuous, timescaledb.materialized_only = false)
AS
SELECT
time_bucket('1 hour', time) as bucket,
stationname,
avg(barometer) as barometer,
max(barometer) as max_barometer,
min(barometer) as min_barometer,
avg(intemp) as intemp,
max(intemp) as max_intemp,
min(intemp) as min_intemp,
avg(extratemp1) as extratemp1,
max(extratemp1) as max_extratemp1,
min(extratemp1) as min_extratemp1,
avg(inhumidity) as inhumidity,
max(inhumidity) as max_inhumidity,
min(inhumidity) as min_inhumidity,
avg(outtemp) as outtemp,
max(outtemp) as max_outtemp,
min(outtemp) as min_outtemp,
avg(outhumidity) as outhumidity,
max(outhumidity) as max_outhumidity,
min(outhumidity) as min_outhumidity,
avg(solarwatts) as solarwatts,
avg(solarjoules) as solarjoules,
circular_avg(winddir) as winddir,
avg(windspeed) as windspeed,
max(windspeed) as max_windspeed,
avg(windchill) as windchill,
min(windchill) as min_windchill,
avg(heatindex) as heatindex,
max(heatindex) as max_heatindex,
sum(rainincremental) as period_rain,
avg(rainrate) as rainrate,
max(rainrate) as max_rainrate,
max(dayrain) as dayrain,
max(monthrain) as monthrain,
max(yearrain) as yearrain,
avg(consbatteryvoltage) as consbatteryvoltage,
avg(stationbatteryvoltage) as stationbatteryvoltage
FROM
weather
GROUP BY bucket, stationname;`
const create1dViewSQL = `CREATE MATERIALIZED VIEW IF NOT EXISTS weather_1d
WITH (timescaledb.continuous, timescaledb.materialized_only = false)
AS
SELECT
time_bucket('1 day', time) as bucket,
stationname,
avg(barometer) as barometer,
max(barometer) as max_barometer,
min(barometer) as min_barometer,
avg(intemp) as intemp,
max(intemp) as max_intemp,
min(intemp) as min_intemp,
avg(extratemp1) as extratemp1,
max(extratemp1) as max_extratemp1,
min(extratemp1) as min_extratemp1,
avg(inhumidity) as inhumidity,
max(inhumidity) as max_inhumidity,
min(inhumidity) as min_inhumidity,
avg(outtemp) as outtemp,
max(outtemp) as max_outtemp,
min(outtemp) as min_outtemp,
avg(outhumidity) as outhumidity,
max(outhumidity) as max_outhumidity,
min(outhumidity) as min_outhumidity,
avg(solarwatts) as solarwatts,
avg(solarjoules) as solarjoules,
circular_avg(winddir) as winddir,
avg(windspeed) as windspeed,
max(windspeed) as max_windspeed,
avg(windchill) as windchill,
min(windchill) as min_windchill,
avg(heatindex) as heatindex,
max(heatindex) as max_heatindex,
sum(rainincremental) as period_rain,
avg(rainrate) as rainrate,
max(rainrate) as max_rainrate,
max(dayrain) as dayrain,
max(monthrain) as monthrain,
max(yearrain) as yearrain,
avg(consbatteryvoltage) as consbatteryvoltage,
avg(stationbatteryvoltage) as stationbatteryvoltage
FROM
weather
GROUP BY bucket, stationname;`
const addAggregationPolicy1mSQL = `SELECT add_continuous_aggregate_policy('weather_1m', '2 days', '1 minutes', '1 minutes', if_not_exists => true);`
const addAggregationPolicy5mSQL = `SELECT add_continuous_aggregate_policy('weather_5m', '2 days', '5 minutes', '5 minutes', if_not_exists => true);`
const addAggregationPolicy1hSQL = `SELECT add_continuous_aggregate_policy('weather_1h', '2 months', '1 hour', '1 hour', if_not_exists => true);`
const addAggregationPolicy1dSQL = `SELECT add_continuous_aggregate_policy('weather_1d', '1 year', '1 day', '1 day', if_not_exists => true);`
const addRetentionPolicy = `SELECT add_retention_policy('weather', INTERVAL '7 days', if_not_exists => true);`
const addRetentionPolicy1m = `SELECT add_retention_policy('weather_1m', INTERVAL '1 month', if_not_exists => true);`
const addRetentionPolicy5m = `SELECT add_retention_policy('weather_5m', INTERVAL '6 month', if_not_exists => true);`
const addRetentionPolicy1h = `SELECT add_retention_policy('weather_1h', INTERVAL '2 year', if_not_exists => true);`