This commit is contained in:
wayanrivan
2026-07-14 10:51:34 +07:00
parent 9ab77e6db4
commit b95d0b2bc5

View File

@ -165,114 +165,6 @@ async function job() {
console.log("Monthly table job completed.") console.log("Monthly table job completed.")
} }
// async function tripGrouping() {
// console.log("Trip grouping job executed:", moment().format("YYYY-MM-DD HH:mm:ss"))
// for (let i = 0; i <= 1; i++) {
// const lastMonth = moment().subtract(1, "months")
// const histMonth = lastMonth.clone().add(i, "months")
// const histYy = histMonth.format("YY")
// const histMm = histMonth.format("MM")
// const histTableName = `tracks_${histYy}${histMm}`
// console.log(`Processing history insertion for table '${histTableName}'...`)
// try {
// console.time(`Trip grouping for ${histTableName}`)
// const startOfMonth = histMonth.clone().startOf("month").unix() - TIMEFIX
// const endOfMonth = histMonth.clone().endOf("month").unix() - TIMEFIX
// const q2 = `
// insert into trips
// (id,name,nopol1,vhc_id,mileage,start,finish,startMileage,finishMileage,startLoc,finishLoc,pool_code,dc_code,fuel_consume,row_count)
// WITH
// gaps AS (
// SELECT
// -- previous gap since previous row > 1 hour (3600s)
// 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 AS (
// SELECT
// -- mark the start of a trip when ignition=4 and previous ignition <> 4
// 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
// *,
// -- 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
// FROM trips
// where
// ignition = 4
// and isStop = 0
// ),
// agg AS (
// SELECT
// v.id,
// v.name,
// v.nopol1,
// vhc_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,
// MIN(a.crt_d) AS start,
// MAX(a.crt_d) AS finish,
// MIN(a.vhc_milleage) AS startMileage,
// 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 = MAX(a.id) LIMIT 1) AS finishLoc,
// COUNT(*) AS row_count,
// max(fuel_count) - min(fuel_count) AS fuel_consume
// 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
// agg.id,name,nopol1,vhc_id,mileage,start,finish,startMileage,finishMileage,startLoc,finishLoc,
// tvd.pool_code, tvd.dc_code,fuel_consume,
// row_count
// FROM agg agg
// join t_vehicles_detail tvd on tvd.vid = agg.id
// ORDER BY agg.id, trip_id
// ON DUPLICATE KEY UPDATE
// mileage = values(mileage),
// start = values(start),
// finish = values(finish),
// startMileage = values(startMileage),
// finishMileage = values(finishMileage),
// startLoc = values(startLoc),
// finishLoc = values(finishLoc),
// row_count = values(row_count),
// fuel_consume = values(fuel_consume)
// `
// const d2 = [startOfMonth, endOfMonth]
// const r2 = await db.query(q2, d2)
// console.log(`Inserted ${r2.affectedRows} rows into 'trips' table from '${histTableName}'`)
// console.timeEnd(`Trip grouping for ${histTableName}`)
// } catch (error) {
// console.error(`Error inserting data into history table '${histTableName}':`, error.message)
// }
// }
// console.log("Trip grouping job completed.")
// }
async function tripGrouping() { async function tripGrouping() {
console.log("Trip grouping job executed:", moment().format("YYYY-MM-DD HH:mm:ss")) console.log("Trip grouping job executed:", moment().format("YYYY-MM-DD HH:mm:ss"))
for (let i = 0; i <= 1; i++) { for (let i = 0; i <= 1; i++) {
@ -343,6 +235,8 @@ async function tripGrouping() {
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,
(SELECT CONCAT (latitude,', ',longitude) FROM ${histTableName} WHERE id = MIN(a.id) LIMIT 1) AS start_lat_long,
(SELECT CONCAT (latitude,', ',longitude) FROM ${histTableName} WHERE ID = MAX(a.id) LIMIT 1) AS finish_lat_long,
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
@ -355,7 +249,7 @@ async function tripGrouping() {
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,start_lat_long,finish_lat_long
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
@ -369,89 +263,12 @@ async function tripGrouping() {
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)
start_lat_long = values(start_lat_long),
finish_lat_long = values(finish_lat_long)
` `
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}'`)
// === TAMBAHAN: insert lat/long ke t_trips_latitude_longitude ===
// Query terpisah, tidak mengubah q2 di atas.
// Mengambil ulang start/finish per trip beserta koordinatnya,
// 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
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) {
console.error(`Error inserting lat/long for '${histTableName}':`, err3.message)
}
// === END TAMBAHAN ===
console.timeEnd(`Trip grouping for ${histTableName}`) console.timeEnd(`Trip grouping for ${histTableName}`)
} catch (error) { } catch (error) {
console.error(`Error inserting data into history table '${histTableName}':`, error.message) console.error(`Error inserting data into history table '${histTableName}':`, error.message)