fleetmap-reports 2.0.363 → 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.363",
3
+ "version": "2.0.364",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -57,6 +57,20 @@ 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: []
@@ -100,41 +114,49 @@ async function create (from, to, userData, traccar) {
100
114
  }
101
115
 
102
116
  const devices = userData.devices
103
- let deviceCount = 0
104
117
  const driversData = new Map()
105
- const overspeedEvents = await getOverspeedEvents(from, to, userData, devices, userData, traccar)
106
118
 
107
- 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
108
125
 
109
- for (const slice of sliced) {
110
- 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)
111
129
 
112
- const result = []
113
- const requests = []
114
- for (const _chunk of automaticReports.sliceArray(slice.map(d => d.id), 1)) {
115
- requests.push(traccar.reports.reportsEventsGet(from, to, _chunk, null, ['alarm']))
116
- }
130
+ for (const slice of sliced) {
131
+ const allInOne = await traccarHelper.getAllInOne(traccar, dateChunk.from, dateChunk.to, slice, true, true, false, false, deviceCount, totalDeviceOperations)
117
132
 
118
- try {
119
- result.push(...(await Promise.all(requests)))
120
- } catch (e) {
121
- console.log(e)
122
- }
123
- 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
+ }
124
138
 
125
- for (const device of slice) {
126
- const route = allInOne.route.filter(t => t.deviceId === device.id)
127
- 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)
128
145
 
129
- const deviceOverspeedEvents = overspeedEvents.find(e => e.device.id === device.id)
130
- 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)
131
149
 
132
- await processDevice(driversData, device, userData, { route, trips }, deviceAlarmEvents, deviceOverspeedEvents)
133
- deviceCount++
134
- }
150
+ const deviceOverspeedEvents = overspeedEvents.find(e => e.device.id === device.id)
151
+ const deviceAlarmEvents = alarmEvents.filter(e => e.deviceId === device.id)
135
152
 
136
- console.log('LOADING_MESSAGE:' + slice[0].name)
137
- 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
+ }
138
160
  }
139
161
 
140
162
  const allDriversData = (Array.from(driversData.values())).filter(d => d.distance > 0)