SystemStat: Show awake/suspend/standby breakdown in % (#9257)

Add an "awake" field in the process, and switch to `time` to prevent precision loss ;)
reviewable/pr9263/r1
NiLuJe 2 years ago committed by GitHub
parent 6d59ca2c6a
commit f0582dd37f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,6 +7,7 @@ local PowerD = require("device"):getPowerDevice()
local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local dbg = require("dbg")
local time = require("ui/time")
local util = require("util")
local _ = require("gettext")
@ -18,13 +19,13 @@ function State:new(o)
self.__index = self
if o.percentage == nil or o.timestamp == nil then
o.percentage = PowerD:getCapacityHW()
o.timestamp = os.time()
o.timestamp = time.boottime_or_realtime_coarse()
end
return o
end
function State:toString()
return string.format("{%d @ %s}", self.percentage, os.date("%c", self.timestamp))
return string.format("{%d @ %s}", self.percentage, os.date("%c", time.to_s(self.timestamp)))
end
local Usage = {}
@ -44,14 +45,14 @@ end
function Usage:append(state)
local curr = State:new()
self.percentage = self.percentage + math.abs(state.percentage - curr.percentage)
self.time = self.time + os.difftime(curr.timestamp - state.timestamp)
self.time = self.time + curr.timestamp - state.timestamp
end
function Usage:percentageRate()
if self.time == 0 then
return 0
else
return self.percentage / self.time
return self.percentage / time.to_s(self.time)
end
end
@ -84,7 +85,7 @@ end
function Usage:dump(kv_pairs, id)
local name = id or _("Consumed:")
table.insert(kv_pairs, {INDENTATION .. _("Total time:"), duration(self.time) })
table.insert(kv_pairs, {INDENTATION .. _("Total time:"), duration(time.to_s(self.time)) })
table.insert(kv_pairs, {INDENTATION .. name, shorten(self.percentage), "%"})
table.insert(kv_pairs, {INDENTATION .. _("Change per hour:"), shorten(self:percentageRatePerHour())})
end

@ -1,6 +1,7 @@
local Device = require("device")
local Dispatcher = require("dispatcher")
local KeyValuePage = require("ui/widget/keyvaluepage")
local Math = require("optmath")
local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local time = require("ui/time")
@ -8,9 +9,10 @@ local util = require("util")
local _ = require("gettext")
local SystemStat = {
start_sec = os.time(),
suspend_sec = nil,
resume_sec = nil,
start_time = time.realtime(),
start_monotonic_time = time.boottime_or_realtime_coarse(),
suspend_time = nil,
resume_time = nil,
wakeup_count = 0,
sleep_count = 0,
charge_count = 0,
@ -27,6 +29,18 @@ function SystemStat:init()
elseif Device:isSDL() then
self.storage_filter = "/dev/sd"
end
-- Account for a start-up mid-charge
local powerd = Device:getPowerDevice()
if Device:hasAuxBattery() and powerd:isAuxBatteryConnected() then
if powerd:isAuxCharging() and not powerd:isAuxCharged() then
self.charge_count = self.charge_count + 1
end
else
if powerd:isCharging() and not powerd:isCharged() then
self.charge_count = self.charge_count + 1
end
end
end
function SystemStat:put(p)
@ -38,22 +52,39 @@ function SystemStat:putSeparator()
end
function SystemStat:appendCounters()
self:put({_("KOReader started at"), os.date("%c", self.start_sec)})
if self.suspend_sec then
self:put({_(" Last suspend time"), os.date("%c", self.suspend_sec)})
self:put({_("KOReader started at"), os.date("%c", time.to_s(self.start_time))})
if self.suspend_time then
self:put({_(" Last suspend time"), os.date("%c", time.to_s(self.suspend_time))})
end
if self.resume_sec then
self:put({_(" Last resume time"), os.date("%c", self.resume_sec)})
if self.resume_time then
self:put({_(" Last resume time"), os.date("%c", time.to_s(self.resume_time))})
end
local uptime = time.boottime_or_realtime_coarse() - self.start_monotonic_time
local suspend = 0
if Device:canSuspend() then
suspend = Device.total_suspend_time
end
local standby = 0
if Device:canStandby() then
standby = Device.total_standby_time
end
self:put({" " .. _("Up time"),
util.secondsToClockDuration("", os.difftime(os.time(), self.start_sec), false, true, true)})
util.secondsToClockDuration("", time.to_s(uptime), false, true, true)})
if Device:canSuspend() or Device:canStandby() then
local awake = uptime - suspend - standby
self:put({" " .. _("Time spent awake"),
util.secondsToClockDuration("", time.to_s(awake), false, true, true)
.. " (" .. Math.round((awake / uptime) * 100) .. "%)"})
end
if Device:canSuspend() then
self:put({" " .. _("Time in suspend"),
util.secondsToClockDuration("", time.to_number(Device.total_suspend_time), false, true, true)})
util.secondsToClockDuration("", time.to_s(suspend), false, true, true)
.. " (" .. Math.round((suspend / uptime) * 100) .. "%)"})
end
if Device:canStandby() then
self:put({" " .. _("Time in standby"),
util.secondsToClockDuration("", time.to_number(Device.total_standby_time), false, true, true)})
util.secondsToClockDuration("", time.to_s(standby), false, true, true)
.. " (" .. Math.round((standby / uptime) * 100) .. "%)"})
end
self:put({_("Counters"), ""})
self:put({_(" wake-ups"), self.wakeup_count})
@ -226,12 +257,12 @@ function SystemStat:appendStorageInfo()
end
function SystemStat:onSuspend()
self.suspend_sec = os.time()
self.suspend_time = time.realtime()
self.sleep_count = self.sleep_count + 1
end
function SystemStat:onResume()
self.resume_sec = os.time()
self.resume_time = time.realtime()
self.wakeup_count = self.wakeup_count + 1
end

