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
class Status:
open: bool
level: int
description: 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'])
)
last_status = None
last_status, last_status_change = None, datetime.now()
while True:
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:
people_cnt = len([lease for lease in registered_leases if _is_human(lease)])
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:
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:
status = Status(open=False, description='EMPTY', text="There are only computers.")
status = Status(level=0, description='EMPTY', text="There are only computers.")
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...")
cur = db.cursor()
@ -150,12 +150,19 @@ def run_forever(address: str, period: int, ssid: str, output: str, webhook_url:
])
db.commit()
if webhook_url and last_status != status:
requests.post(webhook_url, json={
"text": f"Anabasis is now <b>{status.description}</b>! ({status.text})",
"format": "html",
"displayName": "ANABASIS PRESENCE",
})
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:
requests.post(webhook_url, json={
"text": f"Anabasis is now <b>{status.description}</b>! ({status.text})",
"format": "html",
"displayName": "ANABASIS PRESENCE",
})
last_status = status
last_status_change = datetime.now()
for output_file in output:
if output_file.endswith(".csv"):
@ -175,7 +182,7 @@ def run_forever(address: str, period: int, ssid: str, output: str, webhook_url:
last_change = None
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)]
if (len(humans_present) > 0) != status.open:
if (len(humans_present) > 0) != status.level > 0:
last_change = {'ts': ts, 'leases': humans_present}
break
@ -240,8 +247,6 @@ def run_forever(address: str, period: int, ssid: str, output: str, webhook_url:
)
file.write(out_str)
last_status = status
logging.info(f"Sleeping for {period} seconds.")
sleep(period)

View file

@ -62,7 +62,7 @@
<h1>/|\ Anabasis Clients</h1>
<h2>STATUS</h2>
<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}}
</div>
<div class="status-explanation">{{status.text}}</div>
@ -71,7 +71,7 @@
Since:
{% if last_change %}
{{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 %}
forever?
{% endif %}
@ -93,4 +93,4 @@
{% endfor %}
</table>
</body>
</html>
</html>