@golemio/flow 1.0.8-dev.577679507 → 1.0.8-dev.584979260

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', '20220711123652-analytic-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', '20220711123652-analytic-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,7 @@
1
+
2
+ DROP VIEW IF EXISTS analytic.v_pedestrians_detections_15min;
3
+ DROP VIEW IF EXISTS analytic.v_pedestrians_detections_api;
4
+ DROP VIEW IF EXISTS analytic.v_pedestrians_detections_daily;
5
+ DROP VIEW IF EXISTS analytic.v_pedestrians_flow_quality;
6
+ drop table if exists analytic.pedestrians_locations_gates;
7
+ drop table if exists analytic.pedestrians_locations_list;
@@ -0,0 +1,410 @@
1
+ CREATE SCHEMA IF NOT EXISTS analytic;
2
+
3
+ CREATE TABLE analytic.pedestrians_locations_list (
4
+ id serial4 NOT NULL,
5
+ location_name_plain varchar(250) NULL,
6
+ location_name varchar(250) NULL,
7
+ lat numeric NULL,
8
+ lng numeric NULL,
9
+ address varchar(250) NULL,
10
+ city_district varchar(250) NULL,
11
+ tech varchar(250) NULL,
12
+ map_picture varchar(2500) NULL,
13
+ gate_picture varchar(2500) NULL,
14
+ measurement_start timestamptz NULL,
15
+ measurement_end timestamptz NULL,
16
+ id_equiptment varchar(250) NULL,
17
+ cube_id varchar(250) NULL,
18
+ lon varbit NULL,
19
+ map_image varchar(55) NULL,
20
+ place_image varchar(59) NULL,
21
+ "table" varchar(19) NULL,
22
+ CONSTRAINT equipment_list_pkey PRIMARY KEY (id)
23
+ );
24
+ COMMENT ON TABLE analytic.pedestrians_locations_list IS '-- tabulka lokací. obsahuje lokace různých druhů a hodně custom dat, takže ji tvoříme ručně. sporadicky probíhají aktualizace
25
+ -- tabulka se nachází na https://operator.sharepoint.com/:f:/s/Datova_Platforma/EsE5uSdyLzdImWgyrMY8nFcBzwtH7GOr6TXtzNBn3FLIJw?e=fOov5s a jmenuje se locations_list.csv';
26
+
27
+ INSERT INTO analytic.pedestrians_locations_list (location_name_plain,location_name,lat,lng,address,city_district,tech,map_picture,gate_picture,measurement_start,measurement_end,id_equiptment,cube_id,lon,map_image,place_image,"table") VALUES
28
+ ('Výstaviště - viadukt VO 2','107043 - Výstaviště - viadukt VO 2',50.1042106575078,14.431679367811926,'U Výstaviště (viadukt)','Praha 7','kamera',NULL,NULL,'2020-09-02 00:00:00+02',NULL,NULL,'0',NULL,'https://storage.golemio.cz/intenzita-pesi-dopravy/1.png','https://storage.golemio.cz/intenzita-pesi-dopravy/1-map.png','flow_measurements'),
29
+ ('Rytířská 12','101336 - Rytířská 12',50.084781715549276,14.422412102465428,'Na Můstku 12','Praha 1','kamera',NULL,NULL,'2020-09-02 00:00:00+02',NULL,NULL,'1',NULL,'https://storage.golemio.cz/intenzita-pesi-dopravy/2.png','https://storage.golemio.cz/intenzita-pesi-dopravy/2-map.png','flow_measurements'),
30
+ ('Výstaviště - viadukt','Výstaviště - viadukt',50.1042106575078,14.431679367811926,'U Výstaviště (viadukt)','Praha 7','pyro',NULL,NULL,'2020-07-23 00:00:00+02','2020-11-10 00:00:00+01',NULL,'ecoCounter-100061215',NULL,'https://storage.golemio.cz/intenzita-pesi-dopravy/1.png','https://storage.golemio.cz/intenzita-pesi-dopravy/1-map.png','counters_detections'),
31
+ ('Stromovka - U Výstaviště','Stromovka - U Výstaviště',50.10489433551656,14.429609941952332,'U Výstaviště 67','Praha 7','wifi',NULL,NULL,'2020-11-24 00:00:00+01',NULL,NULL,'wifi1',NULL,NULL,'https://storage.golemio.cz/intenzita-pesi-dopravy/4-map.png','xyz'),
32
+ ('Stromovka - U Výstaviště','Stromovka - U Výstaviště',50.10489433551656,14.429609941952332,'U Výstaviště 67','Praha 7','pyro',NULL,NULL,'2020-11-11 00:00:00+01','2021-03-04 00:00:00+01',NULL,'ecoCounter-100065230',NULL,'https://storage.golemio.cz/intenzita-pesi-dopravy/3.png','https://storage.golemio.cz/intenzita-pesi-dopravy/3-map.png','counters_detections'),
33
+ ('Stromovka - U Výstaviště','Stromovka - U Výstaviště',50.10489433551656,14.429609941952332,'U Výstaviště 67','Praha 7','kamera',NULL,NULL,'2020-12-17 00:00:00+01',NULL,NULL,'13',NULL,'https://storage.golemio.cz/intenzita-pesi-dopravy/3.png','https://storage.golemio.cz/intenzita-pesi-dopravy/3-map.png','flow_measurements'),
34
+ ('Staroměstská věž pevná VI','101004 - Staroměstská věž pevná VI',50.08626,14.41301,'Karlův most','Praha 1','kamera',NULL,NULL,'2020-09-02 00:00:00+02',NULL,NULL,'9',NULL,'https://storage.golemio.cz/intenzita-pesi-dopravy/5.png','https://storage.golemio.cz/intenzita-pesi-dopravy/5-map.png','flow_measurements'),
35
+ ('Železniční most - nápl. I (náplavka)','202049 - Železniční most - nápl. I (náplavka)',50.06739,14.41468,'Novoměstská náplavka','Praha 2','kamera',NULL,NULL,'2020-09-02 00:00:00+02',NULL,NULL,'10',NULL,'https://storage.golemio.cz/intenzita-pesi-dopravy/6.png','https://storage.golemio.cz/intenzita-pesi-dopravy/6-map.png','flow_measurements'),
36
+ ('Železniční most - náplavka II','202050 - Železniční most - náplavka II',50.06693,14.41480,'Novoměstská náplavka','Praha 2','kamera',NULL,NULL,'2020-09-02 00:00:00+02',NULL,NULL,'11',NULL,'https://storage.golemio.cz/intenzita-pesi-dopravy/7.png','https://storage.golemio.cz/intenzita-pesi-dopravy/7-map.png','flow_measurements'),
37
+ ('Železniční most - nápl. I (rampa)','202049 - Železniční most - nápl. I (rampa)',50.06739,14.41468,'Novoměstská náplavka','Praha 2','kamera',NULL,NULL,'2020-09-02 00:00:00+02',NULL,NULL,'10',NULL,'https://storage.golemio.cz/intenzita-pesi-dopravy/6.png','https://storage.golemio.cz/intenzita-pesi-dopravy/6-map.png','flow_measurements');
38
+ INSERT INTO analytic.pedestrians_locations_list (location_name_plain,location_name,lat,lng,address,city_district,tech,map_picture,gate_picture,measurement_start,measurement_end,id_equiptment,cube_id,lon,map_image,place_image,"table") VALUES
39
+ ('Železniční most - nápl. I (plný profil)','202049 - Železniční most - nápl. I (plný profil)',50.06739,14.41468,'Novoměstská náplavka','Praha 2','kamera',NULL,NULL,'2020-09-02 00:00:00+02',NULL,NULL,'10',NULL,'https://storage.golemio.cz/intenzita-pesi-dopravy/6.png','https://storage.golemio.cz/intenzita-pesi-dopravy/6-map.png','flow_measurements');
40
+
41
+
42
+ CREATE TABLE analytic.pedestrians_locations_gates (
43
+ direction_id varchar(250) NOT NULL,
44
+ cube_id varchar(250) NOT NULL,
45
+ location_id int4 NOT NULL,
46
+ name varchar(250) NOT NULL,
47
+ direction_type varchar(250) NOT NULL,
48
+ CONSTRAINT equipment_gates_pkey PRIMARY KEY (direction_id, location_id)
49
+ );
50
+
51
+ INSERT INTO analytic.pedestrians_locations_gates (direction_id,cube_id,location_id,"name",direction_type) VALUES
52
+ ('4','0',1,'Směr pod viadukt','Směr 1'),
53
+ ('7','0',1,'Směr Výstaviště','Směr 2'),
54
+ ('2','1',2,'Směr Můstek','Směr 1'),
55
+ ('3','1',2,'Směr Rytířská','Směr 2'),
56
+ ('5','9',7,'Směr Malá strana','Směr 1'),
57
+ ('8','9',7,'Směr Staré město','Směr 2'),
58
+ ('6','10',8,'Náplavka, směr centrum','Směr 1'),
59
+ ('9','10',10,'Rampa, směr centrum','Směr 1'),
60
+ ('10','10',11,'Plný profil, směr centrum','Směr 1'),
61
+ ('11','10',8,'Náplavka, směr Vyšehrad','Směr 2');
62
+ INSERT INTO analytic.pedestrians_locations_gates (direction_id,cube_id,location_id,"name",direction_type) VALUES
63
+ ('13','10',10,'Rampa, směr Vyšehrad','Směr 2'),
64
+ ('15','10',11,'Plný profil, směr Vyšehrad','Směr 2'),
65
+ ('35','11',9,'Plný profil, směr centrum','Směr 1'),
66
+ ('29','11',9,'Plný profil, směr Vyšehrad','Směr 2'),
67
+ ('7','13',6,'Směr Planetarium','Směr 1'),
68
+ ('8','13',6,'Směr Holešovice','Směr 2'),
69
+ ('ecoCounter-103061215','ecoCounter-100061215',3,'Směr pod viadukt','Směr 1'),
70
+ ('ecoCounter-104061215','ecoCounter-100061215',3,'Směr Výstaviště','Směr 2'),
71
+ ('ecoCounter-353245241','ecoCounter-100065230',5,'Směr Planetarium','Směr 1'),
72
+ ('ecoCounter-102065230','ecoCounter-100065230',5,'Směr Planetarium','Směr 1');
73
+ INSERT INTO analytic.pedestrians_locations_gates (direction_id,cube_id,location_id,"name",direction_type) VALUES
74
+ ('ecoCounter-353240728','ecoCounter-100065230',5,'Směr Planetarium','Směr 1'),
75
+ ('ecoCounter-353235722','ecoCounter-100065230',5,'Směr Planetarium','Směr 1'),
76
+ ('ecoCounter-353231243','ecoCounter-100065230',5,'Směr Planetarium','Směr 1'),
77
+ ('ecoCounter-353245239','ecoCounter-100065230',5,'Směr Holešovice','Směr 2'),
78
+ ('ecoCounter-101065230','ecoCounter-100065230',5,'Směr Holešovice','Směr 2'),
79
+ ('ecoCounter-353240727','ecoCounter-100065230',5,'Směr Holešovice','Směr 2'),
80
+ ('ecoCounter-353235721','ecoCounter-100065230',5,'Směr Holešovice','Směr 2'),
81
+ ('ecoCounter-353231242','ecoCounter-100065230',5,'Směr Holešovice','Směr 2'),
82
+ ('dir1','wifi1',4,'Směr Holešovice','Směr 1'),
83
+ ('dir2','wifi1',4,'Směr Planetarium','Směr 2');
84
+
85
+ COMMENT ON TABLE analytic.pedestrians_locations_gates IS '-- tabulka směrů k jednotlivým lokacím, platí to samé co u lokations_list.
86
+ -- nachází se na https://operator.sharepoint.com/:f:/s/Datova_Platforma/EsE5uSdyLzdImWgyrMY8nFcBzwtH7GOr6TXtzNBn3FLIJw?e=fOov5s a jmenuje se directions_list.csv';
87
+
88
+ COMMENT ON TABLE pedestrians_wifi IS '-- tabulka na primární data wifi měření, jsou pouze jako csv na sharepointu https://operator.sharepoint.com/:f:/s/Datova_Platforma/EroMekiKaJdOtMPeF_Q8wPEBlZm5tLKHCgpK13H6hlc0Zg?e=4RbCkT';
89
+
90
+ -- analytic.v_pedestrians_detections_15min source
91
+
92
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_detections_15min
93
+ AS WITH wifi AS (
94
+ SELECT pedestrians_wifi.location_id AS locations_id,
95
+ pedestrians_wifi.direction_id AS directions_id,
96
+ 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,
97
+ sum(pedestrians_wifi.value) AS value,
98
+ count(pedestrians_wifi.value) AS count_n
99
+ FROM flow.pedestrians_wifi
100
+ 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)
101
+ ), pyro AS (
102
+ SELECT cd.locations_id,
103
+ lg.first_dir AS directions_id,
104
+ to_timestamp((cd.measured_from / 1000)::double precision) AS measured_from,
105
+ sum(cd.value) AS value,
106
+ count(cd.value) AS count_n
107
+ FROM flow.counters_detections cd
108
+ LEFT JOIN ( SELECT pedestrians_locations_gates.direction_id,
109
+ pedestrians_locations_gates.direction_type,
110
+ min(pedestrians_locations_gates.direction_id::text) OVER (PARTITION BY pedestrians_locations_gates.cube_id, pedestrians_locations_gates.direction_type) AS first_dir
111
+ FROM analytic.pedestrians_locations_gates) lg ON cd.directions_id::text = lg.direction_id::text
112
+ WHERE cd.category::text = 'pedestrian'::text AND (cd.directions_id::text IN ( SELECT DISTINCT pedestrians_locations_gates.direction_id
113
+ FROM analytic.pedestrians_locations_gates))
114
+ GROUP BY cd.locations_id, lg.first_dir, (to_timestamp((cd.measured_from / 1000)::double precision))
115
+ ), flow AS (
116
+ SELECT flow_measurements.cube_id::character varying(50) AS location_id,
117
+ flow_measurements.sink_id::character varying(50) AS direction_id,
118
+ 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,
119
+ sum(flow_measurements.value) AS value,
120
+ count(flow_measurements.value) AS count_n
121
+ FROM flow.flow_measurements
122
+ 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,
123
+ pedestrians_locations_gates.direction_id
124
+ FROM analytic.pedestrians_locations_gates)) AND flow_measurements.category::text = 'pedestrian'::text
125
+ 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)
126
+ 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))
127
+ ), measurements AS (
128
+ SELECT wifi.locations_id,
129
+ wifi.directions_id,
130
+ wifi.measured_from,
131
+ wifi.value,
132
+ wifi.count_n
133
+ FROM wifi
134
+ UNION ALL
135
+ SELECT pyro.locations_id,
136
+ pyro.directions_id,
137
+ pyro.measured_from,
138
+ pyro.value,
139
+ pyro.count_n
140
+ FROM pyro
141
+ UNION ALL
142
+ SELECT flow.location_id,
143
+ flow.direction_id,
144
+ flow.measured_from,
145
+ flow.value,
146
+ flow.count_n
147
+ FROM flow
148
+ ), calendar AS (
149
+ SELECT generate_series(min(measurements.measured_from), max(measurements.measured_from), '00:15:00'::interval) AS calendar
150
+ FROM measurements
151
+ ), classes AS (
152
+ SELECT calendar.calendar,
153
+ m_1.location_id,
154
+ m_1.direction_id
155
+ FROM calendar
156
+ JOIN ( SELECT DISTINCT measurements.locations_id::character varying(50) AS location_id,
157
+ measurements.directions_id::character varying(50) AS direction_id
158
+ FROM measurements) m_1 ON true
159
+ )
160
+ SELECT c.calendar,
161
+ date_trunc('hour'::text, c.calendar) AS calendar_hour,
162
+ c.calendar::date AS calendar_date,
163
+ ll.measurement_start,
164
+ COALESCE(ll.measurement_end, now()::date::timestamp with time zone) AS measurement_end,
165
+ c.location_id,
166
+ c.direction_id,
167
+ concat(c.location_id, '-', c.direction_id) AS compound_id,
168
+ COALESCE(m.value, 0::bigint) AS value,
169
+ COALESCE(m.count_n, 0::bigint) AS count_n
170
+ FROM classes c
171
+ 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
172
+ LEFT JOIN analytic.pedestrians_locations_list ll ON c.location_id::text = ll.cube_id::text
173
+ WHERE c.calendar >= ll.measurement_start AND c.calendar <= COALESCE(ll.measurement_end, now()::date::timestamp with time zone);
174
+
175
+
176
+ -- analytic.v_pedestrians_detections_api source
177
+
178
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_detections_api
179
+ AS WITH wifi AS (
180
+ SELECT pedestrians_wifi.location_id,
181
+ pedestrians_wifi.direction_id,
182
+ 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,
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 + '00:15:00'::interval AS measured_to,
184
+ sum(pedestrians_wifi.value) AS value,
185
+ count(pedestrians_wifi.value) AS count_n,
186
+ 3 AS quantity
187
+ FROM flow.pedestrians_wifi
188
+ 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)
189
+ ), pyro AS (
190
+ SELECT cd.locations_id AS location_id,
191
+ cd.directions_id AS direction_id,
192
+ to_timestamp((cd.measured_from / 1000)::double precision) AS measured_from,
193
+ to_timestamp((cd.measured_from / 1000)::double precision) + '00:15:00'::interval AS measured_to,
194
+ sum(cd.value) AS value,
195
+ count(cd.value) AS count_n,
196
+ 1 AS quantity
197
+ FROM flow.counters_detections cd
198
+ WHERE cd.category::text = 'pedestrian'::text AND (cd.directions_id::text IN ( SELECT DISTINCT pedestrians_directions_api.direction_id
199
+ FROM flow.pedestrians_directions_api))
200
+ 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)
201
+ ), flow AS (
202
+ SELECT flow_measurements.cube_id::character varying(50) AS location_id,
203
+ flow_measurements.sink_id::character varying(50) AS direction_id,
204
+ 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,
205
+ 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,
206
+ sum(flow_measurements.value) AS value,
207
+ count(flow_measurements.value) AS count_n,
208
+ 3 AS quantity
209
+ FROM flow.flow_measurements
210
+ 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,
211
+ pedestrians_directions_api.direction_id
212
+ FROM flow.pedestrians_directions_api)) AND flow_measurements.category::text = 'pedestrian'::text
213
+ 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)
214
+ 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))
215
+ ), measurements AS (
216
+ SELECT wifi.location_id,
217
+ wifi.direction_id,
218
+ wifi.measured_from,
219
+ wifi.measured_to,
220
+ wifi.value,
221
+ wifi.count_n,
222
+ wifi.quantity
223
+ FROM wifi
224
+ UNION ALL
225
+ SELECT pyro.location_id,
226
+ pyro.direction_id,
227
+ pyro.measured_from,
228
+ pyro.measured_to,
229
+ pyro.value,
230
+ pyro.count_n,
231
+ pyro.quantity
232
+ FROM pyro
233
+ UNION ALL
234
+ SELECT flow.location_id,
235
+ flow.direction_id,
236
+ flow.measured_from,
237
+ flow.measured_to,
238
+ flow.value,
239
+ flow.count_n,
240
+ flow.quantity
241
+ FROM flow
242
+ )
243
+ SELECT measurements.measured_from,
244
+ measurements.measured_to,
245
+ pda.location_id::character varying(50) AS location_id,
246
+ measurements.direction_id,
247
+ measurements.value,
248
+ CASE
249
+ WHEN (measurements.count_n::numeric / measurements.quantity::numeric) > 1::numeric THEN 1::numeric
250
+ ELSE measurements.count_n::numeric / measurements.quantity::numeric
251
+ END AS quality
252
+ FROM measurements
253
+ LEFT JOIN flow.pedestrians_directions_api pda ON measurements.direction_id::text = pda.direction_id::text;
254
+
255
+
256
+ -- analytic.v_pedestrians_detections_daily source
257
+
258
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_detections_daily
259
+ AS WITH wifi AS (
260
+ SELECT pedestrians_wifi.location_id AS locations_id,
261
+ pedestrians_wifi.direction_id AS directions_id,
262
+ pedestrians_wifi.measured_from::date AS measured_from,
263
+ sum(pedestrians_wifi.value) AS value,
264
+ count(pedestrians_wifi.value) AS count_n,
265
+ count(pedestrians_wifi.value)::numeric / 288::numeric AS quality_ratio
266
+ FROM flow.pedestrians_wifi
267
+ GROUP BY pedestrians_wifi.location_id, pedestrians_wifi.direction_id, (pedestrians_wifi.measured_from::date)
268
+ ), pyro AS (
269
+ SELECT cd.locations_id,
270
+ lg.first_dir AS directions_id,
271
+ to_timestamp((cd.measured_from / 1000)::double precision)::date AS measured_from,
272
+ sum(cd.value) AS value,
273
+ count(cd.value) / count(DISTINCT cd.directions_id) AS count_n,
274
+ count(cd.value)::numeric / 96::numeric / count(DISTINCT cd.directions_id)::numeric AS quality_ratio
275
+ FROM flow.counters_detections cd
276
+ LEFT JOIN ( SELECT pedestrians_locations_gates.direction_id,
277
+ pedestrians_locations_gates.direction_type,
278
+ min(pedestrians_locations_gates.direction_id::text) OVER (PARTITION BY pedestrians_locations_gates.cube_id, pedestrians_locations_gates.direction_type) AS first_dir
279
+ FROM analytic.pedestrians_locations_gates) lg ON cd.directions_id::text = lg.direction_id::text
280
+ WHERE cd.category::text = 'pedestrian'::text AND (cd.directions_id::text IN ( SELECT DISTINCT pedestrians_locations_gates.direction_id
281
+ FROM analytic.pedestrians_locations_gates))
282
+ GROUP BY cd.locations_id, lg.first_dir, (to_timestamp((cd.measured_from / 1000)::double precision)::date)
283
+ ), flow AS (
284
+ SELECT flow_measurements.cube_id::character varying(50) AS location_id,
285
+ flow_measurements.sink_id::character varying(50) AS direction_id,
286
+ to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)::date AS measured_from,
287
+ sum(flow_measurements.value) AS value,
288
+ count(flow_measurements.value) AS count_n,
289
+ count(flow_measurements.value)::numeric / 288::numeric AS quality_ratio
290
+ FROM flow.flow_measurements
291
+ 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,
292
+ pedestrians_locations_gates.direction_id
293
+ FROM analytic.pedestrians_locations_gates)) AND flow_measurements.category::text = 'pedestrian'::text
294
+ 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)
295
+ 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))
296
+ ), measurements AS (
297
+ SELECT pyro.locations_id,
298
+ pyro.directions_id,
299
+ pyro.measured_from,
300
+ pyro.value,
301
+ pyro.count_n,
302
+ pyro.quality_ratio
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
+ flow.quality_ratio
311
+ FROM flow
312
+ UNION ALL
313
+ SELECT wifi.locations_id,
314
+ wifi.directions_id,
315
+ wifi.measured_from,
316
+ wifi.value,
317
+ wifi.count_n,
318
+ wifi.quality_ratio
319
+ FROM wifi
320
+ ), calendar AS (
321
+ SELECT generate_series(min(measurements.measured_from)::timestamp with time zone, max(measurements.measured_from)::timestamp with time zone, '1 day'::interval) AS calendar
322
+ FROM measurements
323
+ ), classes AS (
324
+ SELECT calendar.calendar,
325
+ m_1.location_id,
326
+ m_1.direction_id
327
+ FROM calendar
328
+ JOIN ( SELECT DISTINCT measurements.locations_id::character varying(50) AS location_id,
329
+ measurements.directions_id::character varying(50) AS direction_id
330
+ FROM measurements) m_1 ON true
331
+ )
332
+ SELECT c.calendar,
333
+ c.location_id,
334
+ c.direction_id,
335
+ concat(c.location_id, '-', c.direction_id) AS compound_id,
336
+ COALESCE(m.value, 0::bigint) AS value,
337
+ COALESCE(m.count_n, 0::bigint) AS count_n,
338
+ COALESCE(m.quality_ratio, 0::numeric) AS quality_ratio,
339
+ CASE
340
+ WHEN c.calendar < ll.measurement_start OR c.calendar > ll.measurement_end THEN 4
341
+ WHEN COALESCE(m.quality_ratio, 0::numeric) = 0::numeric THEN 3
342
+ WHEN COALESCE(m.quality_ratio, 0::numeric) < 0.9 THEN 2
343
+ WHEN COALESCE(m.quality_ratio, 0::numeric) > 0.9 AND COALESCE(m.quality_ratio, 0::numeric) < 1::numeric THEN 1
344
+ WHEN COALESCE(m.quality_ratio, 0::numeric) >= 1::numeric THEN 0
345
+ ELSE NULL::integer
346
+ END AS quality_code,
347
+ CASE
348
+ WHEN c.calendar < ll.measurement_start OR c.calendar > ll.measurement_end THEN 'Nenainstalovaná technologie'::text
349
+ WHEN COALESCE(m.quality_ratio, 0::numeric) = 0::numeric THEN 'Bez dat (0%)'::text
350
+ WHEN COALESCE(m.quality_ratio, 0::numeric) < 0.9 THEN 'Částečná data (<90%)'::text
351
+ WHEN COALESCE(m.quality_ratio, 0::numeric) > 0.9 AND COALESCE(m.quality_ratio, 0::numeric) < 1::numeric THEN 'Nekompletní data (>90%)'::text
352
+ WHEN COALESCE(m.quality_ratio, 0::numeric) >= 1::numeric THEN 'Kompletní data (100%)'::text
353
+ ELSE NULL::text
354
+ END AS quality_status
355
+ FROM classes c
356
+ 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
357
+ LEFT JOIN analytic.pedestrians_locations_list ll ON c.location_id::text = ll.cube_id::text;
358
+
359
+
360
+ -- analytic.v_pedestrians_flow_quality source
361
+
362
+ CREATE OR REPLACE VIEW analytic.v_pedestrians_flow_quality
363
+ AS WITH timeline_category AS (
364
+ SELECT DISTINCT fs.cube_id,
365
+ fs.id AS sink_id,
366
+ timeline."timestamp"
367
+ FROM flow.flow_sinks fs
368
+ 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
369
+ ), base_table AS (
370
+ SELECT date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision)) AS start_timestamp,
371
+ flow_measurements.cube_id,
372
+ flow_measurements.sink_id,
373
+ flow_measurements.category,
374
+ count(flow_measurements.cube_id) AS count
375
+ FROM flow.flow_measurements
376
+ WHERE flow_measurements.start_timestamp::double precision >= (1000::double precision * date_part('epoch'::text, CURRENT_DATE - '210 days'::interval))
377
+ GROUP BY (date_trunc('hour'::text, to_timestamp((flow_measurements.start_timestamp / 1000)::double precision))), flow_measurements.cube_id, flow_measurements.sink_id, flow_measurements.category
378
+ ), real_nrow AS (
379
+ SELECT base_table.start_timestamp,
380
+ base_table.cube_id,
381
+ base_table.sink_id,
382
+ sum(base_table.count) AS n_row,
383
+ sum(base_table.count) FILTER (WHERE base_table.category::text = 'pedestrian'::text) AS n_row_pedestrian
384
+ FROM base_table
385
+ GROUP BY base_table.start_timestamp, base_table.cube_id, base_table.sink_id
386
+ ), expected_nrow AS (
387
+ SELECT base_table.cube_id,
388
+ base_table.sink_id,
389
+ count(DISTINCT base_table.category) * 12 AS expected_nrow,
390
+ 12 AS expected_nrow_pedestrian
391
+ FROM base_table
392
+ WHERE base_table.start_timestamp >= (CURRENT_DATE - '30 days'::interval)
393
+ GROUP BY base_table.cube_id, base_table.sink_id
394
+ )
395
+ SELECT t."timestamp",
396
+ date_trunc('month'::text, t."timestamp")::date AS month,
397
+ t.cube_id,
398
+ t.sink_id,
399
+ concat(t.cube_id, ' - ', t.sink_id) AS cube_sink,
400
+ COALESCE(r.n_row, 0::bigint::numeric) AS n_row,
401
+ COALESCE(r.n_row_pedestrian, 0::bigint::numeric) AS n_row_pedestrian,
402
+ er.expected_nrow,
403
+ er.expected_nrow_pedestrian
404
+ FROM timeline_category t
405
+ 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
406
+ LEFT JOIN expected_nrow er ON t.cube_id = er.cube_id AND t.sink_id = er.sink_id;
407
+
408
+ -- analytic.v_flow_quality source
409
+
410
+ DROP VIEW IF EXISTS analytic.v_flow_quality
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@golemio/flow",
3
- "version": "1.0.8-dev.577679507",
3
+ "version": "1.0.8-dev.584979260",
4
4
  "description": "Golemio Flow Module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",