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 THEN 1
WHEN (crt_d - LAG(crt_d, 1, NULL) OVER (PARTITION BY vhc_id ORDER BY crt_d)) > 3600 ELSE 0
THEN 1 ELSE 0 END AS isStop,
END AS isStop, t.*
t.* FROM ${histTableName} t
FROM ${histTableName} t WHERE t.latitude IS NOT NULL
WHERE AND t.longitude IS NOT NULL
t.latitude IS NOT NULL AND t.action = 'location'
AND t.longitude IS NOT NULL AND t.crt_d BETWEEN ? AND ?
AND t.action = 'location' ),
AND t.crt_d BETWEEN ? AND ? trips_cte AS (
) SELECT
, trips_cte AS ( CASE
SELECT WHEN ignition = 4 AND LAG(ignition, 1, 0) OVER (PARTITION BY vhc_id ORDER BY crt_d) <> 4
CASE OR LAG(isStop, 1, 0) OVER (PARTITION BY vhc_id ORDER BY crt_d) = 1 THEN 1
WHEN ignition = 4 ELSE 0
AND LAG(ignition, 1, 0) OVER (PARTITION BY vhc_id ORDER BY crt_d) <> 4 END AS trip_start,
or LAG(isStop, 1, 0) over (PARTITION BY vhc_id ORDER BY crt_d) = 1 g.*
THEN 1 ELSE 0 FROM gaps g
END AS trip_start, ),
g.* numbered AS (
FROM gaps g SELECT
) *,
, numbered AS ( SUM(trip_start) OVER (PARTITION BY vhc_id ORDER BY crt_d) AS trip_id
SELECT FROM trips_cte
*, WHERE ignition = 4 AND isStop = 0
SUM(trip_start) OVER (PARTITION BY vhc_id ORDER BY crt_d) AS trip_id ),
FROM trips_cte agg AS (
where SELECT
ignition = 4 a.vhc_id,
and isStop = 0 MIN(a.crt_d) AS start,
), MAX(a.crt_d) AS finish,
agg AS ( -- Mengambil lat/long pertama dan terakhir dalam group menggunakan FIRST_VALUE / LAST_VALUE
SELECT FIRST_VALUE(a.longitude) OVER (PARTITION BY a.vhc_id, a.trip_id ORDER BY a.crt_d ASC) AS start_longitude,
v.id, FIRST_VALUE(a.latitude) OVER (PARTITION BY a.vhc_id, a.trip_id ORDER BY a.crt_d ASC) AS start_latitude,
vhc_id, FIRST_VALUE(a.longitude) OVER (PARTITION BY a.vhc_id, a.trip_id ORDER BY a.crt_d DESC) AS finish_longitude,
MIN(a.crt_d) AS start, FIRST_VALUE(a.latitude) OVER (PARTITION BY a.vhc_id, a.trip_id ORDER BY a.crt_d DESC) AS finish_latitude
MAX(a.crt_d) AS finish, FROM numbered a
(SELECT longitude FROM ${histTableName} WHERE id = MIN(a.id) LIMIT 1) AS start_longitude, JOIN t_vehicles v ON v.id = a.vhc_id
(SELECT latitude FROM ${histTableName} WHERE id = MIN(a.id) LIMIT 1) AS start_latitude, WHERE v.dlt IS NULL AND a.trip_id != 0
(SELECT longitude FROM ${histTableName} WHERE id = MAX(a.id) LIMIT 1) AS finish_longitude, GROUP BY a.vhc_id, a.trip_id
(SELECT latitude FROM ${histTableName} WHERE id = MAX(a.id) LIMIT 1) AS finish_latitude, HAVING COUNT(*) > 1
COUNT(*) AS row_count )
FROM t_vehicles v SELECT DISTINCT
LEFT JOIN numbered a ON a.vhc_id = v.id tr.id AS tripsid,
WHERE agg.start_longitude,
v.dlt is null and trip_id != 0 agg.start_latitude,
GROUP BY v.id, a.trip_id agg.finish_longitude,
HAVING COUNT(*) > 1 agg.finish_latitude
) FROM agg
SELECT INNER JOIN trips tr
tr.id AS tripsid, ON tr.vhc_id = agg.vhc_id
agg.start_longitude, AND tr.start = agg.start
agg.start_latitude, AND tr.finish = agg.finish
agg.finish_longitude, ON DUPLICATE KEY UPDATE
agg.finish_latitude start_longitude = VALUES(start_longitude),
FROM agg agg start_latitude = VALUES(start_latitude),
join trips tr finish_longitude = VALUES(finish_longitude),
on tr.vhc_id = agg.vhc_id finish_latitude = VALUES(finish_latitude);
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) 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) {