fleetmap-reports 1.0.488 → 1.0.490

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.488",
3
+ "version": "1.0.490",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -68,6 +68,10 @@ async function createSpeedingReportByGroup (from, to, userData, traccarInstance)
68
68
  return reportData
69
69
  }
70
70
 
71
+ function processServerSide () {
72
+ return (process.env.PROCESS_HERE_EVENTS && process.env.PROCESS_HERE_EVENTS === 'server')
73
+ }
74
+
71
75
  async function createSpeedingReportByDevice (from, to, userData, traccarInstance) {
72
76
  const devices = devicesToProcess(userData)
73
77
 
@@ -81,6 +85,10 @@ async function createSpeedingReportByDevice (from, to, userData, traccarInstance
81
85
  let deviceCount = 0
82
86
  for (const slice of sliced) {
83
87
  const { routes, events } = await getEvents(traccarInstance, from, to, slice, userData, deviceCount, devices.length, slice.length)
88
+ if (processServerSide()) {
89
+ console.log('LOADING_MESSAGE:' + slice[0].name)
90
+ console.log(`PROGRESS_PERC:${deviceCount / devices.length * 100}`)
91
+ }
84
92
  if (events.length > 0) {
85
93
  const devicesProcessed = await processDevices(from, to, slice, events, routes, userData)
86
94
  allData.devices.push(...devicesProcessed)
@@ -111,8 +119,8 @@ async function createSpeedingReportByDriver (from, to, userData, traccarInstance
111
119
  }
112
120
 
113
121
  async function getEvents (traccarInstance, from, to, devices, userData, deviceCount, totalDevices, sliceSize) {
114
- if (process.env.PROCESS_HERE_EVENTS && process.env.PROCESS_HERE_EVENTS === 'server') {
115
- const url = `${process.env.REPORTS_SERVER}/reports/speeding-report/getEvents`
122
+ if (processServerSide()) {
123
+ const url = `https://${process.env.SERVER_HOST}/reports/speeding-report/getEvents`
116
124
  return axios.post(url, { from, to, devices, userData, deviceCount, totalDevices, sliceSize }, { withCredentials: true }).then(d => d.data)
117
125
  } else {
118
126
  const geofencesFeatures = userData.geofences.filter(g => g.area.startsWith('POLYGON') &&
@@ -226,9 +234,7 @@ async function findEventsPosition (from, to, devices, events, routes, userData)
226
234
  for (const d of devices) {
227
235
  const deviceEvents = events.filter(e => e.deviceId === d.id && e.positionId)
228
236
  if (deviceEvents.length > 0) {
229
- deviceEvents.sort((a, b) => new Date(a.position.fixTime) - new Date(b.position.fixTime))
230
237
  const positions = routes.filter(p => p.deviceId === d.id)
231
-
232
238
  for (const a of deviceEvents) {
233
239
  const pIndex = positions.findIndex(p => p.id === a.positionId)
234
240
  if (pIndex > 0) {
@@ -254,6 +260,7 @@ async function findEventsPosition (from, to, devices, events, routes, userData)
254
260
  return pIndex
255
261
  }
256
262
  }
263
+ deviceEvents.sort((a, b) => new Date(a.position.fixTime) - new Date(b.position.fixTime))
257
264
  }
258
265
  }
259
266
  }
@@ -271,6 +278,7 @@ async function getCustomSpeedLimitEvents (devices, routes, customSpeed) {
271
278
  if (position.speed > maxSpeed && !eventIsActive) {
272
279
  const event = {
273
280
  positionId: position && position.id,
281
+ position,
274
282
  deviceId: d.id,
275
283
  attributes: {
276
284
  speedLimit: maxSpeed,
@@ -291,52 +299,47 @@ async function getCustomSpeedLimitEvents (devices, routes, customSpeed) {
291
299
  async function getHereEvents (devices, routes, threshold) {
292
300
  try {
293
301
  console.log('env', process.env.PROCESS_HERE_EVENTS)
294
- if (process.env.PROCESS_HERE_EVENTS && process.env.PROCESS_HERE_EVENTS === 'server') {
295
- const url = `${process.env.REPORTS_SERVER}/reports/here`
296
- return axios.post(url, { devices, routes, threshold }).then(d => d.data)
297
- } else {
298
- console.log('here speed limit events')
299
- const events = []
300
- const promises = devices.map(d => new Promise((resolve) => {
301
- const positions = routes.filter(p => p.deviceId === d.id)
302
- if (!positions.length) {
303
- console.log('no positions on device', d.name)
304
- resolve()
305
- }
306
- here.routeMatch(positions).then(results => {
307
- const reduced = results.reduce((acc, cur, idx, src) => {
308
- cur.overSpeeding = cur.currentSpeedKmh > parseInt(cur.speedLimit) + (threshold || 0)
309
- const last = acc.length && acc.slice(-1)[0]
310
- cur.position = positions.find(p => new Date(p.fixTime).getTime() === cur.timestamp)
311
- if (cur.overSpeeding && last && src[idx - 1].overSpeeding && last.speedLimit === cur.speedLimit) {
312
- last.eventTime = cur.timestamp - last.timestamp
313
- last.attributes.maxSpeed = Math.max(last.attributes.maxSpeed, cur.position.speed)
314
- last.distance = distance.default(point([cur.position.longitude, cur.position.latitude]), point([last.position.longitude, last.position.latitude]))
315
- } else if (cur.overSpeeding) {
316
- cur.positionId = cur.position && cur.position.id
317
- cur.roadSpeedLimit = cur.speedLimit
318
- cur.deviceId = d.id
319
- cur.attributes = { speedLimit: cur.speedLimit, speed: cur.currentSpeedKmh / 1.85200, maxSpeed: cur.position.speed }
320
- cur.eventTime = 0
321
- cur.distance = 0
322
- acc.push(cur)
323
- }
324
- return acc
325
- }, [])
326
- if (!reduced.length) {
327
- console.log('empty array after filter on device', d.name)
328
- resolve()
302
+ console.log('here speed limit events')
303
+ const events = []
304
+ const promises = devices.map(d => new Promise((resolve) => {
305
+ const positions = routes.filter(p => p.deviceId === d.id)
306
+ if (!positions.length) {
307
+ console.log('no positions on device', d.name)
308
+ resolve()
309
+ }
310
+ here.routeMatch(positions).then(results => {
311
+ const reduced = results.reduce((acc, cur, idx, src) => {
312
+ cur.overSpeeding = cur.currentSpeedKmh > parseInt(cur.speedLimit) + (threshold || 0)
313
+ const last = acc.length && acc.slice(-1)[0]
314
+ cur.position = positions.find(p => new Date(p.fixTime).getTime() === cur.timestamp)
315
+ if (cur.overSpeeding && last && src[idx - 1].overSpeeding && last.speedLimit === cur.speedLimit) {
316
+ last.eventTime = cur.timestamp - last.timestamp
317
+ last.attributes.maxSpeed = Math.max(last.attributes.maxSpeed, cur.position.speed)
318
+ last.distance = distance.default(point([cur.position.longitude, cur.position.latitude]), point([last.position.longitude, last.position.latitude]))
319
+ } else if (cur.overSpeeding) {
320
+ cur.positionId = cur.position && cur.position.id
321
+ cur.roadSpeedLimit = cur.speedLimit
322
+ cur.deviceId = d.id
323
+ cur.attributes = { speedLimit: cur.speedLimit, speed: cur.currentSpeedKmh / 1.85200, maxSpeed: cur.position.speed }
324
+ cur.eventTime = 0
325
+ cur.distance = 0
326
+ acc.push(cur)
329
327
  }
330
- events.push(...reduced)
328
+ return acc
329
+ }, [])
330
+ if (!reduced.length) {
331
+ console.log('empty array after filter on device', d.name)
331
332
  resolve()
332
- }).catch(e => {
333
- console.error('error on route match, moving on', e)
334
- resolve()
335
- })
336
- }))
337
- await Promise.all(promises)
338
- return events
339
- }
333
+ }
334
+ events.push(...reduced)
335
+ resolve()
336
+ }).catch(e => {
337
+ console.error('error on route match, moving on', e)
338
+ resolve()
339
+ })
340
+ }))
341
+ await Promise.all(promises)
342
+ return events
340
343
  } catch (e) {
341
344
  console.error(e)
342
345
  }