collapse log script

This commit is contained in:
metamuffin 2024-10-31 01:25:53 +01:00
parent efd2e6c80e
commit 2eb6754ca4
No known key found for this signature in database
GPG key ID: 718F9749DCDBD654
2 changed files with 35 additions and 0 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@
!/readme.md !/readme.md
!/strichliste.lua !/strichliste.lua
!/.gitignore !/.gitignore
!/collapse_log.lua

34
collapse_log.lua Normal file
View file

@ -0,0 +1,34 @@
local function read_log()
local log = io.open("log", "r")
if log == nil then
return function() return nil end
end
local lines = log:lines("l")
return function()
local l = lines()
if l == "" or l == nil then
return nil
end
local time, username, amount, comment = string.match(l, "(%d+),([%w_ -]+),(-?%d+),([%w_ -]*)")
return tonumber(time), username, tonumber(amount), comment
end
end
local function balances()
local users = {}
for _, username, amount, _ in read_log() do
users[username] = (users[username] or 0) + amount
end
return users
end
local newlog = io.open("log_collapsed","w+")
if newlog == nil then
return print("error failed to open log")
end
for username, amount in pairs(balances()) do
newlog:write(string.format("%d,%s,%d,%s\n", os.time(), username, amount, "Collapsed transaction history"))
end
newlog:close()