bot-status.py: Split getting bot status from writing it
This means we can get status from other (aka Buildkite)
bots before rendering them all as one page.
The favion HTML has been moved to the end of the page
because of this. It still works as before.
Along the way use a tuple as the key to the bot status
dict. It's just as hashable as a string and we don't print
the keys anywhere so readability is not an issue.
Change-Id: I95e1e43c406ef1f81b36f7e7338131b7b769a128
diff --git a/monitor/bot-status.py b/monitor/bot-status.py
index 9ca4092..e27a7ee 100755
--- a/monitor/bot-status.py
+++ b/monitor/bot-status.py
@@ -116,40 +116,35 @@
return status
-def bot_status(config, output_file):
- temp = tempfile.NamedTemporaryFile(mode='w+', delete=False)
-
- today = "{}\n".format(datetime.today().ctime())
-
+# Get status for all bots named in the config
+# Return a dictionary of (base_url, bot name) -> status info
+def get_bots_status(config):
session = requests.Session()
-
- # Get status for all bots
bot_cache = {}
- # Whether we should show the fail favicon
- found_failure = False
for server in filter(not_ignored, config):
base_url = server['base_url']
- builder_url = server['builder_url']
- build_url = server['build_url']
logging.debug('Parsing server {}...'.format(server['name']))
for builder in server['builders']:
logging.debug(' Parsing builders {}...'.format(builder['name']))
for bot in builder['bots']:
- bot_key = "{}/{}".format(base_url, bot['name'])
+ bot_key = (base_url, bot['name'])
if bot_key in bot_cache:
continue
+
logging.debug(' Parsing bot {}...'.format(bot['name']))
- status = get_bot_status(session, bot['name'], base_url, builder_url,
- build_url)
- if not_ignored(bot):
- found_failure |= status['fail']
+ status = get_bot_status(session, bot['name'], base_url, server['builder_url'],
+ server['build_url'])
logging.debug(" FAIL" if status['fail'] else " PASS")
bot_cache[bot_key] = status
- temp.write("<link rel=\"shortcut icon\" href=\"{}\" "
- "type=\"image/x-icon\"/>\n".format(
- 'fail.ico' if found_failure else 'ok.ico'))
+ return bot_cache
+
+def write_bot_status(config, output_file, bots_status):
+ temp = tempfile.NamedTemporaryFile(mode='w+', delete=False)
+ today = "{}\n".format(datetime.today().ctime())
+ # Whether we use the fail favicon or not
+ found_failure = False
# Dump all servers / bots
for server in filter(not_ignored, config):
@@ -168,7 +163,8 @@
"<th>Build #</th><th>Commits</th><th>Failing steps</th></tr>\n")
for bot in builder['bots']:
temp.write("<tr>\n")
- status = bot_cache["{}/{}".format(base_url, bot['name'])]
+ status = bots_status[(base_url, bot['name'])]
+ found_failure |= status['fail']
url = "{}/#/{}/{}".format(base_url, builder_url, status['builderid'])
temp.write(" <td><a href='{}'>{}</a></td>\n".format(url, bot['name']))
temp.write(" <td><font color='{}'>{}</font></td>\n"
@@ -198,6 +194,10 @@
temp.write("</tr>\n")
temp.write("</table>\n")
+ temp.write("<link rel=\"shortcut icon\" href=\"{}\" "
+ "type=\"image/x-icon\"/>\n".format(
+ 'fail.ico' if found_failure else 'ok.ico'))
+
# Move temp to main (atomic change)
temp.close()
shutil.move(temp.name, output_file)
@@ -222,4 +222,4 @@
print("error: failed to read {} config file: {}".format(args.config_file, e))
sys.exit(os.EX_CONFIG)
- bot_status(config, args.output_file)
+ write_bot_status(config, args.output_file, get_bots_status(config))