DAY_SEC = 24*60*60 DAYS_AVG = 20 local function time_to_date(ts) local struct = os.date("*t",ts) return string.format("%d/%02d",struct.year % 100,struct.month), struct.day end function math.round(n) return math.floor(n+0.5) end ROMAN = {"I","II","III","IV","V","VI","VII","VIII","IX","X","XI","XII"} local function mswitch(month) local a,b = month:match("(..)/(..)") return ROMAN[tonumber(b)].."/"..a end local function js_array(tbl) local result = {} for i, t in ipairs(tbl) do if type(t) == "table" then t = js_array(t) elseif type(t) == "string" then t = '"'..t..'"' elseif t == false then t = "null" end table.insert(result,t) end return "["..table.concat(result,",").."]" end local charts = function () local db = assert(dofile("/home/fuxoft/appdata/ffffriends.db")) local months = {} local min = os.time()+9 local max = 0 for k,v in pairs(db) do max = math.max(k, max) min = math.min(k, min) end local tot_money, tot_days = 0,0 for ts = min, max, DAY_SEC do local amount = db[ts] or 0 local mname, day = time_to_date(ts) local month = months[mname] if not month then month = {total = 0,averages = {}, values={}} months[mname] = month end month.total = month.total + amount tot_money = tot_money + amount tot_days = tot_days + 1 assert(not month.values[day],"Already has value?! "..mname..":"..day) month.values[day] = amount local sum = amount local days = DAYS_AVG local ts2 = ts for i = 1,days-1 do ts2 = ts2 - DAY_SEC sum = sum + (db[ts2] or 0) end assert(not month.averages[day],"Already has average?! "..mname..":"..day) month.averages[day] = sum / days end local daily_avg = math.round(tot_money/tot_days) DAILY_AVG = string.format("%.2f", tot_money/tot_days) local mnames = {} for k,v in pairs(months) do table.insert(mnames,k) end table.sort(mnames) local totals = {{"Měsíc","Kč"}} local vsechno = {} for i,mname in ipairs(mnames) do local month = assert(months[mname], "Month doesn't exist:"..mname) table.insert(totals,{mswitch(mname), math.round(month.total)}) local monjs = {{"Den","Denní Kč",DAYS_AVG..'denní průměr',"Celkový průměr"}} local lastavg = 0 for d = 1, 31 do if not month.values[d] then if d <= 15 then --lastavg = math.max(1, math.floor(lastavg / 2)) table.insert(monjs,{"",0,false, false}) end else lastavg=math.round(month.averages[d]) table.insert(monjs,{tostring(d),math.round(month.values[d]),lastavg, daily_avg}) end end SHORT_TERM_AVG = lastavg table.insert (vsechno, monjs) if not FFTEMPL.args.alltime then while #vsechno > 24 do table.remove(vsechno,1) table.remove(totals,2) end end end --[[ for i,mname in ipairs(mnames) do local month = assert(months[mname], "Month doesn't exist:"..mname) table.insert(result,"(h2)Měsíc "..mname..": Přijato "..math.floor(month.total).." Kč(/h2)") table.insert(result,"
") table.insert(result, chart_val(assert(month, mname))) --table.insert(result, " ") --table.insert(result, chart_avg(assert(month.averages))) table.insert(result,"
") end ]] local result = {string.format([[ ]], js_array(vsechno), js_array(totals), #vsechno)} return table.concat(result) end return charts(), (DAYS_AVG or "???"), (DAILY_AVG or "???"), (SHORT_TERM_AVG or "???")