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