@@ -151,44 +151,83 @@ function createBatteryLifeHistoryData(info) {
151
151
152
152
function getGroupedPowerUsageInfoData ( info , filterConditional ) {
153
153
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
+ }
155
158
function getMidnightDateTime ( date ) {
156
159
return new Date ( date . toUTCString ( ) ) . setHours ( 0 , 0 , 0 , 0 )
157
160
}
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 ] )
158
173
if ( past . getDate ( ) === current . getDate ( ) ) {
159
174
if ( future . getDate ( ) === current . getDate ( ) ) {
160
- return ( future - current ) / 1000 / 60
175
+ return [ getReturnItem ( currentItem , ( future - current ) ) ]
161
176
}
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
+ ]
163
200
}
164
- return ( current - getMidnightDateTime ( current ) ) / 1000 / 60
165
201
}
166
202
if ( filterConditional ( item ) ) {
167
203
return data
168
204
}
169
- let [ date , time ] = item [ 0 ] . split ( " " )
170
205
switch ( index ) {
171
206
case info . data . length - 1 :
172
207
return data
173
208
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 ] ] )
177
209
return data
178
210
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 ) )
181
217
return data
182
218
}
183
219
} , [ ] )
184
220
return groupBy ( formattedData , 0 )
185
221
}
186
222
187
- function getDailySumGroupedPowerUsageInfoData ( groupData , filterConditional ) {
223
+ function getDailySumGroupedPowerUsageInfoData ( groupData , filterConditional , segregationConditional ) {
188
224
let powerUsageDataset = [ ]
189
225
for ( let [ , value ] of Object . entries ( groupData ) ) {
190
226
powerUsageDataset . push ( value . reduce ( ( data , item ) => {
191
227
if ( filterConditional ( item ) ) {
228
+ return data
229
+ }
230
+ if ( segregationConditional ( item ) ) {
192
231
data [ 0 ] += item [ 2 ]
193
232
}
194
233
else {
@@ -209,11 +248,13 @@ function formatPowerUsageInfoTime(minutes) {
209
248
210
249
function createCumulativePiePowerUsageInfoData ( info ) {
211
250
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
216
252
} )
253
+ let powerUsageDataset = getDailySumGroupedPowerUsageInfoData (
254
+ groupData ,
255
+ ( item ) => item [ 3 ] . toLowerCase ( ) === 'suspended' ,
256
+ ( item ) => item [ 4 ] . toUpperCase ( ) === 'AC'
257
+ )
217
258
let cumulativePowerUsageDataset = powerUsageDataset . reduce ( ( data , item ) => {
218
259
data [ 0 ] += Math . floor ( item [ 0 ] )
219
260
data [ 1 ] += Math . floor ( item [ 1 ] )
@@ -236,19 +277,18 @@ function createCumulativePiePowerUsageInfoData(info) {
236
277
237
278
function createBarPowerUsageInfoData ( info ) {
238
279
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 ( _ ) {
245
280
return false
246
281
} )
282
+ let powerUsageDataset = getDailySumGroupedPowerUsageInfoData (
283
+ groupData ,
284
+ ( item ) => item [ 3 ] . toLowerCase ( ) === 'suspended' ,
285
+ ( item ) => item [ 4 ] . toUpperCase ( ) === 'AC'
286
+ )
247
287
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
+ )
252
292
253
293
return {
254
294
labels : Object . keys ( groupData ) ,
@@ -284,24 +324,26 @@ function createActiveSuspendedPiePowerUsageInfoData(info) {
284
324
let groupData = getGroupedPowerUsageInfoData ( info , function ( _ ) {
285
325
return false
286
326
} )
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 ) => {
291
333
data [ 0 ] += Math . floor ( item [ 0 ] )
292
334
data [ 1 ] += Math . floor ( item [ 1 ] )
293
335
return data
294
336
} , [ 0 , 0 ] )
295
337
296
338
return {
297
339
labels : [
298
- 'ON ' + formatPowerUsageInfoTime ( cumulativePowerUsageDataset [ 0 ] ) ,
299
- 'OFF ' + formatPowerUsageInfoTime ( cumulativePowerUsageDataset [ 1 ] )
340
+ 'ON ' + formatPowerUsageInfoTime ( cumulativePowerUsageActiveSuspendedDataset [ 0 ] ) ,
341
+ 'OFF ' + formatPowerUsageInfoTime ( cumulativePowerUsageActiveSuspendedDataset [ 1 ] )
300
342
] ,
301
343
datasets : [ {
302
344
label : 'AC vs Battery' ,
303
345
backgroundColor : [ 'blue' , 'red' ] ,
304
- data : cumulativePowerUsageDataset ,
346
+ data : cumulativePowerUsageActiveSuspendedDataset ,
305
347
fill : false ,
306
348
} ]
307
349
}
0 commit comments