fleetmap-reports 1.0.738 → 1.0.740

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": "1.0.738",
3
+ "version": "1.0.740",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1,6 +1,10 @@
1
1
  const moment = require('moment')
2
2
 
3
- function sliceArray (longArray, size = 1) {
3
+ const maxParallelRequests = 25
4
+ function sliceArray (longArray, size) {
5
+ if (!size) {
6
+ size = (longArray.length / maxParallelRequests) + 1
7
+ }
4
8
  const arrayToSlice = longArray.slice()
5
9
 
6
10
  const arrayOfArrays = []
@@ -60,3 +64,5 @@ exports.deviceWithFuelInfo = deviceWithFuelInfo
60
64
  exports.calculateSpentFuel = calculateSpentFuel
61
65
  exports.calculateXpertSpentFuel = calculateXpertSpentFuel
62
66
  exports.calculateXpertDistance = calculateXpertDistance
67
+ exports.maxParallelRequests = maxParallelRequests
68
+
@@ -8,6 +8,7 @@ const { convertToLocaleString, getTranslations } = require('./util/utils')
8
8
  const { devicesToProcess } = require('./util/device')
9
9
  const { isInside } = require('./util/timetable')
10
10
  const { getDigitalPortValue } = require('./location-report')
11
+ const { maxParallelRequests } = require('./automaticReports')
11
12
 
12
13
  const fileName = 'EventReport'
13
14
 
@@ -41,7 +42,7 @@ async function createEventsReport (from, to, userData, traccar) {
41
42
 
42
43
  const devices = devicesToProcess(userData)
43
44
 
44
- const data = await getReportData(from, to, devices, userData.eventTypes, traccar)
45
+ const data = await getReportData(from, to, devices, userData.eventTypes || [], traccar)
45
46
 
46
47
  if (data.length > 0) {
47
48
  reportData.push({
@@ -73,45 +74,47 @@ async function getReportData (from, to, devices, types, traccar) {
73
74
  async function processDevices (from, to, devices, data, traccar, userData) {
74
75
  const devicesResult = []
75
76
  let i = 0
76
- await Promise.all(devices.map(async d => {
77
- const deviceAlerts = data.filter(t => t.deviceId === d.id)
78
-
79
- const response = await traccar.reports.reportsRouteGet(from, to, [d.id])
80
- const positions = response.data
81
-
82
- for (const a of deviceAlerts) {
83
- a.position = positions.find(p => p.id === a.positionId)
84
-
85
- if (a.geofenceId) {
86
- const geofence = userData.geofences.find(g => g.id === a.geofenceId)
87
- if (geofence) {
88
- if (a.type === 'deviceOverspeed') {
89
- a.geofenceName = geofence.name + ' (' + Math.round(a.attributes.speedLimit * 1.85200) + ' Km/h)'
90
- } else {
91
- a.geofenceName = geofence.name
77
+ for (let j = 0; j < devices.length; j += maxParallelRequests) {
78
+ await Promise.all(devices.slice(j, j + maxParallelRequests).map(async d => {
79
+ const deviceAlerts = data.filter(t => t.deviceId === d.id)
80
+
81
+ const response = await traccar.reports.reportsRouteGet(from, to, [d.id])
82
+ const positions = response.data
83
+
84
+ for (const a of deviceAlerts) {
85
+ a.position = positions.find(p => p.id === a.positionId)
86
+
87
+ if (a.geofenceId) {
88
+ const geofence = userData.geofences.find(g => g.id === a.geofenceId)
89
+ if (geofence) {
90
+ if (a.type === 'deviceOverspeed') {
91
+ a.geofenceName = geofence.name + ' (' + Math.round(a.attributes.speedLimit * 1.85200) + ' Km/h)'
92
+ } else {
93
+ a.geofenceName = geofence.name
94
+ }
92
95
  }
93
96
  }
94
- }
95
97
 
96
- if (a.position && a.position.attributes.driverUniqueId) {
97
- const driver = userData.drivers.find(d => d.uniqueId === a.position.attributes.driverUniqueId)
98
- a.driver = driver && driver.name
98
+ if (a.position && a.position.attributes.driverUniqueId) {
99
+ const driver = userData.drivers.find(d => d.uniqueId === a.position.attributes.driverUniqueId)
100
+ a.driver = driver && driver.name
101
+ }
99
102
  }
100
- }
101
103
 
102
- const alerts = deviceAlerts.filter(a => userData.allWeek || !userData.weekDays || (!a.position || isInside(a.position.fixTime, a.position.fixTime, userData)))
103
- console.log('LOADING_MESSAGE:' + d.name)
104
- console.log(`PROGRESS_PERC:${++i / devices.length * 100}`)
105
-
106
- if (alerts.length > 0) {
107
- devicesResult.push({
108
- device: d,
109
- from,
110
- to,
111
- alerts
112
- })
113
- }
114
- }))
104
+ const alerts = deviceAlerts.filter(a => userData.allWeek || !userData.weekDays || (!a.position || isInside(a.position.fixTime, a.position.fixTime, userData)))
105
+ console.log('LOADING_MESSAGE:' + d.name)
106
+ console.log(`PROGRESS_PERC:${++i / devices.length * 100}`)
107
+
108
+ if (alerts.length > 0) {
109
+ devicesResult.push({
110
+ device: d,
111
+ from,
112
+ to,
113
+ alerts
114
+ })
115
+ }
116
+ }))
117
+ }
115
118
 
116
119
  return devicesResult
117
120
  }
@@ -0,0 +1,15 @@
1
+ const { getReports } = require('./index')
2
+ // eslint-disable-next-line no-undef
3
+ describe('events', function () {
4
+ // eslint-disable-next-line no-undef
5
+ it('works with many devices', async () => {
6
+ const report = await getReports(process.env.USER_NOGARTEL, process.env.PASS_NOGARTEL)
7
+ const userData = await report.getUserData()
8
+ console.log(userData.devices.length)
9
+ const result = await report.eventsReport(
10
+ new Date(Date.UTC(2023, 5, 11, 0, 0, 0, 0)),
11
+ new Date(Date.UTC(2023, 5, 11, 23, 59, 59, 0)),
12
+ userData)
13
+ console.log('result', result)
14
+ }, 4000000)
15
+ })
@@ -2,7 +2,6 @@ const { getReports } = require('./index')
2
2
  const { createPerformanceReport } = require('../partnerReports/performance-report')
3
3
  // eslint-disable-next-line no-undef
4
4
  describe('performance', function () {
5
- this.timeout(500000)
6
5
  // eslint-disable-next-line no-undef
7
6
  it('performance report', async () => {
8
7
  const report = await getReports()
@@ -2,7 +2,6 @@ const { getReports } = require('./index')
2
2
  const assert = require('assert')
3
3
  // eslint-disable-next-line no-undef
4
4
  describe('zones', function () {
5
- this.timeout(500000)
6
5
  // eslint-disable-next-line no-undef
7
6
  it('works with ellca', async () => {
8
7
  const report = await getReports()
@@ -48,25 +47,4 @@ describe('zones', function () {
48
47
  assert.equal(first.inTime.fixTime, '2023-01-03T21:23:24.000+0000')
49
48
  assert.equal(first.outTime.fixTime, '2023-01-07T12:22:53.000+0000')
50
49
  }, 4000000)
51
-
52
- // eslint-disable-next-line no-undef
53
- it('works with casais 2', async () => {
54
- const report = await getReports('mario.andre.moreira@casais.gi', 'Casais.23')
55
- const userData = await report.getUserData()
56
- userData.groupByDay = true
57
- userData.devices = userData.devices.filter(d => d.name === 'G-2542-F Fiat Tipo')
58
- userData.geofences = userData.geofences.filter(g => g.name === 'baliza raio verde ')
59
- assert.equal(userData.geofences.length > 0, true)
60
- assert.equal(userData.devices.length > 0, true)
61
- const result = await report.zoneReport(
62
- new Date(2023, 1, 27, 0, 0, 0, 0),
63
- new Date(2023, 1, 27, 23, 59, 59, 0),
64
- userData)
65
- assert.equal(result[0].devices[0].geofences.length, 1)
66
- assert.equal(result[0].devices[0].geofences[0].days.length, 1)
67
- const first = result[0].devices[0].geofences[0].days[0]
68
- assert.equal(first.firstIn, '2023-02-27T21:53:16.000+0000')
69
- assert.equal(first.distanceIn, 93.03917199839687)
70
- assert.equal(first.distanceOut, 819.6685737216337)
71
- }, 4000000)
72
50
  })