fleetmap-reports 1.0.919 → 1.0.921

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.919",
3
+ "version": "1.0.921",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/kms-report.js CHANGED
@@ -7,7 +7,7 @@ const {
7
7
  } = require('./util/pdfDocument')
8
8
  const { getUserPartner } = require('fleetmap-partners')
9
9
  const traccar = require('./util/traccar')
10
- const { getDates, getTranslations } = require('./util/utils')
10
+ const { getDates, getTranslations, isClientSide, executeServerSide } = require('./util/utils')
11
11
  const drivers = require('./util/driver')
12
12
  const { isInsideTimetable, isPartialInsideTimetable, calculateTrip, getDataByDay, checkTripsKms } = require('./util/trips')
13
13
  const traccarHelper = require('./util/traccar')
@@ -43,21 +43,29 @@ async function createKmsReportByDevice (from, to, userData, traccarInstance) {
43
43
  to
44
44
  }
45
45
 
46
- let deviceCount = 0
47
- const sliced = automaticReports.sliceArray(allDevices, 20)
48
- for (const devices of sliced) {
49
- const needRoute = !userData.allWeek
50
- const {
51
- trips,
52
- route
53
- } = await traccar.getAllInOne(traccarInstance, from, to, devices, needRoute, true, false, false, deviceCount, allDevices.length, 10, 2)
54
-
55
- // Process report data
56
- if (trips.length) {
57
- checkTripsKms(traccarInstance, from, to, allDevices, { trips, route })
58
- allData.devices = allData.devices.concat(await processDevices(from, to, allDevices, { trips, route }, userData, traccarInstance))
46
+ if (isClientSide() && allDevices.length > 50 && userData.groupByDay) {
47
+ const sliced = automaticReports.sliceArray(allDevices, 20)
48
+ await executeServerSide('kms-report', allData, sliced, from, to, userData, allDevices.length)
49
+ } else {
50
+ let deviceCount = 0
51
+ const sliced = automaticReports.sliceArray(allDevices, 20)
52
+ for (const devices of sliced) {
53
+ const needRoute = !userData.allWeek
54
+ const {
55
+ trips,
56
+ route
57
+ } = await traccar.getAllInOne(traccarInstance, from, to, devices, needRoute, true, false, false, deviceCount, allDevices.length, 10, 2)
58
+
59
+ // Process report data
60
+ if (trips.length) {
61
+ checkTripsKms(traccarInstance, from, to, allDevices, { trips, route })
62
+ allData.devices = allData.devices.concat(await processDevices(from, to, allDevices, {
63
+ trips,
64
+ route
65
+ }, userData, traccarInstance))
66
+ }
67
+ deviceCount += devices.length
59
68
  }
60
- deviceCount += devices.length
61
69
  }
62
70
 
63
71
  return allData
@@ -166,17 +174,15 @@ async function processDevices (from, to, devices, data, userData, traccarInstanc
166
174
 
167
175
  if (userData.groupByDay) {
168
176
  if (trips.length > 0) {
169
- const days = []
170
177
  const trips = deviceTrips.filter(t => userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, deviceRoute))
171
178
  const dates = getDates(from, to, userData.user.attributes.timezone || getUserPartner(userData.user).timezone)
172
- for (const date of dates) {
179
+ const days = await Promise.all(dates.map(async date => {
173
180
  const { tripsByDay } = await getDataByDay(d, date, { trips }, userData, traccarInstance)
174
- const day = {
181
+ return {
175
182
  date,
176
183
  kms: tripsByDay.reduce((a, b) => a + b.distance, 0)
177
184
  }
178
- days.push(day)
179
- }
185
+ }))
180
186
 
181
187
  devicesResult.push({
182
188
  device: d,
@@ -45,18 +45,19 @@ describe('zones', function () {
45
45
 
46
46
  // eslint-disable-next-line no-undef
47
47
  it('works with casais zones in columns', async () => {
48
- const report = await getReports(process.env.USER_CASAIS, process.env.PASS_CASAIS)
48
+ const report = await getReports('fleetmap.joao.penas2@gmail.com', 'penas46881')
49
49
  const userData = await report.getUserData()
50
50
  userData.zonesByColumn = true
51
- userData.devices = userData.devices.filter(d => d.name === 'G-1101-E Clio')
52
- userData.geofences = userData.geofences.filter(g => g.name === 'baliza 1 - raio verde ')
53
- userData.onlyWithKmsOut = true
51
+ userData.devices = userData.devices.filter(d => d.id === 81202)
52
+ userData.geofences = userData.geofences.filter(g => g.name === 'baliza 1 - raio verde ' ||
53
+ g.name === 'baliza 2 - raio amarelo')
54
+ userData.onlyWithKmsOut = false
54
55
  const result = await report.zoneReport(
55
- new Date(Date.UTC(2023, 0, 1, 0, 0, 0, 0)),
56
- new Date(Date.UTC(2023, 0, 31, 23, 59, 59, 0)),
56
+ new Date(Date.UTC(2023, 6, 13, 0, 0, 0, 0)),
57
+ new Date(Date.UTC(2023, 6, 13, 23, 59, 59, 0)),
57
58
  userData)
58
- const first = result[0].devices[0].days[0]
59
+ const first = result[0].devices[0]
59
60
  console.log(first)
60
- assert.equal(first.distanceOut, 35.52274536571346)
61
+ assert.equal(first.days[0].distanceOut, 28)
61
62
  }, 4000000)
62
63
  })
package/src/util/utils.js CHANGED
@@ -224,6 +224,18 @@ function weekDaySelected (date, weekDays) {
224
224
  (date.getDay() === 6 && weekDays.saturday))
225
225
  }
226
226
 
227
+ async function executeServerSide (type, allData, sliced, from, to, userData, totalDevices) {
228
+ let deviceCount = 0
229
+ for (const devices of sliced) {
230
+ const url = `https://${process.env.SERVER_HOST}/reports/${type}`
231
+ const data = await axios.post(url, { from, to, userData: { ...userData, devices } }, { withCredentials: true }).then(d => d.data)
232
+ allData.devices.push(...(data[0].devices))
233
+ deviceCount = deviceCount + devices.length
234
+ console.log(`PROGRESS_PERC:${deviceCount / totalDevices * 100}`)
235
+ }
236
+ }
237
+
238
+ exports.executeServerSide = executeServerSide
227
239
  exports.getImgFromUrl = getImgFromUrl
228
240
  exports.convertMS = convertMS
229
241
  exports.coordsDistance = coordsDistance