fleetmap-reports 1.0.247 → 1.0.248

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.
@@ -1,11 +1,11 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
2
  <project version="4">
3
3
  <component name="ChangeListManager">
4
- <list default="true" id="eb665c86-c893-4333-bd2a-721dc27980b9" name="Changes" comment="Kms Report - Calculate distance with traccar trips">
4
+ <list default="true" id="eb665c86-c893-4333-bd2a-721dc27980b9" name="Changes" comment="refactor speeding and trip report">
5
5
  <change beforePath="$PROJECT_DIR$/package.json" beforeDir="false" afterPath="$PROJECT_DIR$/package.json" afterDir="false" />
6
6
  <change beforePath="$PROJECT_DIR$/src/index.test.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/index.test.js" afterDir="false" />
7
- <change beforePath="$PROJECT_DIR$/src/speeding-report.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/speeding-report.js" afterDir="false" />
8
- <change beforePath="$PROJECT_DIR$/src/util/traccar.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/util/traccar.js" afterDir="false" />
7
+ <change beforePath="$PROJECT_DIR$/src/kms-report.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/kms-report.js" afterDir="false" />
8
+ <change beforePath="$PROJECT_DIR$/src/util/utils.js" beforeDir="false" afterPath="$PROJECT_DIR$/src/util/utils.js" afterDir="false" />
9
9
  </list>
10
10
  <option name="SHOW_DIALOG" value="false" />
11
11
  <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -37,23 +37,28 @@
37
37
  <property name="ts.external.directory.path" value="$APPLICATION_HOME_DIR$/plugins/JavaScriptLanguage/jsLanguageServicesImpl/external" />
38
38
  <property name="vue.rearranger.settings.migration" value="true" />
39
39
  </component>
40
- <component name="RunManager" selected="Mocha.speedingReport.test speeding">
41
- <configuration name="speedingReport" type="mocha-javascript-test-runner" temporary="true" nameIsGenerated="true">
40
+ <component name="RunManager" selected="Mocha.Reports.test event report">
41
+ <configuration name="Reports.test event report" type="mocha-javascript-test-runner" temporary="true" nameIsGenerated="true">
42
42
  <node-interpreter>project</node-interpreter>
43
43
  <node-options />
44
44
  <mocha-package>$PROJECT_DIR$/node_modules/mocha</mocha-package>
45
45
  <working-directory>$PROJECT_DIR$</working-directory>
46
46
  <pass-parent-env>true</pass-parent-env>
47
+ <envs>
48
+ <env name="email" value="jppenas@gmail.com" />
49
+ <env name="password" value="penas46881" />
50
+ </envs>
47
51
  <ui>bdd</ui>
48
52
  <extra-mocha-options />
49
53
  <test-kind>TEST</test-kind>
50
54
  <test-file>$PROJECT_DIR$/src/index.test.js</test-file>
51
55
  <test-names>
52
- <name value="speedingReport" />
56
+ <name value="Reports" />
57
+ <name value="test event report" />
53
58
  </test-names>
54
59
  <method v="2" />
55
60
  </configuration>
56
- <configuration name="speedingReport.test kmsReport by day" type="mocha-javascript-test-runner" temporary="true" nameIsGenerated="true">
61
+ <configuration name="Reports.test trip report" type="mocha-javascript-test-runner" temporary="true" nameIsGenerated="true">
57
62
  <node-interpreter>project</node-interpreter>
58
63
  <node-options />
59
64
  <mocha-package>$PROJECT_DIR$/node_modules/mocha</mocha-package>
@@ -68,12 +73,12 @@
68
73
  <test-kind>TEST</test-kind>
69
74
  <test-file>$PROJECT_DIR$/src/index.test.js</test-file>
70
75
  <test-names>
71
- <name value="speedingReport" />
72
- <name value="test kmsReport by day" />
76
+ <name value="Reports" />
77
+ <name value="test trip report" />
73
78
  </test-names>
