fleetmap-reports 1.0.647 → 1.0.649

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.647",
3
+ "version": "1.0.649",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -34,8 +34,8 @@
34
34
  "eslint-plugin-import": "^2.26.0",
35
35
  "eslint-plugin-node": "^11.1.0",
36
36
  "eslint-plugin-promise": "^6.0.0",
37
- "jest": "^29.4.3",
38
- "mocha": "^10.2.0",
37
+ "jest": "^27.5.0",
38
+ "mocha": "^9.0.3",
39
39
  "webpack": "^5.24.3",
40
40
  "webpack-cli": "^4.5.0"
41
41
  }
@@ -8,7 +8,7 @@ const automaticReports = require('./automaticReports')
8
8
  const traccarHelper = require('./util/traccar')
9
9
  const { getIdleEvents } = require('./util/route')
10
10
  const { reportByDriver } = require('./util/driver')
11
- const { getNearestPOIs, insideGeofence, loadGroupsData} = require('./util/geofence')
11
+ const { getNearestPOIs, insideGeofence, loadGroupsData } = require('./util/geofence')
12
12
 
13
13
  const fileName = 'IdleReport'
14
14
 
@@ -1,7 +1,7 @@
1
1
  const { devicesToProcess, deviceName } = require('./util/device')
2
2
  const automaticReports = require('./automaticReports')
3
3
  const traccarHelper = require('./util/traccar')
4
- const { getNearestPOIs, insideGeofence, loadGroupsData} = require('./util/geofence')
4
+ const { getNearestPOIs, insideGeofence, loadGroupsData } = require('./util/geofence')
5
5
  const { getTranslations, convertToLocaleString, convertMS, convertToLocaleDateString, convertToLocaleTimeString } = require('./util/utils')
6
6
  const jsPDF = require('jspdf')
7
7
  const { headerFromUser, addTable } = require('./util/pdfDocument')
@@ -363,7 +363,7 @@ describe('Test_Reports', function () {
363
363
  assert.equal(device.geofences[0].outTime.fixTime, '2022-04-26T07:47:35.000+0000')
364
364
  assert.equal(device.geofences[0].totalTime, '28777')
365
365
  assert.equal(device.geofences[0].geofenceName, 'Casa João')
366
- }, 30000)
366
+ }, 990000)
367
367
  // eslint-disable-next-line no-undef
