fleetmap-reports 1.0.963 → 1.0.965

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.963",
3
+ "version": "1.0.965",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1,67 +1,80 @@
1
1
  const traccarHelper = require('../util/traccar')
2
2
  const { isInsideTimetable } = require('../util/trips')
3
3
  const { convertFromUTCDate } = require('../util/utils')
4
+ const automaticReports = require('../automaticReports')
4
5
 
5
6
  async function createDailyUseReport (from, to, userData, traccar) {
6
7
  const reportData = []
7
8
 
8
9
  const devices = userData.devices
9
10
 
10
- const allInOne = await traccarHelper.getAllInOne(traccar, from, to, devices, false, true, true, true)
11
+ const sliced = automaticReports.sliceArray(devices, 5)
12
+ let deviceCount = 0
13
+ for (const slice of sliced) {
14
+ const allInOne = await traccarHelper.getAllInOne(traccar, from, to, slice, false, true, true, true, deviceCount, devices.length)
11
15
 
12
- for (const d of devices) {
13
- const group = userData.groups.find(g => g.id === d.groupId)
16
+ slice.forEach(d => {
17
+ reportData.push(processDeviceData(allInOne, d, userData))
18
+ })
14
19
 
15
- const deviceTrips = allInOne.trips.filter(t => t.deviceId === d.id)
16
- const deviceStops = allInOne.stops.filter(t => t.deviceId === d.id)
20
+ deviceCount = deviceCount + slice.length
21
+ }
17
22
 
18
- userData.weekDays = {
19
- sunday: true,
20
- monday: true,
21
- tuesday: true,
22
- wednesday: true,
23
- thursday: true,
24
- friday: true,
25
- saturday: true
26
- }
27
- userData.dayHours = {
28
- startTime: '00:00',
29
- endTime: '12:00'
30
- }
23
+ return reportData
24
+ }
31
25
 
32
- const morningTrips = deviceTrips.filter(t => isInsideTimetable(t, userData))
33
- const morningStart = morningTrips.length && convertFromUTCDate(new Date(morningTrips[0].startTime), userData.user)
34
- const morningEnd = morningTrips.length && convertFromUTCDate(new Date(morningTrips[morningTrips.length - 1].endTime), userData.user)
26
+ function processDeviceData (allInOne, d, userData) {
27
+ const now = new Date()
28
+ const group = userData.groups.find(g => g.id === d.groupId)
35
29
 
36
- userData.dayHours = {
37
- startTime: '12:00',
38
- endTime: '23:59'
39
- }
30
+ const deviceTrips = allInOne.trips.filter(t => t.deviceId === d.id)
31
+ const deviceStops = allInOne.stops.filter(t => t.deviceId === d.id)
40
32
 
41
- const afternoonTrips = deviceTrips.filter(t => isInsideTimetable(t, userData))
33
+ userData.weekDays = {
34
+ sunday: true,
35
+ monday: true,
36
+ tuesday: true,
37
+ wednesday: true,
38
+ thursday: true,
39
+ friday: true,
40
+ saturday: true
41
+ }
42
+ userData.dayHours = {
43
+ startTime: '00:00',
44
+ endTime: '12:00'
45
+ }
42
46
 
43
- const afternoonStart = afternoonTrips.length && convertFromUTCDate(new Date(afternoonTrips[0].startTime), userData.user)
44
- const afternoonEnd = afternoonTrips.length && convertFromUTCDate(new Date(afternoonTrips[afternoonTrips.length - 1].endTime), userData.user)
47
+ const morningTrips = deviceTrips.filter(t => isInsideTimetable(t, userData))
48
+ const morningStart = morningTrips.length && convertFromUTCDate(new Date(morningTrips[0].startTime), userData.user)
49
+ const morningEnd = morningTrips.length && convertFromUTCDate(new Date(morningTrips[morningTrips.length - 1].endTime), userData.user)
45
50
 
46
- reportData.push({
47
- device: d.name,
48
- licensePlate: d.attributes.license_plate,
49
- group: group ? group.name : '',
50
- morningStart,
51
- morningEnd,
52
- morningTime: morningStart && morningEnd ? morningEnd.getTime() - morningStart.getTime() : 0,
53
- lunch: morningEnd && afternoonStart ? afternoonStart.getTime() - morningEnd.getTime() : 0,
54
- afternoonStart,
55
- afternoonEnd,
56
- afternoonTime: afternoonStart && afternoonEnd ? afternoonEnd.getTime() - afternoonStart.getTime() : 0,
57
- totalTime: morningStart && afternoonEnd ? afternoonEnd.getTime() - morningStart.getTime() : 0,
58
- totalDrivingTime: deviceTrips.reduce((a, b) => a + b.duration, 0),
59
- totalStops: deviceStops.length,
60
- totalDistance: deviceTrips.reduce((a, b) => a + b.distance, 0)
61
- })
51
+ userData.dayHours = {
52
+ startTime: '12:00',
53
+ endTime: '23:59'
62
54
  }
63
55
 
64
- return reportData
56
+ const afternoonTrips = deviceTrips.filter(t => isInsideTimetable(t, userData))
57
+
58
+ const afternoonStart = afternoonTrips.length && convertFromUTCDate(new Date(afternoonTrips[0].startTime), userData.user)
59
+ const afternoonEnd = afternoonTrips.length && convertFromUTCDate(new Date(afternoonTrips[afternoonTrips.length - 1].endTime), userData.user)
60
+
61
+ console.log('device took', new Date() - now, 'ms')
62
+ return {
63
+ device: d.name,
64
+ licensePlate: d.attributes.license_plate,
65
+ group: group ? group.name : '',
66
+ morningStart,
67
+ morningEnd,
68
+ morningTime: morningStart && morningEnd ? morningEnd.getTime() - morningStart.getTime() : 0,
69
+ lunch: morningEnd && afternoonStart ? afternoonStart.getTime() - morningEnd.getTime() : 0,
70
+ afternoonStart,
71
+ afternoonEnd,
72
+ afternoonTime: afternoonStart && afternoonEnd ? afternoonEnd.getTime() - afternoonStart.getTime() : 0,
73
+ totalTime: morningStart && afternoonEnd ? afternoonEnd.getTime() - morningStart.getTime() : 0,
74
+ totalDrivingTime: deviceTrips.reduce((a, b) => a + b.duration, 0),
75
+ totalStops: deviceStops.length,
76
+ totalDistance: deviceTrips.reduce((a, b) => a + b.distance, 0)
77
+ }
65
78
  }
66
79
 
67
80
  exports.createDailyUseReport = createDailyUseReport
@@ -320,8 +320,9 @@ exports.getRoadSpeedLimits = getRoadSpeedLimits
320
320
 
321
321
  async function invokeValhalla (route, i, chunk, country, threshold, results, retry = 3) {
322
322
  const slice = route.slice(i, i + chunk)
323
+ const position = slice.find(p => getCountry(p))
323
324
  try {
324
- const url = `http://valhalla-${getCountry(slice[0]) || country}.pinme.io:8002/trace_attributes`
325
+ const url = `http://valhalla-${(position && getCountry(position)) || country}.pinme.io:8002/trace_attributes`
325
326
  console.log(url)
326
327
  const {
327
328
  // eslint-disable-next-line camelcase
@@ -0,0 +1,19 @@
1
+ const { getReports } = require('./index')
2
+ const assert = require('assert')
3
+ const { createDailyUseReport } = require('../partnerReports/dailyuse-report')
4
+
5
+ describe('dailyuse', function () {
6
+ // eslint-disable-next-line no-undef
7
+ it('dailyuse report', async () => {
8
+ const report = await getReports()
9
+ const userData = await report.getUserData()
10
+
11
+ const data = await createDailyUseReport(
12
+ new Date(Date.UTC(2023, 10, 1, 0, 0, 0, 0)),
13
+ new Date(Date.UTC(2023, 10, 6, 23, 59, 59, 0)),
14
+ userData,
15
+ report.traccar)
16
+ console.log(data)
17
+ assert.equal(data[0].consumption, 19.799999999999997)
18
+ }, 8000000)
19
+ })
@@ -16,7 +16,7 @@ axiosCookieJarSupport(axios)
16
16
 
17
17
  const getReports = async (email, password) => {
18
18
  try {
19
- console.log('email / pass', process.env.email || email, process.env.password || password)
19
+ // console.log('email / pass', process.env.email || email, process.env.password || password)
20
20
  await new SessionApi(traccarConfig, null, axios).sessionPost(email || process.env.email, password || process.env.password)
21
21
  return new Index(traccarConfig, axios, cookieJar)
22
22
  } catch (e) {
package/src/util/fuel.js CHANGED
@@ -12,6 +12,9 @@ function calculateFuelDiff (a, b, device, p) {
12
12
  }
13
13
 
14
14
  function fuelSignalInverter (device) {
15
+ if (!device.attributes.fuel_low_threshold || !device.attributes.fuel_high_threshold) {
16
+ return 1
17
+ }
15
18
  return device.attributes.fuel_low_threshold <= device.attributes.fuel_high_threshold ? 1 : -1
16
19
  }
17
20