fleetmap-reports 2.0.164 → 2.0.165

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": "2.0.164",
3
+ "version": "2.0.165",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -10,6 +10,7 @@ const { getUserPartner } = require('fleetmap-partners')
10
10
  const { getOverspeedEvents } = require('../speeding-report')
11
11
  const { calculateConsumption } = require('../util/fuel')
12
12
  const { getCanAvgConsumption } = require('../fuel-consumption-report')
13
+ const automaticReports = require('../automaticReports')
13
14
 
14
15
  const distanceCoefficient = 0.5
15
16
  const consumptionCoefficient = 0.7
@@ -28,7 +29,6 @@ async function create (from, to, userData, traccar) {
28
29
  drivers: []
29
30
  }
30
31
 
31
- const drivers = userData.drivers
32
32
  const devices = userData.devices
33
33
  userData.geofences = []
34
34
  userData.roadSpeedLimits = true
@@ -37,77 +37,34 @@ async function create (from, to, userData, traccar) {
37
37
  const driversData = new Map()
38
38
  const overspeedEvents = await getOverspeedEvents(from, to, userData, devices, userData, traccar)
39
39
 
40
- for (const device of devices) {
41
- const allInOne = await traccarHelper.getAllInOne(traccar, from, to, [device], true, true, false, false, deviceCount, devices.length)
42
- const deviceOverspeedEvents = overspeedEvents.find(e => e.device.id === device.id)
43
- const { data } = await traccar.reports.reportsEventsGet(from, to, [device.id], null, ['alarm'])
44
-
45
- for (const d of drivers) {
46
- const { trips, route } = await getDriverData(d, allInOne, userData)
47
- const positionsIds = route.map(p => p.id)
48
- const events = data.filter(e => positionsIds.includes(e.positionId))
49
- const driverOverSpeedEvents = deviceOverspeedEvents ? deviceOverspeedEvents.alerts.filter(e => positionsIds.includes(e.position.id)) : []
50
- const spentFuel = calculateConsumption(device, { trips, route })
51
-
52
- const geofenceAlarm = events.filter(e => e.attributes.alarm === 'la ceinture').length
53
- const continuesDrivingAlarm = events.filter(e => e.attributes.alarm === 'Conduite continue').length
54
- const reverseAlarm = events.filter(e => e.attributes.alarm === 'MARCHE ARIERRE').length
55
- const otherAlarm = events.length - (geofenceAlarm + continuesDrivingAlarm + reverseAlarm)
56
-
57
- let driverData = driversData.get(d.id)
58
- if (!driverData) {
59
- driverData = {
60
- name: d.name,
61
- distance: 0,
62
- highEngineRPM: 0,
63
- hardBraking: 0,
64
- hardAcceleration: 0,
65
- hardCornering: 0,
66
- overspeed: 0,
67
- spentFuel: 0,
68
- geofenceAlarm: 0,
69
- continuesDrivingAlarm: 0,
70
- reverseAlarm: 0,
71
- otherAlarm: 0
72
- }
73
- driversData.set(d.id, driverData)
74
- }
40
+ const sliced = automaticReports.sliceArray(devices, 5)
75
41
 
76
- let highEngineRPM = 0
77
- let hardBraking = 0
78
- let hardAcceleration = 0
79
- let hardCornering = 0
80
- if (route.find(p => p.attributes.rawXpert)) {
81
- const xpertMessages = route.map(p => p.attributes.rawXpert).filter(a => a)
82
- const canDriverStyle = getCanDriverStyleMessages(xpertMessages).map(m => parseCanDriverStyleMessage(m))
83
- highEngineRPM = canDriverStyle.reduce((a, b) => a + b.totalTimeWithHighRpmAndTorque, 0)
84
- hardBraking = canDriverStyle.reduce((a, b) => a + b.totalNumberOfHarshBrakes, 0)
85
- hardAcceleration = canDriverStyle.reduce((a, b) => a + b.totalNumberOfHarshAccelerations, 0)
86
- } else {
87
- const highEngineRPMSections = calculateRPMSections(route, d.attributes.highRPM || 1500)
88
- highEngineRPM = highEngineRPMSections.map(a => a.length > 1
89
- ? a.map((p, index) => index === 0 ? 0 : new Date(p.fixTime).getTime() - new Date(a[index - 1].fixTime).getTime()).reduce((a, b) => a + b, 0)
90
- : 0).reduce((a, b) => a + b, 0)
91
-
92
- hardBraking = route.filter(p => p.attributes.alarm === 'hardBraking').length
93
- hardAcceleration = route.filter(p => p.attributes.alarm === 'hardAcceleration').length
94
- hardCornering = route.filter(p => p.attributes.alarm === 'hardCornering').length
95
- }
42
+ for (const slice of sliced) {
43
+ const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, true, true, false, false, deviceCount, devices.length)
44
+
45
+ const result = []
46
+ const requests = []
47
+ for (const _chunk of automaticReports.sliceArray(slice.map(d => d.id), 1)) {
48
+ requests.push(traccar.reports.reportsEventsGet(from, to, _chunk, null, ['alarm']))
49
+ }
96
50
 
97
- driverData.distance = driverData.distance + trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
98
- driverData.highEngineRPM = driverData.highEngineRPM + highEngineRPM
99
- driverData.hardBraking = driverData.hardBraking + hardBraking
100
- driverData.hardAcceleration = driverData.hardAcceleration + hardAcceleration
101
- driverData.hardCornering = driverData.hardCornering + hardCornering
102
- driverData.overspeed = driverData.overspeed + driverOverSpeedEvents.length
103
- driverData.spentFuel = driverData.spentFuel + spentFuel
104
- driverData.geofenceAlarm = driverData.geofenceAlarm + geofenceAlarm
105
- driverData.continuesDrivingAlarm = driverData.continuesDrivingAlarm + continuesDrivingAlarm
106
- driverData.reverseAlarm = driverData.reverseAlarm + reverseAlarm
107
- driverData.otherAlarm = driverData.otherAlarm + otherAlarm
51
+ try {
52
+ result.push(...(await Promise.all(requests)))
53
+ } catch (e) {
54
+ console.log(e)
108
55
  }
56
+ const alarmEvents = result.flatMap(d => d.data)
109
57
 
110
- deviceCount++
58
+ for (const device of slice) {
59
+ const route = allInOne.route.filter(t => t.deviceId === device.id)
60
+ const trips = allInOne.trips.filter(t => t.deviceId === device.id)
61
+
62
+ const deviceOverspeedEvents = overspeedEvents.find(e => e.device.id === device.id)
63
+ const deviceAlarmEvents = alarmEvents.filter(e => e.deviceId === device.id)
64
+
65
+ await processDevice(driversData, device, userData, { route, trips }, deviceAlarmEvents, deviceOverspeedEvents)
66
+ deviceCount++
67
+ }
111
68
  }
112
69
 
113
70
  const allDriversData = (Array.from(driversData.values())).filter(d => d.distance > 0)
@@ -164,6 +121,73 @@ async function create (from, to, userData, traccar) {
164
121
  return reportData
165
122
  }
166
123
 
124
+ async function processDevice (driversData, device, userData, allInOne, alarmEvents, overspeedEvents) {
125
+ for (const d of userData.drivers) {
126
+ const { trips, route } = await getDriverData(d, allInOne, userData)
127
+ const positionsIds = route.map(p => p.id)
128
+ const events = alarmEvents.filter(e => positionsIds.includes(e.positionId))
129
+ const driverOverSpeedEvents = overspeedEvents ? overspeedEvents.alerts.filter(e => positionsIds.includes(e.position.id)) : []
130
+ const spentFuel = calculateConsumption(device, { trips, route })
131
+
132
+ const geofenceAlarm = events.filter(e => e.attributes.alarm === 'la ceinture').length
133
+ const continuesDrivingAlarm = events.filter(e => e.attributes.alarm === 'Conduite continue').length
134
+ const reverseAlarm = events.filter(e => e.attributes.alarm === 'MARCHE ARIERRE').length
135
+ const otherAlarm = events.length - (geofenceAlarm + continuesDrivingAlarm + reverseAlarm)
136
+
137
+ let driverData = driversData.get(d.id)
138
+ if (!driverData) {
139
+ driverData = {
140
+ name: d.name,
141
+ distance: 0,
142
+ highEngineRPM: 0,
143
+ hardBraking: 0,
144
+ hardAcceleration: 0,
145
+ hardCornering: 0,
146
+ overspeed: 0,
147
+ spentFuel: 0,
148
+ geofenceAlarm: 0,
149
+ continuesDrivingAlarm: 0,
150
+ reverseAlarm: 0,
151
+ otherAlarm: 0
152
+ }
153
+ driversData.set(d.id, driverData)
154
+ }
155
+
156
+ let highEngineRPM = 0
157
+ let hardBraking = 0
158
+ let hardAcceleration = 0
159
+ let hardCornering = 0
160
+ if (route.find(p => p.attributes.rawXpert)) {
161
+ const xpertMessages = route.map(p => p.attributes.rawXpert).filter(a => a)
162
+ const canDriverStyle = getCanDriverStyleMessages(xpertMessages).map(m => parseCanDriverStyleMessage(m))
163
+ highEngineRPM = canDriverStyle.reduce((a, b) => a + b.totalTimeWithHighRpmAndTorque, 0)
164
+ hardBraking = canDriverStyle.reduce((a, b) => a + b.totalNumberOfHarshBrakes, 0)
165
+ hardAcceleration = canDriverStyle.reduce((a, b) => a + b.totalNumberOfHarshAccelerations, 0)
166
+ } else {
167
+ const highEngineRPMSections = calculateRPMSections(route, d.attributes.highRPM || 1500)
168
+ highEngineRPM = highEngineRPMSections.map(a => a.length > 1
169
+ ? a.map((p, index) => index === 0 ? 0 : new Date(p.fixTime).getTime() - new Date(a[index - 1].fixTime).getTime()).reduce((a, b) => a + b, 0)
170
+ : 0).reduce((a, b) => a + b, 0)
171
+
172
+ hardBraking = route.filter(p => p.attributes.alarm === 'hardBraking').length
173
+ hardAcceleration = route.filter(p => p.attributes.alarm === 'hardAcceleration').length
174
+ hardCornering = route.filter(p => p.attributes.alarm === 'hardCornering').length
175
+ }
176
+
177
+ driverData.distance = driverData.distance + trips.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0)
178
+ driverData.highEngineRPM = driverData.highEngineRPM + highEngineRPM
179
+ driverData.hardBraking = driverData.hardBraking + hardBraking
180
+ driverData.hardAcceleration = driverData.hardAcceleration + hardAcceleration
181
+ driverData.hardCornering = driverData.hardCornering + hardCornering
182
+ driverData.overspeed = driverData.overspeed + driverOverSpeedEvents.length
183
+ driverData.spentFuel = driverData.spentFuel + spentFuel
184
+ driverData.geofenceAlarm = driverData.geofenceAlarm + geofenceAlarm
185
+ driverData.continuesDrivingAlarm = driverData.continuesDrivingAlarm + continuesDrivingAlarm
186
+ driverData.reverseAlarm = driverData.reverseAlarm + reverseAlarm
187
+ driverData.otherAlarm = driverData.otherAlarm + otherAlarm
188
+ }
189
+ }
190
+
167
191
  const fileName = 'DriverRankingReport'
168
192
  async function exportToExcel (userData, reportData) {
169
193
  console.log('Export to Excel')