updae
This commit is contained in:
@ -180,191 +180,190 @@ 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 = `
|
|
||||||
// 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
|
|
||||||
// -- tambahkan site lain di sini dengan pola UNION ALL SELECT yang sama
|
|
||||||
// )
|
|
||||||
// , 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
|
||||||
(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 (
|
sites AS (
|
||||||
SELECT
|
SELECT 'Bitterlaun' AS name, 'Hera' AS village, 'Cristo Rei' AS subdistrict, 'Dili' AS district, -8.548328 AS lat, 125.623808 AS lng
|
||||||
-- previous gap since previous row > 1 hour (3600s)
|
UNION ALL SELECT 'Bebonuk', 'Comoro', 'Dom Aleixo', 'Dili', -8.547527, 125.548280
|
||||||
CASE
|
UNION ALL SELECT 'Balide', 'Santa Cruz', 'Nain Feto', 'Dili', -8.564396, 125.582151
|
||||||
WHEN (crt_d - LAG(crt_d, 1, NULL) OVER (PARTITION BY vhc_id ORDER BY crt_d)) > 3600
|
)
|
||||||
THEN 1 ELSE 0
|
, gaps AS (
|
||||||
END AS isStop,
|
SELECT
|
||||||
t.*
|
CASE
|
||||||
FROM ${histTableName} t
|
WHEN (crt_d - LAG(crt_d, 1, NULL) OVER (PARTITION BY vhc_id ORDER BY crt_d)) > 3600
|
||||||
WHERE
|
THEN 1 ELSE 0
|
||||||
t.latitude IS NOT NULL
|
END AS isStop,
|
||||||
AND t.longitude IS NOT NULL
|
(
|
||||||
AND t.action = 'location'
|
SELECT CONCAT('Site ', s.name, ', ', s.village, ', ', s.subdistrict, ', ', s.district, ', Timor-Leste')
|
||||||
AND t.crt_d BETWEEN ? AND ?
|
FROM sites s
|
||||||
)
|
WHERE ST_Distance_Sphere(POINT(t.longitude, t.latitude), POINT(s.lng, s.lat)) <= 500
|
||||||
, trips AS (
|
ORDER BY ST_Distance_Sphere(POINT(t.longitude, t.latitude), POINT(s.lng, s.lat)) ASC
|
||||||
SELECT
|
LIMIT 1
|
||||||
-- mark the start of a trip when ignition=4 and previous ignition <> 4
|
) AS site_address,
|
||||||
CASE
|
t.*
|
||||||
WHEN ignition = 4
|
FROM ${histTableName} t
|
||||||
AND LAG(ignition, 1, 0) OVER (PARTITION BY vhc_id ORDER BY crt_d) <> 4
|
WHERE
|
||||||
or LAG(isStop, 1, 0) over (PARTITION BY vhc_id ORDER BY crt_d) = 1
|
t.latitude IS NOT NULL
|
||||||
THEN 1 ELSE 0
|
AND t.longitude IS NOT NULL
|
||||||
END AS trip_start,
|
AND t.action = 'location'
|
||||||
g.*
|
AND t.crt_d BETWEEN ? AND ?
|
||||||
FROM gaps g
|
)
|
||||||
)
|
, trips AS (
|
||||||
, numbered AS (
|
SELECT
|
||||||
SELECT
|
CASE
|
||||||
*,
|
WHEN ignition = 4
|
||||||
-- assign a trip_id by cumulative sum of trip_start
|
AND LAG(ignition, 1, 0) OVER (PARTITION BY vhc_id ORDER BY crt_d) <> 4
|
||||||
SUM(trip_start) OVER (PARTITION BY vhc_id ORDER BY crt_d) AS trip_id
|
or LAG(isStop, 1, 0) over (PARTITION BY vhc_id ORDER BY crt_d) = 1
|
||||||
FROM trips
|
THEN 1 ELSE 0
|
||||||
where
|
END AS trip_start,
|
||||||
ignition = 4
|
g.*
|
||||||
and isStop = 0
|
FROM gaps g
|
||||||
),
|
)
|
||||||
agg AS (
|
, numbered AS (
|
||||||
SELECT
|
SELECT
|
||||||
v.id,
|
*,
|
||||||
v.name,
|
SUM(trip_start) OVER (PARTITION BY vhc_id ORDER BY crt_d) AS trip_id
|
||||||
v.nopol1,
|
FROM trips
|
||||||
vhc_id,
|
where
|
||||||
ROW_NUMBER() OVER (PARTITION BY v.id ORDER BY MIN(a.crt_d)) AS trip_id,
|
ignition = 4
|
||||||
max(a.vhc_milleage) - min(a.vhc_milleage) AS mileage,
|
and isStop = 0
|
||||||
MIN(a.crt_d) AS start,
|
),
|
||||||
MAX(a.crt_d) AS finish,
|
agg AS (
|
||||||
MIN(a.vhc_milleage) AS startMileage,
|
SELECT
|
||||||
MAX(a.vhc_milleage) AS finishMileage,
|
v.id,
|
||||||
(SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = MIN(a.id) LIMIT 1) AS startLoc,
|
v.name,
|
||||||
(SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = MAX(a.id) LIMIT 1) AS finishLoc,
|
v.nopol1,
|
||||||
COUNT(*) AS row_count,
|
vhc_id,
|
||||||
max(fuel_count) - min(fuel_count) AS fuel_consume
|
ROW_NUMBER() OVER (PARTITION BY v.id ORDER BY MIN(a.crt_d)) AS trip_id,
|
||||||
FROM t_vehicles v
|
max(a.vhc_milleage) - min(a.vhc_milleage) AS mileage,
|
||||||
LEFT JOIN numbered a ON a.vhc_id = v.id
|
MIN(a.crt_d) AS start,
|
||||||
WHERE
|
MAX(a.crt_d) AS finish,
|
||||||
v.dlt is null and trip_id != 0
|
MIN(a.vhc_milleage) AS startMileage,
|
||||||
GROUP BY v.id, a.trip_id
|
MAX(a.vhc_milleage) AS finishMileage,
|
||||||
HAVING COUNT(*) > 1
|
(
|
||||||
)
|
SELECT COALESCE(n.site_address, (SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = n.id LIMIT 1))
|
||||||
SELECT
|
FROM numbered n
|
||||||
agg.id,name,nopol1,vhc_id,mileage,start,finish,startMileage,finishMileage,startLoc,finishLoc,
|
WHERE n.id = MIN(a.id)
|
||||||
tvd.pool_code, tvd.dc_code,fuel_consume,
|
LIMIT 1
|
||||||
row_count
|
) AS startLoc,
|
||||||
FROM agg agg
|
(
|
||||||
join t_vehicles_detail tvd on tvd.vid = agg.id
|
SELECT COALESCE(n.site_address, (SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = n.id LIMIT 1))
|
||||||
ORDER BY agg.id, trip_id
|
FROM numbered n
|
||||||
ON DUPLICATE KEY UPDATE
|
WHERE n.id = MAX(a.id)
|
||||||
mileage = values(mileage),
|
LIMIT 1
|
||||||
start = values(start),
|
) AS finishLoc,
|
||||||
finish = values(finish),
|
COUNT(*) AS row_count,
|
||||||
startMileage = values(startMileage),
|
max(fuel_count) - min(fuel_count) AS fuel_consume
|
||||||
finishMileage = values(finishMileage),
|
FROM t_vehicles v
|
||||||
startLoc = values(startLoc),
|
LEFT JOIN numbered a ON a.vhc_id = v.id
|
||||||
finishLoc = values(finishLoc),
|
WHERE
|
||||||
row_count = values(row_count),
|
v.dlt is null and trip_id != 0
|
||||||
fuel_consume = values(fuel_consume)
|
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 = `
|
||||||
|
// 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}'`)
|
||||||
|
|||||||
9955
package-lock.json
generated
9955
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user