Skip to content

Commit e90b102

Browse files
committed
Fixed bar chart bug
1 parent 8101cdf commit e90b102

File tree

7 files changed

+86
-37
lines changed

7 files changed

+86
-37
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ yarn-error.log*
2525
.idea/
2626
dist/
2727
battery-report.html
28+
battery-report.xml
2829
output.json
2930
update.json
3031

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/app/parse.js

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ function parseUsageInfo(data, colElements, cleanerFn) {
1010
break
1111
case 2:
1212
// Source: AC/Battery
13-
// TODO: there seems to be a bug over here
1413
if (formattedElement === "") {
1514
return data[data.length-1][colIndex] + formattedElement
1615
}

public/app/server.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ const logger = require('electron-log')
77

88

99
function getBatteryReport() {
10-
return scraper.generateBatteryReport('powercfg /batteryreport')
10+
return scraper.generateBatteryReport(
11+
'powercfg batteryreport output "public/battery-report.html" duration 5'
12+
)
1113
.catch(error => log('error', error))
1214
.then(_ => scraper.getHtmlFromFile('battery-report.html'))
1315
.catch(error => log(error))

public/browser.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const broadcastUrl = 'https://gist.github.com/SlapBot/4b093f88d97522e22205ae9c9d
44

55

66
function getBatteryReport() {
7-
return scraper.generateBatteryReport('powercfg /batteryreport /output "public/battery-report.html"')
7+
return scraper.generateBatteryReport(
8+
'powercfg batteryreport output "public/battery-report.html" duration 5'
9+
)
810
.catch(error => console.log('error', error))
911
.then(_ => scraper.getHtmlFromFile('public/battery-report.html'))
1012
.catch(error => console.log(error))

src/components/sub-components/Chart.js

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ function Chart(props) {
3131
if (metaData.type === 'cumulativePie') {
3232
return <Pie data={data} />
3333
}
34+
else if(metaData.type === 'cumulativeActiveSuspended') {
35+
return <Pie data={data}/>
36+
}
3437
else if (metaData.type === 'dailyBar') {
3538
return <Bar data={data} options={options}/>
3639
}

src/components/sub-components/ChartData.js

+74-32
Original file line numberDiff line numberDiff line change
@@ -151,44 +151,83 @@ function createBatteryLifeHistoryData(info) {
151151

152152
function getGroupedPowerUsageInfoData(info, filterConditional) {
153153
let formattedData = info.data.reduce((data, item, index) => {
154-
function getDuration(past, current, future) {
154+
function getDuration(pastItem, currentItem, futureItem) {
155+
function getDateObject(dateString) {
156+
return new Date(dateString)
157+
}
155158
function getMidnightDateTime(date) {
156159
return new Date(date.toUTCString()).setHours(0, 0, 0, 0)
157160
}
161+
function getReturnItem(item, duration) {
162+
return [
163+
item[0].split(" ")[0],
164+
item[0].split(" ")[1],
165+
duration / 1000 / 60,
166+
item[1],
167+
item[2]
168+
]
169+
}
170+
let past = getDateObject(pastItem[0])
171+
let current = getDateObject(currentItem[0])
172+
let future = getDateObject(futureItem[0])
158173
if (past.getDate() === current.getDate()) {
159174
if (future.getDate() === current.getDate()) {
160-
return (future - current) / 1000 / 60
175+
return [getReturnItem(currentItem, (future - current))]
161176
}
162-
return (getMidnightDateTime(future) - current) / 1000 / 60
177+
return [getReturnItem(currentItem, (getMidnightDateTime(future) - current))]
178+
}
179+
else {
180+
if (future.getDate() === current.getDate()) {
181+
return [[
182+
currentItem[0].split(" ")[0],
183+
"0:00:00",
184+
(current - getMidnightDateTime(current)) / 1000 / 60,
185+
'suspended',
186+
null
187+
],
188+
getReturnItem(currentItem, (future - current))
189+
]
190+
}
191+
return [[
192+
currentItem[0].split(" ")[0],
193+
"0:00:00",
194+
(current - getMidnightDateTime(current)) / 1000 / 60,
195+
'suspended',
196+
null
197+
],
198+
getReturnItem(currentItem, (getMidnightDateTime(future) - current))
199+
]
163200
}
164-
return (current - getMidnightDateTime(current)) / 1000 / 60
165201
}
166202
if (filterConditional(item)) {
167203
return data
168204
}
169-
let [date, time] = item[0].split(" ")
170205
switch (index) {
171206
case info.data.length-1:
172207
return data
173208
case 0:
174-
data.push([date, time,
175-
(new Date(info.data[index+1][0]) - new Date(item[0])) / 1000 / 60,
176-
item[1], item[2], item[3], item[4]])
177209
return data
178210
default:
179-
let duration = getDuration(new Date(info.data[index-1][0]), new Date(item[0]), new Date(info.data[index+1][0]))
180-
data.push([date, time, duration, item[1], item[2], item[3], item[4]])
211+
let durations = getDuration(
212+
info.data[index-1],
213+
item,
214+
info.data[index+1]
215+
)
216+
durations.map(duration => data.push(duration))
181217
return data
182218
}
183219
}, [])
184220
return groupBy(formattedData, 0)
185221
}
186222

187-
function getDailySumGroupedPowerUsageInfoData(groupData, filterConditional) {
223+
function getDailySumGroupedPowerUsageInfoData(groupData, filterConditional, segregationConditional) {
188224
let powerUsageDataset = []
189225
for (let [, value] of Object.entries(groupData)) {
190226
powerUsageDataset.push(value.reduce((data, item) => {
191227
if (filterConditional(item)) {
228+
return data
229+
}
230+
if (segregationConditional(item)) {
192231
data[0] += item[2]
193232
}
194233
else {
@@ -209,11 +248,13 @@ function formatPowerUsageInfoTime(minutes) {
209248

210249
function createCumulativePiePowerUsageInfoData(info) {
211250
let groupData = getGroupedPowerUsageInfoData(info, function (item) {
212-
return item[1].toLowerCase() === 'suspended'
213-
})
214-
let powerUsageDataset = getDailySumGroupedPowerUsageInfoData(groupData, function (item) {
215-
return item[4].toUpperCase() === 'AC'
251+
return false
216252
})
253+
let powerUsageDataset = getDailySumGroupedPowerUsageInfoData(
254+
groupData,
255+
(item) => item[3].toLowerCase() === 'suspended',
256+
(item) => item[4].toUpperCase() === 'AC'
257+
)
217258
let cumulativePowerUsageDataset = powerUsageDataset.reduce((data, item) => {
218259
data[0] += Math.floor(item[0])
219260
data[1] += Math.floor(item[1])
@@ -236,19 +277,18 @@ function createCumulativePiePowerUsageInfoData(info) {
236277

237278
function createBarPowerUsageInfoData(info) {
238279
let groupData = getGroupedPowerUsageInfoData(info, function (item) {
239-
return item[1].toLowerCase() === 'suspended'
240-
})
241-
let powerUsageDataset = getDailySumGroupedPowerUsageInfoData(groupData, function (item) {
242-
return item[4].toUpperCase() === 'AC'
243-
})
244-
let groupActiveSuspendedData = getGroupedPowerUsageInfoData(info, function(_) {
245280
return false
246281
})
282+
let powerUsageDataset = getDailySumGroupedPowerUsageInfoData(
283+
groupData,
284+
(item) => item[3].toLowerCase() === 'suspended',
285+
(item) => item[4].toUpperCase() === 'AC'
286+
)
247287
let powerUsageActiveSuspendedDataset = getDailySumGroupedPowerUsageInfoData(
248-
groupActiveSuspendedData,
249-
function (item) {
250-
return item[3].toLowerCase() === 'active'
251-
})
288+
groupData,
289+
(item) => false,
290+
(item) => item[3].toLowerCase() === 'active'
291+
)
252292

253293
return {
254294
labels: Object.keys(groupData),
@@ -284,24 +324,26 @@ function createActiveSuspendedPiePowerUsageInfoData(info) {
284324
let groupData = getGroupedPowerUsageInfoData(info, function(_) {
285325
return false
286326
})
287-
let powerUsageDataset = getDailySumGroupedPowerUsageInfoData(groupData, function (item) {
288-
return item[3].toLowerCase() === 'active'
289-
})
290-
let cumulativePowerUsageDataset = powerUsageDataset.reduce((data, item) => {
327+
let powerUsageActiveSuspendedDataset = getDailySumGroupedPowerUsageInfoData(
328+
groupData,
329+
(item) => false,
330+
(item) => item[3].toLowerCase() === 'active'
331+
)
332+
let cumulativePowerUsageActiveSuspendedDataset = powerUsageActiveSuspendedDataset.reduce((data, item) => {
291333
data[0] += Math.floor(item[0])
292334
data[1] += Math.floor(item[1])
293335
return data
294336
}, [0, 0])
295337

296338
return {
297339
labels: [
298-
'ON ' + formatPowerUsageInfoTime(cumulativePowerUsageDataset[0]),
299-
'OFF ' + formatPowerUsageInfoTime(cumulativePowerUsageDataset[1])
340+
'ON ' + formatPowerUsageInfoTime(cumulativePowerUsageActiveSuspendedDataset[0]),
341+
'OFF ' + formatPowerUsageInfoTime(cumulativePowerUsageActiveSuspendedDataset[1])
300342
],
301343
datasets: [{
302344
label: 'AC vs Battery',
303345
backgroundColor: ['blue', 'red'],
304-
data: cumulativePowerUsageDataset,
346+
data: cumulativePowerUsageActiveSuspendedDataset,
305347
fill: false,
306348
}]
307349
}

0 commit comments

Comments
 (0)