updae
This commit is contained in:
@ -180,120 +180,28 @@ 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 '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
|
SELECT
|
||||||
-- 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,
|
||||||
|
(
|
||||||
|
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.*
|
t.*
|
||||||
FROM ${histTableName} t
|
FROM ${histTableName} t
|
||||||
WHERE
|
WHERE
|
||||||
@ -304,7 +212,6 @@ async function tripGrouping() {
|
|||||||
)
|
)
|
||||||
, trips AS (
|
, trips AS (
|
||||||
SELECT
|
SELECT
|
||||||
-- 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
|
||||||
@ -317,7 +224,6 @@ async function tripGrouping() {
|
|||||||
, numbered AS (
|
, numbered AS (
|
||||||
SELECT
|
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
|
SUM(trip_start) OVER (PARTITION BY vhc_id ORDER BY crt_d) AS trip_id
|
||||||
FROM trips
|
FROM trips
|
||||||
where
|
where
|
||||||
@ -336,8 +242,18 @@ async function tripGrouping() {
|
|||||||
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 = MAX(a.id) LIMIT 1) AS finishLoc,
|
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,
|
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
|
||||||
@ -364,7 +280,90 @@ 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)
|
||||||
`
|
`
|
||||||
|
|
||||||
|
// 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}'`)
|
||||||
|
|||||||
5157
package-lock.json
generated
5157
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user