@golemio/flow 1.2.6-dev.960416138 → 1.2.6-dev.985140431

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ var dbm;
4
+ var type;
5
+ var seed;
6
+ var fs = require('fs');
7
+ var path = require('path');
8
+ var Promise;
9
+
10
+ /**
11
+ * We receive the dbmigrate dependency from dbmigrate initially.
12
+ * This enables us to not have to rely on NODE_PATH.
13
+ */
14
+ exports.setup = function(options, seedLink) {
15
+ dbm = options.dbmigrate;
16
+ type = dbm.dataType;
17
+ seed = seedLink;
18
+ Promise = options.Promise;
19
+ };
20
+
21
+ exports.up = function(db) {
22
+ var filePath = path.join(__dirname, 'sqls', '20230828124542-replace-bigint-timestamps-up.sql');
23
+ return new Promise( function( resolve, reject ) {
24
+ fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
25
+ if (err) return reject(err);
26
+ console.log('received data: ' + data);
27
+
28
+ resolve(data);
29
+ });
30
+ })
31
+ .then(function(data) {
32
+ return db.runSql(data);
33
+ });
34
+ };
35
+
36
+ exports.down = function(db) {
37
+ var filePath = path.join(__dirname, 'sqls', '20230828124542-replace-bigint-timestamps-down.sql');
38
+ return new Promise( function( resolve, reject ) {
39
+ fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
40
+ if (err) return reject(err);
41
+ console.log('received data: ' + data);
42
+
43
+ resolve(data);
44
+ });
45
+ })
46
+ .then(function(data) {
47
+ return db.runSql(data);
48
+ });
49
+ };
50
+
51
+ exports._meta = {
52
+ "version": 1
53
+ };
@@ -0,0 +1,587 @@
1
+ -- drop dependent views
2
+ drop view analytic.v_pedestrians_detections_15min;
3
+ drop view analytic.v_pedestrians_detections_api;
4
+ drop view analytic.v_pedestrians_detections_daily;
5
+ drop view analytic.v_pedestrians_flow_quality;
6
+
7
+ -- flow_sinks
8
+ alter table flow_sinks alter column history_start_timestamp type bigint using extract(epoch from history_start_timestamp) * 1000;
9
+
10
+ -- flow_od_measurements
11
+ alter table flow_od_measurements alter column end_timestamp type bigint using extract(epoch from end_timestamp) * 1000;
12
+ alter table flow_od_measurements alter column start_timestamp type bigint using extract(epoch from start_timestamp) * 1000;
13
+
14
+ -- flow_measurements
15
+ drop table if exists flow_measurements_old;
16
+ alter table flow_measurements rename to flow_measurements_old;
17
+
18
+ DO $$
19
+ DECLARE
20
+ partition_name text;
21
+ BEGIN
22
+ FOR partition_name IN (SELECT relname FROM pg_class WHERE relnamespace = (SELECT current_schema() as value)::regnamespace AND relname LIKE 'flow_measurements_%' and relispartition = true and relpartbound is not null)
23
+ LOOP
24
+ EXECUTE format('ALTER TABLE %I.%I RENAME TO %I', (SELECT current_schema() as value), partition_name, partition_name || '_old');
25
+ END LOOP;
26
+ END $$;
27
+
28
+ CREATE TABLE flow_measurements (
29
+ sink_id int4 NOT NULL,
30
+ cube_id int4 NOT NULL,
31
+ sequence_number int4 NOT NULL,
32
+ analytic_id int4 NOT NULL,
33
+ start_timestamp int8 NOT NULL,
34
+ end_timestamp int8 NOT NULL,
35
+ category varchar(150) NOT NULL,
36
+ value int4 NOT NULL,
37
+ create_batch_id int8 NULL,
38
+ created_at timestamptz NULL,
39
+ created_by varchar(150) NULL,
40
+ update_batch_id int8 NULL,
41
+ updated_at timestamptz NULL,
42
+ updated_by varchar(150) NULL,
43
+ data_validity varchar(50) NULL,
44
+ CONSTRAINT flow_measurements_pkey PRIMARY KEY (cube_id, sink_id, start_timestamp, end_timestamp, category, sequence_number)
45
+ )
46
+ PARTITION BY RANGE (start_timestamp);
47
+ CREATE INDEX flow_measurements_created_at ON ONLY flow_measurements USING btree (created_at);
48
+ CREATE INDEX flow_measurements_start_cube_sink ON ONLY flow_measurements USING btree (cube_id, sink_id);
49
+ CREATE INDEX flow_measurements_start_timestamp ON ONLY flow_measurements USING btree (start_timestamp);
50
+
51
+ -- partitions
52
+ CREATE TABLE flow_measurements_min PARTITION OF flow_measurements FOR VALUES FROM (MINVALUE) TO (extract ('epoch' from '2020-09-01'::timestamp)*1000);
53
+ CREATE TABLE flow_measurements_y2020m09 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2020-09-01'::timestamp)*1000) TO (extract ('epoch' from '2020-10-01'::timestamp)*1000);
54
+ CREATE TABLE flow_measurements_y2020m10 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2020-10-01'::timestamp)*1000) TO (extract ('epoch' from '2020-11-01'::timestamp)*1000);
55
+ CREATE TABLE flow_measurements_y2020m11 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2020-11-01'::timestamp)*1000) TO (extract ('epoch' from '2020-12-01'::timestamp)*1000);
56
+ CREATE TABLE flow_measurements_y2020m12 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2020-12-01'::timestamp)*1000) TO (extract ('epoch' from '2021-01-01'::timestamp)*1000);
57
+ --2021
58
+ CREATE TABLE flow_measurements_y2021m01 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-01-01'::timestamp)*1000) TO (extract ('epoch' from '2021-02-01'::timestamp)*1000);
59
+ CREATE TABLE flow_measurements_y2021m02 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-02-01'::timestamp)*1000) TO (extract ('epoch' from '2021-03-01'::timestamp)*1000);
60
+ CREATE TABLE flow_measurements_y2021m03 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-03-01'::timestamp)*1000) TO (extract ('epoch' from '2021-04-01'::timestamp)*1000);
61
+ CREATE TABLE flow_measurements_y2021m04 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-04-01'::timestamp)*1000) TO (extract ('epoch' from '2021-05-01'::timestamp)*1000);
62
+ CREATE TABLE flow_measurements_y2021m05 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-05-01'::timestamp)*1000) TO (extract ('epoch' from '2021-06-01'::timestamp)*1000);
63
+ CREATE TABLE flow_measurements_y2021m06 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-06-01'::timestamp)*1000) TO (extract ('epoch' from '2021-07-01'::timestamp)*1000);
64
+ CREATE TABLE flow_measurements_y2021m07 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-07-01'::timestamp)*1000) TO (extract ('epoch' from '2021-08-01'::timestamp)*1000);
65
+ CREATE TABLE flow_measurements_y2021m08 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-08-01'::timestamp)*1000) TO (extract ('epoch' from '2021-09-01'::timestamp)*1000);
66
+ CREATE TABLE flow_measurements_y2021m09 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-09-01'::timestamp)*1000) TO (extract ('epoch' from '2021-10-01'::timestamp)*1000);
67
+ CREATE TABLE flow_measurements_y2021m10 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-10-01'::timestamp)*1000) TO (extract ('epoch' from '2021-11-01'::timestamp)*1000);
68
+ CREATE TABLE flow_measurements_y2021m11 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-11-01'::timestamp)*1000) TO (extract ('epoch' from '2021-12-01'::timestamp)*1000);
69
+ CREATE TABLE flow_measurements_y2021m12 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2021-12-01'::timestamp)*1000) TO (extract ('epoch' from '2022-01-01'::timestamp)*1000);
70
+ --2022
71
+ CREATE TABLE flow_measurements_y2022m01 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-01-01'::timestamp)*1000) TO (extract ('epoch' from '2022-02-01'::timestamp)*1000);
72
+ CREATE TABLE flow_measurements_y2022m02 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-02-01'::timestamp)*1000) TO (extract ('epoch' from '2022-03-01'::timestamp)*1000);
73
+ CREATE TABLE flow_measurements_y2022m03 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-03-01'::timestamp)*1000) TO (extract ('epoch' from '2022-04-01'::timestamp)*1000);
74
+ CREATE TABLE flow_measurements_y2022m04 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-04-01'::timestamp)*1000) TO (extract ('epoch' from '2022-05-01'::timestamp)*1000);
75
+ CREATE TABLE flow_measurements_y2022m05 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-05-01'::timestamp)*1000) TO (extract ('epoch' from '2022-06-01'::timestamp)*1000);
76
+ CREATE TABLE flow_measurements_y2022m06 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-06-01'::timestamp)*1000) TO (extract ('epoch' from '2022-07-01'::timestamp)*1000);
77
+ CREATE TABLE flow_measurements_y2022m07 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-07-01'::timestamp)*1000) TO (extract ('epoch' from '2022-08-01'::timestamp)*1000);
78
+ CREATE TABLE flow_measurements_y2022m08 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-08-01'::timestamp)*1000) TO (extract ('epoch' from '2022-09-01'::timestamp)*1000);
79
+ CREATE TABLE flow_measurements_y2022m09 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-09-01'::timestamp)*1000) TO (extract ('epoch' from '2022-10-01'::timestamp)*1000);
80
+ CREATE TABLE flow_measurements_y2022m10 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-10-01'::timestamp)*1000) TO (extract ('epoch' from '2022-11-01'::timestamp)*1000);
81
+ CREATE TABLE flow_measurements_y2022m11 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-11-01'::timestamp)*1000) TO (extract ('epoch' from '2022-12-01'::timestamp)*1000);
82
+ CREATE TABLE flow_measurements_y2022m12 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2022-12-01'::timestamp)*1000) TO (extract ('epoch' from '2023-01-01'::timestamp)*1000);
83
+ --2023
84
+ CREATE TABLE flow_measurements_y2023m01 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-01-01'::timestamp)*1000) TO (extract ('epoch' from '2023-02-01'::timestamp)*1000);
85
+ CREATE TABLE flow_measurements_y2023m02 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-02-01'::timestamp)*1000) TO (extract ('epoch' from '2023-03-01'::timestamp)*1000);
86
+ CREATE TABLE flow_measurements_y2023m03 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-03-01'::timestamp)*1000) TO (extract ('epoch' from '2023-04-01'::timestamp)*1000);
87
+ CREATE TABLE flow_measurements_y2023m04 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-04-01'::timestamp)*1000) TO (extract ('epoch' from '2023-05-01'::timestamp)*1000);
88
+ CREATE TABLE flow_measurements_y2023m05 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-05-01'::timestamp)*1000) TO (extract ('epoch' from '2023-06-01'::timestamp)*1000);
89
+ CREATE TABLE flow_measurements_y2023m06 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-06-01'::timestamp)*1000) TO (extract ('epoch' from '2023-07-01'::timestamp)*1000);
90
+ CREATE TABLE flow_measurements_y2023m07 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-07-01'::timestamp)*1000) TO (extract ('epoch' from '2023-08-01'::timestamp)*1000);
91
+ CREATE TABLE flow_measurements_y2023m08 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-08-01'::timestamp)*1000) TO (extract ('epoch' from '2023-09-01'::timestamp)*1000);
92
+ CREATE TABLE flow_measurements_y2023m09 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-09-01'::timestamp)*1000) TO (extract ('epoch' from '2023-10-01'::timestamp)*1000);
93
+ CREATE TABLE flow_measurements_y2023m10 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-10-01'::timestamp)*1000) TO (extract ('epoch' from '2023-11-01'::timestamp)*1000);
94
+ CREATE TABLE flow_measurements_y2023m11 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-11-01'::timestamp)*1000) TO (extract ('epoch' from '2023-12-01'::timestamp)*1000);
95
+ CREATE TABLE flow_measurements_y2023m12 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2023-12-01'::timestamp)*1000) TO (extract ('epoch' from '2024-01-01'::timestamp)*1000);
96
+ --2024
97
+ CREATE TABLE flow_measurements_y2024m01 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-01-01'::timestamp)*1000) TO (extract ('epoch' from '2024-02-01'::timestamp)*1000);
98
+ CREATE TABLE flow_measurements_y2024m02 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-02-01'::timestamp)*1000) TO (extract ('epoch' from '2024-03-01'::timestamp)*1000);
99
+ CREATE TABLE flow_measurements_y2024m03 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-03-01'::timestamp)*1000) TO (extract ('epoch' from '2024-04-01'::timestamp)*1000);
100
+ CREATE TABLE flow_measurements_y2024m04 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-04-01'::timestamp)*1000) TO (extract ('epoch' from '2024-05-01'::timestamp)*1000);
101
+ CREATE TABLE flow_measurements_y2024m05 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-05-01'::timestamp)*1000) TO (extract ('epoch' from '2024-06-01'::timestamp)*1000);
102
+ CREATE TABLE flow_measurements_y2024m06 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-06-01'::timestamp)*1000) TO (extract ('epoch' from '2024-07-01'::timestamp)*1000);
103
+ CREATE TABLE flow_measurements_y2024m07 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-07-01'::timestamp)*1000) TO (extract ('epoch' from '2024-08-01'::timestamp)*1000);
104
+ CREATE TABLE flow_measurements_y2024m08 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-08-01'::timestamp)*1000) TO (extract ('epoch' from '2024-09-01'::timestamp)*1000);
105
+ CREATE TABLE flow_measurements_y2024m09 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-09-01'::timestamp)*1000) TO (extract ('epoch' from '2024-10-01'::timestamp)*1000);
106
+ CREATE TABLE flow_measurements_y2024m10 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-10-01'::timestamp)*1000) TO (extract ('epoch' from '2024-11-01'::timestamp)*1000);
107
+ CREATE TABLE flow_measurements_y2024m11 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-11-01'::timestamp)*1000) TO (extract ('epoch' from '2024-12-01'::timestamp)*1000);
108
+ CREATE TABLE flow_measurements_y2024m12 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2024-12-01'::timestamp)*1000) TO (extract ('epoch' from '2025-01-01'::timestamp)*1000);
109
+ --2025
110
+ CREATE TABLE flow_measurements_y2025m01 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-01-01'::timestamp)*1000) TO (extract ('epoch' from '2025-02-01'::timestamp)*1000);
111
+ CREATE TABLE flow_measurements_y2025m02 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-02-01'::timestamp)*1000) TO (extract ('epoch' from '2025-03-01'::timestamp)*1000);
112
+ CREATE TABLE flow_measurements_y2025m03 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-03-01'::timestamp)*1000) TO (extract ('epoch' from '2025-04-01'::timestamp)*1000);
113
+ CREATE TABLE flow_measurements_y2025m04 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-04-01'::timestamp)*1000) TO (extract ('epoch' from '2025-05-01'::timestamp)*1000);
114
+ CREATE TABLE flow_measurements_y2025m05 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-05-01'::timestamp)*1000) TO (extract ('epoch' from '2025-06-01'::timestamp)*1000);
115
+ CREATE TABLE flow_measurements_y2025m06 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-06-01'::timestamp)*1000) TO (extract ('epoch' from '2025-07-01'::timestamp)*1000);
116
+ CREATE TABLE flow_measurements_y2025m07 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-07-01'::timestamp)*1000) TO (extract ('epoch' from '2025-08-01'::timestamp)*1000);
117
+ CREATE TABLE flow_measurements_y2025m08 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-08-01'::timestamp)*1000) TO (extract ('epoch' from '2025-09-01'::timestamp)*1000);
118
+ CREATE TABLE flow_measurements_y2025m09 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-09-01'::timestamp)*1000) TO (extract ('epoch' from '2025-10-01'::timestamp)*1000);
119
+ CREATE TABLE flow_measurements_y2025m10 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-10-01'::timestamp)*1000) TO (extract ('epoch' from '2025-11-01'::timestamp)*1000);
120
+ CREATE TABLE flow_measurements_y2025m11 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-11-01'::timestamp)*1000) TO (extract ('epoch' from '2025-12-01'::timestamp)*1000);
121
+ CREATE TABLE flow_measurements_y2025m12 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2025-12-01'::timestamp)*1000) TO (extract ('epoch' from '2026-01-01'::timestamp)*1000);
122
+ --2026
123
+ CREATE TABLE flow_measurements_y2026m01 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-01-01'::timestamp)*1000) TO (extract ('epoch' from '2026-02-01'::timestamp)*1000);
124
+ CREATE TABLE flow_measurements_y2026m02 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-02-01'::timestamp)*1000) TO (extract ('epoch' from '2026-03-01'::timestamp)*1000);
125
+ CREATE TABLE flow_measurements_y2026m03 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-03-01'::timestamp)*1000) TO (extract ('epoch' from '2026-04-01'::timestamp)*1000);
126
+ CREATE TABLE flow_measurements_y2026m04 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-04-01'::timestamp)*1000) TO (extract ('epoch' from '2026-05-01'::timestamp)*1000);
127
+ CREATE TABLE flow_measurements_y2026m05 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-05-01'::timestamp)*1000) TO (extract ('epoch' from '2026-06-01'::timestamp)*1000);
128
+ CREATE TABLE flow_measurements_y2026m06 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-06-01'::timestamp)*1000) TO (extract ('epoch' from '2026-07-01'::timestamp)*1000);
129
+ CREATE TABLE flow_measurements_y2026m07 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-07-01'::timestamp)*1000) TO (extract ('epoch' from '2026-08-01'::timestamp)*1000);
130
+ CREATE TABLE flow_measurements_y2026m08 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-08-01'::timestamp)*1000) TO (extract ('epoch' from '2026-09-01'::timestamp)*1000);
131
+ CREATE TABLE flow_measurements_y2026m09 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-09-01'::timestamp)*1000) TO (extract ('epoch' from '2026-10-01'::timestamp)*1000);
132
+ CREATE TABLE flow_measurements_y2026m10 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-10-01'::timestamp)*1000) TO (extract ('epoch' from '2026-11-01'::timestamp)*1000);
133
+ CREATE TABLE flow_measurements_y2026m11 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-11-01'::timestamp)*1000) TO (extract ('epoch' from '2026-12-01'::timestamp)*1000);
134
+ CREATE TABLE flow_measurements_y2026m12 PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2026-12-01'::timestamp)*1000) TO (extract ('epoch' from '2027-01-01'::timestamp)*1000);
135
+ --202
136
+ CREATE TABLE flow_measurements_y2027up PARTITION OF flow_measurements FOR VALUES FROM (extract ('epoch' from '2027-01-01'::timestamp)*1000) TO (maxvalue);
137
+
138
+ -- counters_detections
139
+ alter table counters_detections alter column measured_to type bigint using extract(epoch from measured_to) * 1000;
140
+ alter table counters_detections alter column measured_from type bigint using extract(epoch from measured_from) * 1000;
141
+
142
+ -- prodecures
143
+ CREATE OR replace procedure update_detections_data()
144
+ LANGUAGE plpgsql
145
+ AS $$
146
+ declare
147
+ lastupdatetimestamp timestamptz;
148
+ lastupdateunix bigint;
149
+ begin
150
+ select
151
+ case
152
+ when flowmax.max_measured_from is not null
153
+ then flowmax.max_measured_from
154
+ else to_timestamp(0)
155
+ end as max_measured_from into lastupdatetimestamp
156
+ from (select max(measured_from) - interval '1 hours' as max_measured_from from flow.pedestrians_detections_api) flowMax;
157
+
158
+ lastupdateunix := extract ('epoch' from lastupdatetimestamp) * 1000;
159
+
160
+
161
+ insert into flow.pedestrians_detections_api
162
+ with wifi as (
163
+ select
164
+ pedestrians_wifi.location_id,
165
+ pedestrians_wifi.direction_id,
166
+ date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval as measured_from,
167
+ date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval + '00:15:00'::interval as measured_to,
168
+ sum(pedestrians_wifi.value) as value,
169
+ count(pedestrians_wifi.value) as count_n,
170
+ 3 as quantity
171
+ from
172
+ flow.pedestrians_wifi
173
+ group by
174
+ pedestrians_wifi.location_id,
175
+ pedestrians_wifi.direction_id,
176
+ (date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval),
177
+ (date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval + '00:15:00'::interval)
178
+ ) SELECT wifi.measured_from,
179
+ wifi.measured_to,
180
+ wifi.location_id,
181
+ wifi.direction_id,
182
+ wifi.value,
183
+ wifi.count_n::numeric / wifi.quantity::numeric AS quality
184
+ FROM wifi
185
+ where wifi.measured_from > lastupdatetimestamp
186
+ ON CONFLICT (location_id,direction_id,measured_from,measured_to)
187
+ DO update
188
+ SET value = EXCLUDED.value,
189
+ quality = EXCLUDED.quality;
190
+
191
+ insert into flow.pedestrians_detections_api
192
+ with pyro as (
193
+ select
194
+ cd.locations_id as location_id,
195
+ cd.directions_id as direction_id,
196
+ to_timestamp((cd.measured_from / 1000)::double precision) as measured_from,
197
+ to_timestamp((cd.measured_from / 1000)::double precision) + '00:15:00'::interval as measured_to,
198
+ sum(cd.value) as value,
199
+ 1 as count_n,
200
+ 1 as quantity
201
+ from flow.counters_detections cd
202
+ where
203
+ cd.category::text = 'pedestrian'::text
204
+ and (cd.directions_id::text in (select distinct pedestrians_directions_api.direction_id from flow.pedestrians_directions_api))
205
+ group by
206
+ cd.locations_id,
207
+ cd.directions_id,
208
+ (to_timestamp((cd.measured_from / 1000)::double precision)),
209
+ (to_timestamp((cd.measured_from / 1000)::double precision) + '00:15:00'::interval)
210
+ )
211
+ select
212
+ pyro.measured_from,
213
+ pyro.measured_to,
214
+ pyro.location_id,
215
+ pyro.direction_id,
216
+ pyro.value,
217
+ pyro.count_n::numeric / pyro.quantity::numeric as quality
218
+ from pyro
219
+ where pyro.measured_from > lastupdatetimestamp
220
+ ON CONFLICT (location_id,direction_id,measured_from,measured_to)
221
+ DO update
222
+ SET value = EXCLUDED.value,
223
+ quality = EXCLUDED.quality;
224
+
225
+
226
+ insert into flow.pedestrians_detections_api
227
+ with vyber as (
228
+ select * from flow.flow_measurements where flow_measurements.start_timestamp > lastupdateunix and category::text = 'pedestrian'::text
229
+ ), flow as (
230
+ select
231
+ vyber.cube_id::character varying(50) as location_id,
232
+ vyber.sink_id::character varying(50) as direction_id,
233
+ date_trunc('hour'::text, to_timestamp((vyber.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((vyber.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval as measured_from,
234
+ date_trunc('hour'::text, to_timestamp((vyber.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((vyber.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval + '00:15:00'::interval as measured_to,
235
+ sum(vyber.value) as value,
236
+ count(vyber.value) as count_n,
237
+ 3 as quantity
238
+ from
239
+ vyber
240
+ where
241
+ ((vyber.cube_id::character varying(50)::text,
242
+ vyber.sink_id::character varying(50)::text) in (
243
+ select distinct
244
+ pedestrians_directions_api.cube_id as location_id,
245
+ pedestrians_directions_api.direction_id
246
+ from
247
+ flow.pedestrians_directions_api))
248
+ group by 1,2,3,4
249
+ ) select
250
+ flow.measured_from,
251
+ flow.measured_to,
252
+ flow.location_id,
253
+ flow.direction_id,
254
+ flow.value,
255
+ flow.count_n::numeric / flow.quantity::numeric as quality
256
+ from flow
257
+ where flow.measured_from > lastupdatetimestamp
258
+ ON CONFLICT (location_id,direction_id,measured_from,measured_to)
259
+ DO update
260
+ SET value = EXCLUDED.value,
261
+ quality = EXCLUDED.quality;
262
+ end;
263
+ $$;
264
+
265
+ -- recreate views
266
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_detections_15min
267
+ AS WITH wifi AS (
268
+ SELECT pedestrians_wifi.location_id AS locations_id,
269
+ pedestrians_wifi.direction_id AS directions_id,
270
+ date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval AS measured_from,
271
+ sum(pedestrians_wifi.value) AS value,
272
+ count(pedestrians_wifi.value) AS count_n
273
+ FROM flow.pedestrians_wifi
274
+ GROUP BY pedestrians_wifi.location_id, pedestrians_wifi.direction_id, (date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval)
275
+ ), pyro AS (
276
+ SELECT cd.locations_id,
277
+ lg.first_dir AS directions_id,
278
+ to_timestamp((cd.measured_from / 1000)::double precision) AS measured_from,
279
+ sum(cd.value) AS value,
280
+ count(cd.value) AS count_n
281
+ FROM flow.counters_detections cd
282
+ LEFT JOIN ( SELECT pedestrians_locations_gates.direction_id,
283
+ pedestrians_locations_gates.direction_type,
284
+ min(pedestrians_locations_gates.direction_id::text) OVER (PARTITION BY pedestrians_locations_gates.cube_id, pedestrians_locations_gates.direction_type) AS first_dir
285
+ FROM analytic.pedestrians_locations_gates) lg ON cd.directions_id::text = lg.direction_id::text
286
+ WHERE cd.category::text = 'pedestrian'::text AND (cd.directions_id::text IN ( SELECT DISTINCT pedestrians_locations_gates.direction_id
287
+ FROM analytic.pedestrians_locations_gates))
288
+ GROUP BY cd.locations_id, lg.first_dir, (to_timestamp((cd.measured_from / 1000)::double precision))
289
+ ), flow AS (
290
+ SELECT flow_measurements.cube_id::character varying(50) AS location_id,
291
+ flow_measurements.sink_id::character varying(50) AS direction_id,
292
+ date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval AS measured_from,
293
+ sum(flow_measurements.value) AS value,
294
+ count(flow_measurements.value) AS count_n
295
+ FROM flow.flow_measurements
296
+ WHERE ((flow_measurements.cube_id::character varying(50)::text, flow_measurements.sink_id::character varying(50)::text) IN ( SELECT DISTINCT pedestrians_locations_gates.cube_id AS location_id,
297
+ pedestrians_locations_gates.direction_id
298
+ FROM analytic.pedestrians_locations_gates)) AND flow_measurements.category::text = 'pedestrian'::text
299
+ GROUP BY (flow_measurements.cube_id::character varying(50)), (flow_measurements.sink_id::character varying(50)), (date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval)
300
+ ORDER BY (date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval), (flow_measurements.cube_id::character varying(50)), (flow_measurements.sink_id::character varying(50))
301
+ ), measurements AS (
302
+ SELECT wifi.locations_id,
303
+ wifi.directions_id,
304
+ wifi.measured_from,
305
+ wifi.value,
306
+ wifi.count_n
307
+ FROM wifi
308
+ UNION ALL
309
+ SELECT pyro.locations_id,
310
+ pyro.directions_id,
311
+ pyro.measured_from,
312
+ pyro.value,
313
+ pyro.count_n
314
+ FROM pyro
315
+ UNION ALL
316
+ SELECT flow.location_id,
317
+ flow.direction_id,
318
+ flow.measured_from,
319
+ flow.value,
320
+ flow.count_n
321
+ FROM flow
322
+ ), calendar AS (
323
+ SELECT generate_series(min(measurements.measured_from), max(measurements.measured_from), '00:15:00'::interval) AS calendar
324
+ FROM measurements
325
+ ), classes AS (
326
+ SELECT calendar.calendar,
327
+ m_1.location_id,
328
+ m_1.direction_id
329
+ FROM calendar
330
+ JOIN ( SELECT DISTINCT measurements.locations_id::character varying(50) AS location_id,
331
+ measurements.directions_id::character varying(50) AS direction_id
332
+ FROM measurements) m_1 ON true
333
+ )
334
+ SELECT c.calendar,
335
+ date_trunc('hour'::text, c.calendar) AS calendar_hour,
336
+ c.calendar::date AS calendar_date,
337
+ ll.measurement_start,
338
+ COALESCE(ll.measurement_end, now()::date::timestamp with time zone) AS measurement_end,
339
+ c.location_id,
340
+ c.direction_id,
341
+ concat(c.location_id, '-', c.direction_id) AS compound_id,
342
+ COALESCE(m.value, 0::bigint) AS value,
343
+ COALESCE(m.count_n, 0::bigint) AS count_n
344
+ FROM classes c
345
+ LEFT JOIN measurements m ON c.calendar = m.measured_from AND c.location_id::text = m.locations_id::text AND c.direction_id::text = m.directions_id::text
346
+ LEFT JOIN analytic.pedestrians_locations_list ll ON c.location_id::text = ll.cube_id::text
347
+ WHERE c.calendar >= ll.measurement_start AND c.calendar <= COALESCE(ll.measurement_end, now()::date::timestamp with time zone);
348
+
349
+
350
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_detections_api
351
+ AS WITH wifi AS (
352
+ SELECT pedestrians_wifi.location_id,
353
+ pedestrians_wifi.direction_id,
354
+ date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval AS measured_from,
355
+ date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval + '00:15:00'::interval AS measured_to,
356
+ sum(pedestrians_wifi.value) AS value,
357
+ count(pedestrians_wifi.value) AS count_n,
358
+ 3 AS quantity
359
+ FROM flow.pedestrians_wifi
360
+ GROUP BY pedestrians_wifi.location_id, pedestrians_wifi.direction_id, (date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval), (date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval + '00:15:00'::interval)
361
+ ), pyro AS (
362
+ SELECT cd.locations_id AS location_id,
363
+ cd.directions_id AS direction_id,
364
+ to_timestamp((cd.measured_from / 1000)::double precision) AS measured_from,
365
+ to_timestamp((cd.measured_from / 1000)::double precision) + '00:15:00'::interval AS measured_to,
366
+ sum(cd.value) AS value,
367
+ count(cd.value) AS count_n,
368
+ 1 AS quantity
369
+ FROM flow.counters_detections cd
370
+ WHERE cd.category::text = 'pedestrian'::text AND (cd.directions_id::text IN ( SELECT DISTINCT pedestrians_directions_api.direction_id
371
+ FROM flow.pedestrians_directions_api))
372
+ GROUP BY cd.locations_id, cd.directions_id, (to_timestamp((cd.measured_from / 1000)::double precision)), (to_timestamp((cd.measured_from / 1000)::double precision) + '00:15:00'::interval)
373
+ ), flow AS (
374
+ SELECT flow_measurements.cube_id::character varying(50) AS location_id,
375
+ flow_measurements.sink_id::character varying(50) AS direction_id,
376
+ date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval AS measured_from,
377
+ date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval + '00:15:00'::interval AS measured_to,
378
+ sum(flow_measurements.value) AS value,
379
+ count(flow_measurements.value) AS count_n,
380
+ 3 AS quantity
381
+ FROM flow.flow_measurements
382
+ WHERE ((flow_measurements.cube_id::character varying(50)::text, flow_measurements.sink_id::character varying(50)::text) IN ( SELECT DISTINCT pedestrians_directions_api.cube_id AS location_id,
383
+ pedestrians_directions_api.direction_id
384
+ FROM flow.pedestrians_directions_api)) AND flow_measurements.category::text = 'pedestrian'::text
385
+ GROUP BY (flow_measurements.cube_id::character varying(50)), (flow_measurements.sink_id::character varying(50)), (date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval), (date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval + '00:15:00'::interval)
386
+ ORDER BY (date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) + (date_part('minute'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))::integer / 15)::double precision * '00:15:00'::interval), (flow_measurements.cube_id::character varying(50)), (flow_measurements.sink_id::character varying(50))
387
+ ), measurements AS (
388
+ SELECT wifi.location_id,
389
+ wifi.direction_id,
390
+ wifi.measured_from,
391
+ wifi.measured_to,
392
+ wifi.value,
393
+ wifi.count_n,
394
+ wifi.quantity
395
+ FROM wifi
396
+ UNION ALL
397
+ SELECT pyro.location_id,
398
+ pyro.direction_id,
399
+ pyro.measured_from,
400
+ pyro.measured_to,
401
+ pyro.value,
402
+ pyro.count_n,
403
+ pyro.quantity
404
+ FROM pyro
405
+ UNION ALL
406
+ SELECT flow.location_id,
407
+ flow.direction_id,
408
+ flow.measured_from,
409
+ flow.measured_to,
410
+ flow.value,
411
+ flow.count_n,
412
+ flow.quantity
413
+ FROM flow
414
+ )
415
+ SELECT measurements.measured_from,
416
+ measurements.measured_to,
417
+ pda.location_id::character varying(50) AS location_id,
418
+ measurements.direction_id,
419
+ measurements.value,
420
+ CASE
421
+ WHEN (measurements.count_n::numeric / measurements.quantity::numeric) > 1::numeric THEN 1::numeric
422
+ ELSE measurements.count_n::numeric / measurements.quantity::numeric
423
+ END AS quality
424
+ FROM measurements
425
+ LEFT JOIN flow.pedestrians_directions_api pda ON measurements.direction_id::text = pda.direction_id::text and measurements.location_id = pda.cube_id;
426
+
427
+
428
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_detections_daily
429
+ AS WITH wifi AS (
430
+ SELECT pedestrians_wifi.location_id AS locations_id,
431
+ pedestrians_wifi.direction_id AS directions_id,
432
+ pedestrians_wifi.measured_from::date AS measured_from,
433
+ sum(pedestrians_wifi.value) AS value,
434
+ count(pedestrians_wifi.value) AS count_n,
435
+ count(pedestrians_wifi.value)::numeric / 288::numeric AS quality_ratio
436
+ FROM flow.pedestrians_wifi
437
+ GROUP BY pedestrians_wifi.location_id, pedestrians_wifi.direction_id, (pedestrians_wifi.measured_from::date)
438
+ ), pyro AS (
439
+ SELECT cd.locations_id,
440
+ lg.first_dir AS directions_id,
441
+ to_timestamp((cd.measured_from / 1000)::double precision)::date AS measured_from,
442
+ sum(cd.value) AS value,
443
+ count(cd.value) / count(DISTINCT cd.directions_id) AS count_n,
444
+ count(cd.value)::numeric / 96::numeric / count(DISTINCT cd.directions_id)::numeric AS quality_ratio
445
+ FROM flow.counters_detections cd
446
+ LEFT JOIN ( SELECT pedestrians_locations_gates.direction_id,
447
+ pedestrians_locations_gates.direction_type,
448
+ min(pedestrians_locations_gates.direction_id::text) OVER (PARTITION BY pedestrians_locations_gates.cube_id, pedestrians_locations_gates.direction_type) AS first_dir
449
+ FROM analytic.pedestrians_locations_gates) lg ON cd.directions_id::text = lg.direction_id::text
450
+ WHERE cd.category::text = 'pedestrian'::text AND (cd.directions_id::text IN ( SELECT DISTINCT pedestrians_locations_gates.direction_id
451
+ FROM analytic.pedestrians_locations_gates))
452
+ GROUP BY cd.locations_id, lg.first_dir, (to_timestamp((cd.measured_from / 1000)::double precision)::date)
453
+ ), flow AS (
454
+ SELECT flow_measurements.cube_id::character varying(50) AS location_id,
455
+ flow_measurements.sink_id::character varying(50) AS direction_id,
456
+ to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)::date AS measured_from,
457
+ sum(flow_measurements.value) AS value,
458
+ count(flow_measurements.value) AS count_n,
459
+ count(flow_measurements.value)::numeric / 288::numeric AS quality_ratio
460
+ FROM flow.flow_measurements
461
+ WHERE ((flow_measurements.cube_id::character varying(50)::text, flow_measurements.sink_id::character varying(50)::text) IN ( SELECT DISTINCT pedestrians_locations_gates.cube_id AS location_id,
462
+ pedestrians_locations_gates.direction_id
463
+ FROM analytic.pedestrians_locations_gates)) AND flow_measurements.category::text = 'pedestrian'::text
464
+ GROUP BY (flow_measurements.cube_id::character varying(50)), (flow_measurements.sink_id::character varying(50)), (to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)::date)
465
+ ORDER BY (to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)::date), (flow_measurements.cube_id::character varying(50)) DESC, (flow_measurements.sink_id::character varying(50))
466
+ ), measurements AS (
467
+ SELECT pyro.locations_id,
468
+ pyro.directions_id,
469
+ pyro.measured_from,
470
+ pyro.value,
471
+ pyro.count_n,
472
+ pyro.quality_ratio
473
+ FROM pyro
474
+ UNION ALL
475
+ SELECT flow.location_id,
476
+ flow.direction_id,
477
+ flow.measured_from,
478
+ flow.value,
479
+ flow.count_n,
480
+ flow.quality_ratio
481
+ FROM flow
482
+ UNION ALL
483
+ SELECT wifi.locations_id,
484
+ wifi.directions_id,
485
+ wifi.measured_from,
486
+ wifi.value,
487
+ wifi.count_n,
488
+ wifi.quality_ratio
489
+ FROM wifi
490
+ ), calendar AS (
491
+ SELECT generate_series(min(measurements.measured_from)::timestamp with time zone, max(measurements.measured_from)::timestamp with time zone, '1 day'::interval) AS calendar
492
+ FROM measurements
493
+ ), classes AS (
494
+ SELECT calendar.calendar,
495
+ m_1.location_id,
496
+ m_1.direction_id
497
+ FROM calendar
498
+ JOIN ( SELECT DISTINCT measurements.locations_id::character varying(50) AS location_id,
499
+ measurements.directions_id::character varying(50) AS direction_id
500
+ FROM measurements) m_1 ON true
501
+ )
502
+ SELECT c.calendar,
503
+ c.location_id,
504
+ c.direction_id,
505
+ concat(c.location_id, '-', c.direction_id) AS compound_id,
506
+ COALESCE(m.value, 0::bigint) AS value,
507
+ COALESCE(m.count_n, 0::bigint) AS count_n,
508
+ COALESCE(m.quality_ratio, 0::numeric) AS quality_ratio,
509
+ CASE
510
+ WHEN c.calendar < ll.measurement_start OR c.calendar > ll.measurement_end THEN 4
511
+ WHEN COALESCE(m.quality_ratio, 0::numeric) = 0::numeric THEN 3
512
+ WHEN COALESCE(m.quality_ratio, 0::numeric) < 0.9 THEN 2
513
+ WHEN COALESCE(m.quality_ratio, 0::numeric) > 0.9 AND COALESCE(m.quality_ratio, 0::numeric) < 1::numeric THEN 1
514
+ WHEN COALESCE(m.quality_ratio, 0::numeric) >= 1::numeric THEN 0
515
+ ELSE NULL::integer
516
+ END AS quality_code,
517
+ CASE
518
+ WHEN c.calendar < ll.measurement_start OR c.calendar > ll.measurement_end THEN 'Nenainstalovaná technologie'::text
519
+ WHEN COALESCE(m.quality_ratio, 0::numeric) = 0::numeric THEN 'Bez dat (0%)'::text
520
+ WHEN COALESCE(m.quality_ratio, 0::numeric) < 0.9 THEN 'Částečná data (<90%)'::text
521
+ WHEN COALESCE(m.quality_ratio, 0::numeric) > 0.9 AND COALESCE(m.quality_ratio, 0::numeric) < 1::numeric THEN 'Nekompletní data (>90%)'::text
522
+ WHEN COALESCE(m.quality_ratio, 0::numeric) >= 1::numeric THEN 'Kompletní data (100%)'::text
523
+ ELSE NULL::text
524
+ END AS quality_status
525
+ FROM classes c
526
+ LEFT JOIN measurements m ON c.calendar = m.measured_from AND c.location_id::text = m.locations_id::text AND c.direction_id::text = m.directions_id
527
+ LEFT JOIN analytic.pedestrians_locations_list ll ON c.location_id::text = ll.cube_id::text;
528
+
529
+
530
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_flow_quality
531
+ AS WITH timeline_category AS (
532
+ SELECT DISTINCT fs.cube_id,
533
+ fs.id AS sink_id,
534
+ timeline."timestamp"
535
+ FROM flow_sinks fs
536
+ LEFT JOIN ( SELECT generate_series(date_trunc('month'::text, CURRENT_DATE - '180 days'::interval)::timestamp with time zone, date_trunc('hour'::text, CURRENT_DATE::timestamp with time zone), '01:00:00'::interval) AS "timestamp") timeline ON true
537
+ ), real_nrow AS (
538
+ SELECT date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) AS start_timestamp,
539
+ flow_measurements.cube_id,
540
+ flow_measurements.sink_id,
541
+ count(*) AS n_row,
542
+ count(*) FILTER (WHERE flow_measurements.category::text = 'pedestrian'::text) AS n_row_pedestrian
543
+ FROM flow_measurements
544
+ WHERE flow_measurements.start_timestamp::double precision >= (1000::double precision * date_part('epoch'::text, CURRENT_DATE - '210 days'::interval))
545
+ GROUP BY (date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))), flow_measurements.cube_id, flow_measurements.sink_id
546
+ )
547
+ SELECT t."timestamp",
548
+ date_trunc('month'::text, t."timestamp")::date AS month,
549
+ t.cube_id,
550
+ t.sink_id,
551
+ concat(t.cube_id, ' - ', t.sink_id) AS cube_sink,
552
+ COALESCE(r.n_row::numeric, 0::bigint::numeric) AS n_row,
553
+ COALESCE(r.n_row_pedestrian::numeric, 0::bigint::numeric) AS n_row_pedestrian,
554
+ 108::bigint AS expected_nrow, -- 9 categories * 12 measurements/hr
555
+ 12 AS expected_nrow_pedestrian -- 1 category * 12 measurements/hr
556
+ FROM timeline_category t
557
+ LEFT JOIN real_nrow r ON t."timestamp" = r.start_timestamp AND t.cube_id = r.cube_id AND t.sink_id = r.sink_id;
558
+
559
+
560
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_flow_quality
561
+ AS WITH timeline_category AS (
562
+ SELECT DISTINCT fs.cube_id,
563
+ fs.id AS sink_id,
564
+ timeline."timestamp"
565
+ FROM flow_sinks fs
566
+ LEFT JOIN ( SELECT generate_series(date_trunc('month'::text, CURRENT_DATE - '180 days'::interval)::timestamp with time zone, date_trunc('hour'::text, CURRENT_DATE::timestamp with time zone), '01:00:00'::interval) AS "timestamp") timeline ON true
567
+ ), real_nrow AS (
568
+ SELECT date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) AS start_timestamp,
569
+ flow_measurements.cube_id,
570
+ flow_measurements.sink_id,
571
+ count(*) AS n_row,
572
+ count(*) FILTER (WHERE flow_measurements.category::text = 'pedestrian'::text) AS n_row_pedestrian
573
+ FROM flow_measurements
574
+ WHERE flow_measurements.start_timestamp::double precision >= (1000::double precision * date_part('epoch'::text, CURRENT_DATE - '210 days'::interval))
575
+ GROUP BY (date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))), flow_measurements.cube_id, flow_measurements.sink_id
576
+ )
577
+ SELECT t."timestamp",
578
+ date_trunc('month'::text, t."timestamp")::date AS month,
579
+ t.cube_id,
580
+ t.sink_id,
581
+ concat(t.cube_id, ' - ', t.sink_id) AS cube_sink,
582
+ COALESCE(r.n_row::numeric, 0::bigint::numeric) AS n_row,
583
+ COALESCE(r.n_row_pedestrian::numeric, 0::bigint::numeric) AS n_row_pedestrian,
584
+ 108::bigint AS expected_nrow, -- 9 categories * 12 measurements/hr
585
+ 12 AS expected_nrow_pedestrian -- 1 category * 12 measurements/hr
586
+ FROM timeline_category t
587
+ LEFT JOIN real_nrow r ON t."timestamp" = r.start_timestamp AND t.cube_id = r.cube_id AND t.sink_id = r.sink_id;