74
79
  <method v="2" />
75
80
  </configuration>
76
- <configuration name="speedingReport.test speeding" type="mocha-javascript-test-runner" temporary="true" nameIsGenerated="true">
81
+ <configuration name="speedingReport.test kmsReport by day" type="mocha-javascript-test-runner" temporary="true" nameIsGenerated="true">
77
82
  <node-interpreter>project</node-interpreter>
78
83
  <node-options />
79
84
  <mocha-package>$PROJECT_DIR$/node_modules/mocha</mocha-package>
@@ -89,11 +94,11 @@
89
94
  <test-file>$PROJECT_DIR$/src/index.test.js</test-file>
90
95
  <test-names>
91
96
  <name value="speedingReport" />
92
- <name value="test speeding" />
97
+ <name value="test kmsReport by day" />
93
98
  </test-names>
94
99
  <method v="2" />
95
100
  </configuration>
96
- <configuration name="test reports" type="mocha-javascript-test-runner" temporary="true" nameIsGenerated="true">
101
+ <configuration name="speedingReport.test speeding" type="mocha-javascript-test-runner" temporary="true" nameIsGenerated="true">
97
102
  <node-interpreter>project</node-interpreter>
98
103
  <node-options />
99
104
  <mocha-package>$PROJECT_DIR$/node_modules/mocha</mocha-package>
@@ -105,43 +110,47 @@
105
110
  </envs>
106
111
  <ui>bdd</ui>
107
112
  <extra-mocha-options />
108
- <test-kind>SUITE</test-kind>
113
+ <test-kind>TEST</test-kind>
109
114
  <test-file>$PROJECT_DIR$/src/index.test.js</test-file>
110
115
  <test-names>
111
- <name value="test reports" />
116
+ <name value="speedingReport" />
117
+ <name value="test speeding" />
112
118
  </test-names>
113
119
  <method v="2" />
114
120
  </configuration>
115
- <configuration name="test reports.test queue" type="mocha-javascript-test-runner" temporary="true" nameIsGenerated="true">
121
+ <configuration name="test reports" type="mocha-javascript-test-runner" temporary="true" nameIsGenerated="true">
116
122
  <node-interpreter>project</node-interpreter>
117
123
  <node-options />
118
124
  <mocha-package>$PROJECT_DIR$/node_modules/mocha</mocha-package>
119
125
  <working-directory>$PROJECT_DIR$</working-directory>
120
126
  <pass-parent-env>true</pass-parent-env>
127
+ <envs>
128
+ <env name="email" value="jppenas@gmail.com" />
129
+ <env name="password" value="penas46881" />
130
+ </envs>
121
131
  <ui>bdd</ui>
122
132
  <extra-mocha-options />
123
- <test-kind>TEST</test-kind>
133
+ <test-kind>SUITE</test-kind>
124
134
  <test-file>$PROJECT_DIR$/src/index.test.js</test-file>
125
135
  <test-names>
126
136
  <name value="test reports" />
127
- <name value="test queue" />
128
137
  </test-names>
129
138
  <method v="2" />
130
139
  </configuration>
131
140
  <list>
132
- <item itemvalue="Mocha.speedingReport" />
133
141
  <item itemvalue="Mocha.test reports" />
134
- <item itemvalue="Mocha.test reports.test queue" />
135
142
  <item itemvalue="Mocha.speedingReport.test kmsReport by day" />
136
143
  <item itemvalue="Mocha.speedingReport.test speeding" />
144
+ <item itemvalue="Mocha.Reports.test trip report" />
145
+ <item itemvalue="Mocha.Reports.test event report" />
137
146
  </list>
138
147
  <recent_temporary>
139
148
  <list>
149
+ <item itemvalue="Mocha.Reports.test event report" />
150
+ <item itemvalue="Mocha.Reports.test trip report" />
140
151
  <item itemvalue="Mocha.speedingReport.test speeding" />
