-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhourly_data_rollups.sql
532 lines (482 loc) · 16.2 KB
/
hourly_data_rollups.sql
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
-- The following tables, functions and views are to handle
-- tracking coverage for the system. If possibly we may also want to replace
-- the rollups table above (which uses groups) with the hourly_data table
-- below. Therefor the table below also includes some extended summary stats
SET search_path = public;
CREATE SCHEMA IF NOT EXISTS _measurements_internal;
CREATE TABLE IF NOT EXISTS hourly_data (
sensors_id int NOT NULL --REFERENCES sensors ON DELETE CASCADE
, datetime timestamptz NOT NULL
, measurands_id int NOT NULL --REFERENCES measurands -- required for partition
, datetime_first timestamptz NOT NULL
, datetime_last timestamptz NOT NULL
, value_count int NOT NULL
, value_avg double precision
, value_sd double precision
, value_min double precision
, value_max double precision
, value_p02 double precision
, value_p25 double precision
, value_p50 double precision
, value_p75 double precision
, value_p98 double precision
, threshold_values jsonb
, error_count int NOT NULL DEFAULT 0
, updated_on timestamptz -- last time the sensor was updated
, calculated_on timestamptz-- last time the row rollup was calculated
, UNIQUE(sensors_id, measurands_id, datetime)
) PARTITION BY RANGE (datetime);
--ALTER TABLE hourly_data
--ADD COLUMN error_count int NOT NULL DEFAULT 0;
CREATE INDEX IF NOT EXISTS hourly_data_sensors_id_idx
ON hourly_data
USING btree (sensors_id);
CREATE INDEX IF NOT EXISTS hourly_data_datetime_idx
ON hourly_data
USING btree (datetime);
CREATE UNIQUE INDEX IF NOT EXISTS hourly_data_sensors_id_datetime_idx
ON hourly_data
USING btree (sensors_id, datetime);
CREATE INDEX IF NOT EXISTS hourly_data_measurands_id_idx
ON hourly_data
USING btree (measurands_id);
CREATE INDEX IF NOT EXISTS hourly_data_measurands_id_datetime_idx
ON hourly_data
USING btree (measurands_id, datetime);
-- not really used but here just in case we need it
CREATE OR REPLACE FUNCTION create_hourly_data_partition(sd date, ed date) RETURNS text AS $$
DECLARE
table_name text := 'hourly_data_'||to_char(sd, 'YYYYMMDD')||||to_char(ed, '_YYYYMMDD');
BEGIN
EXECUTE format('
CREATE TABLE IF NOT EXISTS _measurements_internal.%s
PARTITION OF hourly_data
FOR VALUES
FROM (''%s'')
TO (''%s'');',
table_name,
sd,
ed
);
RETURN table_name;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION create_hourly_data_partition(dt date) RETURNS text AS $$
DECLARE
_table_schema text := '_measurements_internal';
_table_name text := 'hourly_data_'||to_char(dt, 'YYYYMM');
sd date := date_trunc('month', dt);
ed date := date_trunc('month', dt + '1month'::interval);
BEGIN
EXECUTE format('
CREATE TABLE IF NOT EXISTS %s.%s
PARTITION OF hourly_data
FOR VALUES
FROM (''%s'')
TO (''%s'');',
_table_schema,
_table_name,
sd,
ed
);
-- register that table
INSERT INTO data_table_partitions (
data_tables_id
, table_schema
, table_name
, start_date
, end_date)
SELECT data_tables_id
, _table_schema
, _table_name
, sd
, ed
FROM data_tables
WHERE table_schema = 'public'
AND table_name = 'hourly_data';
RETURN _table_name;
END;
$$ LANGUAGE plpgsql;
INSERT INTO data_tables (data_tables_id, table_schema, table_name) VALUES
(2, 'public', 'hourly_data');
WITH dates AS (
SELECT generate_series('2016-01-01'::date, date_trunc('month', current_date + '1month'::interval), '1month'::interval) as dt)
SELECT create_hourly_data_partition(dt::date)
FROM dates;
-- store it in local time
CREATE TABLE IF NOT EXISTS hourly_data_queue (
datetime timestamptz NOT NULL
, tz_offset interval NOT NULL
, added_on timestamptz NOT NULL DEFAULT now()
, queued_on timestamptz
, modified_on timestamptz -- last time the hourly data was modified
, modified_count int NOT NULL DEFAULT 0
, calculated_on timestamptz
, calculated_count int NOT NULL DEFAULT 0
, calculated_seconds double precision
, sensor_nodes_count int
, sensors_count int
, measurements_count int
, UNIQUE(datetime, tz_offset)
);
CREATE OR REPLACE FUNCTION reset_hourly_data_queue(
st timestamptz DEFAULT '-infinity'
, et timestamptz DEFAULT 'infinity'
) RETURNS bigint AS $$
WITH first_and_last AS (
SELECT utc_offset(tz.tzid) as tz_offset
, MIN(datetime) as datetime_first
, MAX(datetime) as datetime_last
FROM measurements m
JOIN sensors s ON (m.sensors_id = s.sensors_id)
JOIN sensor_systems sy ON (s.sensor_systems_id = sy.sensor_systems_id)
JOIN sensor_nodes sn ON (sy.sensor_nodes_id = sn.sensor_nodes_id)
JOIN timezones tz ON (sn.timezones_id = tz.timezones_id)
WHERE datetime >= st
AND datetime <= et
GROUP BY 1
), datetimes AS (
SELECT tz_offset
, generate_series(
as_utc_hour(datetime_first, tz_offset)
, as_utc_hour(datetime_last, tz_offset)
, '1hour'::interval) as datetime
FROM first_and_last
) , inserts AS (
INSERT INTO hourly_data_queue (datetime, tz_offset, modified_on)
SELECT datetime
, tz_offset
, now()
FROM datetimes
--WHERE has_hourly_measurement(datetime, tz_offset)
ORDER BY tz_offset, datetime
ON CONFLICT (tz_offset, datetime) DO UPDATE
SET modified_on = GREATEST(EXCLUDED.modified_on, hourly_data_queue.modified_on)
RETURNING 1)
SELECT COUNT(1) FROM inserts;
$$ LANGUAGE SQL;
CREATE TABLE IF NOT EXISTS hourly_stats (
datetime timestamp PRIMARY KEY
, added_on timestamptz NOT NULL DEFAULT now()
, modified_on timestamptz -- last time the hourly data was modified
, calculated_count int NOT NULL DEFAULT 0
, updated_on timestamptz --
, calculated_on timestamptz
, sensor_nodes_count int
, measurements_count int
, sensors_count int
);
SELECT datetime
, tz_offset
, datetime >= '-infinity'::date
, datetime <= current_date - '1hour'::interval
, current_date - '1hour'::interval
FROM hourly_data_queue WHERE calculated_on IS NULL AND queued_on IS NULL ORDER BY datetime DESC LIMIT 10;
CREATE OR REPLACE FUNCTION fetch_hourly_data_jobs2(n int DEFAULT 1, min_hour timestamptz DEFAULT NULL, max_hour timestamptz DEFAULT NULL) RETURNS TABLE(
datetime timestamptz
, tz_offset interval
) AS $$
SELECT q.datetime
, q.tz_offset
FROM hourly_data_queue q
-- Its either not been calculated or its been modified
WHERE q.datetime >= COALESCE(min_hour, '-infinity'::date)
AND q.datetime <= COALESCE(max_hour, now() - '1hour'::interval)
AND (q.calculated_on IS NULL)-- OR (q.modified_on IS NULL OR q.modified_on > q.calculated_on))
-- either its never been or it was resently modified but not queued
--AND (q.queued_on IS NULL -- has not been queued
--OR (
-- q.queued_on < now() - '1h'::interval -- a set amount of time has passed AND
-- AND (
-- q.queued_on < q.modified_on -- its been changed since being queued
-- OR calculated_on IS NULL -- it was never calculated
-- )
-- )
--)
ORDER BY q.datetime, q.tz_offset
LIMIT n;
$$ LANGUAGE sql;
CREATE OR REPLACE FUNCTION fetch_hourly_data_jobs(n int DEFAULT 1, min_hour timestamptz DEFAULT NULL, max_hour timestamptz DEFAULT NULL) RETURNS TABLE(
datetime timestamptz
, tz_offset interval
, queued_on timestamptz
) AS $$
BEGIN
RETURN QUERY
UPDATE hourly_data_queue
SET queued_on = CURRENT_TIMESTAMP
, calculated_count = calculated_count + 1
FROM (
SELECT q.datetime
, q.tz_offset
FROM hourly_data_queue q
-- Its either not been calculated or its been modified
WHERE q.datetime >= COALESCE(min_hour, '-infinity'::date)
AND q.datetime <= COALESCE(max_hour, date_trunc('hour', now()))
AND (q.calculated_on IS NULL OR q.modified_on > q.calculated_on)
-- either its never been or it was resently modified but not queued
AND (q.queued_on IS NULL -- has not been queued
OR (
q.queued_on < now() - '1h'::interval -- a set amount of time has passed AND
AND (
q.queued_on < q.modified_on -- its been changed since being queued
OR calculated_on IS NULL -- it was never calculated
)
))
ORDER BY q.datetime, q.tz_offset
LIMIT n
FOR UPDATE SKIP LOCKED
) as d
WHERE d.datetime = hourly_data_queue.datetime
AND d.tz_offset = hourly_data_queue.tz_offset
RETURNING hourly_data_queue.datetime
, hourly_data_queue.tz_offset
, hourly_data_queue.queued_on;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION update_hourly_data_queue(hr timestamptz, _tz_offset interval) RETURNS bigint AS $$
WITH hourly_inserts AS (
INSERT INTO hourly_data_queue (datetime, tz_offset) VALUES
(date_trunc('hour', hr + _tz_offset + '-1s'::interval)
, _tz_offset)
ON CONFLICT (datetime, tz_offset) DO UPDATE
SET modified_on = now()
, modified_count = hourly_data_queue.modified_count + 1
RETURNING datetime, tz_offset
) SELECT COUNT(*)
FROM hourly_inserts;
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION hourly_data_updated_event(hr timestamptz, _tz_offset interval) RETURNS boolean AS $$
SELECT 't'::boolean;
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION calculate_hourly_data(hr timestamptz DEFAULT now() - '1hour'::interval)
RETURNS TABLE (
sensors_id int
, measurands_id int
, sensor_nodes_id int
, datetime timestamptz
, updated_on timestamptz
, datetime_first timestamptz
, datetime_last timestamptz
, value_count bigint
, value_avg double precision
, value_sd double precision
, value_min double precision
, value_max double precision
, value_p02 double precision
, value_p25 double precision
, value_p50 double precision
, value_p75 double precision
, value_p98 double precision
, error_count bigint
) AS $$
SELECT
m.sensors_id
, s.measurands_id
, sn.sensor_nodes_id
, as_utc_hour(m.datetime + '1h'::interval, t.tzid) as datetime -- this will make sure weird offsets are kept
, MAX(m.added_on) as updated_on
, MIN(datetime) as datetime_first
, MAX(datetime) as datetime_last
, COUNT(1) AS value_count
, AVG(value) as value_avg
, STDDEV(value) as value_sd
, MIN(value) as value_min
, MAX(value) as value_max
, PERCENTILE_CONT(0.02) WITHIN GROUP(ORDER BY value) as value_p02
, PERCENTILE_CONT(0.25) WITHIN GROUP(ORDER BY value) as value_p25
, PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY value) as value_p50
, PERCENTILE_CONT(0.75) WITHIN GROUP(ORDER BY value) as value_p75
, PERCENTILE_CONT(0.98) WITHIN GROUP(ORDER BY value) as value_p98
, SUM((value IS NULL)::int) as error_count
FROM measurements m
JOIN sensors s ON (m.sensors_id = s.sensors_id)
JOIN sensor_systems sy ON (s.sensor_systems_id = sy.sensor_systems_id)
JOIN sensor_nodes sn ON (sy.sensor_nodes_id = sn.sensor_nodes_id)
JOIN timezones t ON (sn.timezones_id = t.timezones_id)
WHERE datetime > hr - '1hour'::interval
AND datetime <= hr
--AND utc_offset_hours(hr, t.tzid) = tz_offset
GROUP BY 1,2,3,4
HAVING COUNT(1) > 0;
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION calculate_hourly_data(hr timestamptz DEFAULT now() - '1hour'::interval, _tz_offset interval DEFAULT '0s')
RETURNS TABLE (
sensors_id int
, measurands_id int
, sensor_nodes_id int
, datetime timestamptz
, updated_on timestamptz
, datetime_first timestamptz
, datetime_last timestamptz
, value_count bigint
, value_avg double precision
, value_sd double precision
, value_min double precision
, value_max double precision
, value_p02 double precision
, value_p25 double precision
, value_p50 double precision
, value_p75 double precision
, value_p98 double precision
, error_count bigint
) AS $$
SELECT
m.sensors_id
, s.measurands_id
, sn.sensor_nodes_id
-- save as utc hour ending (interval makes it time ending)
, as_utc_hour(m.datetime + '1h'::interval, t.tzid) as datetime
, MAX(m.added_on) as updated_on
, MIN(datetime) as datetime_first
, MAX(datetime) as datetime_last
, COUNT(1) AS value_count
, AVG(value) as value_avg
, STDDEV(value) as value_sd
, MIN(value) as value_min
, MAX(value) as value_max
, PERCENTILE_CONT(0.02) WITHIN GROUP(ORDER BY value) as value_p02
, PERCENTILE_CONT(0.25) WITHIN GROUP(ORDER BY value) as value_p25
, PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY value) as value_p50
, PERCENTILE_CONT(0.75) WITHIN GROUP(ORDER BY value) as value_p75
, PERCENTILE_CONT(0.98) WITHIN GROUP(ORDER BY value) as value_p98
, SUM((value IS NULL)::int) as error_count
FROM measurements m
JOIN sensors s ON (m.sensors_id = s.sensors_id)
JOIN sensor_systems sy ON (s.sensor_systems_id = sy.sensor_systems_id)
JOIN sensor_nodes sn ON (sy.sensor_nodes_id = sn.sensor_nodes_id)
JOIN timezones t ON (sn.timezones_id = t.timezones_id)
-- We want to track everything in the hourly_data_queue and that will be in utc time
-- and then we will need to convert to utc_hour for
WHERE datetime > hr - '1hour'::interval
AND datetime <= hr
AND utc_offset(hr, t.tzid) = _tz_offset
GROUP BY 1,2,3,4
HAVING COUNT(1) > 0;
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION insert_hourly_data(hr timestamptz DEFAULT now() - '1hour'::interval, _tz_offset interval DEFAULT '0s')
RETURNS TABLE (
sensor_nodes_count bigint
, sensors_count bigint
, measurements_count bigint
) AS $$
SET LOCAL work_mem = '512MB';
WITH data_rollup AS (
SELECT *
FROM calculate_hourly_data(hr, _tz_offset)
), data_inserted AS (
INSERT INTO hourly_data (
sensors_id
, measurands_id
, datetime
, updated_on
, datetime_first
, datetime_last
, value_count
, value_avg
, value_sd
, value_min
, value_max
, value_p02
, value_p25
, value_p50
, value_p75
, value_p98
, error_count
, calculated_on)
SELECT sensors_id
, measurands_id
, datetime
, updated_on
, datetime_first
, datetime_last
, value_count
, value_avg
, value_sd
, value_min
, value_max
, value_p02
, value_p25
, value_p50
, value_p75
, value_p98
, error_count
, current_timestamp as calculated_on
FROM data_rollup
ON CONFLICT (sensors_id, datetime) DO UPDATE
SET datetime_first = EXCLUDED.datetime_first
, datetime_last = EXCLUDED.datetime_last
, updated_on = EXCLUDED.updated_on
, value_avg = EXCLUDED.value_avg
, value_min = EXCLUDED.value_min
, value_max = EXCLUDED.value_max
, value_count = EXCLUDED.value_count
, value_p02 = EXCLUDED.value_p02
, value_p25 = EXCLUDED.value_p25
, value_p50 = EXCLUDED.value_p50
, value_p75 = EXCLUDED.value_p75
, value_p98 = EXCLUDED.value_p98
, error_count = EXCLUDED.error_count
, calculated_on = EXCLUDED.calculated_on
RETURNING sensors_id, value_count
) SELECT COUNT(DISTINCT sensors_id) as sensors_count
, COUNT(DISTINCT sensor_nodes_id) as sensor_nodes_count
, SUM(value_count) as measurements_count
FROM data_rollup;
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION update_hourly_data(hr timestamptz DEFAULT now() - '1hour'::interval, _tz_offset interval DEFAULT '0s') RETURNS bigint AS $$
DECLARE
nw timestamptz := clock_timestamp();
mc bigint;
BEGIN
WITH inserted AS (
SELECT sensor_nodes_count
, sensors_count
, measurements_count
FROM insert_hourly_data(hr, _tz_offset))
INSERT INTO hourly_data_queue (
datetime
, tz_offset
, calculated_on
, calculated_count
, sensor_nodes_count
, sensors_count
, measurements_count
, calculated_seconds
)
SELECT hr
, _tz_offset
, now()
, 1
, sensor_nodes_count
, sensors_count
, measurements_count
, EXTRACT(EPOCH FROM clock_timestamp() - nw)
FROM inserted i
ON CONFLICT (datetime, tz_offset) DO UPDATE
SET calculated_on = EXCLUDED.calculated_on
, calculated_count = hourly_data_queue.calculated_count + 1
, measurements_count = EXCLUDED.measurements_count
, sensors_count = EXCLUDED.sensors_count
, sensor_nodes_count = EXCLUDED.sensor_nodes_count
, calculated_seconds = EXCLUDED.calculated_seconds
RETURNING measurements_count INTO mc;
PERFORM hourly_data_updated_event(hr, _tz_offset);
RETURN mc;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE PROCEDURE update_hourly_data(n int DEFAULT 5, min_hour timestamptz DEFAULT NULL, max_hour timestamptz DEFAULT NULL) AS $$
DECLARE
rw record;
BEGIN
FOR rw IN (
SELECT datetime
, tz_offset
FROM fetch_hourly_data_jobs(n, min_hour, max_hour))
LOOP
RAISE NOTICE 'updating hour: % - %', rw.datetime, rw.tz_offset;
PERFORM update_hourly_data(rw.datetime, rw.tz_offset);
COMMIT;
END LOOP;
END;
$$ LANGUAGE plpgsql;