@tmlmobilidade/databases 20260528.1745.17 → 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
- * 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.
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,8 +62,8 @@ 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
- * 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.
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
69
  const bearingInferenceLookbackSeconds = 300;
@@ -73,21 +73,35 @@ class SimplifiedVehicleEventsNewClass extends ClickHouseInterfaceTemplate {
73
73
  FROM (
74
74
  SELECT
75
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
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
+ )
82
96
  ) AS bearing
83
97
  )
84
98
  FROM "${this.databaseName}"."${this.tableName}"
85
99
  WHERE created_at > toUnixTimestamp64Milli(now64(3) - INTERVAL ${secondsAgo + bearingInferenceLookbackSeconds} SECOND)
86
- WINDOW w AS (PARTITION BY vehicle_id, agency_id ORDER BY created_at)
100
+ WINDOW w AS (PARTITION BY agency_id, vehicle_id ORDER BY created_at)
87
101
  )
88
102
  WHERE created_at > toUnixTimestamp64Milli(now64(3) - INTERVAL ${secondsAgo} SECOND)
89
103
  ORDER BY created_at DESC
90
- LIMIT 1 BY vehicle_id, agency_id
104
+ LIMIT 1 BY agency_id, vehicle_id
91
105
  `;
92
106
  const result = await this.queryFromString(query);
93
107
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/databases",
3
- "version": "20260528.1745.17",
3
+ "version": "20260528.1805.36",
4
4
  "author": {
5
5
  "email": "iso@tmlmobilidade.pt",
6
6
  "name": "TML-ISO"