This commit is contained in:
wayanrivan
2026-07-13 16:48:30 +07:00
parent dd8dbfa3ff
commit 9ab77e6db4

View File

@ -380,78 +380,71 @@ async function tripGrouping() {
// lalu join ke tabel `trips` (yang baru saja di-insert/update) untuk dapat tripsid. // lalu join ke tabel `trips` (yang baru saja di-insert/update) untuk dapat tripsid.
try { try {
const q3 = ` const q3 = `
insert into t_trips_latitude_longitude INSERT INTO t_trips_latitude_longitude
(tripsid, start_longitude, start_latitude, finish_longitude, finish_latitude) (tripsid, start_longitude, start_latitude, finish_longitude, finish_latitude)
WITH WITH gaps AS (
gaps AS (
SELECT SELECT
CASE CASE
WHEN (crt_d - LAG(crt_d, 1, NULL) OVER (PARTITION BY vhc_id ORDER BY crt_d)) > 3600 WHEN (crt_d - LAG(crt_d, 1, NULL) OVER (PARTITION BY vhc_id ORDER BY crt_d)) > 3600 THEN 1
THEN 1 ELSE 0 ELSE 0
END AS isStop, END AS isStop,
t.* t.*
FROM ${histTableName} t FROM ${histTableName} t
WHERE WHERE t.latitude IS NOT NULL
t.latitude IS NOT NULL
AND t.longitude IS NOT NULL AND t.longitude IS NOT NULL
AND t.action = 'location' AND t.action = 'location'
AND t.crt_d BETWEEN ? AND ? AND t.crt_d BETWEEN ? AND ?
) ),
, trips_cte AS ( trips_cte AS (
SELECT SELECT
CASE CASE
WHEN ignition = 4 WHEN ignition = 4 AND LAG(ignition, 1, 0) OVER (PARTITION BY vhc_id ORDER BY crt_d) <> 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
or LAG(isStop, 1, 0) over (PARTITION BY vhc_id ORDER BY crt_d) = 1 ELSE 0
THEN 1 ELSE 0
END AS trip_start, END AS trip_start,
g.* g.*
FROM gaps g FROM gaps g
) ),
, numbered AS ( numbered AS (
SELECT SELECT
*, *,
SUM(trip_start) OVER (PARTITION BY vhc_id ORDER BY crt_d) AS trip_id SUM(trip_start) OVER (PARTITION BY vhc_id ORDER BY crt_d) AS trip_id
FROM trips_cte FROM trips_cte
where WHERE ignition = 4 AND isStop = 0
ignition = 4
and isStop = 0
), ),
agg AS ( agg AS (
SELECT SELECT
v.id, a.vhc_id,
vhc_id,
MIN(a.crt_d) AS start, MIN(a.crt_d) AS start,
MAX(a.crt_d) AS finish, MAX(a.crt_d) AS finish,
(SELECT longitude FROM ${histTableName} WHERE id = MIN(a.id) LIMIT 1) AS start_longitude, -- Mengambil lat/long pertama dan terakhir dalam group menggunakan FIRST_VALUE / LAST_VALUE
(SELECT latitude FROM ${histTableName} WHERE id = MIN(a.id) LIMIT 1) AS start_latitude, FIRST_VALUE(a.longitude) OVER (PARTITION BY a.vhc_id, a.trip_id ORDER BY a.crt_d ASC) AS start_longitude,
(SELECT longitude FROM ${histTableName} WHERE id = MAX(a.id) LIMIT 1) AS finish_longitude, FIRST_VALUE(a.latitude) OVER (PARTITION BY a.vhc_id, a.trip_id ORDER BY a.crt_d ASC) AS start_latitude,
(SELECT latitude FROM ${histTableName} WHERE id = MAX(a.id) LIMIT 1) AS finish_latitude, FIRST_VALUE(a.longitude) OVER (PARTITION BY a.vhc_id, a.trip_id ORDER BY a.crt_d DESC) AS finish_longitude,
COUNT(*) AS row_count FIRST_VALUE(a.latitude) OVER (PARTITION BY a.vhc_id, a.trip_id ORDER BY a.crt_d DESC) AS finish_latitude
FROM t_vehicles v FROM numbered a
LEFT JOIN numbered a ON a.vhc_id = v.id JOIN t_vehicles v ON v.id = a.vhc_id
WHERE WHERE v.dlt IS NULL AND a.trip_id != 0
v.dlt is null and trip_id != 0 GROUP BY a.vhc_id, a.trip_id
GROUP BY v.id, a.trip_id
HAVING COUNT(*) > 1 HAVING COUNT(*) > 1
) )
SELECT SELECT DISTINCT
tr.id AS tripsid, tr.id AS tripsid,
agg.start_longitude, agg.start_longitude,
agg.start_latitude, agg.start_latitude,
agg.finish_longitude, agg.finish_longitude,
agg.finish_latitude agg.finish_latitude
FROM agg agg FROM agg
join trips tr INNER JOIN trips tr
on tr.vhc_id = agg.vhc_id ON tr.vhc_id = agg.vhc_id
and tr.start = agg.start AND tr.start = agg.start
and tr.finish = agg.finish AND tr.finish = agg.finish
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
start_longitude = values(start_longitude), start_longitude = VALUES(start_longitude),
start_latitude = values(start_latitude), start_latitude = VALUES(start_latitude),
finish_longitude = values(finish_longitude), finish_longitude = VALUES(finish_longitude),
finish_latitude = values(finish_latitude) finish_latitude = VALUES(finish_latitude);
` `;
const r3 = await db.query(q3, d2) const r3 = await db.query(q3, d2)
console.log(`Inserted ${r3.affectedRows} rows into 't_trips_latitude_longitude' table from '${histTableName}'`) console.log(`Inserted ${r3.affectedRows} rows into 't_trips_latitude_longitude' table from '${histTableName}'`)
} catch (err3) { } catch (err3) {