fleetmap-reports 1.0.339 → 1.0.344

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.339",
3
+ "version": "1.0.344",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -28,6 +28,7 @@
28
28
  },
29
29
  "devDependencies": {
30
30
  "axios-debug-log": "^0.8.4",
31
+ "jest": "^27.5.0",
31
32
  "mocha": "^9.0.3",
32
33
  "webpack": "^5.24.3",
33
34
  "webpack-cli": "^4.5.0"
@@ -9,7 +9,7 @@ const {getStyle} = require("./reportStyle")
9
9
  const {getUserPartner} = require("fleetmap-partners")
10
10
  const traccar = require("./util/traccar")
11
11
  const {isInsideTimetable, isPartialInsideTimetable} = require("./util/trips");
12
- const trips = require("./util/trips");
12
+ const tripHelper = require("./util/trips");
13
13
 
14
14
  const fileName = 'ActivityReport'
15
15
 
@@ -58,7 +58,7 @@ async function createActivityReportByGroup(from, to, userData, traccarInstance){
58
58
  if (summaries.length > 0) {
59
59
 
60
60
  console.log('Summary:' + summaries.length)
61
- groupData.devices = processDevices(from, to, devices, {summaries: summaries, trips: trips})
61
+ groupData.devices = processDevices(from, to, devices, {summaries: summaries, trips: trips}, userData)
62
62
 
63
63
  reportData.push(groupData)
64
64
  }
@@ -86,16 +86,13 @@ async function createActivityReportByDevice(from, to, userData, traccarInstance)
86
86
  devicesToProcess.sort((a, b) => (a.name > b.name) ? 1 : -1)
87
87
 
88
88
  let summaries = []
89
- let routeData = []
90
- const allInOne = await traccar.getAllInOne(traccarInstance, from, to, devicesToProcess, userData.groupByDay && !userData.allWeek, true, false, !userData.groupByDay)
91
- const tripsData = allInOne.trips
89
+ const {trips, route, summary} = await traccar.getAllInOne(traccarInstance, from, to, devicesToProcess, userData.groupByDay && !userData.allWeek, true, false, !userData.groupByDay)
92
90
 
93
91
  if(userData.groupByDay) {
94
- routeData = allInOne.route
95
- console.log('trips:' + tripsData.length)
92
+ console.log('trips:' + trips.length)
96
93
 
97
- if (tripsData.length > 0) {
98
- trips.checkTripsKms(traccarInstance, from, to, devicesToProcess, {trips: tripsData, route: routeData})
94
+ if (trips.length) {
95
+ tripHelper.checkTripsKms(traccarInstance, from, to, devicesToProcess, {trips, route})
99
96
  }
100
97
 
101
98
  const dates = getDates(from, to)
@@ -110,14 +107,14 @@ async function createActivityReportByDevice(from, to, userData, traccarInstance)
110
107
  }
111
108
  }
112
109
  } else {
113
- summaries = allInOne.summary
110
+ summaries = summary
114
111
  }
115
112
 
116
113
  console.log('Summary:' + summaries.length)
117
114
 
118
115
  //Process report data
119
- if (summaries.length || tripsData.length) {
120
- allData.devices = processDevices(from, to, devicesToProcess, {summaries: summaries, trips: tripsData, route: routeData})
116
+ if (summaries.length || trips.length) {
117
+ allData.devices = processDevices(from, to, devicesToProcess, {summaries: summaries, trips, route}, userData)
121
118
  }
122
119
 
123
120
  return allData
@@ -175,20 +172,21 @@ function processDevices(from, to, devices, data, userData) {
175
172
  return a > b.maxSpeed ? a : b.maxSpeed
176
173
  }, 0),
177
174
  endOdometer: 0,
178
- startOdometer: 0
175
+ startOdometer: 0,
176
+ startTime: tripsByDay.length && tripsByDay[0].startTime,
177
+ endTime: tripsByDay.length && tripsByDay[tripsByDay.length-1].endTime
179
178
  })
