fleetmap-reports 2.0.164 → 2.0.166

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.166",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -18,7 +18,7 @@
18
18
  "country-reverse-geocoding": "^0.2.2",
19
19
  "docx": "^7.3.0",
20
20
  "file-saver": "^2.0.5",
21
- "fleetmap-partners": "1.0.179",
21
+ "fleetmap-partners": "1.0.185",
22
22
  "json-as-xlsx": "2.5.6",
23
23
  "jspdf": "^2.5.1",
24
24
  "jspdf-autotable": "3.8.1",
@@ -1,11 +1,7 @@
1
1
  const traccarHelper = require('../util/traccar')
2
2
  const { isInsideTimetable } = require('../util/trips')
3
- const { convertFromUTCDate, getTranslations, convertMS, convertToLocaleTimeString, isClientSide } = require('../util/utils')
3
+ const { convertFromUTCDate } = require('../util/utils')
4
4
  const automaticReports = require('../automaticReports')
5
- const jsPDF = require('jspdf')
6
- const { getStyle } = require('../reportStyle')
7
- const { getUserPartner } = require('fleetmap-partners')
8
- const { addTable, headerFromUser } = require('../util/pdfDocument')
9
5
 
10
6
  async function createDailyUseReport (from, to, userData, traccar) {
11
7
  const reportData = []
@@ -81,72 +77,4 @@ function processDeviceData (allInOne, d, userData) {
81
77
  }
82
78
  }
83
79
 
84
- async function exportDailyUseReportToPDF (userData, reportData) {
85
- const translations = getTranslations(userData)
86
- const timezone = userData.user.attributes.timezone
87
- const lang = userData.user.attributes.lang || (isClientSide() && navigator.language)
88
-
89
- const headers = [
90
- translations.report.vehicle,
91
- 'Immatriculation',
92
- 'Groupe',
93
- 'Matin départ',
94
- 'Matin arrêt',
95
- 'Matin temps',
96
- 'Déjeuner',
97
- 'Après-midi départ',
98
- 'Après-midi arrêt',
99
- 'Après-midi temps',
100
- 'Total temps',
101
- 'Total conduit',
102
- 'Total arrêts',
103
- 'Total kms'
104
- ]
105
-
106
- const data = []
107
- reportData.forEach(d => {
108
- const row = [
109
- d.device,
110
- d.licensePlate,
111
- d.group,
112
- convertToLocaleTimeString(d.morningStart, lang, timezone, userData.user),
113
- convertToLocaleTimeString(d.morningEnd, lang, timezone, userData.user),
114
- convertMS(d.morningTime),
115
- convertMS(d.lunch),
116
- convertToLocaleTimeString(d.afternoonStart, lang, timezone, userData.user),
117
- convertToLocaleTimeString(d.afternoonEnd, lang, timezone, userData.user),
118
- convertMS(d.afternoonTime),
119
- convertMS(d.totalTime),
120
- convertMS(d.totalDrivingTime),
121
- d.totalStops,
122
- (d.totalDistance / 100).toFixed(1)
123
- ]
124
- data.push(row)
125
- })
126
-
127
- const doc = new jsPDF.jsPDF('l')
128
- await headerFromUser(doc, 'Rapport de Performance', userData.user)
129
- const style = getStyle(getUserPartner(userData.user))
130
-
131
- const footValues = []
132
-
133
- doc.setFontSize(11)
134
- doc.text(new Date(userData.from).toLocaleString() + ' - ' + new Date(userData.to).toLocaleString(), 20, 33)
135
-
136
- style.headerFontSize = 8
137
- style.bodyFontSize = 7
138
- const columnStyles = {}
139
- for (let i = 3; i < 14; i++) {
140
- columnStyles[i] = { halign: 'right' }
141
- }
142
- addTable(doc, headers, data, footValues, style, 40, columnStyles)
143
- return doc
144
- }
145
-
146
- function exportDailyUseReportToExcel (userData, reportData) {
147
-
148
- }
149
-
150
80
  exports.createDailyUseReport = createDailyUseReport
151
- exports.exportDailyUseReportToPDF = exportDailyUseReportToPDF
152
- exports.exportDailyUseReportToExcel = exportDailyUseReportToExcel
@@ -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')
@@ -65,11 +65,10 @@ describe('Kms_Reports', function () {
65
65
  it('KmsReport by timezone marrocos', async () => {
66
66
  const reports = await getReports(process.env.email, process.env.password)
67
67
  const userData = await reports.getUserData()
68
- userData.groupByDay = true
69
68
  userData.devices = userData.devices.filter(d => d.id === 93497)
70
69
  const data = await reports.kmsReport(convertFromLocal(new Date(2024, 2, 18, 0, 0, 0), userData.user.attributes.timezone), convertFromLocal(new Date(2024, 2, 18, 23, 59, 59), userData.user.attributes.timezone),
71
70
  userData)
72
71
  const device = data[0].devices.find(d => d.device.id === 93497)
73
- assert.equal(device.days.length, 1)
72
+ assert.equal(device, 1)
74
73
  }, 3000000)
75
74
  })
@@ -159,21 +159,4 @@ describe('zones', function () {
159
159
  console.log(first)
160
160
  assert.equal(first.days[6].distanceOut, 867.0554430738234)
161
161
  }, 4000000)
