fleetmap-reports 1.0.369 → 1.0.373

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.369",
3
+ "version": "1.0.373",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1,9 +1,8 @@
1
1
  const moment = require('moment')
2
2
 
3
- function sliceArray(longArray) {
3
+ function sliceArray(longArray, size = 1) {
4
4
  const arrayToSlice = longArray.slice()
5
5
 
6
- const size = 1
7
6
  const arrayOfArrays = []
8
7
  for (let i=0; i<arrayToSlice.length; i+=size) {
9
8
  arrayOfArrays.push(arrayToSlice.slice(i,i+size));
package/src/index.test.js CHANGED
@@ -130,6 +130,18 @@ describe('Test_Reports', function() {
130
130
  const device = data[0].devices.find(d => d.device.id===22326)
131
131
  assert.equal(device.positions.length, 708) // Total Positions
132
132
  },20000)
133
+ it('Location by driver', async () => {
134
+ const report = await getReports()
135
+ const userData = await report.getUserData()
136
+ userData.byDriver=true
137
+ const data = await report.locationReport(new Date(2021, 11, 6, 0, 0, 0, 0),
138
+ new Date(2021, 11, 9, 23, 59, 59, 0),
139
+ userData)
140
+
141
+ assert.equal(data.length, 1)
142
+ const driver = data[0].drivers.find(d => d.driver.id===14020)
143
+ assert.equal(driver.positions.length, 85) // Total Positions
144
+ },20000)
133
145
  it('KmsReport by device', async () => {
134
146
  const reports = await getReports()
135
147
  const userData = await reports.getUserData()
@@ -1,46 +1,84 @@
1
- const automaticReports = require("./automaticReports");
2
1
  const jsPDF = require('jspdf')
3
2
  require('jspdf-autotable')
4
3
  const {headerFromUser} = require("./util/pdfDocument");
5
4
  const {getStyle} = require("./reportStyle")
6
5
  const {getUserPartner} = require("fleetmap-partners");
7
6
  const {convertToLocaleString, getTranslations} = require("./util/utils");
7
+ const traccarHelper = require("./util/traccar");
8
8
 
9
9
  const fileName = 'LocationReport'
10
10
 
11
11
  async function createLocationReport(from, to, userData, traccar) {
12
12
  console.log('Create LocationReport')
13
-
14
13
  const reportData = []
15
14
 
16
- if(userData.byGroup) {
15
+ if (userData.byDriver) {
16
+ console.log("ByDriver")
17
+ const report = await createLocationReportByDriver(from, to, userData, traccar)
18
+ reportData.push(report)
19
+ }
20
+ else if (userData.byGroup) {
17
21
  console.log("ByGroup")
18
- for (const g of userData.groups) {
19
- const devices = userData.devices.filter(d => d.groupId === g.id)
20
-
21
- if(devices.length > 0) {
22
- const groupData = {
23
- devices: [],
24
- group: g,
25
- xpert: devices.filter(d => d.attributes.xpert).length > 0
26
- }
22
+ const allData = await createLocationReportByGroup(from, to, userData, traccar)
23
+ reportData.push(...allData)
24
+ } else {
25
+ console.log("ByDevice")
26
+ const report = await createLocationReportByDevice(from, to, userData, traccar)
27
+ reportData.push(report)
28
+ }
27
29
 
28
- const response = await traccar.reports.reportsRouteGet(from, to, null, [g.id])
29
- const data = response.data
30
+ return reportData
31
+ }
30
32
 
31
- devices.sort((a, b) => (a.name > b.name) ? 1 : -1)
33
+ async function createLocationReportByDriver(from, to, userData, traccar) {
34
+ const devices = await traccar.devices.devicesGet().then(d => d.data)
35
+ console.log(devices.length)
32
36
 
33
- if (data.length > 0) {
37
+ if(!devices.length){
38
+ //Empty report
39
+ return { drivers:[] }
40
+ }
34
41
 
35
- console.log('Locations:'+data.length)
36
- groupData.devices = processDevices(from, to, devices, userData.drivers, data)
42
+ const allInOne = await traccarHelper.getAllInOne(traccar, from, to, devices, true, false, false, false)
43
+ const routeData = allInOne.route
37
44
 
38
- reportData.push(groupData)
39
- }
45
+ return { drivers: processDrivers(from, to, userData, routeData)}
46
+ }
47
+
48
+ async function createLocationReportByGroup(from, to, userData, traccar) {
49
+ const reportData = []
50
+
51
+ for (const g of userData.groups) {
52
+ const devices = userData.devices.filter(d => d.groupId === g.id)
53
+
54
+ if(devices.length > 0) {
55
+ const groupData = {
56
+ devices: [],
57
+ group: g,
58
+ xpert: devices.filter(d => d.attributes.xpert).length > 0
59
+ }
60
+
61
+ const response = await traccar.reports.reportsRouteGet(from, to, null, [g.id])
62
+ const data = response.data
63
+
64
+ devices.sort((a, b) => (a.name > b.name) ? 1 : -1)
65
+
66
+ if (data.length > 0) {
67
+ console.log('Locations:'+data.length)
68
+ groupData.devices = processDevices(from, to, devices, userData.drivers, data)
69
+
70
+ reportData.push(groupData)
40
71
  }
41
72
  }
42
73
  }
43
74
 
75
+ const allData = await createLocationReportByDevice(from, to, userData, traccar)
76
+ reportData.push(allData)
77
+
78
+ return reportData
79
+ }
80
+
81
+ async function createLocationReportByDevice(from, to, userData, traccar) {
44
82
  const groupIds = userData.groups.map(g => g.id)
45
83
  const withoutGroupDevices = userData.byGroup ? userData.devices.filter(d => !groupIds.includes(d.groupId)) : userData.devices
46
84
 
@@ -51,28 +89,16 @@ async function createLocationReport(from, to, userData, traccar) {
51
89
 
52
90
  withoutGroupDevices.sort((a, b) => (a.name > b.name) ? 1 : -1)
53
91
  const devicesToSlice = withoutGroupDevices.slice()
92
+ const allInOne = await traccarHelper.getAllInOne(traccar, from, to, devicesToSlice, true, false, false, false)
93
+ const routeData = allInOne.route
54
94
 
55
- const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
56
- let data = []
57
- let i = 0
58
- for (const a of arrayOfArrays) {
59
- console.log(`PROGRESS_PERC:${++i/arrayOfArrays.length*100}`)
60
- console.log('LOADING_MESSAGE:' + a.map(d => d.name).join(', '))
61
- const response = await traccar.reports.reportsRouteGet(from, to, a.map(d => d.id))
62
- data = data.concat(response.data)
63
- }
64
-
65
- console.log('Locations:'+data.length)
95
+ console.log('Locations:'+routeData.length)
66
96
 
67
- if(data.length === 0) {
68
- return reportData
97
+ if(routeData.length > 0) {
98
+ allData.devices = processDevices(from, to, withoutGroupDevices, userData.drivers, routeData)
69
99
  }
70
100
 
71
- allData.devices = processDevices(from, to, withoutGroupDevices, userData.drivers, data)
72
-
73
- reportData.push(allData)
74
-
75
- return reportData
101
+ return allData
76
102
  }
77
103
 
78
104
  function processDevices(from, to, devices, drivers, data) {
@@ -103,6 +129,34 @@ function processDevices(from, to, devices, drivers, data) {
103
129
  return devicesResult
104
130
  }
105
131
 
132
+ function processDrivers(from, to, userData, route) {
133
+ const driversResult = []
134
+ userData.drivers.forEach(d => {
135
+ const positions = route.filter(p => p.attributes.driverUniqueId === d.uniqueId)
136
+ if (positions.length > 0) {
137
+ positions.sort((a,b)=>new Date(a.fixTime).getTime()-new Date(b.fixTime).getTime());
138
+
139
+ positions.forEach(p => {
140
+ const device = userData.devices.find(d => d.id === p.deviceId)
141
+ if(device){
142
+ p.vehicleName = device.name
143
+ }
144
+ })
145
+
146
+ const driverData = {
147
+ driver: d,
148
+ from: from,
149
+ to: to,
150
+ positions: positions,
151
+ }
152
+ driversResult.push(driverData)
153
+ }
154
+ })
155
+ console.log(driversResult)
156
+
157
+ return driversResult
158
+ }
159
+
106
160
  async function exportLocationReportToPDF(userData, reportData) {
107
161
  console.log('Export to PDF')
108
162
 
@@ -110,7 +164,16 @@ async function exportLocationReportToPDF(userData, reportData) {
110
164
  const lang = userData.user.attributes.lang || (navigator && navigator.language)
111
165
  const timezone = userData.user.attributes.timezone
112
166
 
113
- const headers = [
167
+ const positionsData = userData.byDriver ? reportData.drivers : reportData.devices
168
+
169
+ const headers = userData.byDriver ? [
170
+ translations.report.date,
171
+ translations.report.address,
172
+ translations.report.speed,
173
+ translations.report.ignition,
174
+ translations.report.vehicle,
175
+ translations.report.temperature
176
+ ] : [
114
177
  translations.report.date,
115
178
  translations.report.address,
116
179
  translations.report.speed,
@@ -119,15 +182,15 @@ async function exportLocationReportToPDF(userData, reportData) {
119
182
  translations.report.temperature
120
183
  ]
121
184
 
122
- if (reportData.devices) {
185
+ if (positionsData) {
123
186
  let first = true
124
187
  const doc = new jsPDF.jsPDF('l');
125
188
  await headerFromUser(doc, translations.report.titleLocationReport, userData.user)
126
189
 
127
- reportData.devices.forEach(d => {
190
+ positionsData.forEach(d => {
128
191
  let data = []
129
- const name = deviceName(d.device)
130
- const group = deviceGroupName(userData.groups, d.device, translations)
192
+ const name = userData.byDriver ? d.driver.name : deviceName(d.device)
193
+ const group = userData.byDriver ? '' : deviceGroupName(userData.groups, d.device, translations)
131
194
 
132
195
  let space = 0
133
196
  if (!first) {
@@ -147,7 +210,7 @@ async function exportLocationReportToPDF(userData, reportData) {
147
210
  a.address,
148
211
  Math.round(a.speed * 1.85200),
149
212
  a.attributes.ignition ? translations.report.ignitionOn : translations.report.ignitionOff,
150
- getDriverName(a, userData.drivers),
213
+ userData.byDriver ? a.vehicleName : getDriverName(a, userData.drivers),
151
214
  getTempValue(a)
152
215
  ]
153
216
  data.push(temp)
@@ -199,11 +262,21 @@ function exportLocationReportToExcel(userData, reportData) {
199
262
 
200
263
  const translations = getTranslations(userData)
201
264
 
265
+ const positionsData = userData.byDriver ? reportData.drivers : reportData.devices
266
+
202
267
  const settings = {
203
268
  sheetName: translations.report.titleLocationReport, // The name of the sheet
204
269
  fileName: fileName, // The name of the spreadsheet
205
270
  }
206
- const headers = [
271
+ const headers = userData.byDriver ? [
272
+ {label: translations.report.driver, value:'driver'},
273
+ {label: translations.report.date, value:'date'},
274
+ {label: translations.report.address, value:'address'},
275
+ {label: translations.report.speed, value:'speed'},
276
+ {label: translations.report.ignition, value:'ignition'},
277
+ {label: translations.report.vehicle, value:'name'},
278
+ {label: translations.report.temperature, value:'temperature'}
279
+ ] : [
207
280
  {label: translations.report.vehicle, value:'name'},
208
281
  {label: translations.report.group, value:'group'},
209
282
  {label: translations.report.date, value:'date'},
@@ -214,17 +287,17 @@ function exportLocationReportToExcel(userData, reportData) {
214
287
  {label: translations.report.temperature, value:'temperature'}
215
288
  ]
216
289
  let data = []
217
- if (reportData.devices) {
218
- reportData.devices.forEach(d => {
290
+ if (positionsData) {
291
+ positionsData.forEach(d => {
219
292
  data = data.concat(d.positions.map(a => {
220
293
  return {
221
- name: d.device.name,
222
- group: deviceGroupName(userData.groups, d.device),
294
+ name: userData.byDriver ? a.vehicleName : d.device.name,
295
+ group: userData.byDriver ? '' : deviceGroupName(userData.groups, d.device),
223
296
  date: getLocationDate(a, userData.user),
224
297
  address: a.address,
225
298
  speed: Math.round(a.speed * 1.85200),
226
299
  ignition: a.attributes.ignition ? translations.report.ignitionOn : translations.report.ignitionOff,
227
- driver: getDriverName(a, userData.drivers),
300
+ driver: userData.byDriver ? d.driver.name : getDriverName(a, userData.drivers),
228
301
  temperature: getTempValue(a)
229
302
  }
230
303
  }))
@@ -3,7 +3,7 @@ const automaticReports = require("../automaticReports");
3
3
  async function getRoute(traccar, from, to, devices){
4
4
  const devicesToSlice = devices.slice()
5
5
 
6
- const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
6
+ const arrayOfArrays = automaticReports.sliceArray(devicesToSlice, 50)
7
7
  let requests = []
8
8
  for (const a of arrayOfArrays) {
9
9
  const promise = traccar.reports.reportsRouteGet(from, to, a.map(d => d.id))
@@ -11,13 +11,12 @@ async function getRoute(traccar, from, to, devices){
11
11
  }
12
12
 
13
13
  const result = await Promise.all(requests)
14
-
15
14
  return result.map(r => r.data).flat()
16
15
  }
17
16
 
18
17
  async function getTrips(traccar, from, to, devices){
19
18
  const devicesToSlice = devices.slice()
20
- const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
19
+ const arrayOfArrays = automaticReports.sliceArray(devicesToSlice, 50)
21
20
  let requests = []
22
21
  for (const a of arrayOfArrays) {
23
22
  const promise = traccar.reports.reportsTripsGet(from, to, a.map(d => d.id))
@@ -30,7 +29,7 @@ async function getTrips(traccar, from, to, devices){
30
29
 
31
30
  async function getStops(traccar, from, to, devices){
32
31
  const devicesToSlice = devices.slice()
33
- const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
32
+ const arrayOfArrays = automaticReports.sliceArray(devicesToSlice,50)
34
33
  let requests = []
35
34
  for (const a of arrayOfArrays) {
36
35
  const promise = traccar.reports.reportsStopsGet(from, to, a.map(d => d.id))
@@ -43,7 +42,7 @@ async function getStops(traccar, from, to, devices){
43
42
 
44
43
  async function getEvents(traccar, from, to, devices, events){
45
44
  const devicesToSlice = devices.slice()
46
- const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
45
+ const arrayOfArrays = automaticReports.sliceArray(devicesToSlice, 50)
47
46
  let requests = []
48
47
  for (const a of arrayOfArrays) {
49
48
  const promise = traccar.reports.reportsEventsGet(from, to, a.map(d => d.id), null, events)
@@ -55,7 +54,7 @@ async function getEvents(traccar, from, to, devices, events){
55
54
 
56
55
  async function getSummary(traccar, from, to, devices){
57
56
  const devicesToSlice = devices.slice()
58
- const arrayOfArrays = automaticReports.sliceArray(devicesToSlice)
57
+ const arrayOfArrays = automaticReports.sliceArray(devicesToSlice, 50)
59
58
  let requests = []
60
59
  for (const a of arrayOfArrays) {
61
60
  const promise = traccar.reports.reportsSummaryGet(from, to, a.map(d => d.id))
@@ -74,15 +73,18 @@ async function getAllInOne(traccar, from, to, devices, getRoutes, getTrips, getS
74
73
  if(getSummary) url += `&type=summary`
75
74
  console.log('LOADING_MESSAGE:' + devices.map(d => d.name).join(', '))
76
75
 
77
- const sliced = automaticReports.sliceArray(devices)
76
+ const sliced = automaticReports.sliceArray(devices, 50)
78
77
  let deviceCount = 0
79
- const requests = sliced.map(a =>
80
- traccar.axios.get(url + '&' + a.map(d => `deviceId=${d.id}`).join('&')).then(r => r.data).then(x => {
81
- console.log('LOADING_MESSAGE:' + a.map(d => d.name).join(', '))
82
- console.log(`PROGRESS_PERC:${++deviceCount/sliced.length*100}`)
83
- return x
84
- }))
85
- const result = await Promise.all(requests)
78
+ const result = []
79
+ for(const chunk of sliced) {
80
+ const requests = chunk.map(d =>
81
+ traccar.axios.get(url + '&' + `deviceId=${d.id}`).then(r => r.data).then(x => {
82
+ console.log('LOADING_MESSAGE:' + d.name)
83
+ console.log(`PROGRESS_PERC:${++deviceCount / devices.length * 100}`)
84
+ return x
85
+ }))
86
+ result.push(...(await Promise.all(requests)))
87
+ }
86
88
  return {
87
89
  route: result.filter(t => t.route).map(r => r.route).flat(),
88
90
  trips: result.filter(t => t.trips).map(r => r.trips).flat(),