@tmlmobilidade/databases 20260528.1652.52 → 20260528.1745.17

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 the latest event has no bearing, the immediately previous event's bearing is used if that
56
+ * event is at most 5 minutes older; otherwise bearing stays null.
57
57
  */
58
58
  getPositions(secondsAgo?: number): Promise<SimplifiedVehicleEvent[]>;
59
59
  /**
@@ -62,13 +62,29 @@ 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 the latest event has no bearing, the immediately previous event's bearing is used if that
66
+ * event is at most 5 minutes older; otherwise bearing stays null.
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
+ if(
77
+ bearing IS NULL
78
+ AND lagInFrame(created_at) OVER w IS NOT NULL
79
+ AND (created_at - lagInFrame(created_at) OVER w) <= ${bearingInferenceMaxGapMs},
80
+ lagInFrame(bearing) OVER w,
81
+ bearing
82
+ ) AS bearing
83
+ )
84
+ FROM "${this.databaseName}"."${this.tableName}"
85
+ WHERE created_at > toUnixTimestamp64Milli(now64(3) - INTERVAL ${secondsAgo + bearingInferenceLookbackSeconds} SECOND)
86
+ WINDOW w AS (PARTITION BY vehicle_id, agency_id ORDER BY created_at)
87
+ )
72
88
  WHERE created_at > toUnixTimestamp64Milli(now64(3) - INTERVAL ${secondsAgo} SECOND)
73
89
  ORDER BY created_at DESC
74
90
  LIMIT 1 BY vehicle_id, agency_id
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/databases",
3
- "version": "20260528.1652.52",
3
+ "version": "20260528.1745.17",
4
4
  "author": {
5
5
  "email": "iso@tmlmobilidade.pt",
6
6
  "name": "TML-ISO"