fleetmap-reports 1.0.958 → 1.0.960

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.958",
3
+ "version": "1.0.960",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -61,4 +61,39 @@ describe('zones', function () {
61
61
  assert.equal(first.days[0].distanceOut, 0)
62
62
  assert.equal(first.days[2].distanceOut, 0)
63
63
  }, 4000000)
64
+
65
+ it('works with casais zones in columns 2', async () => {
66
+ const report = await getReports()
67
+ const userData = await report.getUserData()
68
+ userData.zonesByColumn = true
69
+ userData.devices = userData.devices.filter(d => d.id === 81201)
70
+ userData.geofences = userData.geofences.filter(g => g.name === 'baliza 1 - raio verde ' ||
71
+ g.name === 'baliza 2 - raio amarelo')
72
+ userData.onlyWithKmsOut = false
73
+ const result = await report.zoneReport(
74
+ new Date(Date.UTC(2023, 7, 7, 0, 0, 0, 0)),
75
+ new Date(Date.UTC(2023, 7, 7, 23, 59, 59, 0)),
76
+ userData)
77
+ const first = result[0].devices[0]
78
+ console.log(first)
79
+ assert.equal(first.days[0].distanceOut, 554.6037095121401)
80
+ }, 4000000)
81
+
82
+ it('works with casais zones in columns 3', async () => {
83
+ const report = await getReports()
84
+ const userData = await report.getUserData()
85
+ userData.zonesByColumn = true
86
+ userData.devices = userData.devices.filter(d => d.id === 81201)
87
+ userData.geofences = userData.geofences.filter(g => g.name === 'baliza 1 - raio verde ' ||
88
+ g.name === 'baliza 2 - raio amarelo')
89
+ userData.onlyWithKmsOut = false
90
+ const result = await report.zoneReport(
91
+ new Date(Date.UTC(2023, 8, 1, 0, 0, 0, 0)),
92
+ new Date(Date.UTC(2023, 8, 10, 23, 59, 59, 0)),
93
+ userData)
94
+ const first = result[0].devices[0]
95
+ console.log(first)
96
+ assert.equal(first.days[0].distanceOut, 294.0468270741282)
97
+ assert.equal(first.days[1].distanceOut, 35.21209134706803)
98
+ }, 4000000)
64
99
  })
