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): ...@@ -67,6 +67,29 @@ def rfc822(date):
return email.utils.format_datetime(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( def filterWarningStatus(
status_dict, status_dict,
interval, interval,
...@@ -836,7 +859,14 @@ class WebBot: ...@@ -836,7 +859,14 @@ class WebBot:
def run(self, mode): def run(self, mode):
status_dict = None 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) raise NotImplementedError("Unexpected mode: %s" % mode)
if self.config["SQLITE"] == ":memory:": if self.config["SQLITE"] == ":memory:":
...@@ -845,13 +875,21 @@ class WebBot: ...@@ -845,13 +875,21 @@ class WebBot:
mode = "wallwarning" mode = "wallwarning"
elif mode == "error": elif mode == "error":
mode = "wallerror" mode = "wallerror"
elif mode == "recent":
mode = "wallrecent"
else: else:
mode = "all" mode = "all"
self.initDB() self.initDB()
try: try:
if mode in ["crawl", "wallwarning", "wallerror", "all"]: if mode in [
"crawl",
"wallwarning",
"wallerror",
"wallrecent",
"all",
]:
self.crawl() self.crawl()
if mode in [ if mode in [
"status", "status",
...@@ -860,6 +898,8 @@ class WebBot: ...@@ -860,6 +898,8 @@ class WebBot:
"warning", "warning",
"wallerror", "wallerror",
"error", "error",
"wallrecent",
"recent",
]: ]:
status_dict = self.status() status_dict = self.status()
if mode == "pack": if mode == "pack":
...@@ -879,6 +919,11 @@ class WebBot: ...@@ -879,6 +919,11 @@ class WebBot:
float(self.config["WARNING_PERIOD"]), float(self.config["WARNING_PERIOD"]),
keep_warning=("warning" in mode), keep_warning=("warning" in mode),
) )
elif mode in ("wallrecent", "recent"):
filterRecentStatus(
status_dict,
float(self.config["WARNING_PERIOD"]),
)
if self.config["FORMAT"] == "json": if self.config["FORMAT"] == "json":
status_output = json.dumps(status_dict) status_output = json.dumps(status_dict)
else: else:
......
...@@ -29,7 +29,9 @@ from .bot import create_bot ...@@ -29,7 +29,9 @@ from .bot import create_bot
help="The bot operation mode to run.", help="The bot operation mode to run.",
show_default=True, show_default=True,
default="status", default="status",
type=click.Choice(["crawl", "pack", "status", "warning", "error"]), type=click.Choice(
["crawl", "pack", "status", "recent", "warning", "error"]
),
) )
@click.option( @click.option(
"--sqlite", "-s", help="The path of the sqlite DB. (default: :memory:)" "--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