Commit d9b026da authored by Romain Courteaud's avatar Romain Courteaud

Bot: add recent status to see latest updates

parent 55e40205
......@@ -67,6 +67,29 @@ def rfc822(date):
return email.utils.format_datetime(date)
def filterRecentStatus(status_dict, warning_period_duration):
now = datetime.datetime.utcnow()
del status_dict["bot_status"]
del status_dict["warning"]
del status_dict["missing_data"]
for status_key in list(status_dict.keys()):
for i in range(len(status_dict[status_key]) - 1, -1, -1):
status_date = parsedate_to_datetime(
status_dict[status_key][i]["date"]
)
if (warning_period_duration) < (now - status_date).total_seconds():
del status_dict[status_key][i]
status_dict[status_key].sort(
key=lambda x: parsedate_to_datetime(x["date"])
)
if not status_dict[status_key]:
del status_dict[status_key]
def filterWarningStatus(
status_dict,
interval,
......@@ -836,7 +859,14 @@ class WebBot:
def run(self, mode):
status_dict = None
if mode not in ["crawl", "pack", "status", "warning", "error"]:
if mode not in [
"crawl",
"pack",
"status",
"recent",
"warning",
"error",
]:
raise NotImplementedError("Unexpected mode: %s" % mode)
if self.config["SQLITE"] == ":memory:":
......@@ -845,13 +875,21 @@ class WebBot:
mode = "wallwarning"
elif mode == "error":
mode = "wallerror"
elif mode == "recent":
mode = "wallrecent"
else:
mode = "all"
self.initDB()
try:
if mode in ["crawl", "wallwarning", "wallerror", "all"]:
if mode in [
"crawl",
"wallwarning",
"wallerror",
"wallrecent",
"all",
]:
self.crawl()
if mode in [
"status",
......@@ -860,6 +898,8 @@ class WebBot:
"warning",
"wallerror",
"error",
"wallrecent",
"recent",
]:
status_dict = self.status()
if mode == "pack":
......@@ -879,6 +919,11 @@ class WebBot:
float(self.config["WARNING_PERIOD"]),
keep_warning=("warning" in mode),
)
elif mode in ("wallrecent", "recent"):
filterRecentStatus(
status_dict,
float(self.config["WARNING_PERIOD"]),
)
if self.config["FORMAT"] == "json":
status_output = json.dumps(status_dict)
else:
......
......@@ -29,7 +29,9 @@ from .bot import create_bot
help="The bot operation mode to run.",
show_default=True,
default="status",
type=click.Choice(["crawl", "pack", "status", "warning", "error"]),
type=click.Choice(
["crawl", "pack", "status", "recent", "warning", "error"]
),
)
@click.option(
"--sqlite", "-s", help="The path of the sqlite DB. (default: :memory:)"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment