fleetmap-reports 2.0.217 → 2.0.218

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.217",
3
+ "version": "2.0.218",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/util/route.js CHANGED
@@ -41,19 +41,21 @@ function filterPositions (positions) {
41
41
  return positions.filter(filterPosition)
42
42
  }
43
43
 
44
+ function cleanPosition (p) {
45
+ return {
46
+ id: p.id,
47
+ fixTime: p.fixTime,
48
+ address: p.address,
49
+ latitude: p.latitude,
50
+ longitude: p.longitude,
51
+ deviceId: p.deviceId,
52
+ speed: p.speed,
53
+ valid: p.valid
54
+ }
55
+ }
56
+
44
57
  exports.cleanPositions = (positions) => {
45
- return positions.map(p => {
46
- return {
47
- id: p.id,
48
- fixTime: p.fixTime,
49
- address: p.address,
50
- latitude: p.latitude,
51
- longitude: p.longitude,
52
- deviceId: p.deviceId,
53
- speed: p.speed,
54
- valid: p.valid
55
- }
56
- })
58
+ return positions.map(cleanPosition)
57
59
  }
58
60
 
59
61
  function filterPosition (p) {
@@ -68,3 +70,4 @@ function filterPosition (p) {
68
70
 
69
71
  exports.getIdleEvents = getIdleEvents
70
72
  exports.filterPositions = filterPositions
73
+ exports.cleanPositon = cleanPosition
@@ -69,10 +69,10 @@ async function getSummary (traccar, from, to, devices) {
69
69
  }
70
70
 
71
71
  async function getItems (traccar, u, _chunk, counter, devicesPerRequest, ignorePercentage,
72
- totalDevices, currentDeviceCount, routeOnly) {
72
+ totalDevices, currentDeviceCount, routeOnly, filter) {
73
73
  try {
74
- const x = routeOnly
75
- ? await streamJson(traccar, u)
74
+ const x = routeOnly && !isClientSide()
75
+ ? await streamJson(traccar, u.replace('allinone', 'route'), filter)
76
76
  : await traccar.axios.get(u, {
77
77
  withCredentials: true
78
78
  }).then(r => r.data)
@@ -93,11 +93,12 @@ async function getItems (traccar, u, _chunk, counter, devicesPerRequest, ignoreP
93
93
  }
94
94
  }
95
95
 
96
- function streamJson (traccar, u, _cookie) {
97
- const cookie = _cookie || traccar.axios.defaults.headers?.cookie || traccar.axios.defaults.headers?.common?.cookie
96
+ function streamJson (traccar, u, filter) {
97
+ const cookie = traccar.axios.defaults.headers?.cookie || traccar.axios.defaults.headers?.common?.cookie
98
98
  return piscina.run({
99
99
  url: traccar.axios.defaults.baseURL + u,
100
- cookie
100
+ cookie,
101
+ filter
101
102
  },
102
103
  {
103
104
  filename: path.resolve(__dirname, './traccar.worker.js'),
@@ -135,7 +136,8 @@ async function getAllInOne (
135
136
  console.log(u)
136
137
  requests.push(
137
138
  getItems(traccar, u, _chunk, counter, devicesPerRequest, ignorePercentage, totalDevices, currentDeviceCount,
138
- !getTrips && !getStops && !getSummary))
139
+ !getTrips && !getStops && !getSummary)
140
+ )
139
141
  }
140
142
  const now = new Date()
141
143
  console.log('parallel', requests.length)
@@ -1,11 +1,31 @@
1
- module.exports = async ({ url, cookie }) => {
1
+ const { chain } = require('stream-chain')
2
+ const { parser } = require('stream-json')
3
+ const { pick } = require('stream-json/filters/Pick')
4
+ const { streamArray } = require('stream-json/streamers/StreamArray')
5
+ const { Readable } = require('stream')
6
+ const { cleanPosition } = require('./route')
7
+
8
+ module.exports = async ({ url, cookie, filter }) => {
2
9
  const now = new Date()
3
10
  const response = await fetch(url, {
4
11
  headers: { cookie }
5
12
  })
6
13
  if (!response.ok) {
7
- throw new Error(`Fetch failed: ${response.status} ${response.statusText}`)
14
+ throw new Error(`${cookie} ${url} Fetch failed: ${response.status} ${response.statusText}`)
8
15
  }
9
16
  console.log(url, 'took', new Date() - now, 'ms', 'streaming...')
10
- return response.json()
17
+ const nodeStream = Readable.fromWeb(response.body)
18
+
19
+ const result = { route: [] }
20
+ return new Promise((resolve, reject) => {
21
+ const pipeline = chain([
22
+ nodeStream,
23
+ parser(),
24
+ streamArray() // Stream each item in the array
25
+ ])
26
+ pipeline.on('data', ({ value }) => result.route.push(
27
+ filter ? cleanPosition(value) : value))
28
+ pipeline.on('end', () => resolve(result))
29
+ pipeline.on('error', reject)
30
+ })
11
31
  }
@@ -30,7 +30,7 @@ const fileName = 'ZoneReport'
30
30
  async function process (traccar, from, to, slice, deviceCount, devices, reportRangeDays, userData) {
31
31
  const data = await traccarHelper.getAllInOne(
32
32
  traccar, from, to, slice, true, false, false, false,
33
- deviceCount, devices.length, sliceSize, reportRangeDays > 29 ? 1 : deviceChunk, undefined)
33
+ deviceCount, devices.length, sliceSize, reportRangeDays > 29 ? 1 : deviceChunk, undefined, filterPositions)
34
34
 
35
35
  const route = filterPositions(data.route)
36
36
  const alerts = []