fleetmap-reports 1.0.414 → 1.0.415

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.414",
3
+ "version": "1.0.415",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -42,7 +42,11 @@ async function createIdleReportByDriver (from, to, userData, traccarInstance) {
42
42
 
43
43
  const { route } = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, true, false, false, false)
44
44
 
45
- return { drivers: processDrivers(from, to, userData, route) }
45
+ return {
46
+ drivers: processDrivers(from, to, userData, route),
47
+ from,
48
+ to
49
+ }
46
50
  }
47
51
 
48
52
  async function createIdleReportByGroup (from, to, userData, traccarInstance) {
@@ -108,17 +112,21 @@ async function createIdleReportByDevice (from, to, userData, traccarInstance) {
108
112
  function processDrivers (from, to, userData, routes) {
109
113
  const driversResult = []
110
114
  userData.drivers.forEach(d => {
111
- const route = routes.filter(p => p.attributes.driverUniqueId === d.uniqueId || !p.attributes.ignition)
115
+ const route = routes.filter(p => p.attributes.driverUniqueId === d.uniqueId)
112
116
 
113
- const idleEvents = getIdleEvents(route)
114
- console.log(idleEvents)
115
117
  if (route.length > 0) {
118
+ const idleEvents = getIdleEvents(routes, d)
116
119
  const filteredEvents = idleEvents.filter(e => userData.minimumIdleMinutes ? e.idleTime > userData.minimumIdleMinutes * 60 * 1000 : true)
117
120
 
118
121
  if (filteredEvents.length) {
122
+ filteredEvents.forEach(e => {
123
+ const device = userData.devices.find(d => d.id === e.position.deviceId)
124
+ e.deviceName = device ? device.name : ''
125
+ })
126
+
119
127
  const driverData = {
120
128
  driver: d,
121
- idleEvents: filteredEvents
129
+ idleEvents: filteredEvents.sort((a, b) => (new Date(a.position.fixTime) > new Date(b.position.fixTime)) ? 1 : -1)
122
130
  }
123
131
  driversResult.push(driverData)
124
132
  }
package/src/index.test.js CHANGED
@@ -208,8 +208,8 @@ describe('Test_Reports', function () {
208
208
  assert.equal(data.length, 1)
209
209
  const driver = data[0].drivers.find(d => d.driver.id === 14020)
210
210
  const totalIdleTime = driver.idleEvents.reduce((a, b) => a + b.idleTime, 0)
211
- assert.equal(driver.idleEvents.length, 12) // Total Alerts
212
- assert.equal(totalIdleTime, 30785000) // Total Duration
211
+ assert.equal(driver.idleEvents.length, 1) // Total Alerts
212
+ assert.equal(totalIdleTime, 8267000) // Total Duration
213
213
  }, 20000)
214
214
  // eslint-disable-next-line no-undef
215
215
  it('Activity by device', async () => {
@@ -193,7 +193,7 @@ function processDrivers (from, to, userData, data) {
193
193
 
194
194
  trips.sort((a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime())
195
195
 
196
- const idleEvents = getIdleEvents(data.route.filter(p => p.attributes.driverUniqueId === d.uniqueId || !p.attributes.ignition))
196
+ const idleEvents = getIdleEvents(data.route, d)
197
197
 
198
198
  trips.forEach(function (trip, i) {
199
199
  trip.stopEngineHours = getTripIdleTime(trip, idleEvents)
package/src/util/route.js CHANGED
@@ -1,4 +1,4 @@
1
- function getIdleEvents (route) {
1
+ function getIdleEvents (route, driver) {
2
2
  const idleEvents = []
3
3
 
4
4
  const routeByDevice = route.reduce(function (a, x) {
@@ -9,7 +9,7 @@ function getIdleEvents (route) {
9
9
  Object.keys(routeByDevice).forEach(function (key) {
10
10
  let inIdle = false
11
11
  routeByDevice[key].forEach(p => {
12
- if (p.attributes.ignition && p.speed === 0) {
12
+ if (p.attributes.ignition && p.speed === 0 && (!driver || p.attributes.driverUniqueId === driver.uniqueId)) {
13
13
  if (!inIdle) {
14
14
  const idleEvent = {
15
15
  position: p,
@@ -21,6 +21,7 @@ function getIdleEvents (route) {
21
21
  if (!idleEvents[idleEvents.length - 1].position.attributes.driverUniqueId) {
22
22
  idleEvents[idleEvents.length - 1].position.attributes.driverUniqueId = p.attributes.driverUniqueId
23
23
  }
24
+
24
25
  if (p.attributes.idleTime) {
25
26
  idleEvents[idleEvents.length - 1].idleTime = p.attributes.idleTime
26
27
  }