180
179
  } else {
181
- const summaryCurrentDay = data.summaries.filter(s => {
182
- console.log(s.date.getTime(), date.getTime(), s.date.getTime() === date.getTime())
180
+ const summaryCurrentDay = data.summaries.find(s => {
183
181
  return s.deviceId === d.id && s.date.getTime() === date.getTime()})
184
182
 
185
- if (summaryCurrentDay.length) {
186
- summaryCurrentDay.forEach(s => {
187
- s.distance = distance
188
- s.convertedSpentFuel = automaticReports.calculateSpentFuel(s.spentFuel, d)
189
- })
183
+ if (summaryCurrentDay) {
184
+ summaryCurrentDay.distance = distance
185
+ summaryCurrentDay.convertedSpentFuel = automaticReports.calculateSpentFuel(summaryCurrentDay.spentFuel, d)
186
+ summaryCurrentDay.startTime = tripsByDay.length && tripsByDay[0].startTime
187
+ summaryCurrentDay.endTime = tripsByDay.length && tripsByDay[tripsByDay.length-1].endTime
190
188
 
191
- summary.push(...summaryCurrentDay)
189
+ summary.push(summaryCurrentDay)
192
190
  }
193
191
  }
194
192
  }
@@ -197,8 +195,11 @@ function processDevices(from, to, devices, data, userData) {
197
195
 
198
196
  if (summary.length) {
199
197
  summary.forEach(s => {
200
- s.distance = data.trips.filter(t => t.deviceId === d.id).reduce((a, b) => a + b.distance, 0)
198
+ const deviceTrips = data.trips.filter(t => t.deviceId === d.id)
199
+ s.distance = deviceTrips.reduce((a, b) => a + b.distance, 0)
201
200
  s.convertedSpentFuel = automaticReports.calculateSpentFuel(s.spentFuel, d)
201
+ s.startTime = deviceTrips.length && deviceTrips[0].startTime
202
+ s.endTime = deviceTrips.length && deviceTrips[deviceTrips.length-1].endTime
202
203
  })
203
204
  }
204
205
  }
@@ -238,7 +239,9 @@ function processDrivers(from, to, drivers, data, userData) {
238
239
  distance: tripsByDay.filter(t => t.distance > 0).reduce((a, b) => a + b.distance, 0),
239
240
  engineHours: tripsByDay.reduce((a, b) => a + b.duration, 0),
240
241
  maxSpeed: tripsByDay.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
241
- averageSpeed: tripsByDay.length > 0 ? Math.round(tripsByDay.reduce((a, b) => a + b.averageSpeed, 0) / tripsByDay.length) : 0,
242
+ averageSpeed: tripsByDay.length ? Math.round(tripsByDay.reduce((a, b) => a + b.averageSpeed, 0) / tripsByDay.length) : 0,
243
+ startTime: tripsByDay.length && tripsByDay[0].startTime,
244
+ endTime: tripsByDay.length && tripsByDay[tripsByDay.length-1].endTime
242
245
  })
243
246
  }
244
247
  } else {
@@ -247,6 +250,8 @@ function processDrivers(from, to, drivers, data, userData) {
247
250
  engineHours: trips.reduce((a, b) => a + b.duration, 0),
248
251
  maxSpeed: trips.reduce((a, b) => Math.max(a, b.maxSpeed), 0),
249
252
  averageSpeed: Math.round(trips.reduce((a, b) => a + b.averageSpeed, 0) / trips.length),
253
+ startTime: trips.length && trips[0].startTime,
254
+ endTime: trips.length && trips[trips.length-1].endTime
250
255
  })
251
256
  }
252
257
  driversResult.push(driverData)
package/src/index.test.js CHANGED
@@ -1,24 +1,184 @@
1
1
  const {getReports} = require("./tests");
2
+ const assert = require("assert");
2
3
 
