fleetmap-reports 1.0.425 → 1.0.428
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/idle-report.js +25 -10
- package/src/index.test.js +15 -0
- package/src/speeding-report.js +3 -3
- package/src/trip-report.js +23 -10
- package/src/util/driver.js +40 -14
package/package.json
CHANGED
package/src/idle-report.js
CHANGED
|
@@ -7,6 +7,7 @@ const { devicesToProcess } = require('./util/device')
|
|
|
7
7
|
const automaticReports = require('./automaticReports')
|
|
8
8
|
const traccarHelper = require('./util/traccar')
|
|
9
9
|
const { getIdleEvents } = require('./util/route')
|
|
10
|
+
const { reportByDriver } = require('./util/driver')
|
|
10
11
|
|
|
11
12
|
const fileName = 'IdleReport'
|
|
12
13
|
|
|
@@ -31,22 +32,36 @@ async function createIdleReport (from, to, userData, traccarInstance) {
|
|
|
31
32
|
return reportData
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
async function createIdleReportByDriver (from, to, userData,
|
|
35
|
-
const devices = await
|
|
36
|
-
console.log(devices.length)
|
|
35
|
+
async function createIdleReportByDriver (from, to, userData, traccar) {
|
|
36
|
+
const devices = await traccar.devices.devicesGet().then(d => d.data)
|
|
37
37
|
|
|
38
38
|
if (!devices.length) {
|
|
39
39
|
// Empty report
|
|
40
40
|
return { drivers: [] }
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
const
|
|
43
|
+
const results = await reportByDriver(devices, traccar, from, to, userData, processDrivers)
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
drivers:
|
|
45
|
+
const report = {
|
|
46
|
+
drivers: [],
|
|
47
47
|
from,
|
|
48
48
|
to
|
|
49
49
|
}
|
|
50
|
+
|
|
51
|
+
results.drivers.forEach(result => {
|
|
52
|
+
const driver = report.drivers.find(d => d.driver.id === result.driver.id)
|
|
53
|
+
if (driver) {
|
|
54
|
+
driver.idleEvents.push(...result.idleEvents)
|
|
55
|
+
} else {
|
|
56
|
+
report.drivers.push(result)
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
report.drivers.forEach(d => {
|
|
61
|
+
d.idleEvents = d.idleEvents.sort((a, b) => (new Date(a.position.fixTime) > new Date(b.position.fixTime)) ? 1 : -1)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
return report
|
|
50
65
|
}
|
|
51
66
|
|
|
52
67
|
async function createIdleReportByGroup (from, to, userData, traccarInstance) {
|
|
@@ -109,13 +124,13 @@ async function createIdleReportByDevice (from, to, userData, traccarInstance) {
|
|
|
109
124
|
return allData
|
|
110
125
|
}
|
|
111
126
|
|
|
112
|
-
function processDrivers (from, to, userData,
|
|
127
|
+
function processDrivers (from, to, userData, data) {
|
|
113
128
|
const driversResult = []
|
|
114
129
|
userData.drivers.forEach(d => {
|
|
115
|
-
const route =
|
|
130
|
+
const route = data.route.filter(p => p.attributes.driverUniqueId === d.uniqueId)
|
|
116
131
|
|
|
117
132
|
if (route.length > 0) {
|
|
118
|
-
const idleEvents = getIdleEvents(
|
|
133
|
+
const idleEvents = getIdleEvents(data.route, d)
|
|
119
134
|
const filteredEvents = idleEvents.filter(e => userData.minimumIdleMinutes ? e.idleTime > userData.minimumIdleMinutes * 60 * 1000 : true)
|
|
120
135
|
|
|
121
136
|
if (filteredEvents.length) {
|
|
@@ -126,7 +141,7 @@ function processDrivers (from, to, userData, routes) {
|
|
|
126
141
|
|
|
127
142
|
const driverData = {
|
|
128
143
|
driver: d,
|
|
129
|
-
idleEvents: filteredEvents
|
|
144
|
+
idleEvents: filteredEvents
|
|
130
145
|
}
|
|
131
146
|
driversResult.push(driverData)
|
|
132
147
|
}
|
package/src/index.test.js
CHANGED
|
@@ -52,6 +52,21 @@ describe('Test_Reports', function () {
|
|
|
52
52
|
assert.equal(device.trips[1].endPOIName, undefined)
|
|
53
53
|
assert.equal(device.totalDistance, 339204.53999999166) // Total Kms
|
|
54
54
|
}, 20000)
|
|
55
|
+
// eslint-disable-next-line no-undef
|
|
56
|
+
it('Trip by driver', async () => {
|
|
57
|
+
const report = await getReports()
|
|
58
|
+
const userData = await report.getUserData()
|
|
59
|
+
userData.byDriver = true
|
|
60
|
+
const data = await report.tripReport(new Date(2022, 1, 1, 0, 0, 0, 0),
|
|
61
|
+
new Date(2022, 1, 10, 23, 59, 59, 0),
|
|
62
|
+
userData)
|
|
63
|
+
assert.equal(data.length, 1)
|
|
64
|
+
console.log(data[0].drivers)
|
|
65
|
+
const driver = data[0].drivers.find(d => d.driver.id === 14020)
|
|
66
|
+
assert.equal(driver.trips.length, 9) // Total Trips
|
|
67
|
+
assert.equal(driver.totalDuration, 6274000)
|
|
68
|
+
assert.equal(driver.maxSpeed, 70.1944)
|
|
69
|
+
}, 20000)
|
|
55
70
|
it('Trip without addresses', async () => {
|
|
56
71
|
const report = await getReports()
|
|
57
72
|
const userData = await report.getUserData()
|
package/src/speeding-report.js
CHANGED
|
@@ -80,7 +80,7 @@ async function createSpeedingReportByDevice (from, to, userData, traccarInstance
|
|
|
80
80
|
|
|
81
81
|
let deviceCount = 0
|
|
82
82
|
for (const slice of sliced) {
|
|
83
|
-
const { routes, events } = await getEvents(traccarInstance, from, to, slice, userData)
|
|
83
|
+
const { routes, events } = await getEvents(traccarInstance, from, to, slice, userData, deviceCount, devices.length)
|
|
84
84
|
if (events.length > 0) {
|
|
85
85
|
const devicesProcessed = await processDevices(from, to, slice, events, routes, userData)
|
|
86
86
|
allData.devices.push(...devicesProcessed)
|
|
@@ -110,11 +110,11 @@ async function createSpeedingReportByDriver (from, to, userData, traccarInstance
|
|
|
110
110
|
return { drivers: await processDrivers(from, to, events, routes, userData) }
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
async function getEvents (traccarInstance, from, to, devices, userData) {
|
|
113
|
+
async function getEvents (traccarInstance, from, to, devices, userData, deviceCount, totalDevices) {
|
|
114
114
|
const geofencesFeatures = userData.geofences.filter(g => g.area.startsWith('POLYGON') &&
|
|
115
115
|
g.attributes.speedLimit).map(g => convertToFeature(g))
|
|
116
116
|
|
|
117
|
-
const allInOne = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, true, false, false, false)
|
|
117
|
+
const allInOne = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, true, false, false, false, deviceCount, totalDevices)
|
|
118
118
|
const routes = allInOne.route
|
|
119
119
|
|
|
120
120
|
const events = []
|
package/src/trip-report.js
CHANGED
|
@@ -10,6 +10,7 @@ const trips = require('./util/trips')
|
|
|
10
10
|
const { isInsideTimetable, addNearestPOIs, isPartialInsideTimetable, calculateTrip, getTripIdleTime } = require('./util/trips')
|
|
11
11
|
const { devicesToProcess } = require('./util/device')
|
|
12
12
|
const { getIdleEvents } = require('./util/route')
|
|
13
|
+
const { reportByDriver } = require('./util/driver')
|
|
13
14
|
|
|
14
15
|
const fileName = 'TripReport'
|
|
15
16
|
|
|
@@ -114,11 +115,29 @@ async function createTripReportByDriver (from, to, userData, traccar) {
|
|
|
114
115
|
return { drivers: [] }
|
|
115
116
|
}
|
|
116
117
|
|
|
117
|
-
const
|
|
118
|
-
const tripsData = allInOne.trips
|
|
119
|
-
const routeData = allInOne.route
|
|
118
|
+
const results = await reportByDriver(devices, traccar, from, to, userData, processDrivers)
|
|
120
119
|
|
|
121
|
-
|
|
120
|
+
const report = {
|
|
121
|
+
drivers: []
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
results.drivers.forEach(result => {
|
|
125
|
+
const driver = report.drivers.find(d => d.driver.id === result.driver.id)
|
|
126
|
+
if (driver) {
|
|
127
|
+
driver.trips.push(...result.trips)
|
|
128
|
+
} else {
|
|
129
|
+
report.drivers.push(result)
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
report.drivers.forEach(d => {
|
|
134
|
+
d.totalDuration = d.trips.reduce((a, b) => a + b.duration, 0)
|
|
135
|
+
d.totalDistance = d.trips.reduce((a, b) => a + b.distance, 0)
|
|
136
|
+
d.maxSpeed = d.trips.reduce((a, b) => { return a > b.maxSpeed ? a : b.maxSpeed }, 0)
|
|
137
|
+
d.trips = d.trips.sort((a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime())
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
return report
|
|
122
141
|
}
|
|
123
142
|
|
|
124
143
|
function processDevices (from, to, devices, data, userData, traccar) {
|
|
@@ -195,8 +214,6 @@ function processDrivers (from, to, userData, data) {
|
|
|
195
214
|
const trips = data.trips.filter(t => t.driverUniqueId === d.uniqueId &&
|
|
196
215
|
(userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, route)))
|
|
197
216
|
|
|
198
|
-
trips.sort((a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime())
|
|
199
|
-
|
|
200
217
|
const idleEvents = getIdleEvents(data.route, d)
|
|
201
218
|
|
|
202
219
|
trips.forEach(function (trip, i) {
|
|
@@ -211,15 +228,11 @@ function processDrivers (from, to, userData, data) {
|
|
|
211
228
|
driver: d,
|
|
212
229
|
from,
|
|
213
230
|
to,
|
|
214
|
-
totalDuration: trips.reduce((a, b) => a + b.duration, 0),
|
|
215
|
-
totalDistance: trips.reduce((a, b) => a + b.distance, 0),
|
|
216
|
-
maxSpeed: trips.reduce((a, b) => { return a > b.maxSpeed ? a : b.maxSpeed }, 0),
|
|
217
231
|
trips
|
|
218
232
|
}
|
|
219
233
|
driversResult.push(driverData)
|
|
220
234
|
}
|
|
221
235
|
})
|
|
222
|
-
console.log(driversResult)
|
|
223
236
|
|
|
224
237
|
return driversResult
|
|
225
238
|
}
|
package/src/util/driver.js
CHANGED
|
@@ -1,17 +1,43 @@
|
|
|
1
|
-
const traccar = require(
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
const traccar = require('./traccar')
|
|
2
|
+
const automaticReports = require('../automaticReports')
|
|
3
|
+
const traccarHelper = require('./traccar')
|
|
4
|
+
|
|
5
|
+
// get devices used by each driver between two dates
|
|
6
|
+
async function devicesByDriver (traccarInstance, from, to, drivers, devices) {
|
|
7
|
+
const eventsData = await traccar.getEvents(traccarInstance, from, to, devices, ['driverChanged'])
|
|
8
|
+
const deviceIds = []
|
|
9
|
+
for (const d of drivers) {
|
|
10
|
+
const events = eventsData.filter(e => {
|
|
11
|
+
return e.attributes.driverUniqueId === d.uniqueId
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
deviceIds.push(...events.map(e => e.deviceId))
|
|
15
|
+
}
|
|
16
|
+
return devices.filter(d => deviceIds.includes(d.id))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function reportByDriver (devices, traccar, from, to, userData, processor) {
|
|
20
|
+
const report = {
|
|
21
|
+
drivers: []
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const sliced = automaticReports.sliceArray(devices, 5)
|
|
25
|
+
|
|
26
|
+
let deviceCount = 0
|
|
27
|
+
for (const slice of sliced) {
|
|
28
|
+
const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, true, true, false, false, deviceCount, devices.length)
|
|
29
|
+
const tripsData = allInOne.trips
|
|
30
|
+
const routeData = allInOne.route
|
|
31
|
+
|
|
32
|
+
const results = processor(from, to, userData, { trips: tripsData, route: routeData })
|
|
33
|
+
|
|
34
|
+
report.drivers.push(...results)
|
|
35
|
+
|
|
36
|
+
deviceCount = deviceCount + slice.length
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return report
|
|
15
40
|
}
|
|
16
41
|
|
|
17
42
|
exports.devicesByDriver = devicesByDriver
|
|
43
|
+
exports.reportByDriver = reportByDriver
|