-
-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,288 additions
and
383 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
local utf8 = require 'utf8' | ||
return { | ||
makeSummaryTable = function(tbl, sum_key, keys) | ||
local result = {} | ||
local ind = {} | ||
local obj_count = {} | ||
local obj_rem = {} | ||
for _, row in ipairs(tbl) do | ||
group = row[sum_key] | ||
if not ind[group] then | ||
ind[group] = {} | ||
obj_count[group] = {} | ||
obj_rem[group] = {} | ||
end | ||
for _, key in ipairs(keys) do | ||
local val = row[key] | ||
-- if it is not number, just count | ||
if type(val) ~= "number" then | ||
if not obj_count[group][row[key]] then | ||
obj_count[group][row[key]] = 1 | ||
obj_rem[group][key] = val | ||
val = 1 | ||
else | ||
val = 0 | ||
end | ||
end | ||
if not ind[group][key] then | ||
ind[group][key] = val | ||
else | ||
ind[group][key] = ind[group][key] + val | ||
end | ||
end | ||
end | ||
for k,v in pairs(ind) do | ||
v[sum_key] = k | ||
for k1, v1 in pairs(v) do | ||
if v1 == 1 then | ||
v[k1] = obj_rem[k][k1] | ||
end | ||
end | ||
table.insert(result, v) | ||
end | ||
return result | ||
end, | ||
printDataTable = function(tbl, onlykeys, totals) | ||
local sep = "|" | ||
local function spaces(n) | ||
return string.rep(" ", n) | ||
end | ||
if #tbl == 0 then | ||
print "<empty table>" | ||
return | ||
end | ||
local keys = {} | ||
local tostrs = {} | ||
if onlykeys then | ||
for _, v in ipairs(onlykeys) do | ||
table.insert(keys, v[1]) | ||
tostrs[v[1]] = v[2] or tostring | ||
end | ||
else | ||
for k,_ in pairs(tbl[1]) do | ||
table.insert(keys, k) | ||
tostrs[k] = tostring | ||
end | ||
end | ||
|
||
-- uniquie key to store id | ||
-- just sum all the given keys | ||
local uniq_id = "" | ||
for _, key in ipairs(keys) do | ||
uniq_id = uniq_id .. key .. "+" | ||
end | ||
-- adding total's column | ||
table.insert(keys, 1, uniq_id) | ||
tostrs[uniq_id] = tostring | ||
for i,row in ipairs(tbl) do | ||
row[uniq_id] = i | ||
end | ||
|
||
local function avr_counter() | ||
local sum = 0 | ||
local count = 1 | ||
return function(a, b) | ||
if count == 1 then sum = a end | ||
count = count + 1 | ||
sum = sum + b | ||
return sum / count | ||
end | ||
end | ||
|
||
average = avr_counter() | ||
|
||
local total_funcs = { | ||
{"sum", function(a, b) | ||
return a + b | ||
end | ||
}, | ||
{"max", math.max}, | ||
{"avr", average}} | ||
|
||
-- calculate totals | ||
local accums = {} | ||
local akk_count = 0 | ||
local akk_tbl = {} | ||
if totals then | ||
for _, total_func in ipairs(total_funcs) do | ||
local fn_name = total_func[1] | ||
local fn = total_func[2] | ||
if totals[fn_name] then | ||
accums[fn] = {} | ||
for _, key in ipairs(totals[fn_name]) do | ||
accums[fn][key] = tbl[1][key] --initial value is first item in row | ||
end | ||
end | ||
end | ||
-- accumulating total values | ||
for i = 2, #tbl do | ||
for fn, fkeys in pairs(accums) do | ||
for key, _ in pairs(fkeys) do | ||
accums[fn][key] = fn(accums[fn][key], tbl[i][key]) | ||
end | ||
end | ||
end | ||
-- adding totals to general table | ||
-- order must be observed | ||
for _, total_func in ipairs(total_funcs) do | ||
local fn_name = total_func[1] | ||
local fn = total_func[2] | ||
if accums[fn] then | ||
accums[fn][uniq_id] = fn_name | ||
table.insert(akk_tbl, accums[fn]) | ||
akk_count = akk_count + 1 | ||
end | ||
end | ||
end | ||
|
||
local texttbl = {} | ||
-- first row - keys | ||
texttbl[1] = {} | ||
for _,k in ipairs(keys) do | ||
if k == uniq_id then | ||
texttbl[1][k] = "#" | ||
else | ||
texttbl[1][k] = k | ||
end | ||
end | ||
|
||
-- convert to text | ||
for _, tabl in ipairs({tbl, akk_tbl}) do | ||
for i, _ in ipairs(tabl) do | ||
local row = {} | ||
for _, key in ipairs(keys) do | ||
if tabl[i][key] then | ||
row[key] = tostrs[key](tabl[i][key]) | ||
else | ||
row[key] = "-" | ||
end | ||
end | ||
table.insert(texttbl, row) | ||
end | ||
end | ||
|
||
-- calculate widths | ||
local widths = {} | ||
for _,k in ipairs(keys) do | ||
widths[k] = 0 | ||
end | ||
for _, row in ipairs(texttbl) do | ||
for _,k in ipairs(keys) do | ||
widths[k] = math.max(utf8.len(row[k]), widths[k]) | ||
end | ||
end | ||
-- create underline | ||
local total_width = string.len(sep) | ||
for _,v in pairs(widths) do | ||
total_width = total_width + v + 2 + string.len(sep) | ||
end | ||
local underline = string.rep("-", total_width) | ||
|
||
-- update strings | ||
for _, row in ipairs(texttbl) do | ||
for _,k in ipairs(keys) do | ||
row[k] = row[k] .. spaces(widths[k] - utf8.len(row[k])) | ||
end | ||
end | ||
|
||
function print_line(i) | ||
local line = sep | ||
for _,k in ipairs(keys) do | ||
line = line .. " " .. texttbl[i][k] .. " " .. sep | ||
end | ||
print(line) | ||
end | ||
-- print strings | ||
print(underline) | ||
print_line(1) | ||
print(underline) | ||
for i = 2,#texttbl - akk_count do | ||
print_line(i) | ||
end | ||
print(underline) | ||
if akk_count > 0 then | ||
for i = #texttbl - akk_count + 1, #texttbl do | ||
print_line(i) | ||
end | ||
print(underline) | ||
end | ||
end | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.