fleetmap-reports 1.0.651 → 1.0.653

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.651",
3
+ "version": "1.0.653",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -2,7 +2,6 @@ function devicesToProcess (userData) {
2
2
  const groupIds = userData.groups.map(g => g.id)
3
3
  const devices = userData.byGroup ? userData.devices.filter(d => !groupIds.includes(d.groupId)) : userData.devices
4
4
  devices.sort((a, b) => (a.name > b.name) ? 1 : -1)
5
-
6
5
  console.log('LOADING_MESSAGE:' + devices.map(d => d.name).join(', '))
7
6
  return devices
8
7
  }
@@ -27,7 +27,6 @@ async function createZoneReport (from, to, userData, traccar) {
27
27
  const url = `https://${process.env.SERVER_HOST}/reports/zone-report`
28
28
  return axios.post(url, { from, to, userData }, { withCredentials: true }).then(d => d.data)
29
29
  }
30
- console.log('Create ZoneReport')
31
30
  const reportData = []
32
31
  const types = ['geofenceEnter', 'geofenceExit']
33
32
 
@@ -84,9 +83,10 @@ async function createZoneReport (from, to, userData, traccar) {
84
83
  }
85
84
 
86
85
  function getNextIn (outDate, alerts, geofenceId, deviceRoute) {
87
- return alerts
86
+ const next = alerts
88
87
  .filter(a => a.type === 'geofenceEnter' && a.geofenceId === geofenceId)
89
- .find(a => new Date(a.position.fixTime).getTime() > outDate) ||
88
+ .find(a => new Date(a.position.fixTime).getTime() > outDate)
89
+ return (next && next.position && new Date(next.position.fixTime).getTime()) ||
90
90
  new Date(deviceRoute.slice(-1)[0].fixTime).getTime()
91
91
  }
92
92
 
@@ -102,47 +102,45 @@ async function processDevices (from, to, devices, userData, data) {
102
102
  const zoneInData = {}
103
103
 
104
104
  alerts.forEach(a => {
105
- if (a.position) {
106
- if (a.type === 'geofenceEnter') {
107
- const driver = userData.drivers.find(d => a.position.attributes && d.uniqueId === a.position.attributes.driverUniqueId)
108
- a.position.driverName = driver ? driver.name : ''
109
- zoneInData[a.geofenceId] = a
110
- } else if (a.type === 'geofenceExit') {
111
- const geofence = userData.geofences.find(g => g.id === a.geofenceId)
112
- if (zoneInData[a.geofenceId]) {
113
- const totalInTime = moment(a.position.fixTime).diff(moment(zoneInData[a.geofenceId].position.fixTime), 'seconds')
114
- const inDate = new Date(zoneInData[a.geofenceId].position.fixTime).getTime()
115
- const outDate = new Date(a.position.fixTime).getTime()
116
- const routeIn = deviceRoute.filter(p =>
117
- new Date(p.fixTime).getTime() >= inDate &&
105
+ if (!a.position) { return }
106
+ if (a.type === 'geofenceEnter') {
107
+ const driver = userData.drivers.find(d => a.position.attributes && d.uniqueId === a.position.attributes.driverUniqueId)
108
+ a.position.driverName = driver ? driver.name : ''
109
+ zoneInData[a.geofenceId] = a
110
+ } else if (a.type === 'geofenceExit') {
111
+ const geofence = userData.geofences.find(g => g.id === a.geofenceId)
112
+ if (!geofence) { return }
113
+ const outDate = new Date(a.position.fixTime).getTime()
114
+ const routeAfterOut = deviceRoute.filter(p =>
115
+ new Date(p.fixTime).getTime() >= outDate &&
116
+ new Date(p.fixTime).getTime() < getNextIn(outDate, alerts, a.geofenceId, deviceRoute)
117
+ )
118
+ const distanceOut = calculateDistance(routeAfterOut)
119
+ if (zoneInData[a.geofenceId]) {
120
+ const totalInTime = moment(a.position.fixTime).diff(moment(zoneInData[a.geofenceId].position.fixTime), 'seconds')
121
+ const inDate = new Date(zoneInData[a.geofenceId].position.fixTime).getTime()
122
+ const routeIn = deviceRoute.filter(p =>
123
+ new Date(p.fixTime).getTime() >= inDate &&
118
124
  new Date(p.fixTime).getTime() < outDate
119
- )
120
- const routeAfterOut = deviceRoute.filter(p =>
121
- new Date(p.fixTime).getTime() >= outDate &&
122
- new Date(p.fixTime).getTime() < getNextIn(outDate, alerts, a.geofenceId, deviceRoute)
123
- )
124
- if (geofence) {
125
- zoneInOutData.push({
126
- inTime: zoneInData[a.geofenceId].position,
127
- outTime: a.position,
128
- totalTime: totalInTime,
129
- distanceIn: calculateDistance(routeIn),
130
- distanceOut: calculateDistance(routeAfterOut),
131
- geofenceName: geofence.name,
132
- stopped: routeIn.filter(p => !p.attributes.ignition).length > 0,
133
- driverName: zoneInData[a.geofenceId].position.driverName
134
- })
135
- }
136
- zoneInData[a.geofenceId] = null
137
- } else {
138
- if (geofence) {
139
- zoneInOutData.push({
140
- outTime: a.position,
141
- geofenceName: geofence.name,
142
- driverName: ''
143
- })
144
- }
145
- }
125
+ )
126
+ zoneInOutData.push({
127
+ inTime: zoneInData[a.geofenceId].position,
128
+ outTime: a.position,
129
+ totalTime: totalInTime,
130
+ distanceIn: calculateDistance(routeIn),
131
+ distanceOut,
132
+ geofenceName: geofence.name,
133
+ stopped: routeIn.filter(p => !p.attributes.ignition).length > 0,
134
+ driverName: zoneInData[a.geofenceId].position.driverName
135
+ })
136
+ zoneInData[a.geofenceId] = null
137
+ } else {
138
+ zoneInOutData.push({
139
+ outTime: a.position,
140
+ geofenceName: geofence.name,
141
+ driverName: '',
142
+ distanceOut
143
+ })
146
144
  }
147
145
  }
148
146
  })