fleetmap-reports 2.0.246 → 2.0.249

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": "2.0.246",
3
+ "version": "2.0.249",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -8,7 +8,6 @@ const { headerFromUser, AmiriRegular } = require('./util/pdfDocument')
8
8
  require('jspdf-autotable')
9
9
  const { getStyle } = require('./reportStyle')
10
10
  const drivers = require('./util/driver')
11
- const here = require('./here')
12
11
  const {
13
12
  getUserPartner
14
13
  } = require('fleetmap-partners')
@@ -310,14 +309,7 @@ async function getCustomSpeedLimitEvents (devices, routes, customSpeed) {
310
309
  async function getRoadSpeedLimits (devices, routes, threshold, minimumMinutes = 0) {
311
310
  const position = routes.find(p => getCountry(p))
312
311
  const country = position && getCountry(position)
313
- const config = {
314
- Chile: getOSMSpeedingEvents,
315
- CL: getOSMSpeedingEvents,
316
- Brazil: getOSMSpeedingEvents,
317
- BR: getOSMSpeedingEvents
318
- }
319
- const method = config[country] || getHereEvents
320
- return method(devices, routes, threshold, minimumMinutes, country)
312
+ return getOSMSpeedingEvents(devices, routes, threshold, minimumMinutes, country)
321
313
  }
322
314
 
323
315
  exports.getRoadSpeedLimits = getRoadSpeedLimits
@@ -393,9 +385,11 @@ async function getOSMSpeedingEvents (devices, routes, threshold, minimumMinutes
393
385
  for (const d of devices) {
394
386
  const route = routes.filter(r => r.deviceId === d.id)
395
387
  const results = []
388
+ const promises = []
396
389
  for (let i = 0; i < route.length; i += chunk) {
397
- await invokeValhalla(route, i, chunk, country, threshold, results)
390
+ promises.push(invokeValhalla(route, i, chunk, country, threshold, results))
398
391
  }
392
+ await Promise.all(promises)
399
393
  const reduced = results.reduce((acc, cur, idx, src) => {
400
394
  const last = acc.length && acc.slice(-1)[0]
401
395
  if (last && new Date(cur.fixTime) - new Date(src[idx - 1].fixTime) < 1000 * 60 * 2) {
@@ -418,63 +412,6 @@ async function getOSMSpeedingEvents (devices, routes, threshold, minimumMinutes
418
412
  return events.filter(e => e.eventTime >= minimumMinutes * 60 * 1000)
419
413
  }
420
414
 
421
- async function getHereEvents (devices, routes, threshold, minimumMinutes = 0) {
422
- try {
423
- console.log('PROCESS_HERE_EVENTS', process.env.PROCESS_HERE_EVENTS)
424
- console.log('here speed limit events', devices.length, 'devices')
425
- const events = []
426
- const promises = devices.map(d => new Promise((resolve) => {
427
- const positions = routes.filter(p => p.deviceId === d.id)
428
- if (!positions.length) {
429
- console.log('no positions on device', d.name)
430
- resolve()
431
- }
432
- console.log(d.name, 'sending', positions.length, 'to here')
433
- here.routeMatch(positions).then(results => {
434
- console.log(d.name, 'got', results.length, 'from here')
435
- const timestamps = []
436
- for (const p of positions) {
437
- timestamps[new Date(p.fixTime).getTime()] = p
438
- }
439
- const reduced = results.reduce((acc, cur, idx, src) => {
440
- cur.overSpeeding = cur.currentSpeedKmh > parseInt(cur.speedLimit) + (threshold || 0)
441
- const last = acc.length && acc.slice(-1)[0]
442
- cur.position = timestamps[cur.timestamp]
443
- if (cur.overSpeeding && last && src[idx - 1].overSpeeding && last.speedLimit === cur.speedLimit) {
444
- last.eventTime = cur.timestamp - last.timestamp
445
- last.attributes.maxSpeed = Math.max(last.attributes.maxSpeed, cur.position.speed)
446
- last.distance = distance.default(point([cur.position.longitude, cur.position.latitude]), point([last.position.longitude, last.position.latitude]))
447
- } else if (cur.overSpeeding) {
448
- cur.positionId = cur.position && cur.position.id
449
- cur.roadSpeedLimit = cur.speedLimit
450
- cur.deviceId = d.id
451
- cur.attributes = { speedLimit: cur.speedLimit, speed: cur.currentSpeedKmh / 1.85200, maxSpeed: cur.position.speed }
452
- cur.eventTime = 0
453
- cur.distance = 0
454
- acc.push(cur)
455
- }
456
- return acc
457
- }, [])
458
- if (!reduced.length) {
459
- console.log('empty array after filter on device', d.name)
460
- resolve()
461
- } else {
462
- console.log(d.name, 'reduced to', reduced.length)
463
- }
464
- events.push(...reduced)
465
- resolve()
466
- }).catch(e => {
467
- console.warn('route match, moving on', e.message, e.response && e.response.data)
468
- resolve()
469
- })
470
- }))
471
- await Promise.all(promises)
472
- return events.filter(e => e.eventTime >= minimumMinutes * 60 * 1000)
473
- } catch (e) {
474
- console.error(e)
475
- }
476
- }
477
-
478
415
  function calculateEventData (positions, pIndex, alert, device, geofence) {
479
416
  let endEventPosition = alert.position
480
417
  let maxSpeed = alert.position.speed
@@ -485,7 +422,7 @@ function calculateEventData (positions, pIndex, alert, device, geofence) {
485
422
  point([positions[pIndex].longitude, positions[pIndex].latitude]))
486
423
  endEventPosition = positions[pIndex]
487
424
  if (endEventPosition.speed <= alert.attributes.speedLimit || (geofence && !insideGeofence(endEventPosition, geofence))) {
488
- alert.eventTime = new Date(endEventPosition.fixTime) - new Date(alert.position.fixTime) + (device.attributes.overspeedMinimalDuration || 0)*1000
425
+ alert.eventTime = new Date(endEventPosition.fixTime) - new Date(alert.position.fixTime) + (device.attributes.overspeedMinimalDuration || 0) * 1000
489
426
  alert.attributes.maxSpeed = maxSpeed
490
427
  alert.distance = dist
491
428
  break
@@ -13,12 +13,7 @@ process.env.TRACCAR_API_REPORTS = 'https://ltqgfyvcklxzaonv7h4rlmghai0rszop.lamb
13
13
  process.env.SERVER_HOST = 'api.pinme.io'
14
14
  const getReports = async (email, password) => {
15
15
  try {
16
- email = email || process.env.email
17
- password = password || process.env.password
18
- console.log('email / pass', email, password)
19
- const formData = `email=${encodeURIComponent(email)}&password=${encodeURIComponent(password)}`
20
- const [cookie] = (await axios.post('/session', formData)).headers['set-cookie']
21
- axios.defaults.headers.common.cookie = cookie
16
+ axios.defaults.headers.common.cookie = process.env.COOKIE
22
17
  return new Index(traccarConfig, axios)
23
18
  } catch (e) {
24
19
  console.error(e)
@@ -4,8 +4,8 @@ const { getCountry } = require('../util/utils')
4
4
 
5
5
  async function getSpeedingReport (report, userData) {
6
6
  const data = await report.speedingReport(
7
- new Date(Date.UTC(2023, 9, 4, 0, 0, 0, 0)),
8
- new Date(Date.UTC(2023, 9, 4, 23, 59, 59, 0)),
7
+ new Date(Date.UTC(2025, 4, 1, 0, 0, 0, 0)),
8
+ new Date(Date.UTC(2025, 4, 31, 23, 59, 59, 0)),
9
9
  userData)
10
10
 
11
11
  const device = data[0].devices[0]
@@ -19,16 +19,11 @@ describe('speeding tests', function () {
19
19
  it('works with road speed limits in Chile', async () => {
20
20
  const report = await getReports(process.env.USER_CHILE, process.env.PASS_CHILE)
21
21
  const userData = await report.getUserData()
22
- userData.devices = [userData.devices[0]]
22
+ // userData.devices = [userData.devices[0]]
23
23
  userData.roadSpeedLimits = true
24
24
  const { device } = await getSpeedingReport(report, userData)
25
25
  assert.equal(device.alerts.length, 6) // Total Alerts
26
26
  console.log(device.alerts)
27
-
28
- userData.minimumIdleMinutes = 1
29
- const r = await getSpeedingReport(report, userData)
30
- r.device.alerts.forEach(a => assert.equal(true, a.eventTime >= 60000))
31
- assert.equal(r.device.alerts.length, 2) // Total Alerts
32
27
  }, 200000)
33
28
 
34
29
  // eslint-disable-next-line no-undef