fleetmap-reports 2.0.203 → 2.0.205

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.203",
3
+ "version": "2.0.205",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -23,7 +23,7 @@ exports.parallel = (report, method, toSlice, ...args) => {
23
23
  reject(new Error('got empty slice'))
24
24
  }
25
25
  try {
26
- console.log('forking worker', workerCount++, report, method, slice.length)
26
+ console.log('forking worker', workerCount++, report, method, slice)
27
27
  const worker = cluster.fork()
28
28
  worker.send({
29
29
  report,
@@ -37,12 +37,10 @@ exports.parallel = (report, method, toSlice, ...args) => {
37
37
  resolve(_result.flat())
38
38
  }
39
39
  })
40
- worker.on('error', e => { console.error(e); reject(e) })
40
+ worker.on('error', reject)
41
41
  worker.on('exit', (code, signal) => {
42
42
  if (code !== 0) {
43
- const error = `Worker exited with code ${code} and signal ${signal}`
44
- console.error(error)
45
- reject(new Error(error))
43
+ reject(new Error(`Worker exited with code ${code} and signal ${signal}`))
46
44
  }
47
45
  })
48
46
  } catch (e) {
@@ -72,24 +72,28 @@ async function getSummary (traccar, from, to, devices) {
72
72
 
73
73
  async function getItems (traccar, u, _chunk, counter, devicesPerRequest, ignorePercentage,
74
74
  totalDevices, currentDeviceCount, routeOnly) {
75
- const x = routeOnly
76
- ? await streamJson(traccar, u)
77
- : await traccar.axios.get(u, {
78
- jar: traccar.cookieJar,
79
- withCredentials: true,
80
- timeout: 900000
81
- }).then(r => r.data)
82
-
83
- console.log('LOADING_MESSAGE:' + _chunk[0].name)
84
- if (counter) {
85
- counter.count += devicesPerRequest
86
- ignorePercentage || console.log(`PROGRESS_PERC:${Math.round(counter.count / totalDevices * 100)}`)
87
- } else {
88
- currentDeviceCount += devicesPerRequest
89
- ignorePercentage || console.log(`PROGRESS_PERC:${Math.round(currentDeviceCount / totalDevices * 100)}`)
75
+ try {
76
+ const x = routeOnly
77
+ ? await streamJson(traccar, u)
78
+ : await traccar.axios.get(u, {
79
+ jar: traccar.cookieJar,
80
+ withCredentials: true
81
+ }).then(r => r.data)
82
+
83
+ console.log('LOADING_MESSAGE:' + _chunk[0].name)
84
+ if (counter) {
85
+ counter.count += devicesPerRequest
86
+ ignorePercentage || console.log(`PROGRESS_PERC:${Math.round(counter.count / totalDevices * 100)}`)
87
+ } else {
88
+ currentDeviceCount += devicesPerRequest
89
+ ignorePercentage || console.log(`PROGRESS_PERC:${Math.round(currentDeviceCount / totalDevices * 100)}`)
90
+ }
91
+ console.log('done devices', _chunk.map(d => d.id), 'position count', x.route && x.route.length)
92
+ return x
93
+ } catch (e) {
94
+ console.error(e)
95
+ return {}
90
96
  }
91
- console.log('done devices', _chunk.map(d => d.id), 'position count', x.route && x.route.length)
92
- return x
93
97
  }
94
98
 
95
99
  async function streamJson (traccar, u) {
@@ -97,27 +101,19 @@ async function streamJson (traccar, u) {
97
101
  const response = await traccar.axios.get(u, {
98
102
  responseType: 'stream',
99
103
  jar: traccar.cookieJar,
100
- withCredentials: true,
101
- timeout: 900000
104
+ withCredentials: true
102
105
  })
103
106
 
104
- const result = {
105
- route: []
106
- }
107
+ const result = { route: [] }
107
108
 
108
- console.log('waiting for data')
109
109
  return new Promise((resolve, reject) => {
110
110
  const pipeline = chain([
111
- response.data, // Axios stream (Node readable)
112
- parser(), // Tokenize
113
- pick({ filter: 'route' }), // Pick 'route', 'summary', etc.
111
+ response.data,
112
+ parser(),
113
+ pick({ filter: 'route' }),
114
114
  streamArray() // Stream each item in the array
115
115
  ])
116
-
117
- pipeline.on('data', ({ value }) => {
118
- result.route.push(value) // Or stream to a DB/file/processor
119
- console.log('pushed to', result.route.length)
120
- })
116
+ pipeline.on('data', ({ value }) => result.route.push(value))
121
117
  pipeline.on('end', () => resolve(result))
122
118
  pipeline.on('error', reject)
123
119
  })
@@ -23,11 +23,12 @@ const { checkGeofenceIn } = require('./util/geofence')
23
23
  const { parallel } = require('./util/parallel')
24
24
  const { getDataByDay } = require('./util/trips')
25
25
  const axios = require('axios').default
26
- const sliceSize = 4
26
+ const sliceSize = 10
27
+ const deviceChunk = 5
27
28
  const fileName = 'ZoneReport'
28
29
 
29
30
  async function createZoneReport (from, to, userData, traccar) {
30
- console.log('createZoneReport 200', from, to)
31
+ console.log('createZoneReport', from, to)
31
32
  const reportData = []
32
33
  const devices = devicesToProcess(userData)
33
34
  const allData = {
@@ -66,14 +67,13 @@ async function createZoneReport (from, to, userData, traccar) {
66
67
  }
67
68
 
68
69
  const reportRangeDays = (new Date(to).getTime() - new Date(from).getTime()) / (1000 * 60 * 60 * 24)
69
- const devicesPerRequest = reportRangeDays > 28 ? 1 : sliceSize
70
- const sliced = automaticReports.sliceArray(devices, devicesPerRequest)
70
+ const sliced = automaticReports.sliceArray(devices, sliceSize)
71
71
 
72
72
  let deviceCount = 0
73
73
  for (const slice of sliced) {
74
74
  const data = await traccarHelper.getAllInOne(
75
75
  traccar, from, to, slice, true, false, false, false,
76
- deviceCount, devices.length, sliceSize, devicesPerRequest, undefined)
76
+ deviceCount, devices.length, sliceSize, reportRangeDays > 29 ? 1 : deviceChunk, undefined)
77
77
 
78
78
  const route = filterPositions(data.route)
79
79
  const alerts = []