This commit is contained in:
Tomáš Mládek 2021-02-11 01:55:15 +01:00
parent 4c02817848
commit 1c2a27a026
2 changed files with 46 additions and 2 deletions

View file

@ -3,6 +3,7 @@ import logging
import os
import re
import sqlite3
from collections import namedtuple
from dataclasses import dataclass
from datetime import datetime, timedelta
from itertools import groupby
@ -57,13 +58,15 @@ def _get_db(filepath: str):
def _fetch_leases(db, from_ts: datetime):
output = []
c = db.cursor()
query = c.execute(
'SELECT ts, mac, hostname, ip FROM spottings WHERE ts > ? ORDER BY ts DESC',
(int(from_ts.timestamp()),)
)
for row in query:
yield Lease(datetime.fromtimestamp(row[0]), row[1], row[2], row[3])
output.append(Lease(datetime.fromtimestamp(row[0]), row[1], row[2], row[3]))
return output
def _is_human(lease: Lease):
@ -160,13 +163,28 @@ def run_forever(address: str, period: int, ssid: str, output: str):
last_change = {'ts': ts, 'leases': humans_present}
break
log_entry = namedtuple('log_entry', ('ts', 'state', 'lease'))
log = []
last_seen = []
for ts, leases in groupby(reversed(_fetch_leases(db, now - timedelta(days=1))), key=attrgetter('ts')):
leases = list(leases)
for lease in leases:
if lease.mac not in (l.mac for l in last_seen):
log.append(log_entry(ts, True, lease))
for lease in last_seen:
if lease.mac not in (l.mac for l in leases):
log.append(log_entry(ts, False, lease))
last_seen = leases
log.reverse()
logging.debug(f"Outputting HTML file into {output_file}...")
with open(output_file, 'w') as file:
out_str = jinja_env.get_template("index.html").render(
now=now,
leases=registered_leases,
status=status,
last_change=last_change
last_change=last_change,
log=log
)
file.write(out_str)

View file

@ -38,6 +38,14 @@
border-color: green;
}
.log-out {
color: red;
}
.log-in {
color: green;
}
.datetime {
font-weight: bold;
margin: 1em 0;
@ -80,5 +88,23 @@
</tr>
{% endfor %}
</table>
<h2>Log</h2>
<table>
{% for entry in log %}
<tr>
<td>{{entry.ts.strftime("%c")}}</td>
<td>
{% if entry.state %}
<span class="log-in">IN</span>
{% else %}
<span class="log-out">OUT</span>
{% endif %}
</td>
<td>
{{entry.lease.display}}
</td>
</tr>
{% endfor %}
</table>
</body>
</html>