fleetmap-reports 2.0.168 → 2.0.170

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": "2.0.168",
3
+ "version": "2.0.170",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1,7 +1,7 @@
1
1
  const automaticReports = require('./automaticReports')
2
2
  const traccarHelper = require('./util/traccar')
3
3
  const { devicesToProcess, deviceName } = require('./util/device')
4
- const { getTranslations, isClientSide, convertToLocaleString, convertMS } = require('./util/utils')
4
+ const { getTranslations, isClientSide, convertToLocaleString, convertMS, getDates } = require('./util/utils')
5
5
  const jsPDF = require('jspdf')
6
6
  const { headerFromUser, addTable } = require('./util/pdfDocument')
7
7
  const { getStyle } = require('./reportStyle')
@@ -44,12 +44,25 @@ function processDevices (from, to, devices, data, userData) {
44
44
 
45
45
  rows.sort((a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime())
46
46
 
47
- result.push({
48
- device: d,
49
- from,
50
- to,
51
- rows
52
- })
47
+ if (userData.groupByDay) {
48
+ const dates = getDates(from, to, userData.user.attributes.timezone)
49
+
50
+ const data = groupByDay(rows, dates, userData)
51
+
52
+ result.push({
53
+ device: d,
54
+ from,
55
+ to,
56
+ rows: data
57
+ })
58
+ } else {
59
+ result.push({
60
+ device: d,
61
+ from,
62
+ to,
63
+ rows
64
+ })
65
+ }
53
66
  }
54
67
 
55
68
  return result
@@ -74,6 +87,64 @@ async function create (from, to, userData, traccar) {
74
87
  return result
75
88
  }
76
89
 
90
+ function groupByDay (rows, dates) {
91
+ const groupedByDay = {}
92
+
93
+ dates.forEach(dateStr => {
94
+ const dayStart = new Date(dateStr)
95
+ dayStart.setHours(0, 0, 0, 0)
96
+ const dayEnd = new Date(dayStart.getTime() + 86400000 - 1) // 23:59:59
97
+
98
+ rows.forEach(row => {
99
+ if (row.value) {
100
+ const rowStart = new Date(row.startTime)
101
+ const rowEnd = new Date(row.endTime)
102
+
103
+ if (rowStart <= dayEnd && rowEnd >= dayStart) {
104
+ if (!groupedByDay[dateStr]) {
105
+ groupedByDay[dateStr] = {}
106
+ }
107
+
108
+ if (!groupedByDay[dateStr][row.name]) {
109
+ groupedByDay[dateStr][row.name] = {
110
+ firstActivation: null,
111
+ lastActivation: null,
112
+ totalDuration: 0
113
+ }
114
+ }
115
+
116
+ const periodStart = Math.max(rowStart.getTime(), dayStart.getTime())
117
+ const periodEnd = Math.min(rowEnd.getTime(), dayEnd.getTime())
118
+
119
+ const sensorData = groupedByDay[dateStr][row.name]
120
+
121
+ if (!sensorData.firstActivation || periodStart < sensorData.firstActivation) {
122
+ sensorData.firstActivation = periodStart
123
+ }
124
+
125
+ if (!sensorData.lastActivation || periodEnd > sensorData.lastActivation) {
126
+ sensorData.lastActivation = periodEnd
127
+ }
128
+
129
+ sensorData.totalDuration += periodEnd - periodStart
130
+ }
131
+ }
132
+ })
133
+ })
134
+
135
+ // 🖨️ Exibir os dados processados
136
+ Object.entries(groupedByDay).forEach(([date, sensors]) => {
137
+ Object.entries(sensors).forEach(([sensor, data]) => {
138
+ console.log(`Data: ${date}, Sensor: ${sensor}`)
139
+ console.log(` Primeira ativação: ${data.firstActivation ? new Date(data.firstActivation).toLocaleTimeString() : '00:00'}`)
140
+ console.log(` Última ativação: ${data.lastActivation ? new Date(data.lastActivation).toLocaleTimeString() : '23:59'}`)
141
+ console.log(` Duração total: ${(data.totalDuration / 1000 / 60).toFixed(2)} minutos`)
142
+ })
143
+ })
144
+
145
+ return Object.entries(groupedByDay)
146
+ }
147
+
77
148
  async function exportToPDF (userData, reportData) {
78
149
  console.log('exportSensorReportToPDF')
79
150
 
package/src/util/fuel.js CHANGED
@@ -1,21 +1,5 @@
1
- function calculateFuelDiff (a, b, device, p) {
2
- if (p.protocol === 'teltonika') {
3
- return b - a
4
- } else {
5
- const diff = (b - a) * fuelSignalInverter(device)
6
- if (device.attributes.xpert) {
7
- return diff
8
- } else {
9
- return (diff * 100) / Math.abs(device.attributes.fuel_low_threshold - device.attributes.fuel_high_threshold)
10
- }
11
- }
12
- }
13
-
14
- function fuelSignalInverter (device) {
15
- if (!device.attributes.fuel_low_threshold || !device.attributes.fuel_high_threshold) {
16
- return 1
17
- }
18
- return device.attributes.fuel_low_threshold <= device.attributes.fuel_high_threshold ? 1 : -1
1
+ function calculateFuelDiff (a, b) {
2
+ return b - a
19
3
  }
20
4
 
21
5
  function getRefuelingLiters (d, position, diff) {