fleetmap-reports 2.0.360 → 2.0.362

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.360",
3
+ "version": "2.0.362",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -65,17 +65,31 @@ async function create (from, to, userData, traccar) {
65
65
  userData.geofences = []
66
66
  userData.roadSpeedLimits = true
67
67
 
68
+ let deviceIdsByDriver
68
69
  if (!userData.requestToRunServerSide) {
69
- const { devices, drivers } = await devicesByDriver(traccar, from, to, userData.drivers, userData.devices)
70
- userData.devices = devices
71
- userData.drivers = drivers
70
+ const result = await devicesByDriver(traccar, from, to, userData.drivers, userData.devices)
71
+ userData.devices = result.devices
72
+ userData.drivers = result.drivers
73
+ deviceIdsByDriver = result.deviceIdsByDriver
72
74
  }
73
75
 
74
76
  if (isClientSide() && (userData.drivers.length > 50 || ((new Date(to).getTime() - new Date(from).getTime()) > (1000 * 60 * 60 * 24 * 15)))) {
75
77
  const sliced = automaticReports.sliceArray(userData.drivers, 5)
76
78
  userData.requestToRunServerSide = true
77
79
  userData.byDriver = true
78
- const allDriversData = await executeServerSide('driverranking-report', sliced, from, to, userData, userData.drivers.length, traccar, 5)
80
+ // Narrow the devices sent per-slice to just the ones those drivers actually used, so a single
81
+ // server invocation doesn't have to hold the whole fleet's route data in memory at once (was
82
+ // causing Runtime.OutOfMemory on large fleets).
83
+ const allDriversData = await executeServerSide('driverranking-report', sliced, from, to, userData, userData.drivers.length, traccar, 5, (driversSlice) => {
84
+ const deviceIds = new Set()
85
+ driversSlice.forEach(d => {
86
+ const ids = deviceIdsByDriver.get(d.uniqueId)
87
+ if (ids) {
88
+ ids.forEach(id => deviceIds.add(id))
89
+ }
90
+ })
91
+ return { devices: userData.devices.filter(device => deviceIds.has(device.id)) }
92
+ })
79
93
 
80
94
  allDriversData.sort((a, b) => b.overallScore - a.overallScore)
81
95
  allDriversData.forEach((d, index) => { d.rank = index + 1 })
@@ -12,7 +12,16 @@ async function devicesByDriver (traccarInstance, from, to, drivers, devices) {
12
12
  const deviceIdsToProcess = new Set(eventsData.map(e => e.deviceId))
13
13
  const devicesToProcess = devices.filter(d => deviceIdsToProcess.has(d.id))
14
14
 
15
- return { devices: devicesToProcess, drivers: driversToProcess }
15
+ const deviceIdsByDriver = new Map()
16
+ eventsData.forEach(e => {
17
+ const driverUniqueId = e.attributes.driverUniqueId
18
+ if (!deviceIdsByDriver.has(driverUniqueId)) {
19
+ deviceIdsByDriver.set(driverUniqueId, new Set())
20
+ }
21
+ deviceIdsByDriver.get(driverUniqueId).add(e.deviceId)
22
+ })
23
+
24
+ return { devices: devicesToProcess, drivers: driversToProcess, deviceIdsByDriver }
16
25
  }
17
26
 
18
27
  async function reportByDriver (devices, traccar, from, to, userData, processor, allInOneData = { route: true, trips: true, stops: true, summary: false }) {
package/src/util/utils.js CHANGED
@@ -250,7 +250,7 @@ function weekDaySelected (date, weekDays) {
250
250
  (date.getDay() === 6 && weekDays.saturday))
251
251
  }
252
252
 
253
- async function executeServerSide (type, sliced, from, to, userData, totalItems, traccar, maxRequests = 10) {
253
+ async function executeServerSide (type, sliced, from, to, userData, totalItems, traccar, maxRequests = 10, getExtraUserData) {
254
254
  let itemsCount = 0
255
255
  console.log('PROGRESS_PERC:1')
256
256
  const cookie = await axios.get('/pinmeapi/cookie/get', { withCredentials: true }).then(d => d.data)
@@ -267,7 +267,8 @@ async function executeServerSide (type, sliced, from, to, userData, totalItems,
267
267
  ? {
268
268
  ...userData,
269
269
  drivers: items,
270
- geofences: []
270
+ geofences: [],
271
+ ...(getExtraUserData ? getExtraUserData(items) : {})
271
272
  }
272
273
  : {
273
274
  ...userData,
@@ -293,9 +294,9 @@ async function executeServerSide (type, sliced, from, to, userData, totalItems,
293
294
  }
294
295
 
295
296
  if (userData.byDriver) {
296
- reportData.sort((a, b) => (a.driver.name > b.driver.name) ? 1 : -1)
297
+ reportData.sort((a, b) => (((a.driver && a.driver.name) || a.name || '') > ((b.driver && b.driver.name) || b.name || '')) ? 1 : -1)
297
298
  } else {
298
- reportData.sort((a, b) => (a.device.name > b.device.name) ? 1 : -1)
299
+ reportData.sort((a, b) => (((a.device && a.device.name) || a.name || '') > ((b.device && b.device.name) || b.name || '')) ? 1 : -1)
299
300
  }
300
301
 
301
302
  return reportData