fleetmap-reports 1.0.915 → 1.0.917

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.915",
3
+ "version": "1.0.917",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -62,7 +62,7 @@ async function createActivityReportByGroup (from, to, userData, traccarInstance)
62
62
  devices.sort((a, b) => (a.name > b.name) ? 1 : -1)
63
63
 
64
64
  if (summary.length > 0) {
65
- groupData.devices = processDevices(from, to, devices, { summary, trips }, userData)
65
+ groupData.devices = await processDevices(from, to, devices, { summary, trips }, userData, traccarInstance)
66
66
 
67
67
  reportData.push(groupData)
68
68
  }
@@ -113,7 +113,7 @@ async function createActivityReportByDevice (from, to, userData, traccarInstance
113
113
 
114
114
  // Process report data
115
115
  if (summary.length || trips.length) {
116
- allData.devices = allData.devices.concat(processDevices(from, to, devices, { summary, trips, route }, userData))
116
+ allData.devices = allData.devices.concat(await processDevices(from, to, devices, { summary, trips, route }, userData, traccarInstance))
117
117
  }
118
118
  deviceCount += devices.length
119
119
  }
@@ -157,10 +157,10 @@ async function createActivityReportByDriver (from, to, userData, traccarInstance
157
157
  return allData
158
158
  }
159
159
 
