report.lua (3433B)
1 require "lib.utils" 2 3 local _M = {} 4 5 local fns = { 6 tmp = ".tmp", 7 history = ".hist", 8 changes = ".changes" 9 } 10 11 function sincelast(log, what) 12 local all = table.filter(log, function (e) return e.what == what end) 13 table.sort(all, function (e0, e1) return e0.when > e1.when end) 14 local last = all[1].when 15 local since = table.filter(log, function (e) return e.when >= last end) 16 return since, last 17 end 18 19 function format_events(f, es) 20 for _,e in ipairs(es) do 21 f:write(format_event(e)) 22 end 23 end 24 25 function _M.weekly(args, players, log) 26 defs = { 27 YEAR = os.date("%Y"), 28 MONTH = os.date("%m"), 29 DAY = os.date("%d"), 30 MAILDATE = os.date("%a, %d %b %Y %T %z"), 31 CHANGES = string.format("include(%s)", fns.changes) 32 } 33 34 local changes = io.open(fns.changes, "w") 35 local since, last = sincelast(log, "weekly") 36 format_events(changes, since) 37 changes:close() 38 os.execute(string.format("git log --oneline --since=%s >> %s", os.date("%Y-%m-%dT%H:%M", last), fns.changes)) 39 40 os.execute(string.format("m4 %s templates/weekly/weekly.m4 > %s", m4flags(defs), fns.tmp)) 41 if args.p then 42 os.execute(string.format("cat %s", fns.tmp)) 43 os.remove(fns.tmp) 44 else 45 os.execute(string.format("vis %s", fns.tmp)) 46 os.execute(string.format("neomutt -H %s -E", fns.tmp)) 47 if yn("Archive report? [Yn] ") then 48 table.insert(log, { 49 when = os.time(), 50 what = "weekly", 51 height = height, 52 }) 53 os.rename(fns.tmp, os.date("archive/%F-weekly.txt")) 54 else 55 os.remove(fns.tmp) 56 end 57 end 58 os.remove(fns.changes) 59 end 60 61 function _M.monthly(args, players, log) 62 63 defs = { 64 YEAR = os.date("%Y"), 65 MONTH = os.date("%m"), 66 DAY = os.date("%d"), 67 MAILDATE = os.date("%a, %d %b %Y %T %z"), 68 PLAYERS = string.format("include(%s)", fns.history), 69 CHANGES = string.format("include(%s)", fns.changes) 70 } 71 72 local names = {} 73 for n,_ in pairs(players) do 74 table.insert(names, n) 75 end 76 table.sort(names) 77 78 local hist = io.open(fns.history, "w") 79 for i,n in ipairs(names) do 80 local h = players[n].history 81 hist:write("===============\n"..n) 82 if players[n].contact then hist:write(" <"..players[n].contact..">\n") else hist:write("\n") end 83 if players[n].birthday then hist:write(" Birthday: " .. players[n].birthday .. "\n") end 84 for _,e in ipairs(h) do 85 if e.reason == "s" then 86 f = " ({reason}) {name} <{contact}> ({registration}--now)\n" 87 else 88 f = " ({reason}) {name} <{contact}> ({registration}--{deregistration})\n" 89 end 90 hist:write(format(f, e)) 91 if e.observations then 92 hist:write(format(" {observations}\n", e)) 93 end 94 end 95 hist:write("\n") 96 end 97 hist:close() 98 99 local changes = io.open(fns.changes, "w") 100 local since, last = sincelast(log, "monthly") 101 format_events(changes, sincelast(log, "monthly")) 102 changes:close() 103 os.execute(string.format("git log --oneline --since=%s >> %s", os.date("%Y-%m-%dT%H:%M", last), fns.changes)) 104 105 os.execute(string.format("m4 %s templates/monthly/monthly.m4 > %s", m4flags(defs), fns.tmp)) 106 107 if args.p then 108 os.execute(string.format("cat %s", fns.tmp)) 109 os.remove(fns.tmp) 110 else 111 os.execute(string.format("vis %s", fns.tmp)) 112 os.execute(string.format("neomutt -H %s -E", fns.tmp)) 113 if yn("Archive report? [Yn] ") then 114 table.insert(log, { 115 when = os.time(), 116 what = "monthly" 117 }) 118 os.rename(fns.tmp, os.date("archive/%F-monthly.txt")) 119 else 120 os.remove(fns.tmp) 121 end 122 end 123 os.remove(fns.history) 124 os.remove(fns.changes) 125 end 126 127 return _M