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

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,567 @@
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
+ drop table if exists flow_od_measurements_old;
137
+ alter table flow_od_measurements rename to flow_od_measurements_old;
138
+ CREATE TABLE flow_od_measurements (
139
+ cube_id int4 NOT NULL,
140
+ analytic_id int4 NOT NULL,
141
+ sequence_number int4 NOT NULL,
142
+ sink_id int4 NOT NULL,
143
+ start_timestamp timestamptz NOT NULL,
144
+ end_timestamp timestamptz NOT NULL,
145
+ category varchar(150) NOT NULL,
146
+ origin varchar(150) NOT NULL,
147
+ destination varchar(150) NOT NULL,
148
+ value int4 NOT NULL,
149
+ data_validity varchar(50) NULL,
150
+ create_batch_id int8 NULL,
151
+ created_at timestamptz NULL,
152
+ created_by varchar(150) NULL,
153
+ update_batch_id int8 NULL,
154
+ updated_at timestamptz NULL,
155
+ updated_by varchar(150) NULL,
156
+ CONSTRAINT flow_od_measurementspkey_new PRIMARY KEY (cube_id, sink_id, start_timestamp, end_timestamp, category, origin, destination, sequence_number)
157
+ );
158
+ CREATE INDEX flow_od_measurements_created_at_new ON flow_od_measurements USING btree (created_at);
159
+
160
+ -- flow_sinks
161
+ alter table flow_sinks alter column history_start_timestamp type timestamptz using to_timestamp(history_start_timestamp / 1000);
162
+
163
+ -- procedures
164
+ CREATE OR replace procedure update_detections_data()
165
+ LANGUAGE plpgsql
166
+ AS $$
167
+ declare
168
+ lastupdatetimestamp timestamptz;
169
+ begin
170
+ select
171
+ case
172
+ when flowmax.max_measured_from is not null
173
+ then flowmax.max_measured_from
174
+ else to_timestamp(0)
175
+ end as max_measured_from into lastupdatetimestamp
176
+ from (select max(measured_from) - interval '1 hours' as max_measured_from from flow.pedestrians_detections_api) flowMax;
177
+
178
+ insert into flow.pedestrians_detections_api
179
+ with wifi as (
180
+ select
181
+ pedestrians_wifi.location_id,
182
+ pedestrians_wifi.direction_id,
183
+ 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,
184
+ 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,
185
+ sum(pedestrians_wifi.value) as value,
186
+ count(pedestrians_wifi.value) as count_n,
187
+ 3 as quantity
188
+ from
189
+ flow.pedestrians_wifi
190
+ group by
191
+ pedestrians_wifi.location_id,
192
+ pedestrians_wifi.direction_id,
193
+ (date_trunc('hour'::text, pedestrians_wifi.measured_from) + (date_part('minute'::text, pedestrians_wifi.measured_from)::integer / 15)::double precision * '00:15:00'::interval),
194
+ (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)
195
+ ) SELECT wifi.measured_from,
196
+ wifi.measured_to,
197
+ wifi.location_id,
198
+ wifi.direction_id,
199
+ wifi.value,
200
+ wifi.count_n::numeric / wifi.quantity::numeric AS quality
201
+ FROM wifi
202
+ where wifi.measured_from > lastupdatetimestamp
203
+ ON CONFLICT (location_id,direction_id,measured_from,measured_to)
204
+ DO update
205
+ SET value = EXCLUDED.value,
206
+ quality = EXCLUDED.quality;
207
+
208
+ insert into flow.pedestrians_detections_api
209
+ with pyro as (
210
+ select
211
+ cd.locations_id as location_id,
212
+ cd.directions_id as direction_id,
213
+ cd.measured_from as measured_from,
214
+ cd.measured_from + '00:15:00'::interval as measured_to,
215
+ sum(cd.value) as value,
216
+ 1 as count_n,
217
+ 1 as quantity
218
+ from flow.counters_detections cd
219
+ where
220
+ cd.category::text = 'pedestrian'::text
221
+ and (cd.directions_id::text in (select distinct pedestrians_directions_api.direction_id from flow.pedestrians_directions_api))
222
+ group by
223
+ cd.locations_id,
224
+ cd.directions_id,
225
+ (cd.measured_from),
226
+ (cd.measured_from + '00:15:00'::interval)
227
+ )
228
+ select
229
+ pyro.measured_from,
230
+ pyro.measured_to,
231
+ pyro.location_id,
232
+ pyro.direction_id,
233
+ pyro.value,
234
+ pyro.count_n::numeric / pyro.quantity::numeric as quality
235
+ from pyro
236
+ where pyro.measured_from > lastupdatetimestamp
237
+ ON CONFLICT (location_id,direction_id,measured_from,measured_to)
238
+ DO update
239
+ SET value = EXCLUDED.value,
240
+ quality = EXCLUDED.quality;
241
+
242
+
243
+ insert into flow.pedestrians_detections_api
244
+ with vyber as (
245
+ select * from flow.flow_measurements where flow_measurements.start_timestamp > lastupdatetimestamp and category::text = 'pedestrian'::text
246
+ ), flow as (
247
+ select
248
+ pdi.location_id::character varying(50) as location_id,
249
+ vyber.sink_id::character varying(50) as direction_id,
250
+ 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,
251
+ 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,
252
+ sum(vyber.value) as value,
253
+ count(vyber.value) as count_n,
254
+ 3 as quantity
255
+ from vyber
256
+ join flow.pedestrians_directions_api pdi
257
+ on vyber.cube_id::text = pdi.cube_id and vyber.sink_id::text = pdi.direction_id
258
+ group by 1,2,3,4
259
+ ) select
260
+ flow.measured_from,
261
+ flow.measured_to,
262
+ flow.location_id,
263
+ flow.direction_id,
264
+ flow.value,
265
+ flow.count_n::numeric / flow.quantity::numeric as quality
266
+ from flow
267
+ where flow.measured_from > lastupdatetimestamp
268
+ ON CONFLICT (location_id,direction_id,measured_from,measured_to)
269
+ DO update
270
+ SET value = EXCLUDED.value,
271
+ quality = EXCLUDED.quality;
272
+ end;
273
+ $$;
274
+
275
+ -- recreate views
276
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_detections_15min
277
+ AS WITH wifi AS (
278
+ SELECT pedestrians_wifi.location_id AS locations_id,
279
+ pedestrians_wifi.direction_id AS directions_id,
280
+ 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,
281
+ sum(pedestrians_wifi.value) AS value,
282
+ count(pedestrians_wifi.value) AS count_n
283
+ FROM flow.pedestrians_wifi
284
+ 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)
285
+ ), pyro AS (
286
+ SELECT cd.locations_id,
287
+ lg.first_dir AS directions_id,
288
+ cd.measured_from AS measured_from,
289
+ sum(cd.value) AS value,
290
+ count(cd.value) AS count_n
291
+ FROM flow.counters_detections cd
292
+ LEFT JOIN ( SELECT pedestrians_locations_gates.direction_id,
293
+ pedestrians_locations_gates.direction_type,
294
+ min(pedestrians_locations_gates.direction_id::text) OVER (PARTITION BY pedestrians_locations_gates.cube_id, pedestrians_locations_gates.direction_type) AS first_dir
295
+ FROM analytic.pedestrians_locations_gates) lg ON cd.directions_id::text = lg.direction_id::text
296
+ WHERE cd.category::text = 'pedestrian'::text AND (cd.directions_id::text IN ( SELECT DISTINCT pedestrians_locations_gates.direction_id
297
+ FROM analytic.pedestrians_locations_gates))
298
+ GROUP BY cd.locations_id, lg.first_dir, cd.measured_from
299
+ ), flow AS (
300
+ SELECT flow_measurements.cube_id::character varying(50) AS location_id,
301
+ flow_measurements.sink_id::character varying(50) AS direction_id,
302
+ 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,
303
+ sum(flow_measurements.value) AS value,
304
+ count(flow_measurements.value) AS count_n
305
+ FROM flow.flow_measurements
306
+ 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,
307
+ pedestrians_locations_gates.direction_id
308
+ FROM analytic.pedestrians_locations_gates)) AND flow_measurements.category::text = 'pedestrian'::text
309
+ 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)
310
+ 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))
311
+ ), measurements AS (
312
+ SELECT wifi.locations_id,
313
+ wifi.directions_id,
314
+ wifi.measured_from,
315
+ wifi.value,
316
+ wifi.count_n
317
+ FROM wifi
318
+ UNION ALL
319
+ SELECT pyro.locations_id,
320
+ pyro.directions_id,
321
+ pyro.measured_from,
322
+ pyro.value,
323
+ pyro.count_n
324
+ FROM pyro
325
+ UNION ALL
326
+ SELECT flow.location_id,
327
+ flow.direction_id,
328
+ flow.measured_from,
329
+ flow.value,
330
+ flow.count_n
331
+ FROM flow
332
+ ), calendar AS (
333
+ SELECT generate_series(min(measurements.measured_from), max(measurements.measured_from), '00:15:00'::interval) AS calendar
334
+ FROM measurements
335
+ ), classes AS (
336
+ SELECT calendar.calendar,
337
+ m_1.location_id,
338
+ m_1.direction_id
339
+ FROM calendar
340
+ JOIN ( SELECT DISTINCT measurements.locations_id::character varying(50) AS location_id,
341
+ measurements.directions_id::character varying(50) AS direction_id
342
+ FROM measurements) m_1 ON true
343
+ )
344
+ SELECT c.calendar,
345
+ date_trunc('hour'::text, c.calendar) AS calendar_hour,
346
+ c.calendar::date AS calendar_date,
347
+ ll.measurement_start,
348
+ COALESCE(ll.measurement_end, now()::date::timestamp with time zone) AS measurement_end,
349
+ c.location_id,
350
+ c.direction_id,
351
+ concat(c.location_id, '-', c.direction_id) AS compound_id,
352
+ COALESCE(m.value, 0::bigint) AS value,
353
+ COALESCE(m.count_n, 0::bigint) AS count_n
354
+ FROM classes c
355
+ 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
356
+ LEFT JOIN analytic.pedestrians_locations_list ll ON c.location_id::text = ll.cube_id::text
357
+ WHERE c.calendar >= ll.measurement_start AND c.calendar <= COALESCE(ll.measurement_end, now()::date::timestamp with time zone);
358
+
359
+
360
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_detections_api
361
+ AS WITH wifi AS (
362
+ SELECT pedestrians_wifi.location_id,
363
+ pedestrians_wifi.direction_id,
364
+ 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,
365
+ 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,
366
+ sum(pedestrians_wifi.value) AS value,
367
+ count(pedestrians_wifi.value) AS count_n,
368
+ 3 AS quantity
369
+ FROM flow.pedestrians_wifi
370
+ 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)
371
+ ), pyro AS (
372
+ SELECT cd.locations_id AS location_id,
373
+ cd.directions_id AS direction_id,
374
+ cd.measured_from AS measured_from,
375
+ cd.measured_from + '00:15:00'::interval AS measured_to,
376
+ sum(cd.value) AS value,
377
+ count(cd.value) AS count_n,
378
+ 1 AS quantity
379
+ FROM flow.counters_detections cd
380
+ WHERE cd.category::text = 'pedestrian'::text AND (cd.directions_id::text IN ( SELECT DISTINCT pedestrians_directions_api.direction_id
381
+ FROM flow.pedestrians_directions_api))
382
+ GROUP BY cd.locations_id, cd.directions_id, (cd.measured_from), (cd.measured_from + '00:15:00'::interval)
383
+ ), flow AS (
384
+ SELECT flow_measurements.cube_id::character varying(50) AS location_id,
385
+ flow_measurements.sink_id::character varying(50) AS direction_id,
386
+ 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,
387
+ 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,
388
+ sum(flow_measurements.value) AS value,
389
+ count(flow_measurements.value) AS count_n,
390
+ 3 AS quantity
391
+ FROM flow.flow_measurements
392
+ 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,
393
+ pedestrians_directions_api.direction_id
394
+ FROM flow.pedestrians_directions_api)) AND flow_measurements.category::text = 'pedestrian'::text
395
+ 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)
396
+ 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))
397
+ ), measurements AS (
398
+ SELECT wifi.location_id,
399
+ wifi.direction_id,
400
+ wifi.measured_from,
401
+ wifi.measured_to,
402
+ wifi.value,
403
+ wifi.count_n,
404
+ wifi.quantity
405
+ FROM wifi
406
+ UNION ALL
407
+ SELECT pyro.location_id,
408
+ pyro.direction_id,
409
+ pyro.measured_from,
410
+ pyro.measured_to,
411
+ pyro.value,
412
+ pyro.count_n,
413
+ pyro.quantity
414
+ FROM pyro
415
+ UNION ALL
416
+ SELECT flow.location_id,
417
+ flow.direction_id,
418
+ flow.measured_from,
419
+ flow.measured_to,
420
+ flow.value,
421
+ flow.count_n,
422
+ flow.quantity
423
+ FROM flow
424
+ )
425
+ SELECT measurements.measured_from,
426
+ measurements.measured_to,
427
+ pda.location_id::character varying(50) AS location_id,
428
+ measurements.direction_id,
429
+ measurements.value,
430
+ CASE
431
+ WHEN (measurements.count_n::numeric / measurements.quantity::numeric) > 1::numeric THEN 1::numeric
432
+ ELSE measurements.count_n::numeric / measurements.quantity::numeric
433
+ END AS quality
434
+ FROM measurements
435
+ LEFT JOIN flow.pedestrians_directions_api pda ON measurements.direction_id::text = pda.direction_id::text and measurements.location_id = pda.cube_id;
436
+
437
+
438
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_detections_daily
439
+ AS WITH wifi AS (
440
+ SELECT pedestrians_wifi.location_id AS locations_id,
441
+ pedestrians_wifi.direction_id AS directions_id,
442
+ pedestrians_wifi.measured_from::date AS measured_from,
443
+ sum(pedestrians_wifi.value) AS value,
444
+ count(pedestrians_wifi.value) AS count_n,
445
+ count(pedestrians_wifi.value)::numeric / 288::numeric AS quality_ratio
446
+ FROM flow.pedestrians_wifi
447
+ GROUP BY pedestrians_wifi.location_id, pedestrians_wifi.direction_id, (pedestrians_wifi.measured_from::date)
448
+ ), pyro AS (
449
+ SELECT cd.locations_id,
450
+ lg.first_dir AS directions_id,
451
+ cd.measured_from::date AS measured_from,
452
+ sum(cd.value) AS value,
453
+ count(cd.value) / count(DISTINCT cd.directions_id) AS count_n,
454
+ count(cd.value)::numeric / 96::numeric / count(DISTINCT cd.directions_id)::numeric AS quality_ratio
455
+ FROM flow.counters_detections cd
456
+ LEFT JOIN ( SELECT pedestrians_locations_gates.direction_id,
457
+ pedestrians_locations_gates.direction_type,
458
+ min(pedestrians_locations_gates.direction_id::text) OVER (PARTITION BY pedestrians_locations_gates.cube_id, pedestrians_locations_gates.direction_type) AS first_dir
459
+ FROM analytic.pedestrians_locations_gates) lg ON cd.directions_id::text = lg.direction_id::text
460
+ WHERE cd.category::text = 'pedestrian'::text AND (cd.directions_id::text IN ( SELECT DISTINCT pedestrians_locations_gates.direction_id
461
+ FROM analytic.pedestrians_locations_gates))
462
+ GROUP BY cd.locations_id, lg.first_dir, (cd.measured_from::date)
463
+ ), flow AS (
464
+ SELECT flow_measurements.cube_id::character varying(50) AS location_id,
465
+ flow_measurements.sink_id::character varying(50) AS direction_id,
466
+ flow_measurements.start_timestamp::date AS measured_from,
467
+ sum(flow_measurements.value) AS value,
468
+ count(flow_measurements.value) AS count_n,
469
+ count(flow_measurements.value)::numeric / 288::numeric AS quality_ratio
470
+ FROM flow.flow_measurements
471
+ 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,
472
+ pedestrians_locations_gates.direction_id
473
+ FROM analytic.pedestrians_locations_gates)) AND flow_measurements.category::text = 'pedestrian'::text
474
+ GROUP BY (flow_measurements.cube_id::character varying(50)), (flow_measurements.sink_id::character varying(50)), (flow_measurements.start_timestamp::date)
475
+ ORDER BY (flow_measurements.start_timestamp::date), (flow_measurements.cube_id::character varying(50)) DESC, (flow_measurements.sink_id::character varying(50))
476
+ ), measurements AS (
477
+ SELECT pyro.locations_id,
478
+ pyro.directions_id,
479
+ pyro.measured_from,
480
+ pyro.value,
481
+ pyro.count_n,
482
+ pyro.quality_ratio
483
+ FROM pyro
484
+ UNION ALL
485
+ SELECT flow.location_id,
486
+ flow.direction_id,
487
+ flow.measured_from,
488
+ flow.value,
489
+ flow.count_n,
490
+ flow.quality_ratio
491
+ FROM flow
492
+ UNION ALL
493
+ SELECT wifi.locations_id,
494
+ wifi.directions_id,
495
+ wifi.measured_from,
496
+ wifi.value,
497
+ wifi.count_n,
498
+ wifi.quality_ratio
499
+ FROM wifi
500
+ ), calendar AS (
501
+ SELECT generate_series(min(measurements.measured_from)::timestamp with time zone, max(measurements.measured_from)::timestamp with time zone, '1 day'::interval) AS calendar
502
+ FROM measurements
503
+ ), classes AS (
504
+ SELECT calendar.calendar,
505
+ m_1.location_id,
506
+ m_1.direction_id
507
+ FROM calendar
508
+ JOIN ( SELECT DISTINCT measurements.locations_id::character varying(50) AS location_id,
509
+ measurements.directions_id::character varying(50) AS direction_id
510
+ FROM measurements) m_1 ON true
511
+ )
512
+ SELECT c.calendar,
513
+ c.location_id,
514
+ c.direction_id,
515
+ concat(c.location_id, '-', c.direction_id) AS compound_id,
516
+ COALESCE(m.value, 0::bigint) AS value,
517
+ COALESCE(m.count_n, 0::bigint) AS count_n,
518
+ COALESCE(m.quality_ratio, 0::numeric) AS quality_ratio,
519
+ CASE
520
+ WHEN c.calendar < ll.measurement_start OR c.calendar > ll.measurement_end THEN 4
521
+ WHEN COALESCE(m.quality_ratio, 0::numeric) = 0::numeric THEN 3
522
+ WHEN COALESCE(m.quality_ratio, 0::numeric) < 0.9 THEN 2
523
+ WHEN COALESCE(m.quality_ratio, 0::numeric) > 0.9 AND COALESCE(m.quality_ratio, 0::numeric) < 1::numeric THEN 1
524
+ WHEN COALESCE(m.quality_ratio, 0::numeric) >= 1::numeric THEN 0
525
+ ELSE NULL::integer
526
+ END AS quality_code,
527
+ CASE
528
+ WHEN c.calendar < ll.measurement_start OR c.calendar > ll.measurement_end THEN 'Nenainstalovaná technologie'::text
529
+ WHEN COALESCE(m.quality_ratio, 0::numeric) = 0::numeric THEN 'Bez dat (0%)'::text
530
+ WHEN COALESCE(m.quality_ratio, 0::numeric) < 0.9 THEN 'Částečná data (<90%)'::text
531
+ WHEN COALESCE(m.quality_ratio, 0::numeric) > 0.9 AND COALESCE(m.quality_ratio, 0::numeric) < 1::numeric THEN 'Nekompletní data (>90%)'::text
532
+ WHEN COALESCE(m.quality_ratio, 0::numeric) >= 1::numeric THEN 'Kompletní data (100%)'::text
533
+ ELSE NULL::text
534
+ END AS quality_status
535
+ FROM classes c
536
+ 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
537
+ LEFT JOIN analytic.pedestrians_locations_list ll ON c.location_id::text = ll.cube_id::text;
538
+
539
+
540
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_flow_quality
541
+ AS WITH timeline_category AS (
542
+ SELECT DISTINCT fs.cube_id,
543
+ fs.id AS sink_id,
544
+ timeline."timestamp"
545
+ FROM flow_sinks fs
546
+ 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
547
+ ), real_nrow AS (
548
+ SELECT date_trunc('hour'::text, flow_measurements.start_timestamp) AS start_timestamp,
549
+ flow_measurements.cube_id,
550
+ flow_measurements.sink_id,
551
+ count(*) AS n_row,
552
+ count(*) FILTER (WHERE flow_measurements.category::text = 'pedestrian'::text) AS n_row_pedestrian
553
+ FROM flow_measurements
554
+ WHERE extract(epoch from flow_measurements.start_timestamp) >= (1000::double precision * date_part('epoch'::text, CURRENT_DATE - '210 days'::interval))
555
+ GROUP BY (date_trunc('hour'::text, flow_measurements.start_timestamp)), flow_measurements.cube_id, flow_measurements.sink_id
556
+ )
557
+ SELECT t."timestamp",
558
+ date_trunc('month'::text, t."timestamp")::date AS month,
559
+ t.cube_id,
560
+ t.sink_id,
561
+ concat(t.cube_id, ' - ', t.sink_id) AS cube_sink,
562
+ COALESCE(r.n_row::numeric, 0::bigint::numeric) AS n_row,
563
+ COALESCE(r.n_row_pedestrian::numeric, 0::bigint::numeric) AS n_row_pedestrian,
564
+ 108::bigint AS expected_nrow, -- 9 categories * 12 measurements/hr
565
+ 12 AS expected_nrow_pedestrian -- 1 category * 12 measurements/hr
566
+ FROM timeline_category t
567
+ 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,