@tmlmobilidade/databases 20260528.1652.52 → 20260528.1805.36

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.
@@ -52,8 +52,8 @@ declare class SimplifiedVehicleEventsNewClass extends ClickHouseInterfaceTemplat
52
52
  * @param secondsAgo - The number of seconds in the past to consider for retrieving recent positions. Defaults to 90 seconds.
53
53
  * @returns A promise resolving to an array of SimplifiedVehicleEvent objects, each representing the latest event for a vehicle within the specified period.
54
54
  *
55
- * The method filters out events where any of vehicle_id, agency_id, or trip_id are empty or null,
56
- * and also ensures latitude and longitude are not zero. Only the most recent event per vehicle is returned.
55
+ * When bearing is missing, it is computed from the previous position of the same agency_id/vehicle_id
56
+ * pair if that event is at most 5 minutes older and the coordinates changed.
57
57
  */
58
58
  getPositions(secondsAgo?: number): Promise<SimplifiedVehicleEvent[]>;
59
59
  /**
@@ -62,16 +62,46 @@ class SimplifiedVehicleEventsNewClass extends ClickHouseInterfaceTemplate {
62
62
  * @param secondsAgo - The number of seconds in the past to consider for retrieving recent positions. Defaults to 90 seconds.
63
63
  * @returns A promise resolving to an array of SimplifiedVehicleEvent objects, each representing the latest event for a vehicle within the specified period.
64
64
  *
65
- * The method filters out events where any of vehicle_id, agency_id, or trip_id are empty or null,
66
- * and also ensures latitude and longitude are not zero. Only the most recent event per vehicle is returned.
65
+ * When bearing is missing, it is computed from the previous position of the same agency_id/vehicle_id
66
+ * pair if that event is at most 5 minutes older and the coordinates changed.
67
67
  */
68
68
  async getPositions(secondsAgo = 90) {
69
+ const bearingInferenceLookbackSeconds = 300;
70
+ const bearingInferenceMaxGapMs = bearingInferenceLookbackSeconds * 1000;
69
71
  const query = `
70
72
  SELECT *
71
- FROM "${this.databaseName}"."${this.tableName}"
73
+ FROM (
74
+ SELECT
75
+ * REPLACE(
76
+ coalesce(
77
+ bearing,
78
+ if(
79
+ lagInFrame(created_at) OVER w IS NOT NULL
80
+ AND (created_at - lagInFrame(created_at) OVER w) <= ${bearingInferenceMaxGapMs}
81
+ AND (
82
+ abs(latitude - lagInFrame(latitude) OVER w) > 0.000001
83
+ OR abs(longitude - lagInFrame(longitude) OVER w) > 0.000001
84
+ ),
85
+ toInt64(round(mod(
86
+ 360 + degrees(atan2(
87
+ sin(radians(longitude - lagInFrame(longitude) OVER w)) * cos(radians(latitude)),
88
+ cos(radians(lagInFrame(latitude) OVER w)) * sin(radians(latitude))
89
+ - sin(radians(lagInFrame(latitude) OVER w)) * cos(radians(latitude))
90
+ * cos(radians(longitude - lagInFrame(longitude) OVER w))
91
+ )),
92
+ 360
93
+ ))),
94
+ NULL
95
+ )
96
+ ) AS bearing
97
+ )
98
+ FROM "${this.databaseName}"."${this.tableName}"
99
+ WHERE created_at > toUnixTimestamp64Milli(now64(3) - INTERVAL ${secondsAgo + bearingInferenceLookbackSeconds} SECOND)
100
+ WINDOW w AS (PARTITION BY agency_id, vehicle_id ORDER BY created_at)
101
+ )
72
102
  WHERE created_at > toUnixTimestamp64Milli(now64(3) - INTERVAL ${secondsAgo} SECOND)
73
103
  ORDER BY created_at DESC
74
- LIMIT 1 BY vehicle_id, agency_id
104
+ LIMIT 1 BY agency_id, vehicle_id
75
105
  `;
76
106
  const result = await this.queryFromString(query);
77
107
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/databases",
3
- "version": "20260528.1652.52",
3
+ "version": "20260528.1805.36",
4
4
  "author": {
5
5
  "email": "iso@tmlmobilidade.pt",
6
6
  "name": "TML-ISO"