feat(audit): every row keeps its date; dividers for Today, Yesterday, Last 7 days, Older (#5214)
The operator found "Today 08:03" in place of a date odd on the audit log,
and asked for the recency groups as dividers instead. |when now always
writes the date ("Thu 8 Oct 08:03"); mark_day_groups gives each entry its
group in the reader's zone, and a divider row opens each group. A scrolled
page carries the group the last one ended in, so it is not opened twice.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -49,11 +49,11 @@ async def test_the_page_names_and_links_the_entity(app):
|
||||
async def test_the_log_continues_itself_instead_of_a_load_more_link(app):
|
||||
events = [{"event": _ev("host", "h1", "host.renamed"), "detail": {}, "entity": None}]
|
||||
async with app.test_request_context("/audit/"):
|
||||
more = await render_template("audit/list.html", events=events,
|
||||
more = await render_template("audit/list.html", events=events, last_group="Today",
|
||||
after="2026-10-07T03:00:00+00:00|0190abc", action_filter="host.")
|
||||
last = await render_template("audit/_rows.html", events=events, after=None, action_filter="")
|
||||
assert ('data-scroll-more hx-get="/audit/?before=2026-10-07T03%3A00%3A00%2B00%3A00%7C0190abc'
|
||||
'&action=host."') in more
|
||||
'&action=host.&group=Today"') in more
|
||||
assert "Load more" not in more
|
||||
assert "data-scroll-more" not in last and "End of the list." in last
|
||||
|
||||
@@ -107,3 +107,30 @@ async def test_each_entry_shows_its_exact_moment_in_the_readers_zone(app):
|
||||
assert '<div><span class="tone-dim">was</span> web-0</div>' in page
|
||||
table = page[page.index('<table class="table">'):]
|
||||
assert "style=" not in table
|
||||
|
||||
|
||||
def test_dividers_open_each_day_group_once_even_across_pages():
|
||||
"""#5214: the operator wants Today, Yesterday, the last week and older as
|
||||
dividers, with every row's own date beside it."""
|
||||
from steward.audit.service import mark_day_groups
|
||||
now = datetime(2026, 10, 8, 18, 0, tzinfo=timezone.utc)
|
||||
at = lambda d, h=12: NS(timestamp=datetime(2026, 10, d, h, 0, tzinfo=timezone.utc))
|
||||
page1 = [{"event": at(8)}, {"event": at(8, 9)}, {"event": at(7)}, {"event": at(5)}]
|
||||
assert mark_day_groups(page1, timezone.utc, "", now) == "Last 7 days"
|
||||
assert [(i["group"], i["opens_group"]) for i in page1] == [
|
||||
("Today", True), ("Today", False), ("Yesterday", True), ("Last 7 days", True)]
|
||||
# The next page carries the group the last ended in, so it doesn't reopen it.
|
||||
page2 = [{"event": at(3)}, {"event": at(1)}]
|
||||
assert mark_day_groups(page2, timezone.utc, "Last 7 days", now) == "Older"
|
||||
assert [(i["group"], i["opens_group"]) for i in page2] == [("Last 7 days", False), ("Older", True)]
|
||||
|
||||
|
||||
async def test_a_divider_row_heads_each_group(app):
|
||||
events = [{"event": _ev("host", "h1", "host.renamed"), "detail": {}, "entity": None, "facts": [],
|
||||
"group": "Today", "opens_group": True},
|
||||
{"event": _ev("host", "h1", "host.renamed"), "detail": {}, "entity": None, "facts": [],
|
||||
"group": "Today", "opens_group": False}]
|
||||
async with app.test_request_context("/audit/"):
|
||||
page = await render_template("audit/_rows.html", events=events, after=None, action_filter="")
|
||||
assert page.count('<tr class="table-divider"><th colspan="5" scope="rowgroup">Today</th></tr>') == 1
|
||||
assert "Today 1" not in page and ">Tue 6 Oct " in page # the row keeps its date
|
||||
|
||||
@@ -57,7 +57,8 @@ def test_ago_of_nothing_and_of_another_zone():
|
||||
assert 'title="Tue 6 Oct 2026 18:00:00 UTC"' in str(f.ago(NOW.astimezone(est), now=NOW))
|
||||
|
||||
|
||||
# Milestone 490: a moment in the reader's own zone, on a 24-hour clock.
|
||||
# Milestone 490: a moment in the reader's own zone, on a 24-hour clock, always
|
||||
# with its date: "Today" and "Yesterday" are dividers, never the time (#5214).
|
||||
NY = f.zone("America/New_York")
|
||||
|
||||
|
||||
@@ -67,14 +68,14 @@ def test_ago_hovers_the_moment_in_the_readers_zone():
|
||||
|
||||
|
||||
@pytest.mark.parametrize("moment, text, title", [
|
||||
(NOW - timedelta(hours=1), "Today 13:00", "Tue 6 Oct 2026 13:00:00 EDT"),
|
||||
(NOW - timedelta(hours=1), "Tue 6 Oct 13:00", "Tue 6 Oct 2026 13:00:00 EDT"),
|
||||
# 03:30 UTC on the 6th is 23:30 on the 5th in New York: the zone moves the day.
|
||||
(datetime(2026, 10, 6, 3, 30, tzinfo=timezone.utc), "Yesterday 23:30", "Mon 5 Oct 2026 23:30:00 EDT"),
|
||||
(datetime(2026, 10, 7, 6, 0, tzinfo=timezone.utc), "Tomorrow 02:00", "Wed 7 Oct 2026 02:00:00 EDT"),
|
||||
(datetime(2026, 10, 6, 3, 30, tzinfo=timezone.utc), "Mon 5 Oct 23:30", "Mon 5 Oct 2026 23:30:00 EDT"),
|
||||
(datetime(2026, 10, 7, 6, 0, tzinfo=timezone.utc), "Wed 7 Oct 02:00", "Wed 7 Oct 2026 02:00:00 EDT"),
|
||||
(datetime(2026, 9, 30, 12, 0, tzinfo=timezone.utc), "Wed 30 Sep 08:00", "Wed 30 Sep 2026 08:00:00 EDT"),
|
||||
# Before this year the year shows; in winter the zone is EST, an hour further back.
|
||||
(datetime(2025, 12, 25, 12, 0, tzinfo=timezone.utc), "25 Dec 2025 07:00", "Thu 25 Dec 2025 07:00:00 EST"),
|
||||
(datetime(2026, 10, 6, 21, 5), "Today 17:05", "Tue 6 Oct 2026 17:05:00 EDT"), # naive is UTC
|
||||
(datetime(2026, 10, 6, 21, 5), "Tue 6 Oct 17:05", "Tue 6 Oct 2026 17:05:00 EDT"), # naive is UTC
|
||||
])
|
||||
def test_when_writes_the_exact_moment_in_the_readers_zone(moment, text, title):
|
||||
stamp = moment.replace(tzinfo=timezone.utc) if moment.tzinfo is None else moment
|
||||
@@ -83,11 +84,24 @@ def test_when_writes_the_exact_moment_in_the_readers_zone(moment, text, title):
|
||||
|
||||
|
||||
def test_when_is_a_24_hour_clock_and_utc_without_a_zone():
|
||||
assert ">Today 18:00<" in str(f.when(NOW, now=NOW))
|
||||
assert ">Today 23:59<" in str(f.when(NOW.replace(hour=23, minute=59), now=NOW))
|
||||
assert ">Tue 6 Oct 18:00<" in str(f.when(NOW, now=NOW))
|
||||
assert ">Tue 6 Oct 23:59<" in str(f.when(NOW.replace(hour=23, minute=59), now=NOW))
|
||||
assert str(f.when(None)) == "—"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("moment, group", [
|
||||
(NOW - timedelta(hours=1), "Today"),
|
||||
(datetime(2026, 10, 6, 3, 30, tzinfo=timezone.utc), "Yesterday"), # 23:30 on the 5th in New York
|
||||
(datetime(2026, 10, 5, 3, 30, tzinfo=timezone.utc), "Last 7 days"),
|
||||
(datetime(2026, 9, 30, 12, 0, tzinfo=timezone.utc), "Last 7 days"), # six days back
|
||||
(datetime(2026, 9, 29, 12, 0, tzinfo=timezone.utc), "Older"), # seven
|
||||
(datetime(2025, 12, 25, 12, 0, tzinfo=timezone.utc), "Older"),
|
||||
])
|
||||
def test_day_group_counts_calendar_days_in_the_readers_zone(moment, group):
|
||||
assert f.day_group(moment, NY, now=NOW) == group
|
||||
assert group in f.DAY_GROUPS
|
||||
|
||||
|
||||
def test_zone_falls_back_to_utc_and_names_itself():
|
||||
assert f.zone(None) is timezone.utc and f.zone("") is timezone.utc
|
||||
assert f.zone("Not/AZone") is timezone.utc and f.zone("../etc/passwd") is timezone.utc
|
||||
|
||||
Reference in New Issue
Block a user