@ -1,5 +1,5 @@
describe("BatteryState plugin tests #nocov", function()
local MockTime, module
local MockTime, module, time
local stat = function() --luacheck: ignore
return module:new():stat()
@ -9,6 +9,7 @@ describe("BatteryState plugin tests #nocov", function()
require("commonrequire")
package.unloadAll()
require("document/canvascontext"):init(require("device"))
time = require("ui/time")
MockTime = require("mock_time")
MockTime:install()
end)
@ -30,9 +31,9 @@ describe("BatteryState plugin tests #nocov", function()
widget:resetAll()
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(time.s(1), widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(time.s(1), widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onCharging()
@ -44,7 +45,7 @@ describe("BatteryState plugin tests #nocov", function()
assert.are.equal(0, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(0, widget.discharging.time)
assert.are.equal(1, widget.charging.time)
assert.are.equal(time.s(1), widget.charging.time)
widget:onNotCharging()
assert.is_false(widget.was_charging)
@ -52,10 +53,10 @@ describe("BatteryState plugin tests #nocov", function()
MockTime:increase(1)
widget:accumulate()
-- awake & discharging time should be reset.
assert.are.equal(1, widget.awake.time)
assert.are.equal(time.s(1), widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(1, widget.charging.time)
assert.are.equal(time.s(1), widget.discharging.time)
assert.are.equal(time.s(1), widget.charging.time)
widget:onCharging()
assert.is_true(widget.was_charging)
@ -66,7 +67,7 @@ describe("BatteryState plugin tests #nocov", function()
assert.are.equal(0, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(0, widget.discharging.time)
assert.are.equal(1, widget.charging.time)
assert.are.equal(time.s(1), widget.charging.time)
end)
it("should record suspending time", function()
@ -76,9 +77,9 @@ describe("BatteryState plugin tests #nocov", function()
widget:resetAll()
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(time.s(1), widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(time.s(1), widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onSuspend()
@ -86,9 +87,9 @@ describe("BatteryState plugin tests #nocov", function()
assert.is_true(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(1, widget.sleeping.time)
assert.are.equal(2, widget.discharging.time)
assert.are.equal(time.s(1), widget.awake.time)
assert.are.equal(time.s(1), widget.sleeping.time)
assert.are.equal(time.s(2), widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onResume()
@ -96,9 +97,9 @@ describe("BatteryState plugin tests #nocov", function()
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(2, widget.awake.time)
assert.are.equal(1, widget.sleeping.time)
assert.are.equal(3, widget.discharging.time)
assert.are.equal(time.s(2), widget.awake.time)
assert.are.equal(time.s(1), widget.sleeping.time)
assert.are.equal(time.s(3), widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onSuspend()
@ -106,9 +107,9 @@ describe("BatteryState plugin tests #nocov", function()
assert.is_true(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(2, widget.awake.time)
assert.are.equal(2, widget.sleeping.time)
assert.are.equal(4, widget.discharging.time)
assert.are.equal(time.s(2), widget.awake.time)
assert.are.equal(time.s(2), widget.sleeping.time)
assert.are.equal(time.s(4), widget.discharging.time)
assert.are.equal(0, widget.charging.time)
end)
@ -119,9 +120,9 @@ describe("BatteryState plugin tests #nocov", function()
widget:resetAll()
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(time.s(1), widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(time.s(1), widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onCharging()
@ -133,7 +134,7 @@ describe("BatteryState plugin tests #nocov", function()
assert.are.equal(0, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(0, widget.discharging.time)
assert.are.equal(1, widget.charging.time)
assert.are.equal(time.s(1), widget.charging.time)
widget:onCharging()
assert.is_true(widget.was_charging)
@ -143,7 +144,7 @@ describe("BatteryState plugin tests #nocov", function()
assert.are.equal(0, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(0, widget.discharging.time)
assert.are.equal(2, widget.charging.time)
assert.are.equal(time.s(2), widget.charging.time)
end)
it("should not swap the state when several suspending events fired", function()
@ -153,9 +154,9 @@ describe("BatteryState plugin tests #nocov", function()
widget:resetAll()
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(time.s(1), widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(time.s(1), widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onSuspend()
@ -163,9 +164,9 @@ describe("BatteryState plugin tests #nocov", function()
assert.is_true(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(1, widget.sleeping.time)
assert.are.equal(2, widget.discharging.time)
assert.are.equal(time.s(1), widget.awake.time)
assert.are.equal(time.s(1), widget.sleeping.time)
assert.are.equal(time.s(2), widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onSuspend()
@ -173,9 +174,9 @@ describe("BatteryState plugin tests #nocov", function()
assert.is_true(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(2, widget.sleeping.time)
assert.are.equal(3, widget.discharging.time)
assert.are.equal(time.s(1), widget.awake.time)
assert.are.equal(time.s(2), widget.sleeping.time)
assert.are.equal(time.s(3), widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onSuspend()
@ -183,9 +184,9 @@ describe("BatteryState plugin tests #nocov", function()
assert.is_true(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(3, widget.sleeping.time)
assert.are.equal(4, widget.discharging.time)
assert.are.equal(time.s(1), widget.awake.time)
assert.are.equal(time.s(3), widget.sleeping.time)
assert.are.equal(time.s(4), widget.discharging.time)
assert.are.equal(0, widget.charging.time)
end)
end)

Loading…
Cancel
Save