38 lines
1.2 KiB
Lua
38 lines
1.2 KiB
Lua
|
|
-- Sum of the last 7 days' expenses (rolling window).
|
|||
|
|
|
|||
|
|
local lang = jarvis.context.language
|
|||
|
|
local raw_state = jarvis.state.get("entries") or ""
|
|||
|
|
if raw_state == "" then
|
|||
|
|
return jarvis.cmd.not_found(lang == "ru"
|
|||
|
|
and "На этой неделе ничего не тратили."
|
|||
|
|
or "Nothing logged this week.")
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
local now = jarvis.context.time
|
|||
|
|
local now_ts = os.time({
|
|||
|
|
year = now.year, month = now.month, day = now.day,
|
|||
|
|
hour = now.hour, min = now.minute, sec = now.second or 0,
|
|||
|
|
})
|
|||
|
|
local cutoff = now_ts - 7 * 86400
|
|||
|
|
|
|||
|
|
local total = 0
|
|||
|
|
local count = 0
|
|||
|
|
for chunk in raw_state:gmatch("([^|]+)") do
|
|||
|
|
local ts, am = chunk:match("^(%d+),([%-%d%.]+),")
|
|||
|
|
ts = tonumber(ts); am = tonumber(am)
|
|||
|
|
if ts and am and ts >= cutoff then
|
|||
|
|
total = total + am
|
|||
|
|
count = count + 1
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
if total == 0 then
|
|||
|
|
return jarvis.cmd.ok(lang == "ru"
|
|||
|
|
and "На этой неделе тратами не похваляешься, сэр."
|
|||
|
|
or "Nothing tracked this week, sir.")
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
return jarvis.cmd.ok(string.format(lang == "ru"
|
|||
|
|
and "За неделю %d трат на сумму %.0f."
|
|||
|
|
or "This week: %d expenses totalling %.0f.", count, total))
|