fleetmap-reports 1.0.368 → 1.0.371
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 +1 -1
- package/src/automaticReports.js +1 -2
- package/src/index.test.js +12 -0
- package/src/kms-report.js +10 -6
- package/src/location-report.js +123 -50
- package/src/util/traccar.js +11 -8
package/package.json
CHANGED
package/src/automaticReports.js
CHANGED
|
@@ -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()
|
package/src/kms-report.js
CHANGED
|
@@ -9,6 +9,7 @@ const {getDates, convertFromUTC} = require("./util/utils")
|
|
|
9
9
|
const trips = require("./util/trips")
|
|
10
10
|
const drivers = require("./util/driver")
|
|
11
11
|
const { isInsideTimetable, isPartialInsideTimetable, calculateTrip} = require("./util/trips")
|
|
12
|
+
const traccarHelper = require("./util/traccar");
|
|
12
13
|
|
|
13
14
|
const fileName = 'KmsReport'
|
|
14
15
|
|
|
@@ -46,14 +47,15 @@ async function createKmsReportByDevice(from, to, userData, traccarInstance) {
|
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
devicesToProcess.sort((a, b) => (a.name > b.name) ? 1 : -1)
|
|
49
|
-
const
|
|
50
|
-
const tripsData =
|
|
50
|
+
const allInOne = await traccarHelper.getAllInOne(traccarInstance, from, to, devicesToProcess, true, true, false, false)
|
|
51
|
+
const tripsData = allInOne.trips
|
|
52
|
+
const routeData = allInOne.route
|
|
51
53
|
|
|
52
54
|
console.log('trips:' + tripsData.length)
|
|
53
55
|
|
|
54
56
|
if (tripsData.length > 0) {
|
|
55
|
-
trips.checkTripsKms(traccarInstance, from, to, devicesToProcess, {trips: tripsData, route:
|
|
56
|
-
allData.devices = processDevices(from, to, devicesToProcess, {trips: tripsData, route:
|
|
57
|
+
trips.checkTripsKms(traccarInstance, from, to, devicesToProcess, {trips: tripsData, route: routeData})
|
|
58
|
+
allData.devices = processDevices(from, to, devicesToProcess, {trips: tripsData, route: routeData}, userData)
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
return allData
|
|
@@ -68,8 +70,10 @@ async function createKmsReportByDriver(from, to, userData, traccarInstance) {
|
|
|
68
70
|
return { drivers: [] }
|
|
69
71
|
}
|
|
70
72
|
|
|
71
|
-
const
|
|
72
|
-
const
|
|
73
|
+
const allInOne = await traccarHelper.getAllInOne(traccarInstance, from, to, devices, true, true, false, false)
|
|
74
|
+
const tripsData = allInOne.trips
|
|
75
|
+
const routeData = allInOne.route
|
|
76
|
+
|
|
73
77
|
|
|
74
78
|
return { drivers: processDrivers(from, to, userData, {trips: tripsData, route: routeData}) }
|
|
75
79
|
}
|
package/src/location-report.js
CHANGED
|
@@ -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.
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
+
return reportData
|
|
31
|
+
}
|
|
30
32
|
|
|
31
|
-
|
|
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
|
-
|
|
37
|
+
if(!devices.length){
|
|
38
|
+
//Empty report
|
|
39
|
+
return { drivers:[] }
|
|
40
|
+
}
|
|
34
41
|
|
|
35
|
-
|
|
36
|
-
|
|
42
|
+
const allInOne = await traccarHelper.getAllInOne(traccar, from, to, devices, true, false, false, false)
|
|
43
|
+
const routeData = allInOne.route
|
|
37
44
|
|
|
38
|
-
|
|
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
|
-
|
|
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(
|
|
68
|
-
|
|
97
|
+
if(routeData.length > 0) {
|
|
98
|
+
allData.devices = processDevices(from, to, withoutGroupDevices, userData.drivers, routeData)
|
|
69
99
|
}
|
|
70
100
|
|
|
71
|
-
allData
|
|
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
|
|
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 (
|
|
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
|
-
|
|
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 (
|
|
218
|
-
|
|
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
|
}))
|
package/src/util/traccar.js
CHANGED
|
@@ -74,15 +74,18 @@ async function getAllInOne(traccar, from, to, devices, getRoutes, getTrips, getS
|
|
|
74
74
|
if(getSummary) url += `&type=summary`
|
|
75
75
|
console.log('LOADING_MESSAGE:' + devices.map(d => d.name).join(', '))
|
|
76
76
|
|
|
77
|
-
const sliced = automaticReports.sliceArray(devices)
|
|
77
|
+
const sliced = automaticReports.sliceArray(devices, 50)
|
|
78
78
|
let deviceCount = 0
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
79
|
+
const result = []
|
|
80
|
+
for(const chunk of sliced) {
|
|
81
|
+
const requests = chunk.map(d =>
|
|
82
|
+
traccar.axios.get(url + '&' + `deviceId=${d.id}`).then(r => r.data).then(x => {
|
|
83
|
+
console.log('LOADING_MESSAGE:' + d.name)
|
|
84
|
+
console.log(`PROGRESS_PERC:${++deviceCount / devices.length * 100}`)
|
|
85
|
+
return x
|
|
86
|
+
}))
|
|
87
|
+
result.push(...(await Promise.all(requests)))
|
|
88
|
+
}
|
|
86
89
|
return {
|
|
87
90
|
route: result.filter(t => t.route).map(r => r.route).flat(),
|
|
88
91
|
trips: result.filter(t => t.trips).map(r => r.trips).flat(),
|