fleetmap-reports 1.0.785 → 1.0.787

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.785",
3
+ "version": "1.0.787",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1,5 +1,5 @@
1
1
  const automaticReports = require('./automaticReports')
2
- const { convertMS, getDates, getTranslations, weekDaySelected, convertToLocaleString } = require('./util/utils')
2
+ const { convertMS, getDates, getTranslations, weekDaySelected, convertToLocaleString, isClientSide } = require('./util/utils')
3
3
  const jsPDF = require('jspdf')
4
4
  require('jspdf-autotable')
5
5
  const {
@@ -14,6 +14,7 @@ const tripHelper = require('./util/trips')
14
14
  const { devicesToProcess } = require('./util/device')
15
15
  const { getDriverData } = require('./util/driver')
16
16
  const { calculateConsumption } = require('./util/fuel')
17
+ const { default: axios } = require('axios')
17
18
 
18
19
  const fileName = 'ActivityReport'
19
20
 
@@ -73,6 +74,25 @@ async function createActivityReportByGroup (from, to, userData, traccarInstance)
73
74
  return reportData
74
75
  }
75
76
 
77
+ async function executeServerSide (allData, sliced, from, to, userData, totalDevices) {
78
+ let deviceCount = 0
79
+ for (const devices of sliced) {
80
+ const url = `https://${process.env.SERVER_HOST}/reports/activity-report`
81
+ const data = await axios.post(url, {
82
+ from,
83
+ to,
84
+ sliced,
85
+ userData,
86
+ deviceCount,
87
+ totalDevices,
88
+ sliceSize: 50
89
+ }, { withCredentials: true }).then(d => d.data)
90
+
91
+ allData.devices.push(data.devices)
92
+ deviceCount += devices.length
93
+ }
94
+ }
95
+
76
96
  async function createActivityReportByDevice (from, to, userData, traccarInstance) {
77
97
  const allDevices = devicesToProcess(userData)
78
98
 
@@ -83,48 +103,52 @@ async function createActivityReportByDevice (from, to, userData, traccarInstance
83
103
  to
84
104
  }
85
105
 
86
- const sliced = automaticReports.sliceArray(allDevices, 10)
87
- let deviceCount = 0
88
- for (const devices of sliced) {
89
- let summaries = []
90
- const needRoute = userData.groupByDay || !userData.allWeek
91
- const {
92
- trips,
93
- route,
94
- summary
95
- } = await traccar.getAllInOne(traccarInstance, from, to, devices, needRoute, true, false, !userData.groupByDay, deviceCount, allDevices.length)
106
+ const sliced = automaticReports.sliceArray(allDevices, 50)
107
+ if (isClientSide() && allDevices.length > 200) {
108
+ await executeServerSide(allData, sliced, from, to, userData, allDevices.length)
109
+ } else {
110
+ let deviceCount = 0
111
+ for (const devices of sliced) {
112
+ let summaries = []
113
+ const needRoute = userData.groupByDay || !userData.allWeek
114
+ const {
115
+ trips,
116
+ route,
117
+ summary
118
+ } = await traccar.getAllInOne(traccarInstance, from, to, devices, needRoute, true, false, !userData.groupByDay, deviceCount, allDevices.length)
96
119
 
97
- if (userData.groupByDay) {
98
- console.log('trips:' + trips.length)
120
+ if (userData.groupByDay) {
121
+ console.log('trips:' + trips.length)
99
122
 
100
- if (trips.length) {
101
- tripHelper.checkTripsKms(traccarInstance, from, to, devices, { trips, route })
102
- }
123
+ if (trips.length) {
124
+ tripHelper.checkTripsKms(traccarInstance, from, to, devices, { trips, route })
125
+ }
103
126
 
104
- const dates = getDates(from, to)
105
- for (const date of dates) {
106
- if (userData.allWeek || !userData.weekDays || !userData.dayHours) {
107
- const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
108
- const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
127
+ const dates = getDates(from, to)
128
+ for (const date of dates) {
129
+ if (userData.allWeek || !userData.weekDays || !userData.dayHours) {
130
+ const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
131
+ const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
109
132
 
110
- const summary = await traccar.getSummary(traccarInstance, fromByDay, toByDay, devices)
111
- summary.forEach(s => {
112
- s.date = date
113
- })
114
- summaries = summaries.concat(summary)
133
+ const summary = await traccar.getSummary(traccarInstance, fromByDay, toByDay, devices)
134
+ summary.forEach(s => {
135
+ s.date = date
136
+ })
137
+ summaries = summaries.concat(summary)
138
+ }
115
139
  }
140
+ } else {
141
+ summaries = summary
116
142
  }
117
- } else {
118
- summaries = summary
119
- }
120
143
 
121
- console.log('Summary:' + summaries.length)
144
+ console.log('Summary:' + summaries.length)
122
145
 
123
- // Process report data
124
- if (summaries.length || trips.length) {
125
- allData.devices = allData.devices.concat(processDevices(from, to, devices, { summaries, trips, route }, userData))
146
+ // Process report data
147
+ if (summaries.length || trips.length) {
148
+ allData.devices = allData.devices.concat(processDevices(from, to, devices, { summaries, trips, route }, userData))
149
+ }
150
+ deviceCount += devices.length
126
151
  }
127
- deviceCount += devices.length
128
152
  }
129
153
 
130
154
  return allData
@@ -112,7 +112,11 @@ async function getAllInOne (
112
112
  }
113
113
  const now = new Date()
114
114
  console.log('parallel', requests.length)
115
- result.push(...(await Promise.all(requests)))
115
+ try {
116
+ result.push(...(await Promise.all(requests)))
117
+ } catch (e) {
118
+ console.log(e)
119
+ }
116
120
  console.log('took', new Date() - now, 'ms')
117
121
  }
118
122
  return {