This commit is contained in:
wayanrivan
2026-07-08 16:40:03 +07:00
parent 520615e5b2
commit 6d85a36736

View File

@ -180,191 +180,191 @@ async function tripGrouping() {
const startOfMonth = histMonth.clone().startOf("month").unix() - TIMEFIX const startOfMonth = histMonth.clone().startOf("month").unix() - TIMEFIX
const endOfMonth = histMonth.clone().endOf("month").unix() - TIMEFIX const endOfMonth = histMonth.clone().endOf("month").unix() - TIMEFIX
const q2 = ` // const q2 = `
insert into trips // insert into trips
(id,name,nopol1,vhc_id,mileage,start,finish,startMileage,finishMileage,startLoc,finishLoc,pool_code,dc_code,fuel_consume,row_count) // (id,name,nopol1,vhc_id,mileage,start,finish,startMileage,finishMileage,startLoc,finishLoc,pool_code,dc_code,fuel_consume,row_count)
WITH // WITH
sites AS ( // sites AS (
SELECT 'Bitterlaun' AS name, 'Hera' AS village, 'Cristo Rei' AS subdistrict, 'Dili' AS district, -8.548328 AS lat, 125.623808 AS lng // SELECT 'Bitterlaun' AS name, 'Hera' AS village, 'Cristo Rei' AS subdistrict, 'Dili' AS district, -8.548328 AS lat, 125.623808 AS lng
UNION ALL SELECT 'Bebonuk', 'Comoro', 'Dom Aleixo', 'Dili', -8.547527, 125.548280 // UNION ALL SELECT 'Bebonuk', 'Comoro', 'Dom Aleixo', 'Dili', -8.547527, 125.548280
UNION ALL SELECT 'Balide', 'Santa Cruz', 'Nain Feto', 'Dili', -8.564396, 125.582151 // UNION ALL SELECT 'Balide', 'Santa Cruz', 'Nain Feto', 'Dili', -8.564396, 125.582151
-- tambahkan site lain di sini dengan pola UNION ALL SELECT yang sama // -- tambahkan site lain di sini dengan pola UNION ALL SELECT yang sama
) // )
, 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 ELSE 0 // THEN 1 ELSE 0
END AS isStop, // END AS isStop,
( // (
SELECT CONCAT('Site ', s.name, ', ', s.village, ', ', s.subdistrict, ', ', s.district, ', Timor-Leste') // SELECT CONCAT('Site ', s.name, ', ', s.village, ', ', s.subdistrict, ', ', s.district, ', Timor-Leste')
FROM sites s // FROM sites s
WHERE ST_Distance_Sphere(POINT(t.longitude, t.latitude), POINT(s.lng, s.lat)) <= 500 // WHERE ST_Distance_Sphere(POINT(t.longitude, t.latitude), POINT(s.lng, s.lat)) <= 500
ORDER BY ST_Distance_Sphere(POINT(t.longitude, t.latitude), POINT(s.lng, s.lat)) ASC // ORDER BY ST_Distance_Sphere(POINT(t.longitude, t.latitude), POINT(s.lng, s.lat)) ASC
LIMIT 1 // LIMIT 1
) AS site_address, // ) AS site_address,
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 AS ( // , trips 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 // or LAG(isStop, 1, 0) over (PARTITION BY vhc_id ORDER BY crt_d) = 1
THEN 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 // FROM trips
where // where
ignition = 4 // ignition = 4
and isStop = 0 // and isStop = 0
), // ),
agg AS ( // agg AS (
SELECT // SELECT
v.id, // v.id,
v.name, // v.name,
v.nopol1, // v.nopol1,
vhc_id, // vhc_id,
ROW_NUMBER() OVER (PARTITION BY v.id ORDER BY MIN(a.crt_d)) AS trip_id, // ROW_NUMBER() OVER (PARTITION BY v.id ORDER BY MIN(a.crt_d)) AS trip_id,
max(a.vhc_milleage) - min(a.vhc_milleage) AS mileage, // max(a.vhc_milleage) - min(a.vhc_milleage) AS mileage,
MIN(a.crt_d) AS start, // MIN(a.crt_d) AS start,
MAX(a.crt_d) AS finish, // MAX(a.crt_d) AS finish,
MIN(a.vhc_milleage) AS startMileage, // MIN(a.vhc_milleage) AS startMileage,
MAX(a.vhc_milleage) AS finishMileage, // MAX(a.vhc_milleage) AS finishMileage,
( // (
SELECT COALESCE(n.site_address, (SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = n.id LIMIT 1)) // SELECT COALESCE(n.site_address, (SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = n.id LIMIT 1))
FROM numbered n // FROM numbered n
WHERE n.id = MIN(a.id) // WHERE n.id = MIN(a.id)
LIMIT 1 // LIMIT 1
) AS startLoc, // ) AS startLoc,
( // (
SELECT COALESCE(n.site_address, (SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = n.id LIMIT 1)) // SELECT COALESCE(n.site_address, (SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = n.id LIMIT 1))
FROM numbered n // FROM numbered n
WHERE n.id = MAX(a.id) // WHERE n.id = MAX(a.id)
LIMIT 1 // LIMIT 1
) AS finishLoc, // ) AS finishLoc,
COUNT(*) AS row_count, // COUNT(*) AS row_count,
max(fuel_count) - min(fuel_count) AS fuel_consume // max(fuel_count) - min(fuel_count) AS fuel_consume
FROM t_vehicles v // FROM t_vehicles v
LEFT JOIN numbered a ON a.vhc_id = v.id // LEFT JOIN numbered a ON a.vhc_id = v.id
WHERE // WHERE
v.dlt is null and trip_id != 0 // v.dlt is null and trip_id != 0
GROUP BY v.id, a.trip_id // GROUP BY v.id, a.trip_id
HAVING COUNT(*) > 1 // HAVING COUNT(*) > 1
) // )
SELECT // SELECT
agg.id,name,nopol1,vhc_id,mileage,start,finish,startMileage,finishMileage,startLoc,finishLoc, // agg.id,name,nopol1,vhc_id,mileage,start,finish,startMileage,finishMileage,startLoc,finishLoc,
tvd.pool_code, tvd.dc_code,fuel_consume, // tvd.pool_code, tvd.dc_code,fuel_consume,
row_count // row_count
FROM agg agg // FROM agg agg
join t_vehicles_detail tvd on tvd.vid = agg.id // join t_vehicles_detail tvd on tvd.vid = agg.id
ORDER BY agg.id, trip_id // ORDER BY agg.id, trip_id
ON DUPLICATE KEY UPDATE // ON DUPLICATE KEY UPDATE
mileage = values(mileage), // mileage = values(mileage),
start = values(start), // start = values(start),
finish = values(finish), // finish = values(finish),
startMileage = values(startMileage), // startMileage = values(startMileage),
finishMileage = values(finishMileage), // finishMileage = values(finishMileage),
startLoc = values(startLoc), // startLoc = values(startLoc),
finishLoc = values(finishLoc), // finishLoc = values(finishLoc),
row_count = values(row_count), // row_count = values(row_count),
fuel_consume = values(fuel_consume) // fuel_consume = values(fuel_consume)
` // `
// const q2 = ` const q2 = `
// insert into trips insert into trips
// (id,name,nopol1,vhc_id,mileage,start,finish,startMileage,finishMileage,startLoc,finishLoc,pool_code,dc_code,fuel_consume,row_count) (id,name,nopol1,vhc_id,mileage,start,finish,startMileage,finishMileage,startLoc,finishLoc,pool_code,dc_code,fuel_consume,row_count)
// WITH WITH
// gaps AS ( gaps AS (
// SELECT SELECT
// -- previous gap since previous row > 1 hour (3600s) -- previous gap since previous row > 1 hour (3600s)
// 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 ELSE 0 THEN 1 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 AS ( , trips AS (
// SELECT SELECT
// -- mark the start of a trip when ignition=4 and previous ignition <> 4 -- mark the start of a trip when ignition=4 and previous ignition <> 4
// 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 or LAG(isStop, 1, 0) over (PARTITION BY vhc_id ORDER BY crt_d) = 1
// THEN 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
// *, *,
// -- assign a trip_id by cumulative sum of trip_start -- assign a trip_id by cumulative sum of trip_start
// 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 FROM trips
// where where
// ignition = 4 ignition = 4
// and isStop = 0 and isStop = 0
// ), ),
// agg AS ( agg AS (
// SELECT SELECT
// v.id, v.id,
// v.name, v.name,
// v.nopol1, v.nopol1,
// vhc_id, vhc_id,
// ROW_NUMBER() OVER (PARTITION BY v.id ORDER BY MIN(a.crt_d)) AS trip_id, ROW_NUMBER() OVER (PARTITION BY v.id ORDER BY MIN(a.crt_d)) AS trip_id,
// max(a.vhc_milleage) - min(a.vhc_milleage) AS mileage, max(a.vhc_milleage) - min(a.vhc_milleage) AS mileage,
// MIN(a.crt_d) AS start, MIN(a.crt_d) AS start,
// MAX(a.crt_d) AS finish, MAX(a.crt_d) AS finish,
// MIN(a.vhc_milleage) AS startMileage, MIN(a.vhc_milleage) AS startMileage,
// MAX(a.vhc_milleage) AS finishMileage, MAX(a.vhc_milleage) AS finishMileage,
// (SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = MIN(a.id) LIMIT 1) AS startLoc, (SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = MIN(a.id) LIMIT 1) AS startLoc,
// (SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = MAX(a.id) LIMIT 1) AS finishLoc, (SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = MAX(a.id) LIMIT 1) AS finishLoc,
// COUNT(*) AS row_count, COUNT(*) AS row_count,
// max(fuel_count) - min(fuel_count) AS fuel_consume max(fuel_count) - min(fuel_count) AS fuel_consume
// FROM t_vehicles v FROM t_vehicles v
// LEFT JOIN numbered a ON a.vhc_id = v.id LEFT JOIN numbered a ON a.vhc_id = v.id
// WHERE WHERE
// v.dlt is null and trip_id != 0 v.dlt is null and trip_id != 0
// GROUP BY v.id, a.trip_id GROUP BY v.id, a.trip_id
// HAVING COUNT(*) > 1 HAVING COUNT(*) > 1
// ) )
// SELECT SELECT
// agg.id,name,nopol1,vhc_id,mileage,start,finish,startMileage,finishMileage,startLoc,finishLoc, agg.id,name,nopol1,vhc_id,mileage,start,finish,startMileage,finishMileage,startLoc,finishLoc,
// tvd.pool_code, tvd.dc_code,fuel_consume, tvd.pool_code, tvd.dc_code,fuel_consume,
// row_count row_count
// FROM agg agg FROM agg agg
// join t_vehicles_detail tvd on tvd.vid = agg.id join t_vehicles_detail tvd on tvd.vid = agg.id
// ORDER BY agg.id, trip_id ORDER BY agg.id, trip_id
// ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
// mileage = values(mileage), mileage = values(mileage),
// start = values(start), start = values(start),
// finish = values(finish), finish = values(finish),
// startMileage = values(startMileage), startMileage = values(startMileage),
// finishMileage = values(finishMileage), finishMileage = values(finishMileage),
// startLoc = values(startLoc), startLoc = values(startLoc),
// finishLoc = values(finishLoc), finishLoc = values(finishLoc),
// row_count = values(row_count), row_count = values(row_count),
// fuel_consume = values(fuel_consume) fuel_consume = values(fuel_consume)
// ` `
const d2 = [startOfMonth, endOfMonth] const d2 = [startOfMonth, endOfMonth]
const r2 = await db.query(q2, d2) const r2 = await db.query(q2, d2)
console.log(`Inserted ${r2.affectedRows} rows into 'trips' table from '${histTableName}'`) console.log(`Inserted ${r2.affectedRows} rows into 'trips' table from '${histTableName}'`)