@golemio/pid 2.13.6-dev.1310475463 → 2.13.6-dev.1313101349

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', '20240523123717-add-stop-name-and-gps-to-history-data-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', '20240523123717-add-stop-name-and-gps-to-history-data-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,107 @@
1
+ alter table vehiclepositions_stop_times_history
2
+ drop COLUMN stop_name,
3
+ drop COLUMN lat,
4
+ drop COLUMN lng;
5
+
6
+
7
+ drop view v_vehiclepositions_past_stop_times;
8
+
9
+ CREATE VIEW v_vehiclepositions_past_stop_times
10
+ AS SELECT sub.trips_id AS rt_trip_id,
11
+ sub.last_stop_sequence AS stop_sequence,
12
+ sub.last_stop_id AS stop_id,
13
+ sub.last_stop_arrival_time AS stop_arrival,
14
+ sub.last_stop_departure_time AS stop_departure,
15
+ max(sub.delay_stop_arrival) AS stop_arr_delay,
16
+ min(sub.delay_stop_departure) AS stop_dep_delay
17
+ FROM ( SELECT rt_position.trips_id,
18
+ rt_position.last_stop_sequence,
19
+ rt_position.last_stop_id,
20
+ rt_position.last_stop_arrival_time,
21
+ rt_position.last_stop_departure_time,
22
+ rt_position.delay_stop_arrival,
23
+ rt_position.delay_stop_departure,
24
+ row_number() OVER seq AS rn
25
+ FROM vehiclepositions_positions rt_position
26
+ WHERE (rt_position.delay_stop_arrival IS NOT NULL OR rt_position.delay_stop_departure IS NOT NULL) AND (rt_position.state_position::text = ANY (ARRAY['on_track'::character varying::text, 'at_stop'::character varying::text, 'after_track'::character varying::text]))
27
+ WINDOW seq AS (PARTITION BY rt_position.trips_id, rt_position.last_stop_sequence, rt_position.state_position ORDER BY rt_position.id)) sub
28
+ WHERE sub.rn = 1
29
+ GROUP BY sub.trips_id, sub.last_stop_arrival_time, sub.last_stop_departure_time, sub.last_stop_sequence, sub.last_stop_id
30
+ ORDER BY sub.trips_id, sub.last_stop_sequence;
31
+
32
+ CREATE OR REPLACE PROCEDURE vehiclepositions_data_retention(inout numberOfRows int, in dataRetentionMinutes int)
33
+ LANGUAGE plpgsql
34
+ SET search_path FROM CURRENT
35
+ AS $procedure$
36
+ declare
37
+ idsForDelete varchar(255)[];
38
+ begin
39
+ select array_agg(t.id) from vehiclepositions_trips t
40
+ left join vehiclepositions_positions p on
41
+ p.id = t.last_position_id
42
+ where
43
+ p.valid_to < (NOW() - (dataRetentionMinutes || ' minutes')::interval)
44
+ or (t.gtfs_trip_id is null and p.valid_to is null and p.updated_at < NOW() - (dataRetentionMinutes || ' minutes')::interval) -- entries with valid to is null
45
+ into idsForDelete;
46
+
47
+ INSERT INTO vehiclepositions_trips_history
48
+ select * from vehiclepositions_trips where id = ANY(idsForDelete)
49
+ on conflict do nothing;
50
+
51
+ INSERT INTO vehiclepositions_positions_history
52
+ select * from vehiclepositions_positions where trips_id = ANY(idsForDelete)
53
+ on conflict do nothing;
54
+
55
+ insert into vehiclepositions_stop_times_history (
56
+ rt_trip_id,
57
+ gtfs_date,
58
+ gtfs_trip_id,
59
+ gtfs_direction_id,
60
+ gtfs_route_short_name,
61
+ gtfs_route_type,
62
+ run_number,
63
+ vehicle_registration_number,
64
+ gtfs_stop_id,
65
+ gtfs_stop_sequence,
66
+ current_stop_arrival,
67
+ current_stop_departure,
68
+ current_stop_arr_delay,
69
+ current_stop_dep_delay,
70
+ created_at,
71
+ updated_at,
72
+ origin_route_name
73
+ )
74
+ select
75
+ rt_trip.id,
76
+ rt_trip.gtfs_date,
77
+ rt_trip.gtfs_trip_id,
78
+ rt_trip.gtfs_direction_id,
79
+ rt_trip.gtfs_route_short_name,
80
+ rt_trip.gtfs_route_type,
81
+ rt_trip.run_number,
82
+ rt_trip.vehicle_registration_number,
83
+ stop_time.stop_id,
84
+ stop_time.stop_sequence,
85
+ stop_time.stop_arrival,
86
+ stop_time.stop_departure,
87
+ stop_time.stop_arr_delay,
88
+ stop_time.stop_dep_delay,
89
+ now(),
90
+ now(),
91
+ rt_trip.origin_route_name
92
+ from
93
+ vehiclepositions_trips rt_trip
94
+ join
95
+ v_vehiclepositions_past_stop_times stop_time on stop_time.rt_trip_id = rt_trip.id
96
+ where
97
+ rt_trip.id = ANY(idsForDelete)
98
+ on conflict do nothing;
99
+
100
+
101
+ delete from vehiclepositions_positions where trips_id = ANY(idsForDelete);
102
+ delete from vehiclepositions_trips where id = ANY(idsForDelete);
103
+ delete from vehiclepositions_cis_stops where rt_trip_id = ANY(idsForDelete);
104
+
105
+ select array_length(idsForDelete,1) into numberOfRows;
106
+ end;
107
+ $procedure$;
@@ -0,0 +1,118 @@
1
+ alter table vehiclepositions_stop_times_history
2
+ add COLUMN stop_name varchar(255),
3
+ add COLUMN lat numeric,
4
+ add COLUMN lng numeric;
5
+
6
+
7
+ CREATE OR REPLACE VIEW v_vehiclepositions_past_stop_times
8
+ AS SELECT sub.trips_id AS rt_trip_id,
9
+ sub.last_stop_sequence AS stop_sequence,
10
+ sub.last_stop_id AS stop_id,
11
+ sub.last_stop_arrival_time AS stop_arrival,
12
+ sub.last_stop_departure_time AS stop_departure,
13
+ max(sub.delay_stop_arrival) AS stop_arr_delay,
14
+ min(sub.delay_stop_departure) AS stop_dep_delay,
15
+ sub.last_stop_name as stop_name,
16
+ sub.lat as lat,
17
+ sub.lng as lng
18
+ FROM ( SELECT rt_position.trips_id,
19
+ rt_position.last_stop_sequence,
20
+ rt_position.last_stop_id,
21
+ rt_position.last_stop_arrival_time,
22
+ rt_position.last_stop_departure_time,
23
+ rt_position.delay_stop_arrival,
24
+ rt_position.delay_stop_departure,
25
+ rt_position.last_stop_name,
26
+ rs.stop_lat as lat,
27
+ rs.stop_lon as lng,
28
+ row_number() OVER seq AS rn
29
+ FROM vehiclepositions_positions rt_position
30
+ JOIN ropidgtfs_stops rs ON rs.stop_id = rt_position.last_stop_id
31
+ WHERE (rt_position.delay_stop_arrival IS NOT NULL OR rt_position.delay_stop_departure IS NOT NULL) AND (rt_position.state_position::text = ANY (ARRAY['on_track'::character varying::text, 'at_stop'::character varying::text, 'after_track'::character varying::text]))
32
+ WINDOW seq AS (PARTITION BY rt_position.trips_id, rt_position.last_stop_sequence, rt_position.state_position ORDER BY rt_position.id)) sub
33
+ WHERE sub.rn = 1
34
+ GROUP BY sub.trips_id, sub.last_stop_arrival_time, sub.last_stop_departure_time, sub.last_stop_sequence, sub.last_stop_id, sub.last_stop_name, sub.lat, sub.lng
35
+ ORDER BY sub.trips_id, sub.last_stop_sequence;
36
+
37
+ CREATE OR REPLACE PROCEDURE vehiclepositions_data_retention(inout numberOfRows int, in dataRetentionMinutes int)
38
+ LANGUAGE plpgsql
39
+ SET search_path FROM CURRENT
40
+ AS $procedure$
41
+ declare
42
+ idsForDelete varchar(255)[];
43
+ begin
44
+ select array_agg(t.id) from vehiclepositions_trips t
45
+ left join vehiclepositions_positions p on
46
+ p.id = t.last_position_id
47
+ where
48
+ p.valid_to < (NOW() - (dataRetentionMinutes || ' minutes')::interval)
49
+ or (t.gtfs_trip_id is null and p.valid_to is null and p.updated_at < NOW() - (dataRetentionMinutes || ' minutes')::interval) -- entries with valid to is null
50
+ into idsForDelete;
51
+
52
+ INSERT INTO vehiclepositions_trips_history
53
+ select * from vehiclepositions_trips where id = ANY(idsForDelete)
54
+ on conflict do nothing;
55
+
56
+ INSERT INTO vehiclepositions_positions_history
57
+ select * from vehiclepositions_positions where trips_id = ANY(idsForDelete)
58
+ on conflict do nothing;
59
+
60
+ insert into vehiclepositions_stop_times_history (
61
+ rt_trip_id,
62
+ gtfs_date,
63
+ gtfs_trip_id,
64
+ gtfs_direction_id,
65
+ gtfs_route_short_name,
66
+ gtfs_route_type,
67
+ run_number,
68
+ vehicle_registration_number,
69
+ gtfs_stop_id,
70
+ gtfs_stop_sequence,
71
+ current_stop_arrival,
72
+ current_stop_departure,
73
+ current_stop_arr_delay,
74
+ current_stop_dep_delay,
75
+ created_at,
76
+ updated_at,
77
+ origin_route_name,
78
+ stop_name,
79
+ lat,
80
+ lng
81
+ )
82
+ select
83
+ rt_trip.id,
84
+ rt_trip.gtfs_date,
85
+ rt_trip.gtfs_trip_id,
86
+ rt_trip.gtfs_direction_id,
87
+ rt_trip.gtfs_route_short_name,
88
+ rt_trip.gtfs_route_type,
89
+ rt_trip.run_number,
90
+ rt_trip.vehicle_registration_number,
91
+ stop_time.stop_id,
92
+ stop_time.stop_sequence,
93
+ stop_time.stop_arrival,
94
+ stop_time.stop_departure,
95
+ stop_time.stop_arr_delay,
96
+ stop_time.stop_dep_delay,
97
+ now(),
98
+ now(),
99
+ rt_trip.origin_route_name,
100
+ stop_time.stop_name,
101
+ stop_time.lat,
102
+ stop_time.lng
103
+ from
104
+ vehiclepositions_trips rt_trip
105
+ join
106
+ v_vehiclepositions_past_stop_times stop_time on stop_time.rt_trip_id = rt_trip.id
107
+ where
108
+ rt_trip.id = ANY(idsForDelete)
109
+ on conflict do nothing;
110
+
111
+
112
+ delete from vehiclepositions_positions where trips_id = ANY(idsForDelete);
113
+ delete from vehiclepositions_trips where id = ANY(idsForDelete);
114
+ delete from vehiclepositions_cis_stops where rt_trip_id = ANY(idsForDelete);
115
+
116
+ select array_length(idsForDelete,1) into numberOfRows;
117
+ end;
118
+ $procedure$;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@golemio/pid",
3
- "version": "2.13.6-dev.1310475463",
3
+ "version": "2.13.6-dev.1313101349",
4
4
  "description": "Golemio PID Module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",