fleetmap-reports 1.0.342 → 1.0.346

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.342",
3
+ "version": "1.0.346",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -172,19 +172,21 @@ function processDevices(from, to, devices, data, userData) {
172
172
  return a > b.maxSpeed ? a : b.maxSpeed
173
173
  }, 0),
174
174
  endOdometer: 0,
175
- startOdometer: 0
175
+ startOdometer: 0,
176
+ startTime: tripsByDay.length && tripsByDay[0].startTime,
177
+ endTime: tripsByDay.length && tripsByDay[tripsByDay.length-1].endTime
176
178
  })
177
179
  } else {
178
- const summaryCurrentDay = data.summaries.filter(s => {
180
+ const summaryCurrentDay = data.summaries.find(s => {
179
181
  return s.deviceId === d.id && s.date.getTime() === date.getTime()})
180
182
 
181
- if (summaryCurrentDay.length) {
182
- summaryCurrentDay.forEach(s => {
183
- s.distance = distance
184
- s.convertedSpentFuel = automaticReports.calculateSpentFuel(s.spentFuel, d)
185
- })
183
+ if (summaryCurrentDay) {
184
+ summaryCurrentDay.distance = distance
185
+ summaryCurrentDay.convertedSpentFuel = automaticReports.calculateSpentFuel(summaryCurrentDay.spentFuel, d)
186
+ summaryCurrentDay.startTime = tripsByDay.length && tripsByDay[0].startTime
187
+ summaryCurrentDay.endTime = tripsByDay.length && tripsByDay[tripsByDay.length-1].endTime
186
188
 
187
- summary.push(...summaryCurrentDay)
189
+ summary.push(summaryCurrentDay)
188
190
  }
189
191
  }
190
192
  }
@@ -193,8 +195,11 @@ function processDevices(from, to, devices, data, userData) {
193
195
 
194
196
  if (summary.length) {
195
197
  summary.forEach(s => {
196
- s.distance = data.trips.filter(t => t.deviceId === d.id).reduce((a, b) => a + b.distance, 0)
198
+ const deviceTrips = data.trips.filter(t => t.deviceId === d.id)
199
+ s.distance = deviceTrips.reduce((a, b) => a + b.distance, 0)
197
200
  s.convertedSpentFuel = automaticReports.calculateSpentFuel(s.spentFuel, d)
201
+ s.startTime = deviceTrips.length && deviceTrips[0].startTime
202
+ s.endTime = deviceTrips.length && deviceTrips[deviceTrips.length-1].endTime
198
203
  })
199
204
  }
200
205
  }
@@ -234,7 +239,9 @@ function processDrivers(from, to, drivers, data, userData) {
234
239
  distance: tripsByDay.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
235
240
  engineHours: tripsByDay.reduce((a, b) => a + b.duration, 0),
236
241
  maxSpeed: tripsByDay.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
237
- averageSpeed: tripsByDay.length > 0 ? Math.round(tripsByDay.reduce((a, b) => a + b.averageSpeed, 0) / tripsByDay.length) : 0,
242
+ averageSpeed: tripsByDay.length ? Math.round(tripsByDay.reduce((a, b) => a + b.averageSpeed, 0) / tripsByDay.length) : 0,
243
+ startTime: tripsByDay.length && tripsByDay[0].startTime,
244
+ endTime: tripsByDay.length && tripsByDay[tripsByDay.length-1].endTime
238
245
  })
239
246
  }
240
247
  } else {
@@ -243,6 +250,8 @@ function processDrivers(from, to, drivers, data, userData) {
243
250
  engineHours: trips.reduce((a, b) => a + b.duration, 0),
244
251
  maxSpeed: trips.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
245
252
  averageSpeed: Math.round(trips.reduce((a, b) => a + b.averageSpeed, 0) / trips.length),
253
+ startTime: trips.length && trips[0].startTime,
254
+ endTime: trips.length && trips[trips.length-1].endTime
246
255
  })
247
256
  }
248
257
  driversResult.push(driverData)
