fleetmap-reports 2.0.196 → 2.0.198

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.
Files changed (2) hide show
  1. package/package.json +5 -3
  2. package/src/util/traccar.js +56 -34
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "2.0.196",
3
+ "version": "2.0.198",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -14,20 +14,22 @@
14
14
  "@turf/distance": "^6.5.0",
15
15
  "@turf/helpers": "^6.5.0",
16
16
  "@turf/point-to-line-distance": "^6.5.0",
17
- "axios": "1.8.2",
17
+ "axios": "1.6.7",
18
18
  "country-reverse-geocoding": "^0.2.2",
19
19
  "docx": "^7.3.0",
20
20
  "file-saver": "^2.0.5",
21
21
  "fleetmap-partners": "1.0.185",
22
22
  "json-as-xlsx": "2.5.6",
23
- "JSONStream": "^1.3.5",
24
23
  "jspdf": "^2.5.1",
25
24
  "jspdf-autotable": "3.8.1",
26
25
  "moment": "2.30.1",
26
+ "stream-chain": "^3.4.0",
27
+ "stream-json": "^1.9.1",
27
28
  "traccar-api": "^1.0.5",
28
29
  "wkt": "^0.1.1"
29
30
  },
30
31
  "devDependencies": {
32
+ "axios-debug-log": "^0.8.4",
31
33
  "eslint": "^8.15.0",
32
34
  "eslint-config-standard": "^17.0.0",
33
35
  "eslint-plugin-import": "^2.26.0",
@@ -1,6 +1,9 @@
1
1
  const automaticReports = require('../automaticReports')
2
2
  const { convertFromUTC, isClientSide, convertFromLocal } = require('./utils')
3
- const { parse } = require('JSONStream')
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')
4
7
 
5
8
  async function getRoute (traccar, from, to, devices) {
6
9
  const devicesToSlice = devices.slice()
@@ -67,6 +70,56 @@ async function getSummary (traccar, from, to, devices) {
67
70
  return result.map(r => r.summary).flat()
68
71
  }
69
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
+ const response = await traccar.axios.get(u, {
97
+ responseType: 'stream',
98
+ jar: traccar.cookieJar,
99
+ withCredentials: true,
100
+ timeout: 900000
101
+ })
102
+
103
+ const result = {
104
+ route: []
105
+ }
106
+
107
+ return new Promise((resolve, reject) => {
108
+ const pipeline = chain([
109
+ response.data, // Axios stream (Node readable)
110
+ parser(), // Tokenize
111
+ pick({ filter: 'route' }), // Pick 'route', 'summary', etc.
112
+ streamArray() // Stream each item in the array
113
+ ])
114
+
115
+ pipeline.on('data', ({ value }) => {
116
+ result.route.push(value) // Or stream to a DB/file/processor
117
+ })
118
+ pipeline.on('end', () => resolve(result))
119
+ pipeline.on('error', reject)
120
+ })
121
+ }
122
+
70
123
  async function getAllInOne (
71
124
  traccar,
72
125
  from,
@@ -96,39 +149,8 @@ async function getAllInOne (
96
149
  const u = url + '&' + _chunk.map(d => `deviceId=${d.id}`).join('&')
97
150
  console.log(u)
98
151
  requests.push(
99
- traccar.axios.get(u, {
100
- jar: traccar.cookieJar,
101
- withCredentials: true,
102
- timeout: 900000,
103
- responseType: 'stream'
104
- }).then(r => {
105
- const stream = r.data
106
- const jsonStream = parse()
107
- stream.pipe(jsonStream)
108
- let responseData = []
109
- jsonStream.on('data', data => {
110
- responseData = data
111
- })
112
- return new Promise((resolve, reject) => {
113
- jsonStream.on('close', data => {
114
- resolve(responseData)
115
- })
116
- jsonStream.on('error', err => {
117
- reject(err)
118
- })
119
- })
120
- }).then(x => {
121
- console.log('LOADING_MESSAGE:' + _chunk[0].name)
122
- if (counter) {
123
- counter.count += devicesPerRequest
124
- ignorePercentage || console.log(`PROGRESS_PERC:${Math.round(counter.count / totalDevices * 100)}`)
125
- } else {
126
- currentDeviceCount += devicesPerRequest
127
- ignorePercentage || console.log(`PROGRESS_PERC:${Math.round(currentDeviceCount / totalDevices * 100)}`)
128
- }
129
- console.log('done devices', _chunk.map(d => d.id), 'position count:', x.route && x.route.length)
130
- return x
131
- }))
152
+ getItems(traccar, u, _chunk, counter, devicesPerRequest, ignorePercentage, totalDevices, currentDeviceCount,
153
+ !getTrips && !getStops && !getSummary))
132
154
  }
133
155
  const now = new Date()
134
156
  console.log('parallel', requests.length)