fleetmap-reports 2.0.370 → 2.0.372

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.370",
3
+ "version": "2.0.372",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -211,11 +211,16 @@ async function runServerSideGrid (from, to, userData, deviceIdsByDriver, traccar
211
211
  const url = `https://${getServerHost()}/pinmeapi/reports/driverranking-report`
212
212
  const rawRows = []
213
213
  let completed = 0
214
- const maxConcurrent = 20
215
-
216
- for (let i = 0; i < cells.length; i += maxConcurrent) {
217
- const batch = cells.slice(i, i + maxConcurrent)
218
- await Promise.all(batch.map(async ({ driverSlice, dateChunk }) => {
214
+ let nextCellIndex = 0
215
+ const maxConcurrent = 10
216
+
217
+ // A worker pool, not fixed-size Promise.all batches: with batches, one slow cell (a device with
218
+ // way more GPS data than its peers) stalls the *entire* next batch behind it, since Promise.all
219
+ // waits for every item before starting the next round. Here, as soon as any worker finishes a
220
+ // cell it immediately grabs the next one, so a slow cell only delays itself.
221
+ async function worker () {
222
+ while (nextCellIndex < cells.length) {
223
+ const { driverSlice, dateChunk } = cells[nextCellIndex++]
219
224
  const deviceIds = new Set()
220
225
  driverSlice.forEach(d => {
221
226
  const ids = deviceIdsByDriver.get(d.uniqueId)
@@ -249,9 +254,12 @@ async function runServerSideGrid (from, to, userData, deviceIdsByDriver, traccar
249
254
  completed++
250
255
  console.log('LOADING_MESSAGE:' + driverSlice[0].name)
251
256
  console.log(`PROGRESS_PERC:${(completed / cells.length) * 100}`)
252
- }))
257
+ }
253
258
  }
254
259
 
260
+ const workerCount = Math.min(maxConcurrent, cells.length)
261
+ await Promise.all(Array.from({ length: workerCount }, () => worker()))
262
+
255
263
  return rawRows
256
264
  }
257
265
 
package/src/util/utils.js CHANGED
@@ -274,9 +274,15 @@ async function executeServerSide (type, sliced, from, to, userData, totalItems,
274
274
  console.log('PROGRESS_PERC:1')
275
275
  const cookie = await axios.get('/pinmeapi/cookie/get', { withCredentials: true }).then(d => d.data)
276
276
  const reportData = []
277
- for (let i = 0; i < sliced.length; i += maxRequests) {
278
- const _sliced = sliced.slice(i, i + maxRequests)
279
- await Promise.all(_sliced.map(async items => {
277
+ let nextIndex = 0
278
+
279
+ // A worker pool, not fixed-size Promise.all batches: with batches, one slow item (e.g. a device
280
+ // with way more data than its peers) stalls the *entire* next batch behind it, since Promise.all
281
+ // waits for every item before starting the next round. Here, as soon as any worker finishes an
282
+ // item it immediately grabs the next one, so a slow item only delays itself.
283
+ async function worker () {
284
+ while (nextIndex < sliced.length) {
285
+ const items = sliced[nextIndex++]
280
286
  const url = `https://${getServerHost()}/pinmeapi/reports/${type}`
281
287
  try {
282
288
  await axios.post(url, {
@@ -309,9 +315,12 @@ async function executeServerSide (type, sliced, from, to, userData, totalItems,
309
315
  console.log('LOADING_MESSAGE:' + items[0].name)
310
316
  }
311
317
  console.log(`PROGRESS_PERC:${itemsCount / totalItems * 100}`)
312
- }))
318
+ }
313
319
  }
314
320
 
321
+ const workerCount = Math.min(maxRequests, sliced.length)
322
+ await Promise.all(Array.from({ length: workerCount }, () => worker()))
323
+
315
324
  if (userData.byDriver) {
316
325
  reportData.sort((a, b) => (((a.driver && a.driver.name) || a.name || '') > ((b.driver && b.driver.name) || b.name || '')) ? 1 : -1)
317
326
  } else {