fleetmap-reports 1.0.427 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.427",
3
+ "version": "1.0.428",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -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, traccarInstance) {
35
- const devices = await traccarInstance.devices.devicesGet().then(d => d.data)
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 { route } = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, true, false, false, false)
43
+ const results = await reportByDriver(devices, traccar, from, to, userData, processDrivers)
44
44
 
45
- return {
46
- drivers: processDrivers(from, to, userData, route),
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, routes) {
127
+ function processDrivers (from, to, userData, data) {
113
128
  const driversResult = []
114
129
  userData.drivers.forEach(d => {
115
- const route = routes.filter(p => p.attributes.driverUniqueId === d.uniqueId)
130
+ const route = data.route.filter(p => p.attributes.driverUniqueId === d.uniqueId)
116
131
 
117
132
  if (route.length > 0) {
118
- const idleEvents = getIdleEvents(routes, d)
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.sort((a, b) => (new Date(a.position.fixTime) > new Date(b.position.fixTime)) ? 1 : -1)
144
+ idleEvents: filteredEvents
130
145
  }
131
146
  driversResult.push(driverData)
132
147
  }
@@ -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,40 +115,29 @@ async function createTripReportByDriver (from, to, userData, traccar) {
114
115
  return { drivers: [] }
115
116
  }
116
117
 
117
- const allData = {
118
+ const results = await reportByDriver(devices, traccar, from, to, userData, processDrivers)
119
+
120
+ const report = {
118
121
  drivers: []
119
122
  }
120
123
 
121
- const sliced = automaticReports.sliceArray(devices, 5)
122
-
123
- let deviceCount = 0
124
- for (const slice of sliced) {
125
- const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, true, true, false, false, deviceCount, devices.length)
126
- const tripsData = allInOne.trips
127
- const routeData = allInOne.route
128
-
129
- const results = processDrivers(from, to, userData, { trips: tripsData, route: routeData })
130
-
131
- results.forEach(result => {
132
- const driver = allData.drivers.find(d => d.driver.id === result.driver.id)
133
- if (driver) {
134
- driver.trips.push(...result.trips)
135
- } else {
136
- allData.drivers.push(result)
137
- }
138
- })
139
-
140
- deviceCount = deviceCount + slice.length
141
- }
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
+ })
142
132
 
143
- allData.drivers.forEach(d => {
133
+ report.drivers.forEach(d => {
144
134
  d.totalDuration = d.trips.reduce((a, b) => a + b.duration, 0)
145
135
  d.totalDistance = d.trips.reduce((a, b) => a + b.distance, 0)
146
136
  d.maxSpeed = d.trips.reduce((a, b) => { return a > b.maxSpeed ? a : b.maxSpeed }, 0)
147
137
  d.trips = d.trips.sort((a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime())
148
138
  })
149
139
 
150
- return allData
140
+ return report
151
141
  }
152
142
 
153
143
  function processDevices (from, to, devices, data, userData, traccar) {
@@ -1,17 +1,43 @@
1
- const traccar = require("./traccar");
2
-
3
- //get devices used by each driver between two dates
4
- async function devicesByDriver(traccarInstance, from, to, drivers, devices){
5
- const eventsData = await traccar.getEvents(traccarInstance, from, to , devices, ['driverChanged'])
6
- const deviceIds = []
7
- for(const d of drivers) {
8
- const events = eventsData.filter(e => {
9
- return e.attributes.driverUniqueId === d.uniqueId
10
- })
11
-
12
- deviceIds.push(...events.map(e => e.deviceId))
13
- }
14
- return devices.filter(d => deviceIds.includes(d.id))
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