aboutsummaryrefslogtreecommitdiffstats
path: root/generate-html.lua
blob: 25d4ddd4b831168ca2532073f389402aeadc528c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/lua5.3

local json = require("cjson")
local inspect = require("inspect")
local lustache = require("lustache")
local utils = require("utils")
local conf = require("config")

function get_branches(indexes)
	local res = {}
	for k,v in ipairs(indexes.master.branch) do
		table.insert(res, v.name)
	end
	return res
end

----
-- need to create one flipped table with both mirros and master
function flip_branches(branches)
	local res = {}
	for _,b in ipairs(branches) do
		for _,r in ipairs(b.repo) do
			for _,a in ipairs(r.arch) do
				if type(res[b.name]) == "nil" then res[b.name] = {} end
				if type(res[b.name][r.name]) == "nil" then
					res[b.name][r.name] = {}
				end
				res[b.name][r.name][a.name] = a
			end
		end
	end
	return res
end

----
-- convert table to array (values to keys)
function get_repo_arch(indexes)
	local t = {}
	for _,b in ipairs(indexes.master.branch) do
		for _,r in ipairs(b.repo) do
			for _,a in ipairs(r.arch) do
				if type(t[r.name]) == "nil" then t[r.name] = {} end
				t[r.name][a.name] = 1
			end
		end
	end
	local res = {}
	for repo in utils.kpairs(t, utils.sort_repo) do
		for arch in utils.kpairs(t[repo], utils.sort_arch) do
			table.insert(res, repo.."/"..arch)
		end
	end
	return res
end

----
-- convert table to array (values to keys) for each mirror
function flip_mirrors(mirrors)
	local res = {}
	for _,m in ipairs(mirrors) do
		res[m.url] = flip_branches(m.branch)
	end
	return res
end

----
-- format timestamp difference
function format_age(ts)
	local res = {}
	if ts < 3600 then
		res.text = "OK"
		res.class = "status-ok"
	elseif ts < 86400 then
		res.text = ("%dh"):format(math.ceil(ts/3600))
		res.class = "status-warn"
	elseif ts > 86400 then
		res.text = ("%dd"):format(math.ceil(ts/86400))
		res.class = "status-error"
	end
	return res
end

----
-- format status based on http status message
function format_status(status, age)
	local res = {}
	if status ~= "failed" then
		if status == "200" then
			res = format_age(age)
		elseif status == "404" then
			res.class = "status-na"
			res.text = "N/A"
		else
			res.class = "status-unk"
			res.text = status
		end
	else
		res.class = "status-na"
		res.text = "N/A"
	end
	return res
end

----
-- get status and format it based on http status and modified time
function get_status(fm, fb, mirror, branch, repo, arch)
	local res = { text = "N/A", class = "status-na" }
	if type(fm[mirror]) == "table" and
		type(fm[mirror][branch]) == "table" and
		type(fm[mirror][branch][repo]) == "table" and
		type(fm[mirror][branch][repo][arch]) == "table" then
		if type(fm[mirror][branch][repo][arch]) == "table" then
			local status = fm[mirror][branch][repo][arch]
			local age
			if type(status.modified) == "number" then
				age = fb[branch][repo][arch].modified - status.modified
			end
			res = format_status(status.status, age)
		end
	end
	return res
end

----
-- build the html table
function build_tables(indexes)
	local res = {}
	local fm = flip_mirrors(indexes.mirrors)
	local fb = flip_branches(indexes.master.branch)
	for idx,mirror in ipairs(indexes.mirrors) do
		local rows = {}
		for _,ra in ipairs(get_repo_arch(indexes)) do
			local repo, arch = ra:match("(.*)/(.*)")
			local row = {}
			table.insert(row, {text = ra})
			for _,branch in ipairs(get_branches(indexes)) do
				local status = get_status(fm, fb, mirror.url, branch, repo, arch)
				table.insert(row, status)
			end
			table.insert(rows, { row = row })
		end
		res[idx] = { 
			url = mirror.url, tbody = rows, duration = mirror.duration, 
			count = mirror.count
		}
	end
	return res
end

local out_json = ("%s/%s"):format(conf.outdir, conf.mirrors_json)
local indexes = json.decode(utils.read_file(out_json))
local thead = get_branches(indexes)
table.insert(thead, 1, "branch/release")
local view = { lupdate = os.date("%c", indexes.date), mirrors = build_tables(indexes), thead = thead }
local tpl = utils.read_file("index.tpl")
local out_html = ("%s/%s"):format(conf.outdir, conf.mirrors_html)
utils.write_file(out_html, lustache:render(tpl, view))