From 9ab77e6db4e736655276433f9ae0eefc72c06943 Mon Sep 17 00:00:00 2001 From: wayanrivan Date: Mon, 13 Jul 2026 16:48:30 +0700 Subject: [PATCH] update --- cron/TripsWorker.js | 137 +++++++++++++++++++++----------------------- 1 file changed, 65 insertions(+), 72 deletions(-) diff --git a/cron/TripsWorker.js b/cron/TripsWorker.js index 9286f1d..48ff457 100644 --- a/cron/TripsWorker.js +++ b/cron/TripsWorker.js @@ -380,78 +380,71 @@ async function tripGrouping() { // lalu join ke tabel `trips` (yang baru saja di-insert/update) untuk dapat tripsid. try { const q3 = ` - insert into t_trips_latitude_longitude - (tripsid, start_longitude, start_latitude, finish_longitude, finish_latitude) - WITH - gaps AS ( - SELECT - CASE - WHEN (crt_d - LAG(crt_d, 1, NULL) OVER (PARTITION BY vhc_id ORDER BY crt_d)) > 3600 - THEN 1 ELSE 0 - END AS isStop, - t.* - FROM ${histTableName} t - WHERE - t.latitude IS NOT NULL - AND t.longitude IS NOT NULL - AND t.action = 'location' - AND t.crt_d BETWEEN ? AND ? - ) - , trips_cte AS ( - SELECT - CASE - WHEN ignition = 4 - AND LAG(ignition, 1, 0) OVER (PARTITION BY vhc_id ORDER BY crt_d) <> 4 - or LAG(isStop, 1, 0) over (PARTITION BY vhc_id ORDER BY crt_d) = 1 - THEN 1 ELSE 0 - END AS trip_start, - g.* - FROM gaps g - ) - , numbered AS ( - SELECT - *, - SUM(trip_start) OVER (PARTITION BY vhc_id ORDER BY crt_d) AS trip_id - FROM trips_cte - where - ignition = 4 - and isStop = 0 - ), - agg AS ( - SELECT - v.id, - vhc_id, - MIN(a.crt_d) AS start, - MAX(a.crt_d) AS finish, - (SELECT longitude FROM ${histTableName} WHERE id = MIN(a.id) LIMIT 1) AS start_longitude, - (SELECT latitude FROM ${histTableName} WHERE id = MIN(a.id) LIMIT 1) AS start_latitude, - (SELECT longitude FROM ${histTableName} WHERE id = MAX(a.id) LIMIT 1) AS finish_longitude, - (SELECT latitude FROM ${histTableName} WHERE id = MAX(a.id) LIMIT 1) AS finish_latitude, - COUNT(*) AS row_count - FROM t_vehicles v - LEFT JOIN numbered a ON a.vhc_id = v.id - WHERE - v.dlt is null and trip_id != 0 - GROUP BY v.id, a.trip_id - HAVING COUNT(*) > 1 - ) - SELECT - tr.id AS tripsid, - agg.start_longitude, - agg.start_latitude, - agg.finish_longitude, - agg.finish_latitude - FROM agg agg - join trips tr - on tr.vhc_id = agg.vhc_id - and tr.start = agg.start - and tr.finish = agg.finish - ON DUPLICATE KEY UPDATE - start_longitude = values(start_longitude), - start_latitude = values(start_latitude), - finish_longitude = values(finish_longitude), - finish_latitude = values(finish_latitude) - ` + INSERT INTO t_trips_latitude_longitude + (tripsid, start_longitude, start_latitude, finish_longitude, finish_latitude) + WITH gaps AS ( + SELECT + CASE + WHEN (crt_d - LAG(crt_d, 1, NULL) OVER (PARTITION BY vhc_id ORDER BY crt_d)) > 3600 THEN 1 + ELSE 0 + END AS isStop, + t.* + FROM ${histTableName} t + WHERE t.latitude IS NOT NULL + AND t.longitude IS NOT NULL + AND t.action = 'location' + AND t.crt_d BETWEEN ? AND ? + ), + trips_cte AS ( + SELECT + CASE + WHEN ignition = 4 AND LAG(ignition, 1, 0) OVER (PARTITION BY vhc_id ORDER BY crt_d) <> 4 + OR LAG(isStop, 1, 0) OVER (PARTITION BY vhc_id ORDER BY crt_d) = 1 THEN 1 + ELSE 0 + END AS trip_start, + g.* + FROM gaps g + ), + numbered AS ( + SELECT + *, + SUM(trip_start) OVER (PARTITION BY vhc_id ORDER BY crt_d) AS trip_id + FROM trips_cte + WHERE ignition = 4 AND isStop = 0 + ), + agg AS ( + SELECT + a.vhc_id, + MIN(a.crt_d) AS start, + MAX(a.crt_d) AS finish, + -- Mengambil lat/long pertama dan terakhir dalam group menggunakan FIRST_VALUE / LAST_VALUE + FIRST_VALUE(a.longitude) OVER (PARTITION BY a.vhc_id, a.trip_id ORDER BY a.crt_d ASC) AS start_longitude, + FIRST_VALUE(a.latitude) OVER (PARTITION BY a.vhc_id, a.trip_id ORDER BY a.crt_d ASC) AS start_latitude, + FIRST_VALUE(a.longitude) OVER (PARTITION BY a.vhc_id, a.trip_id ORDER BY a.crt_d DESC) AS finish_longitude, + FIRST_VALUE(a.latitude) OVER (PARTITION BY a.vhc_id, a.trip_id ORDER BY a.crt_d DESC) AS finish_latitude + FROM numbered a + JOIN t_vehicles v ON v.id = a.vhc_id + WHERE v.dlt IS NULL AND a.trip_id != 0 + GROUP BY a.vhc_id, a.trip_id + HAVING COUNT(*) > 1 + ) + SELECT DISTINCT + tr.id AS tripsid, + agg.start_longitude, + agg.start_latitude, + agg.finish_longitude, + agg.finish_latitude + FROM agg + INNER JOIN trips tr + ON tr.vhc_id = agg.vhc_id + AND tr.start = agg.start + AND tr.finish = agg.finish + ON DUPLICATE KEY UPDATE + start_longitude = VALUES(start_longitude), + start_latitude = VALUES(start_latitude), + finish_longitude = VALUES(finish_longitude), + finish_latitude = VALUES(finish_latitude); +`; const r3 = await db.query(q3, d2) console.log(`Inserted ${r3.affectedRows} rows into 't_trips_latitude_longitude' table from '${histTableName}'`) } catch (err3) {