30m delay before "lower" Status is notified via webhooks

This commit is contained in:
Tomáš Mládek 2021-02-24 22:08:21 +01:00
parent 453f4fd438
commit 098eac7364
2 changed files with 23 additions and 18 deletions

View file

@ -36,7 +36,7 @@ class Lease:
@dataclass @dataclass
class Status: class Status:
open: bool level: int
description: str description: str
text: str text: str
@ -101,7 +101,7 @@ def run_forever(address: str, period: int, ssid: str, output: str, webhook_url:
autoescape=select_autoescape(['html', 'xml']) autoescape=select_autoescape(['html', 'xml'])
) )
last_status = None last_status, last_status_change = None, datetime.now()
while True: while True:
logging.info(f"Querying router at {address}...") logging.info(f"Querying router at {address}...")
@ -135,13 +135,13 @@ def run_forever(address: str, period: int, ssid: str, output: str, webhook_url:
if len(registered_leases) > 0: if len(registered_leases) > 0:
people_cnt = len([lease for lease in registered_leases if _is_human(lease)]) people_cnt = len([lease for lease in registered_leases if _is_human(lease)])
if people_cnt > 4: if people_cnt > 4:
status = Status(open=True, description='FILLED', text="There seems to be a lot of people!") status = Status(level=2, description='FILLED', text="There seems to be a lot of people!")
elif people_cnt > 0: elif people_cnt > 0:
status = Status(open=True, description='POPULATED', text="There seem to be people!") status = Status(level=1, description='POPULATED', text="There seem to be people!")
else: else:
status = Status(open=False, description='EMPTY', text="There are only computers.") status = Status(level=0, description='EMPTY', text="There are only computers.")
else: else:
status = Status(open=False, description='VOID', text="There are no devices connected?") status = Status(level=0, description='VOID', text="There are no devices connected?")
logging.debug("Logging into the database...") logging.debug("Logging into the database...")
cur = db.cursor() cur = db.cursor()
@ -150,6 +150,10 @@ def run_forever(address: str, period: int, ssid: str, output: str, webhook_url:
]) ])
db.commit() db.commit()
if not last_status or \
status.level >= last_status.level or \
datetime.now() - last_status_change > timedelta(minutes=30):
if webhook_url and last_status != status: if webhook_url and last_status != status:
requests.post(webhook_url, json={ requests.post(webhook_url, json={
"text": f"Anabasis is now <b>{status.description}</b>! ({status.text})", "text": f"Anabasis is now <b>{status.description}</b>! ({status.text})",
@ -157,6 +161,9 @@ def run_forever(address: str, period: int, ssid: str, output: str, webhook_url:
"displayName": "ANABASIS PRESENCE", "displayName": "ANABASIS PRESENCE",
}) })
last_status = status
last_status_change = datetime.now()
for output_file in output: for output_file in output:
if output_file.endswith(".csv"): if output_file.endswith(".csv"):
logging.debug(f"Outputting CSV file into {output_file}...") logging.debug(f"Outputting CSV file into {output_file}...")
@ -175,7 +182,7 @@ def run_forever(address: str, period: int, ssid: str, output: str, webhook_url:
last_change = None last_change = None
for ts, leases in groupby(_fetch_leases(db, now - timedelta(days=7)), key=attrgetter('ts')): for ts, leases in groupby(_fetch_leases(db, now - timedelta(days=7)), key=attrgetter('ts')):
humans_present = [lease for lease in leases if _is_human(lease)] humans_present = [lease for lease in leases if _is_human(lease)]
if (len(humans_present) > 0) != status.open: if (len(humans_present) > 0) != status.level > 0:
last_change = {'ts': ts, 'leases': humans_present} last_change = {'ts': ts, 'leases': humans_present}
break break
@ -240,8 +247,6 @@ def run_forever(address: str, period: int, ssid: str, output: str, webhook_url:
) )
file.write(out_str) file.write(out_str)
last_status = status
logging.info(f"Sleeping for {period} seconds.") logging.info(f"Sleeping for {period} seconds.")
sleep(period) sleep(period)

View file

@ -62,7 +62,7 @@
<h1>/|\ Anabasis Clients</h1> <h1>/|\ Anabasis Clients</h1>
<h2>STATUS</h2> <h2>STATUS</h2>
<div class="status-container"> <div class="status-container">
<div class="status status-{{'populated' if status.open else 'empty'}}"> <div class="status status-{{'populated' if status.level > 0 else 'empty'}}">
{{status.description}} {{status.description}}
</div> </div>
<div class="status-explanation">{{status.text}}</div> <div class="status-explanation">{{status.text}}</div>
@ -71,7 +71,7 @@
Since: Since:
{% if last_change %} {% if last_change %}
{{last_change['ts'].strftime("%c")}} {{last_change['ts'].strftime("%c")}}
{% if not status.open %}({{last_change.leases | map(attribute='display') | join(', ')}}){% endif %} {% if status.level == 0 %}({{last_change.leases | map(attribute='display') | join(', ')}}){% endif %}
{% else %} {% else %}
forever? forever?
{% endif %} {% endif %}