3
- describe('speedingReport', function() {
4
- this.timeout(500000)
5
- it('test speeding', async () => {
4
+ describe('Test_Reports', function() {
5
+ it('Speeding by device', async () => {
6
6
  const report = await getReports()
7
7
  const userData = await report.getUserData()
8
8
 
9
- await report.speedingReport(new Date(2021, 2, 30, 0, 0, 0, 0),
10
- new Date(2021, 2, 30, 23, 59, 59, 0),
9
+ const data = await report.speedingReport(new Date(2021, 11, 6, 0, 0, 0, 0),
10
+ new Date(2021, 11, 8, 23, 59, 59, 0),
11
11
  userData)
12
- })
13
- it('test kmsReport by day', async () => {
12
+ assert.equal(data.length, 1)
13
+ const device = data[0].devices.find(d => d.device.id===22326)
14
+ const totalDistance = device.alerts.reduce((a, b) => a + b.distance, 0)
15
+ const totalEventTime = device.alerts.reduce((a, b) => a + b.eventTime, 0)
16
+ assert.equal(device.alerts.length, 15) // Total Alerts
17
+ assert.equal(totalDistance, 19.605972526552154) // Total Kms
18
+ assert.equal(totalEventTime, 562000) // Total Duration
19
+ }, 20000)
20
+ it('Speeding by device with custom speed', async () => {
21
+ const report = await getReports()
22
+ const userData = await report.getUserData()
23
+
24
+ userData.useVehicleSpeedLimit = false
25
+ userData.customSpeed = 100
26
+
27
+ const data = await report.speedingReport(new Date(2021, 11, 6, 0, 0, 0, 0),
28
+ new Date(2021, 11, 8, 23, 59, 59, 0),
29
+ userData)
30
+ assert.equal(data.length, 1)
31
+ const device = data[0].devices.find(d => d.device.id===22326)
32
+ const totalDistance = device.alerts.reduce((a, b) => a + b.distance, 0)
33
+ const totalEventTime = device.alerts.reduce((a, b) => a + b.eventTime, 0)
34
+ assert.equal(device.alerts.length, 24) // Total Alerts
35
+ assert.equal(totalDistance, 49.49653008204987) // Total Kms
36
+ assert.equal(totalEventTime, 1523000) // Total Duration
37
+ }, 20000)
38
+ it('Trip by device', async () => {
39
+ const report = await getReports()
40
+ const userData = await report.getUserData()
41
+
42
+ const data = await report.tripReport(new Date(2022, 0, 3, 0, 0, 0, 0),
43
+ new Date(2022, 0, 4, 23, 59, 59, 0),
44
+ userData)
45
+
46
+ assert.equal(data.length, 1)
47
+ const device = data[0].devices.find(d => d.device.id===22326)
48
+ assert.equal(device.trips.length, 7) // Total Trips
49
+ assert.equal(device.totalDistance, 339204.53999999166) // Total Kms
50
+ }, 20000)
51
+ it('Trip by device with time period ', async () => {
52
+ const report = await getReports()
53
+ const userData = await report.getUserData()
54
+
55
+ userData.allWeek = false
56
+ userData.weekDays = {
57
+ sunday: true,
58
+ monday: true,
59
+ tuesday: true,
60
+ wednesday: true,
61
+ thursday: true,
62
+ friday: true,
63
+ saturday: true
64
+ }
65
+ userData.dayHours = {
66
+ startTime: '15:00',
67
+ endTime: '19:00'
68
+ }
69
+
70
+ const data = await report.tripReport(new Date(2022, 0, 3, 0, 0, 0, 0),
71
+ new Date(2022, 0, 4, 23, 59, 59, 0),
72
+ userData)
73
+
74
+ assert.equal(data.length, 1)
75
+ const device = data[0].devices.find(d => d.device.id===22326)
76
+ assert.equal(device.trips.length, 4) // Total Trips
77
+ assert.equal(device.totalDistance, 265155.1099999994) // Total Kms
78
+ },20000)
79
+ it('Trip by device with time period 2', async () => {
80
+ const report = await getReports()
81
+ const userData = await report.getUserData()
82
+
83
+ userData.allWeek = false
84
+ userData.weekDays = {
85
+ sunday: true,
86
+ monday: true,
87
+ tuesday: true,
88
+ wednesday: true,
89
+ thursday: true,
90
+ friday: true,
91
+ saturday: true
92
+ }
93
+ userData.dayHours = {
94
+ startTime: '18:00',
95
+ endTime: '09:00'
96
+ }
97
+
98
+ const data = await report.tripReport(new Date(2022, 0, 10, 0, 0, 0, 0),
99
+ new Date(2022, 0, 14, 23, 59, 59, 0),
100
+ userData)
101
+
102
+ assert.equal(data.length, 1)
103
+ const device = data[0].devices.find(d => d.device.id===22326)
104
+ assert.equal(device.trips.length, 18) // Total Trips
105
+ assert.equal(device.totalDistance, 133505.6100000143) // Total Kms
106
+ },20000)
107
+ it('Location by device', async () => {
108
+ const report = await getReports()
109
+ const userData = await report.getUserData()
110
+
111
+ const data = await report.locationReport(new Date(2022, 0, 4, 0, 0, 0, 0),
112
+ new Date(2022, 0, 4, 23, 59, 59, 0),
113
+ userData)
114
+
115
+ assert.equal(data.length, 1)
116
+ const device = data[0].devices.find(d => d.device.id===22326)
117
+ assert.equal(device.positions.length, 708) // Total Positions
118
+ },20000)
119
+ it('KmsReport by device', async () => {
120
+ const reports = await getReports()
121
+ const userData = await reports.getUserData()
122
+ const data = await reports.kmsReport(new Date(2021, 10, 1, 0, 0,0), new Date(2021, 10, 31, 23, 59, 59),
123
+ userData)
124
+ assert.equal(data.length, 1)
125
+ assert.equal(data.length, 1)
126
+ const device = data[0].devices.find(d => d.device.id===22326)
127
+ assert.equal(device.summary.distance, 1193284.3100000024) // Total Kms
128
+ },30000)
129
+ it('KmsReport byDevice groupByDay', async () => {
14
130
  const reports = await getReports()
15
131
  const userData = await reports.getUserData()
16
132
  userData.groupByDay = true
17
- const report = await reports.kmsReport(new Date(2021, 9, 30), new Date(2021, 10),
133
+ const data = await reports.kmsReport(new Date(2021, 10, 1, 0, 0,0), new Date(2021, 10, 10, 23, 59, 59),
18
134
  userData)
19
- console.log('report', report)
20
- console.log(reports.kmsReportToExcel(userData, report[0]))
21
- })
135
+ assert.equal(data.length, 1)
136
+ assert.equal(data.length, 1)
137
+ const device = data[0].devices.find(d => d.device.id===22326)
138
+ assert.equal(device.days.length, 10) // Total Kms
139
+ assert.equal(device.days[5].kms, 23183.010000005364) // Total Kms
140
+ },90000)
141
+ it('Idle by device', async () => {
142
+ const report = await getReports()
143
+ const userData = await report.getUserData()
144
+ userData.minimumIdleMinutes = 2
145
+ const data = await report.idleReport(new Date(2022, 0, 3, 0, 0, 0, 0),
146
+ new Date(2022, 0, 7, 23, 59, 59, 0),
147
+ userData)
148
+ assert.equal(data.length, 1)
149
+ const device = data[0].devices.find(d => d.device.id===22326)
150
+ const totalIdleTime = device.idleEvents.reduce((a, b) => a + b.idleTime, 0)
151
+ assert.equal(device.idleEvents.length, 8) // Total Alerts
152
+ assert.equal(totalIdleTime, 1294000) // Total Duration
153
+ },20000)
154
+ it('Activity by device', async () => {
155
+ const report = await getReports()
156
+ const userData = await report.getUserData()
157
+
158
+ const data = await report.activityReport(new Date(2022, 0, 1, 0, 0, 0, 0),
159
+ new Date(2022, 0, 31, 23, 59, 59, 0),
160
+ userData)
161
+ assert.equal(data.length, 1)
162
+ const device = data[0].devices.find(d => d.device.id===22326)
163
+ assert.equal(device.summary[0].startOdometer, 122502742.59)
164
+ assert.equal(device.summary[0].distance, 1386519.1300000101)
165
+ assert.equal(device.summary[0].startTime, '2022-01-01T13:35:47.000+0000')
166
+ assert.equal(device.summary[0].endTime, '2022-01-30T13:48:18.000+0000')
167
+ }, 20000)
168
+ it('Activity byDevice groupByDay', async () => {
169
+ const report = await getReports()
170
+ const userData = await report.getUserData()
171
+ userData.groupByDay = true
172
+ const data = await report.activityReport(new Date(2022, 0, 1, 0, 0, 0, 0),
173
+ new Date(2022, 0, 20, 23, 59, 59, 0),
174
+ userData)
175
+ assert.equal(data.length, 1)
176
+ const device = data[0].devices.find(d => d.device.id===22326)
177
+ assert.equal(device.summary[5].startOdometer, 122923290.95)
178
+ assert.equal(device.summary[5].distance, 77020.37999999523)
179
+ assert.equal(device.summary[5].startTime, '2022-01-06T18:35:04.000+0000')
180
+ assert.equal(device.summary[5].endTime, '2022-01-06T19:54:27.000+0000')
181
+ }, 60000)
22
182
  it('test allinone', async() => {
23
183
  console.log('Start')
24
184
  const reports = await getReports()
@@ -31,6 +191,28 @@ describe('speedingReport', function() {
31
191
  true,
32
192
  true,
33
193
  false)
34
- console.log('getAllInOne',r)
35
- })
194
+
195
+ assert.equal(r.trips.filter(t => t.deviceId === 22327).length, 10)
196
+ assert.equal(r.stops.filter(t => t.deviceId === 22327).length, 11)
197
+ }, 20000)
198
+ it('Total KMS', async () => {
199
+ const report = await getReports()
200
+ const userData = await report.getUserData()
201
+
202
+ const tripsReport = await report.tripReport(new Date(2022, 1, 11, 0, 0, 0, 0),
203
+ new Date(2022, 1, 14, 23, 59, 59, 0),
204
+ userData)
205
+
206
+ const kmsReport = await report.kmsReport(new Date(2022, 1, 11, 0, 0, 0, 0),
207
+ new Date(2022, 1, 14, 23, 59, 59, 0),
208
+ userData)
209
+
210
+ assert.equal(tripsReport.length, 1)
211
+ const device1 = tripsReport[0].devices.find(d => d.device.id===11681)
212
+ assert.equal(device1.trips.length, 44) // Total Trips
213
+ assert.equal(device1.totalDistance, 883076.9928612101) // Total Kms
214
+
215
+ const device2 = kmsReport[0].devices.find(d => d.device.id===11681)
216
+ assert.equal(device2.summary.distance, device1.totalDistance) // Total Kms
217
+ }, 30000)
36
218
  })
@@ -84,8 +84,8 @@ async function getAllInOne(traccar, from, to, devices, getRoutes, getTrips, getS
84
84
  return {
85
85
  route: result.filter(t => t.route).map(r => r.route).flat(),
86
86
  trips: result.filter(t => t.trips).map(r => r.trips).flat(),
87
- stops: result.filter(t => t.result).map(r => r.stops).flat(),
88
- summary: result.filter(t => t.summay).map(r => r.summary).flat(),
87
+ stops: result.filter(t => t.stops).map(r => r.stops).flat(),
88
+ summary: result.filter(t => t.summary).map(r => r.summary).flat(),
89
89
  }
90
90
  }
91
91
 
package/src/util/trips.js CHANGED
@@ -34,7 +34,18 @@ function checkTripsKms(traccarInstance, from, to, devices, data) {
34
34
  const tripRoute = data.route.filter(p => p.deviceId === t.deviceId
35
35
  && new Date(p.fixTime).getTime() >= new Date(t.startTime).getTime()
36
36
  && new Date(p.fixTime).getTime() <= new Date(t.endTime).getTime())
37
- t.distance = tripRoute.reduce((a, b) => a + b.attributes.distance, 0)
37
+
38
+ let current = null
39
+ const distances = []
40
+ for(const p of tripRoute) {
41
+ if(current) {
42
+ distances.push(coordsDistance(parseFloat(current.longitude),parseFloat(current.latitude),
43
+ parseFloat(p.longitude),parseFloat(p.latitude)))
44
+ }
45
+ current = p
46
+ }
47
+
48
+ t.distance = distances.reduce((a, b) => a + b, 0)
38
49
  }
39
50
  })
40
51
  }
package/src/here.test.js DELETED
@@ -1,25 +0,0 @@
1
- const here = require('./here')
2
-
3
- require('axios-debug-log')({
4
- request: function (debug, config) {
5
- console.log('Req headers:', config.headers.common)
6
- },
7
- response: function (debug, response) {
8
- console.log(
9
- 'Response Headers:', response.headers,
10
- 'from', response.config.url
11
- )
12
- },
13
- error: function (debug, error) {
14
- // Read https://www.npmjs.com/package/axios#handling-errors for more info
15
- console.error('Boom', error)
16
- }
17
- })
18
-
19
- describe('here', async () => {
20
- it('here', async () => {
21
- console.log(process.env.HERE_API_KEY)
22
- const rows = [{latitude:0, longitude:0, fixtime:new Date().toISOString(), speed:0, course:0}]
23
- console.dir(await here.routeMatch(rows), {depth: null})
24
- })
25
- })