absurdor

Files related to my duties are Absurdor of Agora Nomic
Log | Files | Refs

log.lua (1294B)


      1 require "lib.util"
      2 
      3 function is_valid_entry(e)
      4 	return (
      5 		(e.what ~= nil) and
      6 		(e.when ~= nil) and
      7 		(e.what ~= "push" or e.who ~= nil) and
      8 		(e.what ~= "report" or e.height ~= nil)
      9 	)
     10 end
     11 
     12 function parse_jsonl(f)
     13 	local log = {}
     14 	for l in f:lines() do
     15 		local e = json.decode(l)
     16 		die(not is_valid_entry(e), string.format("Invalid log entry:\n    %s", l))
     17 		table.insert(log, e)
     18 	end
     19 	return log
     20 end
     21 
     22 function write_jsonl(l)
     23 	local res = ""
     24 	for _,v in ipairs(l) do
     25 		res = res .. json.encode(v) .. "\n"
     26 	end
     27 	return res
     28 end
     29 
     30 function fmt_event(e)
     31 	date = os.date("!%Y-%m-%d %H:%M:%S %z", e.when)
     32 	args = {ts = date}
     33 	if e.what == "report" then
     34 		fmt = "[{ts}] Reported boulder at height {height}"
     35 		args.height = e.height 
     36 	elseif e.what == "push" then
     37 		fmt = "[{ts}] {who} pushed the boulder"
     38 		args.who = e.who
     39 	elseif e.what == "transfer" then
     40 		fmt = "[{ts}] {who} transfered the Veblen for {payed}"
     41 		args.who = e.who
     42 		args.payed= e.payed
     43 	elseif e.what == "devalue" then
     44 		fmt = "[{ts}] {who} devalued the Veblen to {value}"
     45 		args.who = e.who
     46 		args.value = e.value
     47 	else
     48 		die(true, "Unknown event type '%s'", e.what)
     49 	end
     50 	return format(fmt, args)
     51 end
     52 
     53 function is_duplicate(ts, log)
     54 	for _,e in ipairs(log) do
     55 		if e.when == ts then
     56 			return true
     57 		end
     58 	end
     59 	return false
     60 end
     61