160
- function processDevices (from, to, devices, data, userData) {
160
+ async function processDevices (from, to, devices, data, userData, traccarInstance) {
161
161
  const devicesResult = []
162
162
 
163
- devices.forEach(d => {
163
+ for (const d of devices) {
164
164
  const deviceTrips = data.trips.filter(t => t.deviceId === d.id)
165
165
  const deviceRoute = data.route.filter(t => t.deviceId === d.id)
166
166
 
@@ -177,7 +177,7 @@ function processDevices (from, to, devices, data, userData) {
177
177
  const trips = deviceTrips.filter(t => userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, deviceRoute))
178
178
  const dates = getDates(from, to, userData.user.attributes.timezone || getUserPartner(userData.user).timezone)
179
179
  for (const date of dates) {
180
- const { tripsByDay, routeByDay } = getDataByDay(date, trips, userData, deviceRoute)
180
+ const { tripsByDay, routeByDay } = await getDataByDay(d, date, { trips, route: deviceRoute }, userData, traccarInstance)
181
181
 
182
182
  const distance = tripsByDay.reduce((a, b) => a + b.distance, 0)
183
183
  if (!userData.allWeek && userData.weekDays && userData.dayHours) {
@@ -263,7 +263,7 @@ function processDevices (from, to, devices, data, userData) {
263
263
  if ((deviceData.summary.length && deviceData.summary.some(s => s.startTime)) || !userData.onlyWithActivity) {
264
264
  devicesResult.push(deviceData)
265
265
  }
266
- })
266
+ }
267
267
 
268
268
  return devicesResult
269
269
  }
package/src/kms-report.js CHANGED
@@ -8,12 +8,12 @@ const {
8
8
  const { getUserPartner } = require('fleetmap-partners')
9
9
  const traccar = require('./util/traccar')
10
10
  const { getDates, getTranslations } = require('./util/utils')
11
- const trips = require('./util/trips')
12
11
  const drivers = require('./util/driver')
13
- const { isInsideTimetable, isPartialInsideTimetable, calculateTrip, getDataByDay } = require('./util/trips')
12
+ const { isInsideTimetable, isPartialInsideTimetable, calculateTrip, getDataByDay, checkTripsKms } = require('./util/trips')
14
13
  const traccarHelper = require('./util/traccar')
15
14
  const { devicesToProcess } = require('./util/device')
16
15
  const { getDriverData } = require('./util/driver')
16
+ const automaticReports = require('./automaticReports')
17
17
 
18
18
  const fileName = 'KmsReport'
19
19
 
@@ -35,7 +35,7 @@ async function createKmsReport (from, to, userData, traccarInstance) {
35
35
  }
36
36
 
37
37
  async function createKmsReportByDevice (from, to, userData, traccarInstance) {
38
- const devices = devicesToProcess(userData)
38
+ const allDevices = devicesToProcess(userData)
39
39
 
40
40
  const allData = {
41
41
  devices: [],
@@ -43,16 +43,21 @@ async function createKmsReportByDevice (from, to, userData, traccarInstance) {
43
43
  to
44
44
  }
45
45
 
46
- const needRoute = !userData.allWeek || userData.groupByDay
47
- const allInOne = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, needRoute, true, false, false, 0, devices.length, 20)
48
- const tripsData = allInOne.trips
49
- const routeData = allInOne.route
50
-
51
- console.log('trips:' + tripsData.length)
52
-
53
- if (tripsData.length > 0) {
54
- trips.checkTripsKms(traccarInstance, from, to, devices, { trips: tripsData, route: routeData })
55
- allData.devices = processDevices(from, to, devices, { trips: tripsData, route: routeData }, userData)
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)
54
+
55
+ // Process report data
56
+ if (trips.length) {
57
+ checkTripsKms(traccarInstance, from, to, allDevices, { trips, route })
58
+ allData.devices = await processDevices(from, to, allDevices, { trips, route }, userData, traccarInstance)
59
+ }
60
+ deviceCount += devices.length
56
61
  }
57
62
 
58
63
  return allData
@@ -92,8 +97,8 @@ async function createKmsReportByGroup (from, to, userData, traccarInstance) {
92
97
  console.log('trips:' + tripsData.length)
93
98
 
94
99
  if (tripsData.length > 0) {
95
- trips.checkTripsKms(traccarInstance, from, to, devices, { trips: tripsData, route })
96
- groupData.devices = processDevices(from, to, devices, { trips: tripsData, route }, userData)
100
+ checkTripsKms(traccarInstance, from, to, devices, { trips: tripsData, route })
101
+ groupData.devices = await processDevices(from, to, devices, { trips: tripsData, route }, userData, traccarInstance)
97
102
  }
98
103
 
99
104
  reportData.push(groupData)
@@ -142,7 +147,7 @@ function processDrivers (from, to, userData, data) {
142
147
  return driversResult
143
148
  }
144
149
 
145
- function processDevices (from, to, devices, data, userData) {
150
+ async function processDevices (from, to, devices, data, userData, traccarInstance) {
146
151
  const devicesResult = []
147
152
 
148
153
  for (const d of devices) {
@@ -165,7 +170,7 @@ function processDevices (from, to, devices, data, userData) {
165
170
  const trips = deviceTrips.filter(t => userData.allWeek || !userData.weekDays || isInsideTimetable(t, userData) || isPartialInsideTimetable(t, userData, deviceRoute))
166
171
  const dates = getDates(from, to, userData.user.attributes.timezone || getUserPartner(userData.user).timezone)
167
172
  for (const date of dates) {
168
- const { tripsByDay } = getDataByDay(date, trips, userData, deviceRoute)
173
+ const { tripsByDay } = await getDataByDay(d, date, { trips }, userData, traccarInstance)
169
174
  const day = {
170
175
  date,
171
176
  kms: tripsByDay.reduce((a, b) => a + b.distance, 0)
@@ -48,16 +48,16 @@ describe('zones', function () {
48
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-5983-E Clio')
51
+ userData.devices = userData.devices.filter(d => d.id === 81202)
52
52
  userData.geofences = userData.geofences.filter(g => g.name === 'baliza 1 - raio verde ' ||
53
53
  g.name === 'baliza 2 - raio amarelo')
54
- userData.onlyWithKmsOut = true
54
+ userData.onlyWithKmsOut = false
55
55
  const result = await report.zoneReport(
56
- new Date(Date.UTC(2023, 8, 1, 0, 0, 0, 0)),
57
- new Date(Date.UTC(2023, 8, 30, 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)),
58
58
  userData)
59
59
  const first = result[0].devices[0]
60
60
  console.log(first)
61
- assert.equal(first.distanceOut, 0)
61
+ assert.equal(first.days[0].distanceOut, 28)
62
62
  }, 4000000)
63
63
  })
@@ -78,7 +78,8 @@ async function getAllInOne (
78
78
  totalDevices = devices.length,
79
79
  sliceSize = 5,
80
80
  devicesPerRequest = 1,
81
- counter = undefined) {
81
+ counter = undefined,
82
+ ignorePercentage = false) {
82
83
  let url = `/reports/10240/allinone?from=${from.toISOString()}&to=${to.toISOString()}`
83
84
  if (getRoutes) url += '&type=route'
84
85
  if (getTrips) url += '&type=trips'
@@ -101,10 +102,10 @@ async function getAllInOne (
101
102
  console.log('LOADING_MESSAGE:' + _chunk[0].name)
102
103
  if (counter) {
103
104
  counter.count += devicesPerRequest
104
- console.log(`PROGRESS_PERC:${counter.count / totalDevices * 100}`)
105
+ ignorePercentage || console.log(`PROGRESS_PERC:${counter.count / totalDevices * 100}`)
105
106
  } else {
106
107
  currentDeviceCount += devicesPerRequest
107
- console.log(`PROGRESS_PERC:${currentDeviceCount / totalDevices * 100}`)
108
+ ignorePercentage || console.log(`PROGRESS_PERC:${currentDeviceCount / totalDevices * 100}`)
108
109
  }
109
110
  console.log(_chunk.map(d => `deviceId=${d.id}`), x.route && x.route.length)
110
111
  return x
package/src/util/trips.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const { coordsDistance, convertFromUTC, weekDaySelected, convertFromLocal, isClientSide, calculateDistance } = require('./utils')
2
2
  const { isInside } = require('./timetable')
3
+ const traccarHelper = require('./traccar')
3
4
 
4
5
  const minDistance = 0
5
6
  const minAvgSpeed = 0
@@ -30,7 +31,7 @@ function checkTripsKms (traccarInstance, from, to, devices, data) {
30
31
  }
31
32
  }
32
33
 
33
- function calculatePartialTrip (startDate, endDate, route, t) {
34
+ async function calculatePartialTrip (device, startDate, endDate, route, t, traccarInstance) {
34
35
  let isPartialInside = false
35
36
  const tripStart = new Date(t.startTime)
36
37
  const tripEnd = new Date(t.endTime)
@@ -39,6 +40,10 @@ function calculatePartialTrip (startDate, endDate, route, t) {
39
40
  if (tripStart.getTime() < endDate.getTime() &&
40
41
  tripEnd.getTime() > endDate.getTime()) {
41
42
  // Trip starts inside time period and ends outside time period
43
+ if (!route) {
44
+ const allInOneData = await traccarHelper.getAllInOne(traccarInstance, startDate, endDate, [device], true, false, false, false, undefined, undefined, 5, 1, undefined, true)
45
+ route = allInOneData.route
46
+ }
42
47
  const routeInside = route.filter(p => new Date(p.fixTime).getTime() < new Date(endDate.toUTCString()).getTime() && new Date(p.fixTime).getTime() > new Date(t.startTime).getTime() && t.deviceId === p.deviceId)
43
48
  updateTrip(t, routeInside)
44
49
  t.endTime = endDate.toISOString().replace('Z', '+0000')
@@ -49,6 +54,10 @@ function calculatePartialTrip (startDate, endDate, route, t) {
49
54
  if (tripStart.getTime() < startDate.getTime() &&
50
55
  new Date(t.endTime).getTime() > startDate.getTime()) {
51
56
  // Trip starts outside time period and ends inside time period
57
+ if (!route) {
58
+ const allInOneData = await traccarHelper.getAllInOne(traccarInstance, startDate, endDate, [device], true, false, false, false, undefined, undefined, 5, 1, undefined, true)
59
+ route = allInOneData.route
60
+ }
52
61
  const routeInside = route.filter(p => new Date(p.fixTime).getTime() > startDate.getTime() && new Date(p.fixTime).getTime() < new Date(t.endTime).getTime() && t.deviceId === p.deviceId)
53
62
  updateTrip(t, routeInside)
54
63
  t.startTime = startDate.toISOString().replace('Z', '+0000')
@@ -59,6 +68,10 @@ function calculatePartialTrip (startDate, endDate, route, t) {
59
68
  if (tripStart.getTime() < startDate.getTime() &&
60
69
  tripEnd.getTime() > startDate.getTime()) {
61
70
  // Trip starts outside time period and ends inside time period
71
+ if (!route) {
72
+ const allInOneData = await traccarHelper.getAllInOne(traccarInstance, startDate, endDate, [device], true, false, false, false, undefined, undefined, 5, 1, undefined, true)
73
+ route = allInOneData.route
74
+ }
62
75
  const routeInside = route.filter(p => new Date(p.fixTime).getTime() > new Date(startDate.toUTCString()).getTime() && new Date(p.fixTime).getTime() < new Date(t.endTime).getTime() && t.deviceId === p.deviceId)
63
76
  updateTrip(t, routeInside)
64
77
  t.startTime = startDate.toISOString().replace('Z', '+0000')
@@ -69,6 +82,10 @@ function calculatePartialTrip (startDate, endDate, route, t) {
69
82
  if (tripStart.getTime() < endDate.getTime() &&
70
83
  tripEnd.getTime() > endDate.getTime()) {
71
84
  // Trip starts inside time period and ends outside time period
85
+ if (!route) {
86
+ const allInOneData = await traccarHelper.getAllInOne(traccarInstance, startDate, endDate, [device], true, false, false, false, undefined, undefined, 5, 1, undefined, true)
87
+ route = allInOneData.route
88
+ }
72
89
  const routeInside = route.filter(p => new Date(p.fixTime).getTime() < endDate.getTime() && new Date(p.fixTime).getTime() > new Date(t.startTime).getTime() && t.deviceId === p.deviceId)
73
90
  updateTrip(t, routeInside)
74
91
  t.endTime = endDate.toISOString().replace('Z', '+0000')
@@ -93,10 +110,10 @@ function isPartialInsideTimetable (t, userData, route) {
93
110
  return false
94
111
  }
95
112
 
96
- function getDataByDay (date, trips, userData, deviceRoute) {
113
+ async function getDataByDay (device, date, data, userData, traccarInstance) {
97
114
  const fromByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
98
115
  const toByDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59)
99
- const tripsByDay = trips.filter(t => new Date(t.endTime) > fromByDay && new Date(t.startTime) < toByDay).map(t => {
116
+ const tripsByDay = data.trips.filter(t => new Date(t.endTime) > fromByDay && new Date(t.startTime) < toByDay).map(t => {
100
117
  return { ...t }
101
118
  })
102
119
 
@@ -104,10 +121,11 @@ function getDataByDay (date, trips, userData, deviceRoute) {
104
121
  const endDateLocal = new Date(convertFromUTC(date, userData.user.attributes.timezone).toISOString().split('T')[0] + ' ' + userData.dayHours.endTime)
105
122
  const startDate = isClientSide() ? startDateLocal : convertFromLocal(startDateLocal, userData.user.attributes.timezone)
106
123
  const endDate = isClientSide() ? endDateLocal : convertFromLocal(endDateLocal, userData.user.attributes.timezone)
107
- tripsByDay.forEach(t => calculatePartialTrip(startDate, endDate, deviceRoute, t))
108
- console.log(date, tripsByDay)
124
+ for (const t of tripsByDay) {
125
+ await calculatePartialTrip(device, startDate, endDate, data.route, t, traccarInstance)
126
+ }
109
127
 
110
- const routeByDay = deviceRoute.filter(p => (new Date(p.fixTime) > fromByDay && new Date(p.fixTime) < toByDay))
128
+ const routeByDay = data.route ? data.route.filter(p => (new Date(p.fixTime) > fromByDay && new Date(p.fixTime) < toByDay)) : []
111
129
  return { tripsByDay, routeByDay }
112
130
  }
113
131
 
@@ -261,8 +261,13 @@ function getAnyNextIn (outDate, alerts, deviceRoute) {
261
261
  const next = alerts
262
262
  .filter(a => a.type === 'geofenceEnter')
263
263
  .find(a => new Date(a.position.fixTime).getTime() > outDate)
264
- return (next && next.position && new Date(next.position.fixTime).getTime()) ||
265
- new Date(deviceRoute.slice(-1)[0].fixTime).getTime()
264
+
265
+ if (!alerts.filter(a => a.type === 'geofenceExit' &&
266
+ new Date(a.position.fixTime).getTime() > outDate &&
267
+ new Date(a.position.fixTime).getTime() < new Date(next.position.fixTime).getTime()).length) {
268
+ return (next && next.position && new Date(next.position.fixTime).getTime()) ||
269
+ new Date(deviceRoute.slice(-1)[0].fixTime).getTime()
270
+ }
266
271
  }
267
272
 
268
273
  function calculateDistanceIn (zoneInOutDayData, deviceRoute, dayRoute, fromByDay, toByDay, to, device) {
@@ -289,9 +294,8 @@ function calculateDistanceIn (zoneInOutDayData, deviceRoute, dayRoute, fromByDay
289
294
 
290
295
  function calculateDistanceOut (zoneInOutDayData, dayRoute, fromByDay, toByDay, field, device) {
291
296
  if (zoneInOutDayData.length) {
292
- const dataInsideDay = zoneInOutDayData.filter(z => (z.inTime && new Date(z.inTime.fixTime) > fromByDay) && (z.outTime && new Date(z.outTime.fixTime) > toByDay))
293
- let distanceOut = dataInsideDay.reduce((a, b) => a + (b[field] || 0), 0) -
294
- (dataInsideDay.length ? (dataInsideDay[dataInsideDay.length - 1][field] || 0) : 0)
297
+ const dataInsideDay = zoneInOutDayData.filter(z => z.outTime && new Date(z.outTime.fixTime) < toByDay)
298
+ let distanceOut = dataInsideDay.reduce((a, b) => a + (b[field] || 0), 0)
295
299
 
296
300
  if (zoneInOutDayData[0].inTime && new Date(zoneInOutDayData[0].inTime.fixTime) > fromByDay) {
297
301
  // Add distanceOut before the first entry
@@ -303,6 +307,7 @@ function calculateDistanceOut (zoneInOutDayData, dayRoute, fromByDay, toByDay, f
303
307
  }
304
308
 
305
309
  if (zoneInOutDayData[zoneInOutDayData.length - 1].outTime && new Date(zoneInOutDayData[zoneInOutDayData.length - 1].outTime.fixTime) < toByDay) {
310
+ distanceOut = distanceOut - zoneInOutDayData[zoneInOutDayData.length - 1][field]
306
311
  const routeOut = dayRoute.filter(p =>
307
312
  new Date(p.fixTime) >= new Date(zoneInOutDayData[zoneInOutDayData.length - 1].outTime.fixTime).getTime() &&
308
313
  new Date(p.fixTime) < toByDay.getTime())