141
152
  <item itemvalue="Mocha.speedingReport.test kmsReport by day" />
142
153
  <item itemvalue="Mocha.test reports" />
143
- <item itemvalue="Mocha.test reports.test queue" />
144
- <item itemvalue="Mocha.speedingReport" />
145
154
  </list>
146
155
  </recent_temporary>
147
156
  </component>
@@ -176,7 +185,14 @@
176
185
  <workItem from="1637868513425" duration="1650000" />
177
186
  <workItem from="1637928687573" duration="37000" />
178
187
  <workItem from="1637931438656" duration="621000" />
179
- <workItem from="1637952151523" duration="10352000" />
188
+ <workItem from="1637952151523" duration="17766000" />
189
+ <workItem from="1638270786569" duration="6960000" />
190
+ <workItem from="1638455902437" duration="120000" />
191
+ <workItem from="1638486745343" duration="4126000" />
192
+ <workItem from="1638792860278" duration="2140000" />
193
+ <workItem from="1638839462028" duration="50000" />
194
+ <workItem from="1638840110567" duration="597000" />
195
+ <workItem from="1638888787940" duration="18005000" />
180
196
  </task>
181
197
  <task id="LOCAL-00001" summary="Fix getStyle">
182
198
  <created>1636983717385</created>
@@ -227,7 +243,21 @@
227
243
  <option name="project" value="LOCAL" />
228
244
  <updated>1638179219256</updated>
229
245
  </task>
230
- <option name="localTasksCounter" value="8" />
246
+ <task id="LOCAL-00008" summary="fix speeding report">
247
+ <created>1638186780966</created>
248
+ <option name="number" value="00008" />
249
+ <option name="presentableId" value="LOCAL-00008" />
250
+ <option name="project" value="LOCAL" />
251
+ <updated>1638186780967</updated>
252
+ </task>
253
+ <task id="LOCAL-00009" summary="refactor speeding and trip report">
254
+ <created>1638288337133</created>
255
+ <option name="number" value="00009" />
256
+ <option name="presentableId" value="LOCAL-00009" />
257
+ <option name="project" value="LOCAL" />
258
+ <updated>1638288337135</updated>
259
+ </task>
260
+ <option name="localTasksCounter" value="10" />
231
261
  <servers />
232
262
  </component>
233
263
  <component name="TypeScriptGeneratedFilesManager">
@@ -253,6 +283,8 @@
253
283
  <MESSAGE value="Fix pdf logo" />
254
284
  <MESSAGE value="kms report with parallel requests" />
255
285
  <MESSAGE value="Kms Report - Calculate distance with traccar trips" />
256
- <option name="LAST_COMMIT_MESSAGE" value="Kms Report - Calculate distance with traccar trips" />
286
+ <MESSAGE value="fix speeding report" />
287
+ <MESSAGE value="refactor speeding and trip report" />
288
+ <option name="LAST_COMMIT_MESSAGE" value="refactor speeding and trip report" />
257
289
  </component>
258
290
  </project>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetmap-reports",
3
- "version": "1.0.247",
3
+ "version": "1.0.248",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.test.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const {getReports} = require("./tests");
2
2
 
