fleetmap-reports 1.0.622 → 1.0.624

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.622",
3
+ "version": "1.0.624",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1,16 +1,36 @@
1
1
  const traccar = require('../util/traccar')
2
+ const { getIdleEvents } = require('../util/route')
3
+ const { insideGeofence } = require('../util/geofence')
2
4
 
3
5
  async function createActivityReport (from, to, userData, traccarInstance) {
4
- const allInOne = await traccar.getAllInOne(traccarInstance, from, to, userData.devices, false, true, true, false)
6
+ const group = userData.groups.find(g => g.name === 'Zones Non Autorisées')
7
+
8
+ const unauthorizedGeofences = []
9
+ if (group) {
10
+ const geofenceIds = traccarInstance.geofences.geofencesGet(undefined, undefined, undefined, group.id, undefined, undefined).then(r => r.data)
11
+ unauthorizedGeofences.push(...(userData.geofences.filter(g => geofenceIds.includes(g.id))))
12
+ }
13
+
14
+ const allInOne = await traccar.getAllInOne(traccarInstance, from, to, userData.devices, true, true, true, false)
5
15
 
6
16
  const reportData = []
7
17
  userData.devices.forEach(d => {
8
18
  const trips = allInOne.trips.filter(t => t.deviceId === d.id)
19
+ const route = allInOne.route.filter(p => p.deviceId === d.id)
9
20
  const stops = allInOne.stops.filter(s => s.deviceId === d.id)
10
21
  const group = userData.groups.find(g => g.id === d.groupId)
11
22
 
23
+ const unauthorizedStops = stops.filter(s =>
24
+ unauthorizedGeofences.find(g => insideGeofence({ latitude: s.latitude, longitude: s.longitude }, g))
25
+ )
26
+
27
+ const idleEvents = getIdleEvents(route)
28
+
12
29
  const totalDistance = trips.reduce((a, b) => a + b.distance, 0)
13
- const idleTime = stops.reduce((a, b) => a + b.engineHours, 0)
30
+ const drivingTime = trips.reduce((a, b) => a + b.duration, 0)
31
+ const stopTime = stops.reduce((a, b) => a + (b.duration > b.engineHours ? (b.duration - b.engineHours) : 0), 0)
32
+
33
+ const idleTimeArray = idleEvents.map(e => e.idleTime).filter(a => a > 0)
14
34
 
15
35
  const deviceData = {
16
36
  device: d.name,
@@ -19,10 +39,16 @@ async function createActivityReport (from, to, userData, traccarInstance) {
19
39
  startTime: trips.length ? trips[0].startTime : '',
20
40
  endAddress: trips.length ? trips[trips.length - 1].endAddress : '',
21
41
  endTime: trips.length ? trips[trips.length - 1].endTime : '',
22
- drivingTime: trips.reduce((a, b) => a + b.duration, 0),
23
- stopTime: stops.reduce((a, b) => a + b.duration, 0) - idleTime,
24
- idleTime,
42
+ drivingTime,
43
+ stopTime,
44
+ idleTime: idleEvents.reduce((a, b) => a + b.idleTime, 0),
25
45
  distance: totalDistance,
46
+ totalIdleEvents: idleEvents.length,
47
+ minIdleEvent: idleTimeArray.length > 0 ? Math.min(...idleTimeArray) : 0,
48
+ maxIdleEvent: idleTimeArray.length > 0 ? Math.max(...idleTimeArray) : 0,
49
+ authorizedStops: stops.length - unauthorizedStops.length,
50
+ unauthorizedStops: unauthorizedStops.length,
51
+ authorizedStopsTime: unauthorizedStops.reduce((a, b) => a + (b.duration > b.engineHours ? (b.duration - b.engineHours) : 0), 0),
26
52
  averageSpeed: totalDistance > 0 ? ((trips.reduce((a, b) => a + (b.averageSpeed * b.distance), 0)) / totalDistance) : 0,
27
53
  maxSpeed: trips.reduce((a, b) => {
28
54
  return a > b.maxSpeed ? a : b.maxSpeed