fleetmap-reports 2.0.199 → 2.0.200

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.199",
3
+ "version": "2.0.200",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -23,6 +23,8 @@
23
23
  "jspdf": "^2.5.1",
24
24
  "jspdf-autotable": "3.8.1",
25
25
  "moment": "2.30.1",
26
+ "stream-chain": "^3.4.0",
27
+ "stream-json": "^1.9.1",
26
28
  "traccar-api": "^1.0.5",
27
29
  "wkt": "^0.1.1"
28
30
  },
@@ -37,7 +37,7 @@ 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', e => { console.error(e); reject(e) })
41
41
  worker.on('exit', (code, signal) => {
42
42
  if (code !== 0) {
43
43
  const error = `Worker exited with code ${code} and signal ${signal}`
@@ -1,5 +1,9 @@
1
1
  const automaticReports = require('../automaticReports')
2
2
  const { convertFromUTC, isClientSide, convertFromLocal } = require('./utils')
3
+ const { parser } = require('stream-json')
4
+ const { streamArray } = require('stream-json/streamers/StreamArray')
5
+ const { chain } = require('stream-chain')
6
+ const { pick } = require('stream-json/filters/Pick')
3
7
 
4
8
  async function getRoute (traccar, from, to, devices) {
5
9
  const devicesToSlice = devices.slice()
@@ -66,6 +70,58 @@ async function getSummary (traccar, from, to, devices) {
66
70
  return result.map(r => r.summary).flat()
67
71
  }
68
72
 
73
+ async function getItems (traccar, u, _chunk, counter, devicesPerRequest, ignorePercentage,
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)}`)
90
+ }
91
+ console.log('done devices', _chunk.map(d => d.id), 'position count', x.route && x.route.length)
92
+ return x
93
+ }
94
+
95
+ async function streamJson (traccar, u) {
96
+ console.log('streaming', u)
97
+ const response = await traccar.axios.get(u, {
98
+ responseType: 'stream',
99
+ jar: traccar.cookieJar,
100
+ withCredentials: true,
101
+ timeout: 900000
102
+ })
103
+
104
+ const result = {
105
+ route: []
106
+ }
107
+
108
+ return new Promise((resolve, reject) => {
109
+ const pipeline = chain([
110
+ response.data, // Axios stream (Node readable)
111
+ parser(), // Tokenize
112
+ pick({ filter: 'route' }), // Pick 'route', 'summary', etc.
113
+ streamArray() // Stream each item in the array
114
+ ])
115
+
116
+ pipeline.on('data', ({ value }) => {
117
+ result.route.push(value) // Or stream to a DB/file/processor
118
+ console.log('pushed to', result.route.length)
119
+ })
120
+ pipeline.on('end', () => resolve(result))
121
+ pipeline.on('error', reject)
122
+ })
123
+ }
124
+
69
125
  async function getAllInOne (
70
126
  traccar,
71
127
  from,
@@ -95,36 +151,15 @@ async function getAllInOne (
95
151
  const u = url + '&' + _chunk.map(d => `deviceId=${d.id}`).join('&')
96
152
  console.log(u)
97
153
  requests.push(
98
- traccar.axios.get(u, {
99
- jar: traccar.cookieJar,
100
- withCredentials: true,
101
- timeout: 900000
102
- }).catch(e => {
103
- console.log(e.message, 'try again', u)
104
- return traccar.axios.get(u, {
105
- jar: traccar.cookieJar,
106
- withCredentials: true,
107
- timeout: 900000
108
- })
109
- }).then(r => r.data).then(x => {
110
- console.log('LOADING_MESSAGE:' + _chunk[0].name)
111
- if (counter) {
112
- counter.count += devicesPerRequest
113
- ignorePercentage || console.log(`PROGRESS_PERC:${Math.round(counter.count / totalDevices * 100)}`)
114
- } else {
115
- currentDeviceCount += devicesPerRequest
116
- ignorePercentage || console.log(`PROGRESS_PERC:${Math.round(currentDeviceCount / totalDevices * 100)}`)
117
- }
118
- console.log('done devices', _chunk.map(d => d.id), 'position count:', x.route && x.route.length)
119
- return x
120
- }))
154
+ getItems(traccar, u, _chunk, counter, devicesPerRequest, ignorePercentage, totalDevices, currentDeviceCount,
155
+ !getTrips && !getStops && !getSummary))
121
156
  }
122
157
  const now = new Date()
123
158
  console.log('parallel', requests.length)
124
159
  try {
125
160
  result.push(...(await Promise.all(requests)))
126
161
  } catch (e) {
127
- console.log('parallel', e.message, e.config)
162
+ console.error('parallel', e.message, e.config)
128
163
  }
129
164
  console.log('took', new Date() - now, 'ms')
130
165
  }
@@ -23,10 +23,11 @@ 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 = 3
26
+ const sliceSize = 4
27
27
  const fileName = 'ZoneReport'
28
28
 
29
29
  async function createZoneReport (from, to, userData, traccar) {
30
+ console.log('createZoneReport 200', from, to)
30
31
  const reportData = []
31
32
  const devices = devicesToProcess(userData)
32
33
  const allData = {
@@ -482,7 +483,7 @@ function analyseAlerts (alerts, deviceRoute, userData, from, to, device) {
482
483
  async function getInAndOutEvents (devices, route, userData) {
483
484
  const events = []
484
485
  const geofencesFeatures = userData.geofences.map(g => convertToFeature(g))
485
- console.log('getInAndOutEvents', devices)
486
+ console.log('getInAndOutEvents', devices.length)
486
487
  devices.forEach(d => {
487
488
  const deviceRoute = route.filter(p => p.deviceId === d.id)
488
489
  const routePoints = deviceRoute.sort(sortPositions).map(p => convertPositionToFeature(p))