@@ -275,7 +275,7 @@ function getAnyLastExit (inDate, alerts, deviceRoute) {
275
275
  .filter(a => a.type === 'geofenceExit')
276
276
  .find(a => new Date(a.position.fixTime).getTime() < inDate)
277
277
 
278
- if (next && !alerts.filter(a => a.type === 'geofenceExit' &&
278
+ if (next && !alerts.filter(a => a.type === 'geofenceEnter' &&
279
279
  new Date(a.position.fixTime).getTime() < inDate &&
280
280
  new Date(a.position.fixTime).getTime() > new Date(next.position.fixTime).getTime()).length) {
281
281
  return (next && next.position && new Date(next.position.fixTime).getTime()) ||
@@ -336,7 +336,7 @@ function calculateDistanceOut (zoneInOutDayData, dayRoute, fromByDay, toByDay, f
336
336
 
337
337
  function analyseAlerts (alerts, deviceRoute, userData, from, to, device) {
338
338
  const zoneInOutData = []
339
- const zoneInData = {}
339
+ let zoneInData = []
340
340
 
341
341
  alerts.forEach(a => {
342
342
  if (!a.position) {
@@ -345,11 +345,12 @@ function analyseAlerts (alerts, deviceRoute, userData, from, to, device) {
345
345
  if (a.type === 'geofenceEnter') {
346
346
  const driver = userData.drivers.find(d => a.position.attributes && d.uniqueId === a.position.attributes.driverUniqueId)
347
347
  a.position.driverName = driver ? driver.name : ''
348
- if (Object.keys(zoneInData).length === 0) {
349
- a.anyLastExit = getAnyLastExit(new Date(a.position.fixTime).getTime(), alerts, deviceRoute)
348
+ if (zoneInData.length === 0) {
349
+ const anyLastExist = getAnyLastExit(new Date(a.position.fixTime).getTime(), alerts, deviceRoute)
350
+ a.anyLastExit = anyLastExist || from
350
351
  }
351
352
 
352
- zoneInData[a.geofenceId] = a
353
+ zoneInData.push(a)
353
354
  } else if (a.type === 'geofenceExit') {
354
355
  const geofence = userData.geofences.find(g => g.id === a.geofenceId)
355
356
  if (!geofence) {
@@ -364,22 +365,27 @@ function analyseAlerts (alerts, deviceRoute, userData, from, to, device) {
364
365
  const totalOutTime = nextIn - outDate
365
366
  const distanceOut = calculateDistance(routeAfterOut, device.attributes['report.ignoreOdometer'])
366
367
 
367
- const anyNextIn = getAnyNextIn(outDate, alerts, deviceRoute)
368
- const routeAfterOutAndBeforeAnyNextIn = deviceRoute.filter(p =>
369
- new Date(p.fixTime).getTime() >= outDate &&
370
- new Date(p.fixTime).getTime() <= anyNextIn
371
- )
372
- const distanceOutAny = Object.keys(zoneInData).length < 2 ? Math.round(calculateDistance(routeAfterOutAndBeforeAnyNextIn, device.attributes['report.ignoreOdometer'])) : 0
368
+ let anyNextIn
369
+ let distanceOutAny = 0
370
+ if (zoneInData.length < 2) {
371
+ anyNextIn = getAnyNextIn(outDate, alerts, deviceRoute) || to
372
+ const routeAfterOutAndBeforeAnyNextIn = deviceRoute.filter(p =>
373
+ new Date(p.fixTime).getTime() >= outDate &&
374
+ new Date(p.fixTime).getTime() <= anyNextIn
375
+ )
376
+ distanceOutAny = Math.round(calculateDistance(routeAfterOutAndBeforeAnyNextIn, device.attributes['report.ignoreOdometer']))
377
+ }
373
378
 
374
- if (zoneInData[a.geofenceId]) {
375
- const totalInTime = new Date(a.position.fixTime).getTime() - new Date(zoneInData[a.geofenceId].position.fixTime).getTime()
376
- const inDate = new Date(zoneInData[a.geofenceId].position.fixTime).getTime()
379
+ const inData = zoneInData.find(z => z.geofenceId === a.geofenceId)
380
+ if (inData) {
381
+ const totalInTime = new Date(a.position.fixTime).getTime() - new Date(inData.position.fixTime).getTime()
382
+ const inDate = new Date(inData.position.fixTime).getTime()
377
383
  const routeIn = deviceRoute.filter(p =>
378
384
  new Date(p.fixTime).getTime() >= inDate &&
379
385
  new Date(p.fixTime).getTime() < outDate
380
386
  )
381
387
  zoneInOutData.push({
382
- inTime: zoneInData[a.geofenceId].position,
388
+ inTime: inData.position,
383
389
  outTime: a.position,
384
390
  totalInTime,
385
391
  totalOutTime,
@@ -391,9 +397,9 @@ function analyseAlerts (alerts, deviceRoute, userData, from, to, device) {
391
397
  geofenceName: geofence.name,
392
398
  geofenceId: geofence.id,
393
399
  stopped: routeIn.filter(p => !p.attributes.ignition).length > 0,
394
- driverName: zoneInData[a.geofenceId].position.driverName
400
+ driverName: inData.position.driverName
395
401
  })
396
- delete zoneInData[a.geofenceId]
402
+ zoneInData = zoneInData.filter(z => z.geofenceId !== a.geofenceId)
397
403
  } else {
398
404
  const totalInTime = new Date(a.position.fixTime).getTime() - from.getTime()
399
405
  const routeIn = deviceRoute.filter(p =>
@@ -417,24 +423,23 @@ function analyseAlerts (alerts, deviceRoute, userData, from, to, device) {
417
423
  }
418
424
  })
419
425
 
420
- for (const i in zoneInData) {
421
- if (zoneInData[i]) {
422
- const geofence = userData.geofences.find(g => g.id === zoneInData[i].geofenceId)
423
- if (geofence) {
424
- const totalInTime = to.getTime() - new Date(zoneInData[i].position.fixTime).getTime()
425
- const routeIn = deviceRoute.filter(p =>
426
- new Date(p.fixTime).getTime() >= new Date(zoneInData[i].position.fixTime).getTime() &&
426
+ for (const inData of zoneInData) {
427
+ const geofence = userData.geofences.find(g => g.id === inData.geofenceId)
428
+ if (geofence) {
429
+ const totalInTime = to.getTime() - new Date(inData.position.fixTime).getTime()
430
+ const routeIn = deviceRoute.filter(p =>
431
+ new Date(p.fixTime).getTime() >= new Date(inData.position.fixTime).getTime() &&
427
432
  new Date(p.fixTime).getTime() < to.getTime()
428
- )
429
- zoneInOutData.push({
430
- inTime: zoneInData[i].position,
431
- totalInTime,
432
- distanceIn: calculateDistance(routeIn, device.attributes['report.ignoreOdometer']),
433
- geofenceName: geofence.name,
434
- geofenceId: geofence.id,
435
- driverName: zoneInData[i].position.driverName
436
- })
437
- }
433
+ )
434
+ zoneInOutData.push({
435
+ inTime: inData.position,
436
+ totalInTime,
437
+ distanceIn: calculateDistance(routeIn, device.attributes['report.ignoreOdometer']),
438
+ geofenceName: geofence.name,
439
+ geofenceId: geofence.id,
440
+ driverName: inData.position.driverName,
441
+ anyLastExit: inData.anyLastExit
442
+ })
438
443
  }
439
444
  }
440
445