3
- describe('speedingReport', function() {
3
+ describe('Reports', function() {
4
4
  this.timeout(500000)
5
5
  it('test speeding', async () => {
6
6
  const report = await getReports()
@@ -12,6 +12,28 @@ describe('speedingReport', function() {
12
12
 
13
13
  console.log(reportData)
14
14
 
15
+ })
16
+ it('test trip report', async () => {
17
+ const report = await getReports()
18
+ const userData = await report.getUserData()
19
+ userData.byGroup = true
20
+ const reportData = await report.tripReport(new Date(2021, 2, 30, 0, 0, 0, 0),
21
+ new Date(2021, 2, 30, 23, 59, 59, 0),
22
+ userData)
23
+
24
+ console.log(reportData)
25
+
26
+ })
27
+ it('test event report', async () => {
28
+ const report = await getReports()
29
+ const userData = await report.getUserData()
30
+ userData.eventTypes = []
31
+ const reportData = await report.eventsReport(new Date(2021, 10, 20, 0, 0, 0, 0),
32
+ new Date(2021, 10, 30, 23, 59, 59, 0),
33
+ userData)
34
+
35
+ console.log(reportData)
36
+
15
37
  })
16
38
  it('test kmsReport by day', async () => {
17
39
  const reports = await getReports()
package/src/kms-report.js CHANGED
@@ -9,10 +9,11 @@ const traccar = require("./util/traccar");
9
9
 
10
10
 
11
11
  let traccarInstance
12
+ let userData
12
13
 
13
14
  const fileName = 'KmsReport'
14
15
 
15
- async function createKmsReportByDevice(from, to, userData) {
16
+ async function createKmsReportByDevice(from, to) {
16
17
  const groupIds = userData.groups.map(g => g.id)
17
18
  const devicesToProcess = userData.byGroup ? userData.devices.filter(d => !groupIds.includes(d.groupId)) : userData.devices
18
19
 
@@ -23,54 +24,99 @@ async function createKmsReportByDevice(from, to, userData) {
23
24
  }
24
25
 
25
26
  devicesToProcess.sort((a, b) => (a.name > b.name) ? 1 : -1)
26
- //const data = await traccar.getRoute(traccarInstance, from, to, devicesToProcess)
27
- const data = await traccar.getTrips(traccarInstance, from, to, devicesToProcess)
27
+ const route = userData.allWeek ? [] : await traccar.getRoute(traccarInstance, from, to, devicesToProcess)
28
+ const trips = await traccar.getTrips(traccarInstance, from, to, devicesToProcess)
28
29
 
29
- console.log('trips:' + data.length)
30
+ console.log('trips:' + trips.length)
30
31
 
31
- if (data.length > 0) {
32
- allData.devices = processDevices(from, to, devicesToProcess, data)
32
+ if (trips.length > 0) {
33
+ allData.devices = processDevices(from, to, devicesToProcess, route, trips)
33
34
  }
34
35
 
35
36
  return allData
36
37
  }
37
38
 
38
- async function createKmsReportByDriver(from, to, userData) {
39
+ async function createKmsReportByDriver(from, to) {
39
40
 
40
41
  }
41
42
 
42
- async function createKmsReportByGroup(from, to, userData) {
43
+ async function createKmsReportByGroup(from, to) {
43
44
 
44
45
  }
45
46
 
46
- function processDevices(from, to, devices, data) {
47
+ function processDevices(from, to, devices, route, trips) {
47
48
  const devicesResult = []
48
49
 
49
50
  for (const d of devices) {
50
- const trips = data.filter(t => t.deviceId === d.id)
51
+ const deviceTrips = trips.filter(t => t.deviceId === d.id)
51
52
 
52
- if (trips.length > 0) {
53
- trips.forEach(t => t.tripDay = new Date(t.startTime).toISOString().split('T')[0] + ' 12:00 PM')
54
- const groupedTrips = trips.reduce(
53
+ if (deviceTrips.length > 0) {
54
+ deviceTrips.forEach(t => t.tripDay = new Date(t.startTime).toISOString().split('T')[0] + ' 12:00 PM')
55
+ const groupedTrips = deviceTrips.reduce(
55
56
  (entryMap, e) => entryMap.set(e.tripDay, [...entryMap.get(e.tripDay)||[], e]),
56
57
  new Map()
57
58
  )
58
59
 
59
60
  const days = []
60
61
  const keys = Array.from(groupedTrips.keys())
61
-
62
62
  keys.forEach(key => {
63
- const dayTrips = groupedTrips.get(key)
63
+ const currentDate = new Date(key)
64
+ let dayTrips = groupedTrips.get(key)
64
65
  const day = {
65
- date: new Date(key),
66
- kms: dayTrips.reduce((a, b) => a + b.distance, 0) || 0
66
+ date: currentDate,
67
+ kms: 0
68
+ }
69
+
70
+ if(userData.allWeek){
71
+ day.kms = dayTrips.reduce((a, b) => a + b.distance, 0) || 0
72
+ } else {
73
+ if((currentDate.getDay() === 0 && userData.weekDays.sunday) ||
74
+ (currentDate.getDay() === 1 && userData.weekDays.monday) ||
75
+ (currentDate.getDay() === 2 && userData.weekDays.tuesday) ||
76
+ (currentDate.getDay() === 3 && userData.weekDays.wednesday) ||
77
+ (currentDate.getDay() === 4 && userData.weekDays.thursday) ||
78
+ (currentDate.getDay() === 5 && userData.weekDays.friday) ||
79
+ (currentDate.getDay() === 6 && userData.weekDays.saturday)) {
80
+
81
+ const startDate = new Date(new Date(key).toISOString().split('T')[0] + ' '+userData.dayHours.startTime)
82
+ const endDate = new Date(new Date(key).toISOString().split('T')[0] + ' '+userData.dayHours.endTime)
83
+
84
+ console.log(key, startDate, endDate)
85
+
86
+ //Trips inside time period
87
+ const filteredTrips = dayTrips.filter(t => new Date(t.startTime).getTime() > startDate.getTime()
88
+ && new Date(t.endTime).getTime() < endDate.getTime())
89
+
90
+ //Trip starts outside time period and ends inside time period
91
+ const startIncompleteTrip = dayTrips.filter(t => new Date(t.startTime).getTime() < startDate.getTime()
92
+ && new Date(t.endTime).getTime() > startDate.getTime())
93
+
94
+ //Trip starts inside time period and ends outside time period
95
+ const endIncompleteTrip = dayTrips.filter(t => new Date(t.startTime).getTime() < endDate.getTime()
96
+ && new Date(t.endTime).getTime() > endDate.getTime())
97
+
98
+ day.kms = filteredTrips.reduce((a, b) => a + b.distance, 0) || 0
99
+ console.log('Inside trips:'+day.kms)
100
+
101
+ if(startIncompleteTrip.length){
102
+ const routeInside = route.filter(p => new Date(p.fixTime).getTime() > startDate.getTime() && new Date(p.fixTime).getTime() < new Date(startIncompleteTrip[0].endTime).getTime())
103
+ day.kms = day.kms + (routeInside.reduce((a, b) => a + b.distance, 0) || 0)
104
+ console.log('startIncompleteTrip:'+day.kms)
105
+ }
106
+
107
+ if(endIncompleteTrip.length){
108
+ const routeInside = route.filter(p => new Date(p.fixTime).getTime() < endDate.getTime() && new Date(p.fixTime).getTime() > new Date(endIncompleteTrip[0].startTime).getTime())
109
+ day.kms = day.kms + (routeInside.reduce((a, b) => a + b.distance, 0) || 0)
110
+ console.log('endIncompleteTrip:'+day.kms)
111
+ }
112
+ }
67
113
  }
114
+
68
115
  days.push(day)
69
116
  })
70
-
71
117
  devicesResult.push({
72
- device: d,
73
- days: days
118
+ device: d,
119
+ days: days
74
120
  })
75
121
  }
76
122
  }
@@ -78,23 +124,26 @@ function processDevices(from, to, devices, data) {
78
124
  return devicesResult
79
125
  }
80
126
 
81
- async function createKmsReport(from, to, userData, traccar) {
127
+ async function createKmsReport(from, to, reportUserData, traccar) {
82
128
  console.log('Create KmsReport')
129
+
130
+ console.log(from, to)
83
131
  traccarInstance = traccar
132
+ userData = reportUserData
84
133
  if(userData.groupByDay){
85
134
  const reportData = []
86
135
 
87
136
  if(userData.byDriver){
88
- const allData = await createKmsReportByDriver(from, to, userData)
137
+ const allData = await createKmsReportByDriver(from, to)
89
138
  reportData.push(allData)
90
139
  }
91
140
  else if(userData.byGroup){
92
- const allGroupsData = await createKmsReportByGroup(from, to, userData)
141
+ const allGroupsData = await createKmsReportByGroup(from, to)
93
142
  allGroupsData.forEach(data => reportData.push(data))
94
- const withoutGroupDevices = await createKmsReportByDevice(from, to, userData)
143
+ const withoutGroupDevices = await createKmsReportByDevice(from, to)
95
144
  reportData.push(withoutGroupDevices)
96
145
  } else {
97
- const allData = await createKmsReportByDevice(from, to, userData)
146
+ const allData = await createKmsReportByDevice(from, to)
98
147
  reportData.push(allData)
99
148
  }
100
149
 
@@ -1,4 +1,3 @@
1
- const automaticReports = require("./automaticReports")
2
1
  const messages = require('../lang')
3
2
  const jsPDF = require('jspdf')
4
3
  const {convertMS} = require("./util/utils")
@@ -38,7 +37,8 @@ async function createSpeedingReport(from, to, userData, traccar, roadSpeedLimits
38
37
  xpert: devices.filter(d => d.attributes.xpert).length > 0
39
38
  }
40
39
 
41
- const data = await getReportData(from, to, devices)
40
+ const events = ['deviceOverspeed']
41
+ const data = await traccar.getEvents(traccarInstance, from, to, devices, events)
42
42
 
43
43
  devices.sort((a, b) => (a.name > b.name) ? 1 : -1)
44
44
 
@@ -80,23 +80,20 @@ async function createSpeedingReportByDevice(from, to, userData, roadSpeedLimits)
80
80
 
81
81
  async function createSpeedingReportByDriver(from, to, userData) {
82
82
  const deviceIds = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
83
- console.log(deviceIds.length)
83
+ console.log('Devices with driver',deviceIds.length)
84
84
 
85
85
  let eventsData = []
86
86
  if(deviceIds.length > 0) {
87
- console.log('getReportData', deviceIds.length)
88
87
  const devices = userData.devices.filter(d => deviceIds.includes(d.id))
89
- eventsData = await getReportData(from, to, devices)
88
+ eventsData = await traccar.getEvents(traccarInstance, from, to, devices,['deviceOverspeed'])
90
89
  }
91
90
 
92
91
  const allData = {
93
92
  drivers: []
94
93
  }
95
-
96
94
  console.log(eventsData.length)
97
95
 
98
96
  await getEventsPosition(from , to, userData.devices, userData.geofences, userData.drivers, eventsData)
99
-
100
97
  allData.drivers = await processDrivers(from, to, userData.drivers, eventsData)
101
98
  console.log(allData.drivers.length)
102
99
  return allData
@@ -127,24 +124,6 @@ async function processDrivers(from, to, drivers, data) {
127
124
  return driversResult
128
125
  }
129
126
 
130
- async function getReportData(from, to, devices) {
131
- const types = ['deviceOverspeed']
132
- let data = []
133
- const arrayOfArrays = automaticReports.sliceArray(devices)
134
- for (const a of arrayOfArrays) {
135
- try {
136
- const devices = a.map(d => d.id)
137
- console.log('reportsEventsGet', from, to, devices, null, types)
138
- const response = await traccarInstance.reports.reportsEventsGet(from, to, devices, null, types)
139
- data = data.concat(response.data)
140
- } catch (e) {
141
- console.error(e)
142
- }
143
- }
144
- console.log('Alerts:', data.length)
145
- return data
146
- }
147
-
148
127
  async function processDevices(from, to, devices, geofences, drivers, data, roadSpeedLimits) {
149
128
  const devicesResult = []
150
129
 
@@ -5,9 +5,10 @@ const {convertMS} = require("./util/utils")
5
5
  require('jspdf-autotable')
6
6
  const {coordsDistance} = require("./util/utils")
7
7
  const drivers = require("./util/driver")
8
- const {getUserPartner} = require("fleetmap-partners");
8
+ const {getUserPartner} = require("fleetmap-partners")
9
9
  const {headerFromUser} = require("./util/pdfDocument")
10
10
  const {getStyle} = require("./reportStyle")
11
+ const traccar = require("./util/traccar")
11
12
 
12
13
 
13
14
  let traccarInstance
@@ -16,7 +17,6 @@ const fileName = 'TripReport'
16
17
 
17
18
  async function createTripReport(from, to, userData, traccar) {
18
19
  console.log('Create TripReport')
19
-
20
20
  traccarInstance = traccar
21
21
  const reportData = []
22
22
 
@@ -26,72 +26,74 @@ async function createTripReport(from, to, userData, traccar) {
26
26
  }
27
27
  else if(userData.byGroup){
28
28
  console.log("ByGroup")
29
- for (const g of userData.groups) {
30
- const devices = userData.devices.filter(d => d.groupId === g.id)
31
- console.log(g.name + ' devices:' + devices.length)
32
- if(devices.length > 0) {
33
- const groupData = {
34
- devices: [],
35
- group: g,
36
- xpert: devices.filter(d => d.attributes.xpert).length > 0
37
- }
38
-
39
- const response = await traccarInstance.reports.reportsTripsGet(from, to, null, [g.id])
40
- const data = response.data
41
-
42
- const stopsResponse = await traccarInstance.reports.reportsStopsGet(from, to, null, [g.id])
43
- const stopsData = stopsResponse.data
44
-
45
- devices.sort((a, b) => (a.name > b.name) ? 1 : -1)
46
-
47
- if (data.length > 0) {
48
-
49
- console.log('Trips:' + data.length)
50
- groupData.devices = processDevices(from, to, devices, data, stopsData)
51
-
52
- reportData.push(groupData)
53
- }
54
- }
55
- }
29
+ const allData = await createTripReportByGroup(from, to, userData)
30
+ reportData.push(...allData)
56
31
  }
57
32
 
58
33
  const groupIds = userData.groups.map(g => g.id)
59
34
  const withoutGroupDevices = userData.byGroup ? userData.devices.filter(d => !groupIds.includes(d.groupId)) : userData.devices
60
35
 
36
+ const allData = await createTripReportByDevice(from, to, userData, withoutGroupDevices)
37
+ reportData.push(allData)
38
+
39
+ return reportData
40
+ }
41
+
42
+ async function createTripReportByDevice(from, to, userData, devices) {
61
43
  const allData = {
62
44
  devices: [],
63
- xpert: withoutGroupDevices.filter(d => d.attributes.xpert).length > 0
45
+ xpert: devices.filter(d => d.attributes.xpert).length > 0
64
46
  }
65
47
 
66
- withoutGroupDevices.sort((a, b) => (a.name > b.name) ? 1 : -1)
67
- const devicesToSlice = withoutGroupDevices.slice()
68
-
69
- const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
70
- let data = []
71
- let stopsData = []
72
- for (const a of arrayOfArrays) {
73
- const response = await traccarInstance.reports.reportsTripsGet(from, to, a.map(d => d.id))
74
- data = data.concat(response.data)
48
+ devices.sort((a, b) => (a.name > b.name) ? 1 : -1)
75
49
 
76
- const stopsResponse = await traccarInstance.reports.reportsStopsGet(from, to, a.map(d => d.id))
77
- stopsData = stopsResponse.data
78
- }
50
+ const devicesToSlice = devices.slice()
51
+ const data = await traccar.getTrips(traccarInstance, from, to, devicesToSlice)
52
+ const stopsData = await traccar.getStops(traccarInstance, from, to, devicesToSlice)
79
53
 
80
54
  console.log('Trips:' + data.length)
81
55
 
82
- if (data.length === 0) {
83
- return reportData
56
+ if (data.length > 0) {
57
+ allData.devices = processDevices(from, to, devices, data, stopsData, userData)
58
+
84
59
  }
85
60
 
86
- allData.devices = processDevices(from, to, withoutGroupDevices, data, stopsData, userData)
61
+ return allData
62
+ }
87
63
 
88
- reportData.push(allData)
64
+ async function createTripReportByGroup(from, to, userData) {
65
+ const reportData = []
66
+ for (const g of userData.groups) {
67
+ const devices = userData.devices.filter(d => d.groupId === g.id)
68
+ console.log(g.name + ' devices:' + devices.length)
69
+ if(devices.length > 0) {
70
+ const groupData = {
71
+ devices: [],
72
+ group: g,
73
+ xpert: devices.filter(d => d.attributes.xpert).length > 0
74
+ }
75
+
76
+ const response = await traccarInstance.reports.reportsTripsGet(from, to, null, [g.id])
77
+ const data = response.data
89
78
 
79
+ const stopsResponse = await traccarInstance.reports.reportsStopsGet(from, to, null, [g.id])
80
+ const stopsData = stopsResponse.data
81
+
82
+ devices.sort((a, b) => (a.name > b.name) ? 1 : -1)
83
+
84
+ if (data.length > 0) {
85
+
86
+ console.log('Trips:' + data.length)
87
+ groupData.devices = processDevices(from, to, devices, data, stopsData, userData)
88
+
89
+ reportData.push(groupData)
90
+ }
91
+ }
92
+ }
90
93
  return reportData
91
94
  }
92
95
 
93
96
  async function createTripReportByDriver(from, to, userData){
94
-
95
97
  const deviceIds = await drivers.devicesByDriver(traccarInstance, from, to, userData.drivers, userData.devices)
96
98
  console.log(deviceIds.length)
97
99
 
@@ -1,14 +1,8 @@
1
- const automaticReports = require("../automaticReports")
1
+ const traccar = require("./traccar");
2
2
 
3
3
  //get devices used by each driver between two dates
4
- async function devicesByDriver(traccar, from, to, drivers, devices){
5
- let eventsData = []
6
- let tripsData = []
7
- const arrayOfArrays = automaticReports.sliceArray(devices)
8
- for (const a of arrayOfArrays) {
9
- const response = await traccar.reports.reportsEventsGet(from, to, a.map(d => d.id), null, ['driverChanged'])
10
- eventsData = eventsData.concat(response.data)
11
- }
4
+ async function devicesByDriver(traccarInstance, from, to, drivers, devices){
5
+ const eventsData = traccar.getEvents(traccarInstance, from, to , devices, ['driverChanged'])
12
6
 
13
7
  const deviceIds = []
14
8
  for(const d of drivers) {
@@ -27,6 +27,19 @@ async function getTrips(traccar, from, to, devices){
27
27
  return result.map(r => r.data).flat()
28
28
  }
29
29
 
30
+ async function getStops(traccar, from, to, devices){
31
+ const devicesToSlice = devices.slice()
32
+ const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
33
+ let requests = []
34
+ for (const a of arrayOfArrays) {
35
+ const promise = traccar.reports.reportsStopsGet(from, to, a.map(d => d.id))
36
+ requests = requests.concat(promise)
37
+ }
38
+
39
+ const result = await Promise.all(requests)
40
+ return result.map(r => r.data).flat()
41
+ }
42
+
30
43
  async function getEvents(traccar, from, to, devices, events){
31
44
  const devicesToSlice = devices.slice()
32
45
  const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
@@ -41,4 +54,5 @@ async function getEvents(traccar, from, to, devices, events){
41
54
 
42
55
  exports.getRoute = getRoute
43
56
  exports.getTrips = getTrips
57
+ exports.getStops = getStops
44
58
  exports.getEvents = getEvents