Browse Source

Move earwigbot.mako's code to its own module.

pull/24/head
Ben Kurtovic 11 years ago
parent
commit
eae1ec0a0b
2 changed files with 46 additions and 44 deletions
  1. +2
    -44
      pages/earwigbot.mako
  2. +44
    -0
      toolserver/earwigbot.py

+ 2
- 44
pages/earwigbot.mako View File

@@ -1,46 +1,5 @@
<%!
from datetime import datetime
import os
import re
from shlex import split
from subprocess import check_output, CalledProcessError, STDOUT

os.environ["SGE_ROOT"] = "/sge/GE"

def format_date(d):
start = datetime.strptime(d, "%a %b %d %H:%M:%S %Y")
since = start.strftime("%b %d, %Y %H:%M:%S UTC")
diff = (datetime.utcnow() - start)
if diff.days:
uptime = "{0} days".format(diff.days)
if diff.seconds >= 3600:
uptime += ", {0} hours".format(diff.seconds / 3600)
else:
if diff.seconds > 3600:
uptime = "{0} hours".format(diff.seconds / 3600)
elif diff.seconds > 60:
uptime = "{0} minutes".format(diff.seconds / 60)
else:
uptime = "{0} seconds".format(diff.seconds)
return (since, uptime)

def collect_status_info():
try:
result = check_output(["qstat", "-j", "earwigbot"], stderr=STDOUT)
except CalledProcessError as e:
return ["offline", None, None, None]

if result.startswith("Following jobs do not exist:"):
return ["offline", None, None, None]

lines = result.splitlines()[1:]
re_key = "^(.*?):\s"
re_val = ":\s*(.*?)$"
data = dict((re.search(re_key, line).group(1), re.search(re_val, line).group(1)) for line in lines if re.search(re_key, line) and re.search(re_val, line))
since, uptime = format_date(data["submission_time"])
host = data["sge_o_host"]
return ["online", since, uptime, host]
%>\
<%include file="/support/header.mako" args="environ=environ, cookies=cookies, title='EarwigBot Status'"/>\
<%namespace module="toolserver.earwigbot" import="collect_status_info"/>\
<%def name="get_status()" filter="trim">
<% status, since, uptime, host = collect_status_info() %>
${"has been" if status == "online" else "is"} <span class="${status}">${status}</span>
@@ -48,7 +7,6 @@
since ${since} (${uptime} uptime) on <tt>${host}</tt>
% endif
</%def>\
<%include file="/support/header.mako" args="environ=environ, cookies=cookies, title='EarwigBot Status'"/>
<h1>EarwigBot Status</h1>
<p>EarwigBot ${get_status()}.</p>
<p>Additional information: <a href="http://status.toolserver.org/">status.toolserver.org</a></p>


+ 44
- 0
toolserver/earwigbot.py View File

@@ -0,0 +1,44 @@
from datetime import datetime
import os
import re
from shlex import split
from subprocess import check_output, CalledProcessError, STDOUT

os.environ["SGE_ROOT"] = "/sge/GE"

def collect_status_info(context):
try:
result = str(check_output(split("qstat -j earwigbot"), stderr=STDOUT))
except CalledProcessError:
return ["offline", None, None, None]

if result.startswith("Following jobs do not exist:"):
return ["offline", None, None, None]

lines = result.splitlines()[1:]
data = {}
for line in lines:
re_key = re.match(r"(.*?):\s", line)
re_val = re.search(r":\s*(.*?)$", line)
if re_key and re_val:
data[re_key.group(1)] = re_val
since, uptime = _format_date(data["submission_time"])
host = data["sge_o_host"]
return ["online", since, uptime, host]

def _format_date(time):
start = datetime.strptime(time, "%a %b %d %H:%M:%S %Y")
since = start.strftime("%b %d, %Y %H:%M:%S UTC")
diff = (datetime.utcnow() - start)
if diff.days:
uptime = "{0} days".format(diff.days)
if diff.seconds >= 3600:
uptime += ", {0} hours".format(diff.seconds / 3600)
else:
if diff.seconds > 3600:
uptime = "{0} hours".format(diff.seconds / 3600)
elif diff.seconds > 60:
uptime = "{0} minutes".format(diff.seconds / 60)
else:
uptime = "{0} seconds".format(diff.seconds)
return (since, uptime)

Loading…
Cancel
Save