fleetmap-reports 1.0.623 → 1.0.625

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