Skip to content

Commit 2bc2b62

Browse files
committed
Fixed bugs around estimated battery life graph
1 parent 613c39d commit 2bc2b62

File tree

7 files changed

+82
-49
lines changed

7 files changed

+82
-49
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ battery-report.html
2828
battery-report.xml
2929
output.json
3030
update.json
31-
31+
battery-debug-reports/

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ and it can be copied from system to system seamlessly without any dependencies o
124124
### Desktop App (Main Mode)
125125

126126
- Simply go to [Releases](https://github.com/SlapBot/powir/releases) and pick the latest version to download.
127-
- Download Current Latest Version (Powir v1): [Github](https://github.com/SlapBot/powir/releases/latest/download/powir.1.0.1.exe) or [GDrive](https://drive.google.com/drive/folders/1ntEjEenEzZXMP8L_nXNoHnIFqNSLpwD9): **Please take a note below before downloading the app**
127+
- Download Current Latest Version (Powir v1): [Github](https://github.com/SlapBot/powir/releases/latest/download/powir.1.0.2.exe) or [GDrive](https://drive.google.com/drive/folders/1ntEjEenEzZXMP8L_nXNoHnIFqNSLpwD9): **Please take a note below before downloading the app**
128128

129129
#### Browser And Windows Defender Warnings
130130

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "powir",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Windows 10 based tool to monitor and analyze your system's power and battery usage",
55
"main": "public/electron.js",
66
"repository": {

public/app/scrape.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ function getNote(soup, index) {
7373
case 1:
7474
return "Information about each currently installed battery"
7575
case 2:
76-
return "Power states over the last 3 days"
76+
return "Power states over the last few days"
7777
case 3:
78-
return "Battery drains over the last 3 days"
78+
return "Battery drains over the last few days"
7979
case 4:
8080
return "History of system usage on AC and battery"
8181
case 5:

src/components/sub-components/ChartData.js

+49-38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {groupBy, meanBy, sortBy} from 'lodash'
1+
import {groupBy, sortBy} from 'lodash'
22

33
function createData(info, type, metaData) {
44
switch (type) {
@@ -90,66 +90,84 @@ function createBatteryLifeHistoryData(info) {
9090
* is not an int, then really you should average the two elements on either
9191
* side to find q1.
9292
*/
93-
let q1 = values[Math.floor((values.length / 4))];
93+
let q1 = values[Math.floor((values.length / 4))]
9494
// Likewise for q3.
95-
let q3 = values[Math.ceil((values.length * (3 / 4)))];
95+
let q3 = values[Math.ceil((values.length * (3 / 4)))]
9696
let iqr = q3 - q1;
9797

9898
// Then find min and max values
9999
let maxValue = q3 + iqr*1.5;
100100

101-
// Then filter anything beyond or beneath these values.
102-
return array.filter(function(x) {
103-
return (x <= maxValue)
104-
});
101+
// Then reduce anything beyond or beneath these values.
102+
return array.reduce((data, item, index) => {
103+
if (item <= maxValue) {
104+
data.indexes.push(index)
105+
data.data.push(item)
106+
return data
107+
}
108+
return data
109+
}, {
110+
data: [],
111+
indexes: []
112+
})
105113
}
106114

107-
function filterDataset(value, index) {
108-
let activeDatasetRowAverage = meanBy(value, index)
115+
function averageDatasetByMonth(value, index) {
109116
return value.reduce((data, element) => {
110-
let value = element[index]
111-
if (data / activeDatasetRowAverage < 5) {
112-
data += value
113-
}
117+
data += element[index]
114118
return data
115119
}, 0) / value.length
116120
}
117121

118-
let formattedInfo = groupBy(info.data.map(item => {
119-
return [
122+
let formattedInfo = groupBy(info.data.reduce((data, item) => {
123+
if (item[1] === "0:00:00" || item[3] === "0:00:00") {
124+
return data
125+
}
126+
data.push([
120127
chooseDate(item[0]),
121128
Number(item[1].split(":")[0])*60 + Number(item[1].split(":")[1]),
122129
Number(item[3].split(":")[0])*60 + Number(item[3].split(":")[1]),
123-
]
124-
}), 0)
130+
])
131+
return data
132+
}, []), 0)
125133

126134
let activeDataset = []
127135
let designDataset = []
128136

129137
for (let [, value] of Object.entries(formattedInfo)) {
130-
activeDataset.push(filterDataset(value, 1))
131-
designDataset.push(filterDataset(value, 2))
138+
activeDataset.push(averageDatasetByMonth(value, 1))
139+
designDataset.push(averageDatasetByMonth(value, 2))
132140
}
141+
let filteredDataset = filterOutliers(activeDataset)
142+
let filteredActiveDataset = filterOutliers(activeDataset).data
143+
let filteredDesignDataset = designDataset.filter((item, index) => filteredDataset.indexes.includes(index))
144+
let labelsDataset = Object.keys(formattedInfo).reduce((data, item, index) => {
145+
if (filteredDataset.indexes.includes(index)) {
146+
data.push(item.slice(0, 4) + "/" + item.slice(4, 6))
147+
return data
148+
}
149+
return data
150+
}, [])
133151

134152
return {
135-
labels: Object.keys(formattedInfo),
153+
labels: labelsDataset,
136154
datasets: [{
137155
label: info.keys[1],
138156
backgroundColor: 'red',
139157
borderColor: 'red',
140-
data: filterOutliers(activeDataset),
158+
data: filteredActiveDataset,
141159
fill: false,
142160
}, {
143161
label: info.keys[3],
144162
backgroundColor: 'blue',
145163
borderColor: 'blue',
146-
data: filterOutliers(designDataset),
164+
data: filteredDesignDataset,
147165
fill: false,
148166
}]
149167
}
150168
}
151169

152-
function getGroupedPowerUsageInfoData(info, filterConditional) {
170+
function getGroupedPowerUsageInfoData(info) {
153171
let formattedData = info.data.reduce((data, item, index) => {
154172
function getDuration(pastItem, currentItem, futureItem) {
155173
function getDateObject(dateString) {
@@ -199,9 +217,6 @@ function getGroupedPowerUsageInfoData(info, filterConditional) {
199217
]
200218
}
201219
}
202-
if (filterConditional(item)) {
203-
return data
204-
}
205220
switch (index) {
206221
case info.data.length-1:
207222
return data
@@ -247,9 +262,7 @@ function formatPowerUsageInfoTime(minutes) {
247262
}
248263

249264
function createCumulativePiePowerUsageInfoData(info) {
250-
let groupData = getGroupedPowerUsageInfoData(info, function (item) {
251-
return false
252-
})
265+
let groupData = getGroupedPowerUsageInfoData(info)
253266
let powerUsageDataset = getDailySumGroupedPowerUsageInfoData(
254267
groupData,
255268
(item) => item[3].toLowerCase() === 'suspended',
@@ -276,17 +289,15 @@ function createCumulativePiePowerUsageInfoData(info) {
276289
}
277290

278291
function createBarPowerUsageInfoData(info) {
279-
let groupData = getGroupedPowerUsageInfoData(info, function (item) {
280-
return false
281-
})
292+
let groupData = getGroupedPowerUsageInfoData(info)
282293
let powerUsageDataset = getDailySumGroupedPowerUsageInfoData(
283294
groupData,
284295
(item) => item[3].toLowerCase() === 'suspended',
285296
(item) => item[4].toUpperCase() === 'AC'
286297
)
287298
let powerUsageActiveSuspendedDataset = getDailySumGroupedPowerUsageInfoData(
288299
groupData,
289-
(item) => false,
300+
(_) => false,
290301
(item) => item[3].toLowerCase() === 'active'
291302
)
292303

@@ -321,12 +332,10 @@ function createBarPowerUsageInfoData(info) {
321332
}
322333

323334
function createActiveSuspendedPiePowerUsageInfoData(info) {
324-
let groupData = getGroupedPowerUsageInfoData(info, function(_) {
325-
return false
326-
})
335+
let groupData = getGroupedPowerUsageInfoData(info)
327336
let powerUsageActiveSuspendedDataset = getDailySumGroupedPowerUsageInfoData(
328337
groupData,
329-
(item) => false,
338+
(_) => false,
330339
(item) => item[3].toLowerCase() === 'active'
331340
)
332341
let cumulativePowerUsageActiveSuspendedDataset = powerUsageActiveSuspendedDataset.reduce((data, item) => {
@@ -353,7 +362,9 @@ function createLinePowerUsageInfoData(info) {
353362
let percentageDataset = info.data.reduce((data, item, index) => {
354363
let dateString = (date) => date.getMonth()+1 + "/" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes()
355364
if (index > 1) {
356-
if (item[item.length-2] === info.data[index-2][info.data[index-2].length-2]) {
365+
let currentPercentage = item[item.length-2]
366+
let lastPercentage = info.data[index-1][info.data[index-1].length-2]
367+
if (currentPercentage === lastPercentage) {
357368
return data
358369
}
359370
}

src/components/sub-components/RecentBatteryEstimate.js

+27-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,29 @@ import React from "react";
22

33

44
function RecentBatteryEstimate(props) {
5-
function renderValue(value) {
5+
function renderValue(formattedData, type) {
6+
switch (type) {
7+
case 'MINIMUM':
8+
if (formattedData.minimum === Infinity) {
9+
return 'NONE'
10+
}
11+
return formatTime(formattedData.minimum)
12+
case 'MAXIMUM':
13+
if (formattedData.maximum === 0) {
14+
return 'NONE'
15+
}
16+
return formatTime(formattedData.maximum)
17+
case 'AVERAGE':
18+
if (formattedData.maximum === 0 || formattedData.minimum === Infinity) {
19+
return 'NONE'
20+
}
21+
return formatTime(formattedData.sum/formattedData.count)
22+
default:
23+
return 'NONE'
24+
}
25+
}
26+
27+
function formatTime(value) {
628
let hours = Math.floor(value / 60)
729
let minutes = Math.floor(value % 60)
830
let seconds = Math.floor((value - Math.floor(value)) * 100)
@@ -26,7 +48,7 @@ function RecentBatteryEstimate(props) {
2648
data.count += 1
2749
return data
2850
}, {
29-
minimum: 9999999999,
51+
minimum: Infinity,
3052
maximum: 0,
3153
sum: 0,
3254
count: 0
@@ -43,15 +65,15 @@ function RecentBatteryEstimate(props) {
4365
<div className="flex">
4466
<div className="mr-1 flex">
4567
<h5 className="mr-1">Minimum: </h5>
46-
<span className="badge bg-red">{renderValue(formattedData.minimum)}</span>
68+
<span className="badge bg-red">{renderValue(formattedData, 'MINIMUM')}</span>
4769
</div>
4870
<div className="mr-1 flex">
4971
<h5 className="mr-1">Maximum: </h5>
50-
<span className="badge bg-blue">{renderValue(formattedData.maximum)}</span>
72+
<span className="badge bg-blue">{renderValue(formattedData, 'MAXIMUM')}</span>
5173
</div>
5274
<div className="flex">
5375
<h5 className="mr-1">Average: </h5>
54-
<span className="badge success">{renderValue(formattedData.sum/formattedData.count)}</span>
76+
<span className="badge success">{renderValue(formattedData, 'AVERAGE')}</span>
5577
</div>
5678
</div>
5779
</div>

src/components/utils/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export default {
2-
version: '1.0.1',
2+
version: '1.0.2',
33
liteMode: process.env.REACT_APP_LITE_MODE === '1'
44
}

0 commit comments

Comments
 (0)