This commit is contained in:
wayanrivan
2026-07-13 10:54:43 +07:00
parent 4f16b1c6a7
commit a0cd3a7d8b

View File

@ -165,122 +165,20 @@ async function job() {
console.log("Monthly table job completed.") console.log("Monthly table 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++) {
const lastMonth = moment().subtract(1, "months") // const lastMonth = moment().subtract(1, "months")
const histMonth = lastMonth.clone().add(i, "months") // const histMonth = lastMonth.clone().add(i, "months")
const histYy = histMonth.format("YY") // const histYy = histMonth.format("YY")
const histMm = histMonth.format("MM") // const histMm = histMonth.format("MM")
const histTableName = `tracks_${histYy}${histMm}` // const histTableName = `tracks_${histYy}${histMm}`
console.log(`Processing history insertion for table '${histTableName}'...`) // console.log(`Processing history insertion for table '${histTableName}'...`)
try { // try {
console.time(`Trip grouping for ${histTableName}`) // console.time(`Trip grouping for ${histTableName}`)
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 = `
insert into trips
(id,name,nopol1,vhc_id,mileage,start,finish,startMileage,finishMileage,startLoc,finishLoc,pool_code,dc_code,fuel_consume,row_count)
WITH
sites AS (
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 'Balide', 'Santa Cruz', 'Nain Feto', 'Dili', -8.564396, 125.582151
)
, 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,
(
SELECT CONCAT('Site ', s.name, ', ', s.village, ', ', s.subdistrict, ', ', s.district, ', Timor-Leste')
FROM sites s
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
LIMIT 1
) AS site_address,
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
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
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 COALESCE(n.site_address, (SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = n.id LIMIT 1))
FROM numbered n
WHERE n.id = MIN(a.id)
LIMIT 1
) AS startLoc,
(
SELECT COALESCE(n.site_address, (SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = n.id LIMIT 1))
FROM numbered n
WHERE n.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 q2 = ` // const q2 = `
// insert into trips // insert into trips
@ -364,9 +262,203 @@ async function tripGrouping() {
// 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 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() {
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 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
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)
`
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)