fleetmap-reports 2.0.362 → 2.0.364

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.362",
3
+ "version": "2.0.364",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -57,13 +57,26 @@ function toGrade (score) {
57
57
  return grades.find(g => score >= g.min).grade
58
58
  }
59
59
 
60
+ // Splits [from, to] into consecutive sub-ranges of at most chunkDays each
61
+ function splitDateRange (from, to, chunkDays) {
62
+ const chunkMs = chunkDays * 24 * 60 * 60 * 1000
63
+ const end = new Date(to).getTime()
64
+ const chunks = []
65
+ let chunkStart = new Date(from).getTime()
66
+ while (chunkStart < end) {
67
+ const chunkEnd = Math.min(chunkStart + chunkMs, end)
68
+ chunks.push({ from: new Date(chunkStart), to: new Date(chunkEnd) })
69
+ chunkStart = chunkEnd
70
+ }
71
+ return chunks.length ? chunks : [{ from: new Date(from), to: new Date(to) }]
72
+ }
73
+
60
74
  async function create (from, to, userData, traccar) {
61
75
  const reportData = {
62
76
  drivers: []
63
77
  }
64
78
 
65
79
  userData.geofences = []
66
- userData.roadSpeedLimits = true
67
80
 
68
81
  let deviceIdsByDriver
69
82
  if (!userData.requestToRunServerSide) {
@@ -101,41 +114,49 @@ async function create (from, to, userData, traccar) {
101
114
  }
102
115
 
103
116
  const devices = userData.devices
104
- let deviceCount = 0
105
117
  const driversData = new Map()
106
- const overspeedEvents = await getOverspeedEvents(from, to, userData, devices, userData, traccar)
107
118
 
108
- const sliced = automaticReports.sliceArray(devices, 5)
119
+ // Process the period in chunks so we never hold more than ~a week of raw GPS positions for the
120
+ // whole device set in memory at once - a single busy device can have tens of thousands of
121
+ // positions over a month, which was causing Runtime.OutOfMemory even after narrowing devices/drivers.
122
+ const dateChunks = splitDateRange(from, to, 7)
123
+ const totalDeviceOperations = devices.length * dateChunks.length
124
+ let deviceCount = 0
109
125
 
110
- for (const slice of sliced) {
111
- const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, true, true, false, false, deviceCount, devices.length)
126
+ for (const dateChunk of dateChunks) {
127
+ const overspeedEvents = await getOverspeedEvents(dateChunk.from, dateChunk.to, userData, devices, userData, traccar)
128
+ const sliced = automaticReports.sliceArray(devices, 5)
112
129
 
113
- const result = []
114
- const requests = []
115
- for (const _chunk of automaticReports.sliceArray(slice.map(d => d.id), 1)) {
116
- requests.push(traccar.reports.reportsEventsGet(from, to, _chunk, null, ['alarm']))
117
- }
130
+ for (const slice of sliced) {
131
+ const allInOne = await traccarHelper.getAllInOne(traccar, dateChunk.from, dateChunk.to, slice, true, true, false, false, deviceCount, totalDeviceOperations)
118
132
 
119
- try {
120
- result.push(...(await Promise.all(requests)))
121
- } catch (e) {
122
- console.log(e)
123
- }
124
- const alarmEvents = result.flatMap(d => d.data)
133
+ const result = []
134
+ const requests = []
135
+ for (const _chunk of automaticReports.sliceArray(slice.map(d => d.id), 1)) {
136
+ requests.push(traccar.reports.reportsEventsGet(dateChunk.from, dateChunk.to, _chunk, null, ['alarm']))
137
+ }
125
138
 
126
- for (const device of slice) {
127
- const route = allInOne.route.filter(t => t.deviceId === device.id)
128
- const trips = allInOne.trips.filter(t => t.deviceId === device.id)
139
+ try {
140
+ result.push(...(await Promise.all(requests)))
141
+ } catch (e) {
142
+ console.log(e)
143
+ }
144
+ const alarmEvents = result.flatMap(d => d.data)
129
145
 
130
- const deviceOverspeedEvents = overspeedEvents.find(e => e.device.id === device.id)
131
- const deviceAlarmEvents = alarmEvents.filter(e => e.deviceId === device.id)
146
+ for (const device of slice) {
147
+ const route = allInOne.route.filter(t => t.deviceId === device.id)
148
+ const trips = allInOne.trips.filter(t => t.deviceId === device.id)
132
149
 
133
- await processDevice(driversData, device, userData, { route, trips }, deviceAlarmEvents, deviceOverspeedEvents)
134
- deviceCount++
135
- }
150
+ const deviceOverspeedEvents = overspeedEvents.find(e => e.device.id === device.id)
151
+ const deviceAlarmEvents = alarmEvents.filter(e => e.deviceId === device.id)
136
152
 
137
- console.log('LOADING_MESSAGE:' + slice[0].name)
138
- console.log(`PROGRESS_PERC:${deviceCount / devices.length * 100}`)
153
+ await processDevice(driversData, device, userData, { route, trips }, deviceAlarmEvents, deviceOverspeedEvents)
154
+ deviceCount++
155
+ }
156
+
157
+ console.log('LOADING_MESSAGE:' + slice[0].name)
158
+ console.log(`PROGRESS_PERC:${deviceCount / totalDeviceOperations * 100}`)
159
+ }
139
160
  }
140
161
 
141
162
  const allDriversData = (Array.from(driversData.values())).filter(d => d.distance > 0)