fleetmap-reports 2.0.138 → 2.0.141

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.138",
3
+ "version": "2.0.141",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -7,48 +7,71 @@ const jsPDF = require('jspdf')
7
7
  const { headerFromUser, addTable } = require('../util/pdfDocument')
8
8
  const { getStyle } = require('../reportStyle')
9
9
  const { getUserPartner } = require('fleetmap-partners')
10
- const { getRoadSpeedLimits } = require('../speeding-report')
10
+ const { getOverspeedEvents } = require('../speeding-report')
11
11
  const { calculateConsumption } = require('../util/fuel')
12
12
  const { getCanAvgConsumption } = require('../fuel-consumption-report')
13
13
 
14
14
  const distanceTarget = 10000 // meters
15
- const distanceCoefficient = 0.9
15
+ const distanceCoefficient = 0.5
16
16
 
17
17
  const consumptionTarget = 11.5 // minutes
18
- const consumptionCoefficient = 0.13
18
+ const consumptionCoefficient = 0.7
19
19
 
20
20
  const rpmTarget = 60 // minutes
21
- const rpmCoefficient = 0.13
21
+ const rpmCoefficient = 0.8
22
22
 
23
23
  const hardBrakingTarget = 1
24
- const hardBrakingCoefficient = 0.15
24
+ const hardBrakingCoefficient = 0.10
25
25
 
26
26
  const hardAccelerationTarget = 1
27
- const hardAccelerationCoefficient = 0.15
27
+ const hardAccelerationCoefficient = 0.10
28
28
 
29
29
  const hardCorneringTarget = 1
30
- const hardCorneringCoefficient = 0.15
30
+ const hardCorneringCoefficient = 0.10
31
31
 
32
32
  const overspeedTarget = 2
33
- const overspeedCoefficient = 0.2
33
+ const overspeedCoefficient = 0.15
34
+
35
+ const geofenceTarget = 1
36
+ const geofenceCoefficient = 0.15
37
+
38
+ const continuesDrivingTarget = 1
39
+ const continuesDrivingCoefficient = 0.1
40
+
41
+ const reverseTarget = 1
42
+ const reverseCoefficient = 0.1
43
+
44
+ const otherTarget = 1
45
+ const otherCoefficient = 0.1
34
46
 
