fleetmap-reports 1.0.338 → 1.0.343

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.338",
3
+ "version": "1.0.343",
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
@@ -179,7 +176,6 @@ function processDevices(from, to, devices, data, userData) {
179
176
  })
180
177
  } else {
181
178
  const summaryCurrentDay = data.summaries.filter(s => {
182
- console.log(s.date.getTime(), date.getTime(), s.date.getTime() === date.getTime())
183
179
  return s.deviceId === d.id && s.date.getTime() === date.getTime()})
184
180
 
185
181
  if (summaryCurrentDay.length) {
package/src/index.test.js CHANGED
@@ -1,24 +1,180 @@
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
+ },20000)
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
+ },20000)
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
+ }, 20000)
166
+ it('Activity byDevice groupByDay', async () => {
167
+ const report = await getReports()
168
+ const userData = await report.getUserData()
169
+ userData.groupByDay = true
170
+ const data = await report.activityReport(new Date(2022, 0, 1, 0, 0, 0, 0),
171
+ new Date(2022, 0, 20, 23, 59, 59, 0),
172
+ userData)
173
+ assert.equal(data.length, 1)
174
+ const device = data[0].devices.find(d => d.device.id===22326)
175
+ assert.equal(device.summary[5].startOdometer, 122923290.95)
176
+ assert.equal(device.summary[5].distance, 77020.37999999523)
177
+ }, 60000)
22
178
  it('test allinone', async() => {
23
179
  console.log('Start')
24
180
  const reports = await getReports()
@@ -31,6 +187,28 @@ describe('speedingReport', function() {
31
187
  true,
32
188
  true,
33
189
  false)
34
- console.log('getAllInOne',r)
35
- })
190
+
191
+ assert.equal(r.trips.filter(t => t.deviceId === 22327).length, 10)
192
+ assert.equal(r.stops.filter(t => t.deviceId === 22327).length, 11)
193
+ }, 20000)
194
+ it('Total KMS', async () => {
195
+ const report = await getReports()
196
+ const userData = await report.getUserData()
197
+
198
+ const tripsReport = await report.tripReport(new Date(2022, 1, 11, 0, 0, 0, 0),
199
+ new Date(2022, 1, 14, 23, 59, 59, 0),
200
+ userData)
201
+
202
+ const kmsReport = await report.kmsReport(new Date(2022, 1, 11, 0, 0, 0, 0),
203
+ new Date(2022, 1, 14, 23, 59, 59, 0),
204
+ userData)
205
+
206
+ assert.equal(tripsReport.length, 1)
207
+ const device1 = tripsReport[0].devices.find(d => d.device.id===11681)
208
+ assert.equal(device1.trips.length, 44) // Total Trips
209
+ assert.equal(device1.totalDistance, 883076.9928612101) // Total Kms
210
+
211
+ const device2 = kmsReport[0].devices.find(d => d.device.id===11681)
212
+ assert.equal(device2.summary.distance, device1.totalDistance) // Total Kms
213
+ }, 30000)
36
214
  })
@@ -82,10 +82,10 @@ async function getAllInOne(traccar, from, to, devices, getRoutes, getTrips, getS
82
82
  }))
83
83
  const result = await Promise.all(requests)
84
84
  return {
85
- route: result.map(r => r.route).flat(),
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.map(r => r.stops).flat(),
88
- summary: result.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
- })