fleetmap-reports 1.0.424 → 1.0.427

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.424",
3
+ "version": "1.0.427",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.test.js CHANGED
@@ -52,6 +52,21 @@ describe('Test_Reports', function () {
52
52
  assert.equal(device.trips[1].endPOIName, undefined)
53
53
  assert.equal(device.totalDistance, 339204.53999999166) // Total Kms
54
54
  }, 20000)
55
+ // eslint-disable-next-line no-undef
56
+ it('Trip by driver', async () => {
57
+ const report = await getReports()
58
+ const userData = await report.getUserData()
59
+ userData.byDriver = true
60
+ const data = await report.tripReport(new Date(2022, 1, 1, 0, 0, 0, 0),
61
+ new Date(2022, 1, 10, 23, 59, 59, 0),
62
+ userData)
63
+ assert.equal(data.length, 1)
64
+ console.log(data[0].drivers)
65
+ const driver = data[0].drivers.find(d => d.driver.id === 14020)
66
+ assert.equal(driver.trips.length, 9) // Total Trips
67
+ assert.equal(driver.totalDuration, 6274000)
68
+ assert.equal(driver.maxSpeed, 70.1944)
69
+ }, 20000)
55
70
  it('Trip without addresses', async () => {
56
71
  const report = await getReports()
57
72
  const userData = await report.getUserData()
@@ -1,6 +1,6 @@
1
1
  const messages = require('../lang')
2
2
  const jsPDF = require('jspdf')
3
- const { convertMS, convertToLocaleString } = require('./util/utils')
3
+ const { convertMS, convertToLocaleString, convertToFeature, convertPositionToFeature } = require('./util/utils')
4
4
  const { headerFromUser, AmiriRegular } = require('./util/pdfDocument')
5
5
  require('jspdf-autotable')
6
6
  const { getStyle } = require('./reportStyle')
@@ -12,6 +12,8 @@ const { devicesToProcess } = require('./util/device')
12
12
  const { point } = require('@turf/helpers')
13
13
  const distance = require('@turf/distance')
14
14
  const { insideGeofence } = require('./util/geofence')
15
+ const booleanPointInPolygon = require('@turf/boolean-point-in-polygon')
16
+ const automaticReports = require('./automaticReports')
15
17
 
16
18
  const fileName = 'SpeedingReport'
17
19
  const eventTypes = ['deviceOverspeed']
@@ -69,29 +71,24 @@ async function createSpeedingReportByGroup (from, to, userData, traccarInstance)
69
71
  async function createSpeedingReportByDevice (from, to, userData, traccarInstance) {
70
72
  const devices = devicesToProcess(userData)
71
73
 
72
- const allInOne = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, true, false, false, false)
73
- const routes = allInOne.route
74
- const events = []
75
-
76
- if (userData.roadSpeedLimits) {
77
- events.push(...await getHereEvents(devices, routes, userData.maxSpeedThreshold))
78
- } else {
79
- if (!userData.useVehicleSpeedLimit && userData.customSpeed) {
80
- events.push(...await getCustomSpeedLimitEvents(devices, routes, userData.customSpeed))
81
- } else {
82
- events.push(...await traccarHelper.getEvents(traccarInstance, from, to, devices, eventTypes))
83
- }
74
+ const allData = {
75
+ devices: [],
76
+ xpert: devices.filter(d => d && d.attributes && d.attributes.xpert).length > 0
84
77
  }
85
78
 
86
- if (!events.length) {
87
- // empty report
88
- return { devices: [] }
89
- }
79
+ const sliced = automaticReports.sliceArray(devices, 5)
90
80
 
91
- return {
92
- devices: await processDevices(from, to, devices, events, routes, userData),
93
- xpert: devices.filter(d => d && d.attributes && d.attributes.xpert).length > 0
81
+ let deviceCount = 0
82
+ for (const slice of sliced) {
83
+ const { routes, events } = await getEvents(traccarInstance, from, to, slice, userData, deviceCount, devices.length)
84
+ if (events.length > 0) {
85
+ const devicesProcessed = await processDevices(from, to, slice, events, routes, userData)
86
+ allData.devices.push(...devicesProcessed)
87
+ }
88
+ deviceCount = deviceCount + slice.length
94
89
  }
90
+
91
+ return allData
95
92
  }
96
93
 
97
94
  async function createSpeedingReportByDriver (from, to, userData, traccarInstance) {
@@ -103,26 +100,73 @@ async function createSpeedingReportByDriver (from, to, userData, traccarInstance
103
100
  return { drivers: [] }
104
101
  }
105
102
 
106
- const allInOne = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, true, false, false, false)
103
+ const { routes, events } = await getEvents(traccarInstance, from, to, devices, userData)
104
+
105
+ if (!events.length) {
106
+ // empty report
107
+ return { drivers: [] }
108
+ }
109
+
110
+ return { drivers: await processDrivers(from, to, events, routes, userData) }
111
+ }
112
+
113
+ async function getEvents (traccarInstance, from, to, devices, userData, deviceCount, totalDevices) {
114
+ const geofencesFeatures = userData.geofences.filter(g => g.area.startsWith('POLYGON') &&
115
+ g.attributes.speedLimit).map(g => convertToFeature(g))
116
+
117
+ const allInOne = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, true, false, false, false, deviceCount, totalDevices)
107
118
  const routes = allInOne.route
108
119
 
109
120
  const events = []
110
121
  if (!userData.useVehicleSpeedLimit && userData.customSpeed) {
111
122
  events.push(...await getCustomSpeedLimitEvents(devices, routes, userData.customSpeed))
112
123
  } else {
113
- events.push(...await traccarHelper.getEvents(traccarInstance, from, to, devices, eventTypes))
124
+ const traccarEvents = await traccarHelper.getEvents(traccarInstance, from, to, devices, eventTypes)
125
+ if (geofencesFeatures.length) {
126
+ events.push(...getGeofenceSpeedLimitEvents(geofencesFeatures, routes, devices))
127
+ events.push(...traccarEvents.filter(e => !(e.geofenceId)))
128
+ } else {
129
+ events.push(...traccarEvents)
130
+ }
114
131
  }
115
132
 
116
133
  if (userData.roadSpeedLimits) {
117
134
  events.push(...await getHereEvents(devices, routes, userData.maxSpeedThreshold))
118
135
  }
136
+ return { routes, events }
137
+ }
119
138
 
120
- if (!events.length) {
121
- // empty report
122
- return { drivers: [] }
123
- }
139
+ function getGeofenceSpeedLimitEvents (geofencesFeatures, routes, devices) {
140
+ const events = []
124
141
 
125
- return { drivers: await processDrivers(from, to, events, routes, userData) }
142
+ devices.forEach(d => {
143
+ const deviceRoute = routes.filter(p => p.deviceId === d.id)
144
+ const routePoints = deviceRoute.map(p => convertPositionToFeature(p))
145
+ geofencesFeatures.forEach(g => {
146
+ let eventIsActive = false
147
+ routePoints.forEach(p => {
148
+ if (booleanPointInPolygon.default(p, g) && p.properties.position.speed > g.properties.geofence.attributes.speedLimit) {
149
+ if (!eventIsActive) {
150
+ const event = {
151
+ positionId: p.properties.position.id,
152
+ deviceId: d.id,
153
+ geofenceId: g.properties.geofence.id,
154
+ attributes: {
155
+ speedLimit: g.properties.geofence.attributes.speedLimit,
156
+ speed: p.properties.position.speed
157
+ }
158
+ }
159
+ events.push(event)
160
+ }
161
+ eventIsActive = true
162
+ } else {
163
+ eventIsActive = false
164
+ }
165
+ })
166
+ })
167
+ })
168
+
169
+ return events
126
170
  }
127
171
 
128
172
  async function processDrivers (from, to, events, routes, userData) {
@@ -114,11 +114,40 @@ async function createTripReportByDriver (from, to, userData, traccar) {
114
114
  return { drivers: [] }
115
115
  }
116
116
 
117
- const allInOne = await traccarHelper.getAllInOne(traccar, from, to, devices, true, true, false, false)
118
- const tripsData = allInOne.trips
119
- const routeData = allInOne.route
117
+ const allData = {
118
+ drivers: []
119
+ }
120
+
121
+ const sliced = automaticReports.sliceArray(devices, 5)
122
+
123
+ let deviceCount = 0
124
+ for (const slice of sliced) {
125
+ const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, true, true, false, false, deviceCount, devices.length)
126
+ const tripsData = allInOne.trips
127
+ const routeData = allInOne.route
120
128
 
121
- return { drivers: processDrivers(from, to, userData, { trips: tripsData, route: routeData }) }
129
+ const results = processDrivers(from, to, userData, { trips: tripsData, route: routeData })
130
+
131
+ results.forEach(result => {
132
+ const driver = allData.drivers.find(d => d.driver.id === result.driver.id)
133
+ if (driver) {
134
+ driver.trips.push(...result.trips)
135
+ } else {
136
+ allData.drivers.push(result)
137
+ }
138
+ })
139
+
140
+ deviceCount = deviceCount + slice.length
141
+ }
142
+
143
+ allData.drivers.forEach(d => {
144
+ d.totalDuration = d.trips.reduce((a, b) => a + b.duration, 0)
145
+ d.totalDistance = d.trips.reduce((a, b) => a + b.distance, 0)
146
+ d.maxSpeed = d.trips.reduce((a, b) => { return a > b.maxSpeed ? a : b.maxSpeed }, 0)
147
+ d.trips = d.trips.sort((a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime())
148
+ })
149
+
150
+ return allData
122
151
  }
123
152
 
124
153
  function processDevices (from, to, devices, data, userData, traccar) {
@@ -195,8 +224,6 @@ function processDrivers (from, to, userData, data) {
195
224
  const trips = data.trips.filter(t => t.driverUniqueId === d.uniqueId &&
196
225
  (userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, route)))
197
226
 
198
- trips.sort((a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime())
199
-
200
227
  const idleEvents = getIdleEvents(data.route, d)
201
228
 
202
229
  trips.forEach(function (trip, i) {
@@ -211,15 +238,11 @@ function processDrivers (from, to, userData, data) {
211
238
  driver: d,
212
239
  from,
213
240
  to,
214
- totalDuration: trips.reduce((a, b) => a + b.duration, 0),
215
- totalDistance: trips.reduce((a, b) => a + b.distance, 0),
216
- maxSpeed: trips.reduce((a, b) => { return a > b.maxSpeed ? a : b.maxSpeed }, 0),
217
241
  trips
218
242
  }
219
243
  driversResult.push(driverData)
220
244
  }
221
245
  })
222
- console.log(driversResult)
223
246
 
224
247
  return driversResult
225
248
  }