@@ -277,6 +286,8 @@ async function exportActivityReportToPDF(userData, reportData) {
277
286
 
278
287
  function exportActivityReportToPDF_Driver(doc, translations, reportData, userData) {
279
288
  const headers = [translations.report.driver,
289
+ translations.report.start,
290
+ translations.report.end,
280
291
  translations.report.distance,
281
292
  translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)',
282
293
  translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)',
@@ -289,6 +300,8 @@ function exportActivityReportToPDF_Driver(doc, translations, reportData, userDat
289
300
  reportData.drivers.forEach(d => {
290
301
  const temp = [
291
302
  d.driver.name,
303
+ d.summary[0].startTime ? new Date(d.summary[0].startTime).toLocaleString() : '',
304
+ d.summary[0].endTime ? new Date(d.summary[0].endTime).toLocaleString() : '',
292
305
  Number((d.summary[0].distance / 1000).toFixed(2)),
293
306
  Math.round(d.summary[0].averageSpeed * 1.85200),
294
307
  Math.round(d.summary[0].maxSpeed * 1.85200),
@@ -298,6 +311,7 @@ function exportActivityReportToPDF_Driver(doc, translations, reportData, userDat
298
311
  })
299
312
 
300
313
  const footValues = ['Total:' + reportData.drivers.length,
314
+ '','',
301
315
  (reportData.drivers.reduce((a, b) => a + b.summary[0].distance, 0) / 1000).toFixed(0),
302
316
  getSumAvgSpeed(reportData.drivers, userData),
303
317
  getSumMaxSpeed(reportData.drivers, userData),
@@ -310,6 +324,8 @@ function exportActivityReportToPDF_Driver(doc, translations, reportData, userDat
310
324
  function exportActivityReportToPDF_Vehicle(doc, translations, reportData, userData) {
311
325
  const headers = [translations.report.vehicle,
312
326
  translations.report.group,
327
+ translations.report.start,
328
+ translations.report.end,
313
329
  translations.report.distance,
314
330
  translations.report.start + ' ' + translations.report.odometer,
315
331
  translations.report.end + ' ' + translations.report.odometer,
@@ -328,6 +344,8 @@ function exportActivityReportToPDF_Vehicle(doc, translations, reportData, userDa
328
344
  const temp = [
329
345
  d.summary[0].deviceName,
330
346
  group ? group.name : '',
347
+ d.summary[0].startTime ? new Date(d.summary[0].startTime).toLocaleString() : '',
348
+ d.summary[0].endTime ? new Date(d.summary[0].endTime).toLocaleString() : '',
331
349
  Number((d.summary[0].distance / 1000).toFixed(2)),
332
350
  Number((d.summary[0].startOdometer / 1000).toFixed(2)),
333
351
  Number((d.summary[0].endOdometer / 1000).toFixed(2)),
@@ -340,7 +358,7 @@ function exportActivityReportToPDF_Vehicle(doc, translations, reportData, userDa
340
358
  })
341
359
 
342
360
  const footValues = ['Total:' + reportData.devices.length,
343
- '',
361
+ '','','',
344
362
  (reportData.devices.reduce((a, b) => a + b.summary[0].distance, 0) / 1000).toFixed(0),
345
363
  '', '',
346
364
  getSumAvgSpeed(reportData.devices, userData),
@@ -354,6 +372,8 @@ function exportActivityReportToPDF_Vehicle(doc, translations, reportData, userDa
354
372
 
355
373
  function exportActivityReportToPDF_Vehicle_GroupByDay(doc, translations, reportData, userData) {
356
374
  const headers = [translations.report.date,
375
+ translations.report.start,
376
+ translations.report.end,
357
377
  translations.report.distance,
358
378
  translations.report.start + ' ' + translations.report.odometer,
359
379
  translations.report.end + ' ' + translations.report.odometer,
@@ -403,6 +423,8 @@ function exportActivityReportToPDF_Vehicle_GroupByDay(doc, translations, reportD
403
423
  d.summary.forEach(s => {
404
424
  const temp = [
405
425
  new Date(s.date).toLocaleDateString() + ' - ' + weekDays[s.date.getDay()],
426
+ s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
427
+ s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
406
428
  Number((s.distance / 1000).toFixed(2)),
407
429
  Number((s.startOdometer / 1000).toFixed(2)),
408
430
  Number((s.endOdometer / 1000).toFixed(2)),
@@ -416,6 +438,7 @@ function exportActivityReportToPDF_Vehicle_GroupByDay(doc, translations, reportD
416
438
  })
417
439
 
418
440
  const footValues = ['Total:' + d.summary.length,
441
+ '','',
419
442
  (d.summary.reduce((a, b) => a + b.distance, 0) / 1000).toFixed(0),
420
443
  '', '',
421
444
  getSumAvgSpeed(d.summary, userData),
@@ -433,6 +456,8 @@ function exportActivityReportToExcel_Vehicle_GroupByDay(settings, translations,
433
456
  {label: translations.report.group, value: 'group'},
434
457
  {label: translations.report.date, value: 'date'},
435
458
  {label: translations.report.weekDay, value: 'weekday'},
459
+ {label: translations.report.start, value: 'start'},
460
+ {label: translations.report.end, value: 'end'},
436
461
  {label: translations.report.distance, value: 'distance'},
437
462
  {label: translations.report.start + ' ' + translations.report.odometer, value: 'startOdometer'},
438
463
  {label: translations.report.end + ' ' + translations.report.odometer, value: 'endOdometer'},
@@ -463,6 +488,8 @@ function exportActivityReportToExcel_Vehicle_GroupByDay(settings, translations,
463
488
  group: group ? group.name : '',
464
489
  date: new Date(s.date).toLocaleDateString(),
465
490
  weekday: weekDays[s.date.getDay()],
491
+ start: s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
492
+ end:s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
466
493
  distance: Number((s.distance / 1000).toFixed(0)),
467
494
  startOdometer: Number((s.startOdometer / 1000).toFixed(0)),
468
495
  endOdometer: Number((s.endOdometer / 1000).toFixed(0)),
@@ -484,6 +511,8 @@ function exportActivityReportToExcel_Vehicle_GroupByDay(settings, translations,
484
511
 
485
512
  function exportActivityReportToPDF_Driver_GroupByDay(doc, translations, reportData, userData) {
486
513
  const headers = [translations.report.date,
514
+ translations.report.start,
515
+ translations.report.end,
487
516
  translations.report.distance,
488
517
  translations.report.avgSpeed + ' (Km/h)',
489
518
  translations.report.maxSpeed + ' (Km/h)',
@@ -530,6 +559,8 @@ function exportActivityReportToPDF_Driver_GroupByDay(doc, translations, reportDa
530
559
  d.summary.forEach(s => {
531
560
  const temp = [
532
561
  new Date(s.date).toLocaleDateString() + ' - ' + weekDays[s.date.getDay()],
562
+ s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
563
+ s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
533
564
  Number((s.distance / 1000).toFixed(2)),
534
565
  Math.round(s.averageSpeed * 1.85200),
535
566
  Math.round(s.maxSpeed * 1.85200),
@@ -540,6 +571,7 @@ function exportActivityReportToPDF_Driver_GroupByDay(doc, translations, reportDa
540
571
  })
541
572
 
542
573
  const footValues = ['Total:' + d.summary.length,
574
+ '','',
543
575
  (d.summary.reduce((a, b) => a + b.distance, 0) / 1000).toFixed(0),
544
576
  getSumAvgSpeed(d.summary, userData),
545
577
  getSumMaxSpeed(d.summary, userData),
@@ -555,6 +587,8 @@ function exportActivityReportToExcel_Driver_GroupByDay(settings, translations, r
555
587
  {label: translations.report.group, value: 'group'},
556
588
  {label: translations.report.date, value: 'date'},
557
589
  {label: translations.report.weekDay, value: 'weekday'},
590
+ {label: translations.report.start, value: 'start'},
591
+ {label: translations.report.end, value: 'end'},
558
592
  {label: translations.report.distance, value: 'distance'},
559
593
  {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
560
594
  {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
@@ -582,6 +616,8 @@ function exportActivityReportToExcel_Driver_GroupByDay(settings, translations, r
582
616
  group: group ? group.name : '',
583
617
  date: new Date(s.date).toLocaleDateString(),
584
618
  weekday: weekDays[s.date.getDay()],
619
+ start: s.startTime ? new Date(s.startTime).toLocaleTimeString() : '',
620
+ end:s.endTime ? new Date(s.endTime).toLocaleTimeString() : '',
585
621
  distance: Number((s.distance / 1000).toFixed(0)),
586
622
  avgSpeed: Math.round(s.averageSpeed * 1.85200),
587
623
  maxSpeed: Math.round(s.maxSpeed * 1.85200),
@@ -600,6 +636,8 @@ function exportActivityReportToExcel_Driver_GroupByDay(settings, translations, r
600
636
 
601
637
  function exportActivityReportToExcel_Driver(settings, translations, reportData) {
602
638
  const headers = [{label: translations.report.driver, value: 'driver'},
639
+ {label: translations.report.start, value: 'start'},
640
+ {label: translations.report.end, value: 'end'},
603
641
  {label: translations.report.distance, value: 'distance'},
604
642
  {label: translations.report.speed + ' ' + translations.report.avgSpeed + ' (Km/h)', value: 'avgSpeed'},
605
643
  {label: translations.report.speed + ' ' + translations.report.maxSpeed + ' (Km/h)', value: 'maxSpeed'},
@@ -610,6 +648,8 @@ function exportActivityReportToExcel_Driver(settings, translations, reportData)
610
648
  reportData.drivers.forEach(d => {
611
649
  data = data.concat([{
612
650
  driver: d.driver.name,
651
+ start: d.summary[0].startTime ? new Date(d.summary[0].startTime).toLocaleString() : '',
652
+ end: d.summary[0].endTime ? new Date(d.summary[0].endTime).toLocaleString() : '',
613
653
  distance: Number((d.summary[0].distance / 1000).toFixed(0)),
614
654
  avgSpeed: Math.round(d.summary[0].averageSpeed * 1.85200),
615
655
  maxSpeed: Math.round(d.summary[0].maxSpeed * 1.85200),
@@ -628,6 +668,8 @@ function exportActivityReportToExcel_Driver(settings, translations, reportData)
628
668
  function exportActivityReportToExcel_Vehicle(settings, translations, reportData, userData) {
629
669
  const headers = [{label: translations.report.vehicle, value: 'name'},
630
670
  {label: translations.report.group, value: 'group'},
671
+ {label: translations.report.start, value: 'start'},
672
+ {label: translations.report.end, value: 'end'},
631
673
  {label: translations.report.distance, value: 'distance'},
632
674
  {label: translations.report.start + ' ' + translations.report.odometer, value: 'startOdometer'},
633
675
  {label: translations.report.end + ' ' + translations.report.odometer, value: 'endOdometer'},
@@ -644,6 +686,8 @@ function exportActivityReportToExcel_Vehicle(settings, translations, reportData,
644
686
  data = data.concat([{
645
687
  name: d.summary[0].deviceName,
646
688
  group: group ? group.name : '',
689
+ start: d.summary[0].startTime ? new Date(d.summary[0].startTime).toLocaleString() : '',
690
+ end: d.summary[0].endTime ? new Date(d.summary[0].endTime).toLocaleString() : '',
647
691
  distance: Number((d.summary[0].distance / 1000).toFixed(0)),
648
692
  startOdometer: Number((d.summary[0].startOdometer / 1000).toFixed(0)),
649
693
  endOdometer: Number((d.summary[0].endOdometer / 1000).toFixed(0)),
package/src/index.test.js CHANGED
@@ -125,7 +125,7 @@ describe('Test_Reports', function() {
125
125
  assert.equal(data.length, 1)
126
126
  const device = data[0].devices.find(d => d.device.id===22326)
127
127
  assert.equal(device.summary.distance, 1193284.3100000024) // Total Kms
128
- },20000)
128
+ },30000)
129
129
  it('KmsReport byDevice groupByDay', async () => {
130
130
  const reports = await getReports()
131
131
  const userData = await reports.getUserData()
@@ -137,7 +137,7 @@ describe('Test_Reports', function() {
137
137
  const device = data[0].devices.find(d => d.device.id===22326)
138
138
  assert.equal(device.days.length, 10) // Total Kms
139
139
  assert.equal(device.days[5].kms, 23183.010000005364) // Total Kms
140
- },20000)
140
+ },90000)
141
141
  it('Idle by device', async () => {
142
142
  const report = await getReports()
143
143
  const userData = await report.getUserData()
@@ -162,6 +162,8 @@ describe('Test_Reports', function() {
162
162
  const device = data[0].devices.find(d => d.device.id===22326)
163
163
  assert.equal(device.summary[0].startOdometer, 122502742.59)
164
164
  assert.equal(device.summary[0].distance, 1386519.1300000101)
165
+ assert.equal(device.summary[0].startTime, '2022-01-01T13:35:47.000+0000')
166
+ assert.equal(device.summary[0].endTime, '2022-01-30T13:48:18.000+0000')
165
167
  }, 20000)
166
168
  it('Activity byDevice groupByDay', async () => {
167
169
  const report = await getReports()
@@ -174,7 +176,9 @@ describe('Test_Reports', function() {
174
176
  const device = data[0].devices.find(d => d.device.id===22326)
175
177
  assert.equal(device.summary[5].startOdometer, 122923290.95)
176
178
  assert.equal(device.summary[5].distance, 77020.37999999523)
177
- }, 40000)
179
+ assert.equal(device.summary[5].startTime, '2022-01-06T18:35:04.000+0000')
180
+ assert.equal(device.summary[5].endTime, '2022-01-06T19:54:27.000+0000')
181
+ }, 60000)
178
182
  it('test allinone', async() => {
179
183
  console.log('Start')
180
184
  const reports = await getReports()
@@ -187,7 +191,28 @@ describe('Test_Reports', function() {
187
191
  true,
188
192
  true,
189
193
  false)
190
- assert.equal(r.trips.length, 10)
191
- assert.equal(r.stops.length, 11)
194
+
195
+ assert.equal(r.trips.filter(t => t.deviceId === 22327).length, 10)
196
+ assert.equal(r.stops.filter(t => t.deviceId === 22327).length, 11)
192
197
  }, 20000)
198
+ it('Total KMS', async () => {
199
+ const report = await getReports()
200
+ const userData = await report.getUserData()
201
+
202
+ const tripsReport = await report.tripReport(new Date(2022, 1, 11, 0, 0, 0, 0),
203
+ new Date(2022, 1, 14, 23, 59, 59, 0),
204
+ userData)
205
+
206
+ const kmsReport = await report.kmsReport(new Date(2022, 1, 11, 0, 0, 0, 0),
207
+ new Date(2022, 1, 14, 23, 59, 59, 0),
208
+ userData)
209
+
210
+ assert.equal(tripsReport.length, 1)
211
+ const device1 = tripsReport[0].devices.find(d => d.device.id===11681)
212
+ assert.equal(device1.trips.length, 44) // Total Trips
213
+ assert.equal(device1.totalDistance, 883076.9928612101) // Total Kms
214
+
215
+ const device2 = kmsReport[0].devices.find(d => d.device.id===11681)
216
+ assert.equal(device2.summary.distance, device1.totalDistance) // Total Kms
217
+ }, 30000)
193
218
  })
@@ -238,7 +238,6 @@ async function getHereEvents(devices, routes, threshold){
238
238
  continue
239
239
  }
240
240
  let hereAlerts = null
241
- try {
242
241
  const results = await here.routeMatch(positions)
243
242
  hereAlerts = results.filter(r => r.currentSpeedKmh > (parseInt(r.speedLimit) + threshold)).map(r => {
244
243
  const position = positions.find(p => new Date(p.fixTime).getTime() === r.timestamp)
@@ -265,9 +264,6 @@ async function getHereEvents(devices, routes, threshold){
265
264
  return acc
266
265
  })
267
266
  events.push(...reduced)
268
- } catch (e) {
269
- console.error(hereAlerts, 'threshold', threshold, e)
270
- }
271
267
  }
272
268
  return events
273
269
  }
package/src/util/trips.js CHANGED
@@ -34,7 +34,18 @@ function checkTripsKms(traccarInstance, from, to, devices, data) {
34
34
  const tripRoute = data.route.filter(p => p.deviceId === t.deviceId
35
35
  && new Date(p.fixTime).getTime() >= new Date(t.startTime).getTime()
36
36
  && new Date(p.fixTime).getTime() <= new Date(t.endTime).getTime())
37
- t.distance = tripRoute.reduce((a, b) => a + b.attributes.distance, 0)
37
+
38
+ let current = null
39
+ const distances = []
40
+ for(const p of tripRoute) {
41
+ if(current) {
42
+ distances.push(coordsDistance(parseFloat(current.longitude),parseFloat(current.latitude),
43
+ parseFloat(p.longitude),parseFloat(p.latitude)))
44
+ }
45
+ current = p
46
+ }
47
+
48
+ t.distance = distances.reduce((a, b) => a + b, 0)
38
49
  }
39
50
  })
40
51
  }