add leaderboard

This commit is contained in:
Tomáš Mládek 2021-02-11 02:06:30 +01:00
parent 1c2a27a026
commit bb0c787c9f
2 changed files with 24 additions and 1 deletions

View file

@ -177,6 +177,21 @@ def run_forever(address: str, period: int, ssid: str, output: str):
last_seen = leases
log.reverse()
leaderboard_tmp = {}
mac_to_hostname = {}
for lease in _fetch_leases(db, now - timedelta(days=120)):
if _is_human(lease):
leaderboard_tmp.setdefault(lease.mac, 0)
leaderboard_tmp[lease.mac] += 1
mac_to_hostname.setdefault(lease.mac, lease.hostname)
leaderboard_entry = namedtuple('leaderboard_entry', ('name', 'total'))
leaderboard = []
for mac, minutes in leaderboard_tmp.items():
leaderboard.append(leaderboard_entry(mac_to_hostname.get(mac) or mac, timedelta(minutes=minutes)))
leaderboard.sort(key=lambda l: l.total, reverse=True)
leaderboard = leaderboard[:10]
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(
@ -184,7 +199,8 @@ def run_forever(address: str, period: int, ssid: str, output: str):
leases=registered_leases,
status=status,
last_change=last_change,
log=log
log=log,
leaderboard=leaderboard
)
file.write(out_str)

View file

@ -88,6 +88,13 @@
</tr>
{% endfor %}
</table>
<h2>Leaderboard</h2>
<ol>
{% for entry in leaderboard %}
<li>{{entry.name}} ({{entry.total}})</li>
{% endfor %}
</ol>
<h2>Log</h2>
<table>
{% for entry in log %}