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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
# Copyright (C) 2012 Codethink Limited
#
import os
import time
from cgi import escape
state_names = [
"Initialisation",
"Load Troves",
"Remove old repos",
"Create new repos",
"Process Lorries",
"Finished"
]
def format_time(time_t):
return time.strftime("%Y-%m-%d %H:%M:%S UTC", time.gmtime(time_t))
class HTMLStatusManager(object):
'''Manage the HTML status page for lorry-controller.'''
def __init__(self, app):
self.app = app
self.state = 0
self.series = None
self.filename = self.app.settings['html-file']
self.mgr = None
self.all_processing = set()
self.processing = None
self.processing_time = time.time()
self.failing = None
self.all_lorries_ever = set()
self.bump_time = time.time()
def set_failing(self, failmsg):
self.failing = failmsg
self.write_out_status()
def set_mgr(self, mgr):
self.mgr = mgr
def set_processing(self, proc):
if self.processing is not None:
self.all_processing.add(self.processing)
self.processing = proc
self.processing_time = time.time()
self.write_out_status()
def bump_state(self):
self.state = self.state + 1
self.bump_time = time.time()
self.processing = None
self.write_out_status()
def write_out_status(self):
if self.filename is None: return
try:
with open(self.filename + ".new", "w") as ofh:
ofh.write("<!DOCTYPE html>\n")
ofh.write(self.gen_html())
ofh.write("\n")
target = self.filename
if self.series is not None:
target += ".%d" % self.series
self.series += 1
os.rename(self.filename + ".new", target)
except:
os.unlink(self.filename + ".new")
raise
def gen_html(self):
head = self.gen_head()
body = self.gen_body()
return self.tag("html", content=head+"\n"+body, gap=True)
def gen_head(self):
title = self.tag("title", content="Lorry Controller")
css = self.tag("link", href="trove.css", rel="stylesheet",
type="text/css")
script = self.tag("script", type="text/JavaScript", content='''
<!--
function reloadAfter(timeout) {
setTimeout("location.reload(true);", timeout);
}
// -->
''') + self.tag("script", type="text/javascript", src="/table.js", content="")
return self.tag("head", content=title+css+script)
def gen_body(self):
# 1. Rough header, branded as trove
curtime = format_time(time.time())
link = "/"
if self.series is not None:
link = self.filename + ".%d" % (self.series + 1)
header = '''
<table id='header'><tr><td class='logo' rowspan='2'>
<a href='%s'><img src='trove.png' alt='trove logo'/></a></td>
<td class='main'>Status of Lorry Controller</td></tr>
<tr><td class='sub'>Updated at %s</td></tr></table>
''' % (link, curtime)
# 2. List of steps and where we are currently
steps = self.gen_steps()
# 4. Main content
content = self.gen_content()
# 5. footer
footer = self.gen_footer()
# magic args
kwargs = {
"onload": "JavaScript:reloadAfter(5000);",
}
if self.state >= (len(state_names)-1):
kwargs["onload"] = "JavaScript:reloadAfter(10000);",
return self.tag("body", content=self.tag(
"div", content=(header+steps+content+footer),
Class="lorrycontroller"), **kwargs)
def gen_content(self):
if self.failing is not None:
return self.tag("div", Class="failure", content=self.failing)
# 1. Troves known
troves = self.gen_troves()
# 2. Lorries known
lorries = self.gen_lorries()
return self.tag("div", Class="content", content=
self.tag("div", id="troves", content=troves) +
self.tag("div", id="lorries", content=lorries))
def gen_troves(self):
troves = []
now = time.time()
for trove in self.app.conf.troves:
troveinfo = {}
if self.mgr is not None:
troveinfo = self.mgr.trove_state.get(trove['uuid'], {})
uuid = self.tag("td", content=escape(trove['uuid']))
state = "Up to date"
if self.processing == trove['uuid']:
state = "Processing since " + \
format_time(self.processing_time)
elif troveinfo.get('next-vls', now - 1) < now:
if self.state < len(state_names) - 1:
state = "Due to be checked this run."
else:
state = "Due to be checked on the next run"
state = self.tag("td", content=escape(state))
nextdue = self.tag("td", content=escape(format_time(
troveinfo.get('next-vls', now - 1))))
lorrycount = len([l for l in self.app.conf.lorries.itervalues()
if l['controller-uuid'] == trove['uuid']])
lorrycount = self.tag("td", content=str(lorrycount))
troves.append(self.tag("tr", content=
uuid+state+nextdue+lorrycount))
if len(troves) == 0:
content = "No troves detected"
else:
header = self.tag("tr", content=
self.tag("th", content="Trove UUID") +
self.tag("th", content="Status") +
self.tag("th", content="Next due") +
self.tag("th", content="Lorries created"))
content = self.tag("table", content=
header + "".join(troves))
return content
def gen_lorries(self):
lorries = []
now = time.time()
all_lorry_names = set(self.app.conf.lorries.keys())
if self.mgr is not None:
all_lorry_names.update(set(self.mgr.lorry_state.keys()))
self.all_lorries_ever.update(all_lorry_names)
all_lorry_names = list(self.all_lorries_ever)
all_lorry_names.sort()
for lorry_name in all_lorry_names:
lorry = self.app.conf.lorries.get(lorry_name, None)
dead_lorry = False
dead_and_gone = False
if lorry is None:
lorrystate = self.mgr.lorry_state.get(lorry_name, None)
if lorrystate is None:
dead_and_gone = True
lorry = {}
else:
lorry = lorrystate['lorry']
dead_lorry = True
lorryinfo = {}
if self.mgr is not None:
lorryinfo = self.mgr.lorry_state.get(lorry_name, {})
uuid = self.tag("td", content=
escape(lorry.get('controller-uuid', 'Dead')))
state = "Waiting "
if self.processing == lorry_name:
state = "Processing since " + \
format_time(self.processing_time)
elif lorryinfo.get('next-due', now - 1) < self.bump_time:
if self.state < len(state_names) - 1:
state = "Due to be checked this run."
else:
state = "Due to be checked on the next run"
if self.mgr is not None:
if self.mgr.lorry_state.get(lorry_name, None) is None:
state = "Needs creating"
elif lorry_name in self.all_processing:
state = "Processed"
if dead_lorry:
state = "Dead - To be removed"
if dead_and_gone:
state = "Dead"
if self.processing == lorry_name:
state = "Removing since " + \
format_time(self.processing_time)
state = self.tag("td", content=escape(state))
lastresult = self.tag("td", content=self.tag(
"pre", content=escape(lorryinfo.get('result', '-'))))
nextdue = self.tag("td", content=escape(format_time(
lorryinfo.get('next-due', now - 1))))
lorryname = self.tag("td", content=escape(lorry_name))
lorries.append(self.tag("tr", content=
lorryname+uuid+state+lastresult+nextdue))
if len(lorries) == 0:
content = "No lorries detected yet"
else:
header = self.tag("tr", content=
self.tag("th",
Class="table-sortable:alphanumeric",
content="Lorry Name") +
self.tag("th",
Class="table-sortable:alphanumeric",
content="Comes From") +
self.tag("th",
Class="table-sortable:alphanumeric",
content="Status") +
self.tag("th",
Class="table-sortable:alphanumeric",
content="Last result") +
self.tag("th",
Class="table-sortable:alphanumeric",
content="Next due"))
header = self.tag("thead", content=header)
content = self.tag("table", Class="table-autosort:4", content=
header + "\n" + "\n".join(lorries))
return content
def gen_footer(self):
curtime = format_time(time.time())
return self.tag("div", Class="footer", content=
"Generated by Lorry Controller at " + curtime)
def gen_steps(self):
steps = []
for idx in xrange(len(state_names)):
if idx < self.state:
Class = "donestep"
elif idx == self.state:
Class = "activestep"
else:
Class = "pendingstep"
steps.append(self.tag("span", Class=Class,
content=state_names[idx]))
return self.tag("table", Class="steps", content=
self.tag("tr", content=
self.tag("td", content=
"".join(steps))))
def tag(self, tagname, content=None, gap=False, **kwargs):
tagval = " ".join([tagname] +
["%s=%r" % (k.lower(), v) for k, v in kwargs.iteritems()])
gap = "\n" if gap else ""
if content is None:
return "<%s />" % tagval
else:
return "<%s>%s%s%s</%s>" % (tagval, gap, content, gap, tagname)
|