368
368
  it('Zone Report With Stops', async () => {
369
369
  const report = await getReports()
@@ -6,10 +6,10 @@ describe('trips', function () {
6
6
  userData.roadSpeedLimits = false
7
7
  userData.customSpeed = false
8
8
  userData.useVehicleSpeedLimit = true
9
- const data = await report.tripReport(
9
+ await report.tripReport(
10
10
  new Date(Date.UTC(2022, 9, 22, 0, 0, 0, 0)),
11
11
  new Date(Date.UTC(2022, 9, 22, 23, 59, 59, 0)),
12
12
  userData)
13
- await report.tripReportToPDF(userData, data)
13
+ // await report.tripReportToPDF(userData, data)
14
14
  }, 40000)
15
15
  })
package/src/util/utils.js CHANGED
@@ -234,3 +234,15 @@ exports.getLogo = (user) => {
234
234
  }
235
235
  exports.convertToFeature = convertToFeature
236
236
  exports.convertPositionToFeature = convertPositionToFeature
237
+ exports.calculateDistance = (positions) => {
238
+ return positions
239
+ .sort((a, b) => new Date(a.fixTime).getTime() - new Date(b.fixTime).getTime())
240
+ .map(p => [p.longitude, p.latitude])
241
+ .reduce((previousValue, currentValue, currentIndex, array) => {
242
+ return currentIndex < array.length - 1
243
+ ? previousValue + distance.default(currentValue, array[currentIndex + 1])
244
+ : previousValue
245
+ }, 0) || 0
246
+ }
247
+
248
+ exports.sortPositions = (a, b) => new Date(a).getTime() - new Date(b).getTime()
@@ -1,6 +1,10 @@
1
1
  const moment = require('moment')
2
2
  const automaticReports = require('./automaticReports')
3
- const { convertMS, convertToLocaleString, getTranslations, convertToFeature, convertPositionToFeature } = require('./util/utils')
3
+ const {
4
+ convertMS, convertToLocaleString, getTranslations, convertToFeature, convertPositionToFeature,
5
+ calculateDistance,
6
+ sortPositions
7
+ } = require('./util/utils')
4
8
  const jsPDF = require('jspdf')
5
9
  require('jspdf-autotable')
6
10
  const traccarHelper = require('./util/traccar')
@@ -84,6 +88,19 @@ async function createZoneReport (from, to, userData, traccar) {
84
88
 
85
89
  return reportData
86
90
  }
91
+ function getPreviousOut(inDate, alerts, geofenceId, deviceRoute) {
92
+ return alerts
93
+ .filter(a => a.type === 'geofenceExit' && a.geofenceId === geofenceId)
94
+ .find(a => new Date(a.position.fixTime).getTime() < inDate) ||
95
+ new Date(deviceRoute[0].fixTime).getTime()
96
+ }
97
+
98
+ function getNextIn(outDate, alerts, geofenceId, deviceRoute) {
99
+ return alerts
100
+ .filter(a => a.type === 'geofenceEnter' && a.geofenceId === geofenceId)
101
+ .find(a => new Date(a.position.fixTime).getTime() > outDate) ||
102
+ new Date(deviceRoute.slice(-1)[0].fixTime).getTime()
103
+ }
87
104
 
88
105
  async function processDevices (from, to, devices, userData, data) {
89
106
  const devicesResult = []
@@ -106,15 +123,28 @@ async function processDevices (from, to, devices, userData, data) {
106
123
  const geofence = userData.geofences.find(g => g.id === a.geofenceId)
107
124
  if (zoneInData[a.geofenceId]) {
108
125
  const totalInTime = moment(a.position.fixTime).diff(moment(zoneInData[a.geofenceId].position.fixTime), 'seconds')
109
- const route = deviceRoute.filter(p => new Date(p.fixTime).getTime() >= new Date(zoneInData[a.geofenceId].position.fixTime).getTime() &&
110
- new Date(p.fixTime).getTime() <= new Date(a.position.fixTime).getTime())
126
+ const inDate = new Date(zoneInData[a.geofenceId].position.fixTime).getTime()
127
+ const outDate = new Date(a.position.fixTime).getTime()
128
+ const routeBeforeIn = deviceRoute.filter(p =>
129
+ new Date(p.fixTime).getTime() >= getPreviousOut(inDate, alerts, a.geofenceId, deviceRoute) &&
130
+ new Date(p.fixTime).getTime() < inDate)
131
+ const routeIn = deviceRoute.filter(p =>
132
+ new Date(p.fixTime).getTime() >= inDate &&
133
+ new Date(p.fixTime).getTime() < outDate
134
+ )
135
+ const routeAfterOut = deviceRoute.filter(p =>
136
+ new Date(p.fixTime).getTime() >= outDate &&
137
+ new Date(p.fixTime).getTime() < getNextIn(outDate, alerts, a.geofenceId, deviceRoute)
138
+ )
111
139
  if (geofence) {
112
140
  zoneInOutData.push({
113
141
  inTime: zoneInData[a.geofenceId].position,
114
142
  outTime: a.position,
115
143
  totalTime: totalInTime,
144
+ distanceIn: calculateDistance(routeIn),
145
+ distanceOut: calculateDistance(routeBeforeIn) + calculateDistance(routeAfterOut),
116
146
  geofenceName: geofence.name,
117
- stopped: route.filter(p => !p.attributes.ignition).length > 0,
147
+ stopped: routeIn.filter(p => !p.attributes.ignition).length > 0,
118
148
  driverName: zoneInData[a.geofenceId].position.driverName
119
149
  })
120
150
  }
@@ -166,7 +196,7 @@ function getInAndOutEvents (devices, route, userData) {
166
196
  devices.forEach(d => {
167
197
  const deviceRoute = route.filter(p => p.deviceId === d.id)
168
198
 
169
- const routePoints = deviceRoute.map(p => convertPositionToFeature(p))
199
+ const routePoints = deviceRoute.sort(sortPositions).map(p => convertPositionToFeature(p))
170
200
  const geofencesFeatures = userData.geofences.map(g => convertToFeature(g))
171
201
 
172
202
  const entryMap = []