35
47
  async function create (from, to, userData, traccar) {
36
48
  const reportData = []
37
49
 
38
50
  const drivers = userData.drivers
39
51
  const devices = userData.devices
52
+ userData.geofences = []
53
+ userData.roadSpeedLimits = true
40
54
 
41
55
  let deviceCount = 0
42
56
  const driversData = new Map()
57
+ const overspeedEvents = await getOverspeedEvents(from, to, userData, devices, userData, traccar)
58
+
43
59
  for (const device of devices) {
44
60
  const allInOne = await traccarHelper.getAllInOne(traccar, from, to, [device], true, true, false, false, deviceCount, devices.length)
45
- const hereResults = await getRoadSpeedLimits([device], allInOne.route, 5)
61
+ const deviceOverspeedEvents = overspeedEvents.find(e => e.device.id === device.id)
62
+ const { data } = await traccar.reports.reportsEventsGet(from, to, [device.id], null, ['alarm'])
46
63
 
47
64
  for (const d of drivers) {
48
65
  const { trips, route } = await getDriverData(d, allInOne, userData)
49
- const positionIds = route.map(p => p.id)
50
- const driverOverSpeedEvents = hereResults.filter(e => positionIds.includes(e.positionId))
51
- const spentFuel = calculateConsumption(d, { trips, route })
66
+ const positionsIds = route.map(p => p.id)
67
+ const events = data.filter(e => positionsIds.includes(e.positionId))
68
+ const driverOverSpeedEvents = deviceOverspeedEvents ? deviceOverspeedEvents.alerts.filter(e => positionsIds.includes(e.position.id)) : []
69
+ const spentFuel = calculateConsumption(device, { trips, route })
70
+
71
+ const geofenceAlarm = events.filter(e => e.attributes.alarm === 'la ceinture').length
72
+ const continuesDrivingAlarm = events.filter(e => e.attributes.alarm === 'Conduite continue').length
73
+ const reverseAlarm = events.filter(e => e.attributes.alarm === 'MARCHE ARIERRE').length
74
+ const otherAlarm = events.length - (geofenceAlarm + continuesDrivingAlarm + reverseAlarm)
52
75
 
53
76
  let driverData = driversData.get(d.id)
54
77
  if (!driverData) {
@@ -60,7 +83,11 @@ async function create (from, to, userData, traccar) {
60
83
  hardAcceleration: 0,
61
84
  hardCornering: 0,
62
85
  overspeed: 0,
63
- spentFuel: 0
86
+ spentFuel: 0,
87
+ geofenceAlarm: 0,
88
+ continuesDrivingAlarm: 0,
89
+ reverseAlarm: 0,
90
+ otherAlarm: 0
64
91
  }
65
92
  driversData.set(d.id, driverData)
66
93
  }
@@ -93,6 +120,10 @@ async function create (from, to, userData, traccar) {
93
120
  driverData.hardCornering = driverData.hardCornering + hardCornering
94
121
  driverData.overspeed = driverData.overspeed + driverOverSpeedEvents.length
95
122
  driverData.spentFuel = driverData.spentFuel + spentFuel
123
+ driverData.geofenceAlarm = driverData.geofenceAlarm + geofenceAlarm
124
+ driverData.continuesDrivingAlarm = driverData.continuesDrivingAlarm + continuesDrivingAlarm
125
+ driverData.reverseAlarm = driverData.reverseAlarm + reverseAlarm
126
+ driverData.otherAlarm = driverData.otherAlarm + otherAlarm
96
127
  }
97
128
 
98
129
  deviceCount++
@@ -107,14 +138,34 @@ async function create (from, to, userData, traccar) {
107
138
  const hardCorneringScore = (d.hardCornering / hardCorneringTarget) * hardCorneringCoefficient
108
139
  const overspeedScore = (d.overspeed / overspeedTarget) * overspeedCoefficient
109
140
  const consumptionScore = (getCanAvgConsumption(d.distance, d.spentFuel).byKms / consumptionTarget) * consumptionCoefficient
141
+ const geofenceAlarmScore = (d.geofenceAlarm / geofenceTarget) * geofenceCoefficient
142
+ const continuesDrivingAlarmScore = (d.continuesDrivingAlarm / continuesDrivingTarget) * continuesDrivingCoefficient
143
+ const reverseAlarmScore = (d.reverseAlarm / reverseTarget) * reverseCoefficient
144
+ const otherAlarmScore = (d.otherAlarm / otherTarget) * otherCoefficient
110
145
 
111
146
  d.avgConsumption = getCanAvgConsumption(d.distance, d.spentFuel).byKms
112
- d.score = distanceScore + rpmScore + hardBrakingScore + hardAccelerationScore + hardCorneringScore + consumptionScore + overspeedScore
147
+ d.score = distanceScore + rpmScore + hardBrakingScore + hardAccelerationScore +
148
+ hardCorneringScore + consumptionScore + overspeedScore + geofenceAlarmScore +
149
+ continuesDrivingAlarmScore + reverseAlarmScore + otherAlarmScore
113
150
  })
114
151
  allDriversData.sort((a, b) => (a.score > b.score) ? 1 : -1)
115
152
 
116
153
  reportData.push(...allDriversData)
117
154
 
155
+ reportData.targetValues = {
156
+ distanceTarget,
157
+ rpmTarget,
158
+ hardBrakingTarget,
159
+ hardAccelerationTarget,
160
+ hardCorneringTarget,
161
+ consumptionTarget,
162
+ overspeedTarget,
163
+ geofenceTarget,
164
+ continuesDrivingTarget,
165
+ reverseTarget,
166
+ otherCoefficient
167
+ }
168
+
118
169
  return reportData
119
170
  }
120
171
 
@@ -79,20 +79,13 @@ function processServerSide () {
79
79
  return (process.env.PROCESS_HERE_EVENTS && process.env.PROCESS_HERE_EVENTS === 'server')
80
80
  }
81
81
 
82
- async function createSpeedingReportByDevice (from, to, userData, traccarInstance) {
83
- const devices = devicesToProcess(userData)
84
-
85
- const allData = {
86
- devices: [],
87
- xpert: devices.filter(d => d && d.attributes && d.attributes.xpert).length > 0
88
- }
89
-
82
+ async function getOverspeedEvents (from, to, userData, devices, allData, traccarInstance) {
90
83
  const _sliced = automaticReports.sliceArray(automaticReports.sliceArray(devices, 5), 5)
91
-
92
84
  let deviceCount = 0
85
+ const overspeedEvents = []
93
86
  for (const sliced of _sliced) {
94
87
  await Promise.all(sliced.map(async slice => {
95
- allData.devices = allData.devices.concat(await getEvents(traccarInstance, from, to, slice, userData, deviceCount, devices.length, slice.length))
88
+ overspeedEvents.push(...await getEvents(traccarInstance, from, to, slice, userData, deviceCount, devices.length, slice.length))
96
89
  deviceCount = deviceCount + slice.length
97
90
  if (processServerSide()) {
98
91
  console.log('LOADING_MESSAGE:' + slice[0].name)
@@ -100,6 +93,18 @@ async function createSpeedingReportByDevice (from, to, userData, traccarInstance
100
93
  }
101
94
  }))
102
95
  }
96
+ return overspeedEvents
97
+ }
98
+
99
+ async function createSpeedingReportByDevice (from, to, userData, traccarInstance) {
100
+ const devices = devicesToProcess(userData)
101
+
102
+ const allData = {
103
+ devices: [],
104
+ xpert: devices.filter(d => d && d.attributes && d.attributes.xpert).length > 0
105
+ }
106
+
107
+ allData.devices = await getOverspeedEvents(from, to, userData, devices, allData, traccarInstance)
103
108
 
104
109
  return allData
105
110
  }
@@ -351,7 +356,7 @@ async function invokeValhalla (route, i, chunk, country, threshold, results, ret
351
356
  lon: p.longitude,
352
357
  lat: p.latitude
353
358
  }))
354
- }, { timeout: 10000 })
359
+ }, { timeout: 50000 })
355
360
  .then(r => r.data)
356
361
  countSuccess++
357
362
  lastSuccessDuration = new Date() - now
@@ -703,4 +708,4 @@ exports.exportSpeedingReportToExcel = exportSpeedingReportToExcel
703
708
  exports.create = createSpeedingReport
704
709
  exports.exportToPDF = exportSpeedingReportToPDF
705
710
  exports.exportToExcel = exportSpeedingReportToExcel
706
- exports.getEvents = getEvents
711
+ exports.getOverspeedEvents = getOverspeedEvents
package/src/util/utils.js CHANGED
@@ -320,7 +320,7 @@ exports.convertFromUTCDate = (value, user) => {
320
320
  return convertFromUTC(value, user.attributes.timezone || getUserPartner(user).timezone)
321
321
  }
322
322
 
323
- exports.getCountry = position => position && position.attributes.address && position.attributes.address.split(',').slice(-1)[0].trim()
323
+ exports.getCountry = position => position && position.address && position.address.split(',').slice(-1)[0].trim()
324
324
 
325
325
  exports.processServerSide = (userData) => {
326
326
  const devices = devicesToProcess(userData)