fleetmap-reports 1.0.739 → 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.739",
3
+ "version": "1.0.740",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -64,3 +64,5 @@ exports.deviceWithFuelInfo = deviceWithFuelInfo
64
64
  exports.calculateSpentFuel = calculateSpentFuel
65
65
  exports.calculateXpertSpentFuel = calculateXpertSpentFuel
66
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
+ })