162
-
163
- it('works with casais zones in columns 7', async () => {
164
- const report = await getReports()
165
- const userData = await report.getUserData()
166
- userData.zonesByColumn = true
167
- userData.devices = userData.devices.filter(d => d.id === 81166)
168
- userData.geofences = userData.geofences.filter(g => g.name === 'baliza 1 - raio verde ' ||
169
- g.name === 'baliza 2 - raio amarelo')
170
- userData.onlyWithKmsOut = false
171
- const result = await report.zoneReport(
172
- new Date(Date.UTC(2023, 9, 9, 0, 0, 0, 0)),
173
- new Date(Date.UTC(2023, 9, 31, 23, 59, 59, 0)),
174
- userData)
175
- const first = result[0].devices[0]
176
- console.log(first)
177
- assert.equal(first.days[17].distanceOut, 720.7985320478997)
178
- }, 4000000)
179
162
  })
Binary file
@@ -1,46 +0,0 @@
1
- async function createPassengerReport (from, to, userData, traccar) {
2
- //const allDevices = await traccar.axios.get('/devices').then(d => d.data)
3
- const devices = userData.devices
4
- const drivers = await traccar.axios.get('/drivers').then(d => d.data)
5
- const groups = await traccar.axios.get('/groups').then(d => d.data)
6
- await Promise.all(groups.map(async g => {
7
- const driversByGroup = await traccar.axios.get(`/drivers?groupId=${g.id}`).then(d => d.data)
8
- driversByGroup.forEach(d => {
9
- const _driver = drivers.find(a => a.id === d.id)
10
- if (_driver) {
11
- _driver.groupName = g.name
12
- }
13
- })
14
- }))
15
- const eventsUrl = `/reports/events?${devices.map(d => 'deviceId=' + d.id).join('&')
16
- }&from=${from.toISOString()
17
- }&to=${to.toISOString()
18
- }`
19
- const positionsUrl = `/reports/route?${devices.map(d => 'deviceId=' + d.id).join('&')
20
- }&from=${from.toISOString()
21
- }&to=${to.toISOString()
22
- }`
23
- const events = await traccar.axios.get(eventsUrl).then(d => d.data)
24
- const positions = await traccar.axios.get(positionsUrl).then(d => d.data)
25
- events.forEach(e => {
26
- delete e.attributes
27
- const driver = getDriver(e, positions, drivers)
28
- const position = positions.find(p => p.id === e.positionId)
29
- e.deviceName = devices.find(d => d.id === e.deviceId).name
30
- e.groupName = driver.groupName || ''
31
- e.fixtime = position && new Date(position.fixTime)
32
- e.notes = driver && driver.attributes.notes
33
- e.driverName = (driver && driver.name) || (position && position.attributes.driverUniqueId)
34
- })
35
- return events
36
- }
37
-
38
- function getDriver (event, positions, drivers) {
39
- const p = positions.find(p => p.id === event.positionId)
40
- if (!p) { return '' }
41
- const d = drivers.find(d => d.uniqueId === p.attributes.driverUniqueId)
42
- if (!d) { return '' }
43
- return d
44
- }
45
-
46
- exports.createPassengerReport = createPassengerReport
@@ -1,19 +0,0 @@
1
- const { getReports } = require('./index')
2
- const assert = require('assert')
3
- const { createDailyUseReport } = require('../partnerReports/dailyuse-report')
4
-
5
- describe('dailyuse', function () {
6
- // eslint-disable-next-line no-undef
7
- it('dailyuse report', async () => {
8
- const report = await getReports()
9
- const userData = await report.getUserData()
10
-
11
- const data = await createDailyUseReport(
12
- new Date(Date.UTC(2023, 10, 1, 0, 0, 0, 0)),
13
- new Date(Date.UTC(2023, 10, 6, 23, 59, 59, 0)),
14
- userData,
15
- report.traccar)
16
- console.log(data)
17
- assert.equal(data[0].consumption, 19.799999999999997)
18
- }, 8000000)
19
- })
@@ -1,23 +0,0 @@
1
- const { getReports } = require('./index')
2
- const assert = require('assert')
3
- const { convertMS } = require('../util/utils')
4
- const { createGPSJumpReport } = require('../partnerReports/gpsjump-report')
5
-
6
- // eslint-disable-next-line no-undef
7
- describe('gpsjump', function () {
8
- // eslint-disable-next-line no-undef
9
- it('gpsjump report', async () => {
10
- const report = await getReports()
11
- const userData = await report.getUserData()
12
- userData.devices = userData.devices.filter(d => d.id === 22326)
13
- const data = await createGPSJumpReport(
14
- new Date(Date.UTC(2023, 6, 1, 0, 0, 0, 0)),
15
- new Date(Date.UTC(2023, 6, 17, 23, 59, 59, 0)),
16
- userData,
17
- report.traccar)
18
- console.log(data)
19
- assert.equal(data[0].consumption, 19.799999999999997)
20
- assert.equal(data[0].distance, 326.7657)
21
- assert.equal(convertMS(data[1].time * 1000, false), '08:19')
22
- }, 8000000)
23
- })