add naturalized datetimes

This commit is contained in:
Tomáš Mládek 2022-01-17 16:39:22 +01:00
parent 9ec739a9b4
commit 74420f8f34
2 changed files with 41 additions and 9 deletions

View file

@ -229,15 +229,19 @@ def run_forever(
writer.writerow((lease.ip, lease.mac, lease.hostname or "???"))
elif output_file.endswith(".html"):
last_change = None
last_change_natural = None
for ts, leases in groupby(
_fetch_leases(db, now - timedelta(days=7)), key=attrgetter("ts")
):
humans_present = [lease for lease in leases if lease.is_human]
if (len(humans_present) > 0) != (status.level > 0):
last_change = {"ts": ts, "leases": humans_present}
last_change_natural = humanize.naturaltime(datetime.now() - ts)
break
log_entry = namedtuple("log_entry", ("ts", "state", "lease"))
log_entry = namedtuple(
"log_entry", ("ts", "ts_natural", "state", "lease")
)
log = []
last_seen = []
for ts, leases in groupby(
@ -247,10 +251,24 @@ def run_forever(
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))
log.append(
log_entry(
ts,
humanize.naturaltime(datetime.now() - 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))
log.append(
log_entry(
ts,
humanize.naturaltime(datetime.now() - ts),
False,
lease,
)
)
last_seen = leases
collapse_thresh = timedelta(minutes=10)
@ -301,6 +319,7 @@ def run_forever(
leases=registered_leases,
status=status,
last_change=last_change,
last_change_natural=last_change_natural,
log=log,
leaderboard=leaderboard,
internal=False,
@ -314,6 +333,7 @@ def run_forever(
leases=registered_leases,
status=status,
last_change=last_change,
last_change_natural=last_change_natural,
log=log,
leaderboard=leaderboard,
internal=True,

View file

@ -58,7 +58,7 @@
}
.nonhuman {
opacity: .66;
opacity: 0.66;
}
.log {
@ -73,6 +73,10 @@
color: green;
}
.ago {
opacity: 0.66;
}
.datetime {
font-weight: 600;
margin: 1em 0;
@ -103,10 +107,15 @@
<div class="status-explanation">{{status.text}}</div>
<div class="level">{{status.level}}</div>
<div>
<strong>Since:</strong> {% if last_change %}
{{last_change['ts'].strftime("%c")}} {% if status.level == 0
%}({{last_change.leases | map(attribute='display') | join(', ')}}){%
endif %} {% else %} as far as I can tell? {% endif %}
<strong>Since:</strong>
{% if last_change %}
{{ last_change['ts'].strftime("%c") }} - {{ last_change_natural}}
{% if status.level == 0 %}
({{last_change.leases | map(attribute='display') | join(', ')}})
{% endif %}
{% else %}
as far as I can tell?
{% endif %}
</div>
</div>
<h2>Current clients</h2>
@ -130,7 +139,10 @@
<table class="log">
{% for entry in log %}
<tr>
<td>{{entry.ts.strftime("%c")}}</td>
<td>
{{entry.ts.strftime("%c")}}
<span class="ago">({{entry.ts_natural}})</span>
</td>
<td>
{% if entry.state %}
<span class="log-in">IN</span>