Skip to content

Commit

Permalink
feat: serve logs and events from the newest to oldest, but keep the
Browse files Browse the repository at this point in the history
ordering

So basically initial implementation, with limit=1, would emit the oldest
record in series. New implementation will return the most recent one
instead

The response is still sorted by ascension
  • Loading branch information
arcan1s committed Sep 4, 2024
1 parent 5c34c05 commit 4c4c9b2
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
12 changes: 7 additions & 5 deletions src/ahriman/core/database/operations/event_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ def run(connection: Connection) -> list[Event]:
Event.from_json(row)
for row in connection.execute(
"""
select created, event, object_id, message, data from auditlog
where (:event is null or event = :event)
and (:object_id is null or object_id = :object_id)
and repository = :repository
order by created limit :limit offset :offset
select created, event, object_id, message, data from (
select * from auditlog
where (:event is null or event = :event)
and (:object_id is null or object_id = :object_id)
and repository = :repository
order by created desc limit :limit offset :offset
) order by created asc
""",
{
"event": event,
Expand Down
8 changes: 5 additions & 3 deletions src/ahriman/core/database/operations/logs_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ def run(connection: Connection) -> list[tuple[float, str]]:
(row["created"], row["record"])
for row in connection.execute(
"""
select created, record from logs
where package_base = :package_base and repository = :repository
order by created limit :limit offset :offset
select created, record from (
select * from logs
where package_base = :package_base and repository = :repository
order by created desc limit :limit offset :offset
) order by created asc
""",
{
"package_base": package_base,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ def test_event_insert_get_pagination(database: SQLite) -> None:
"""
database.event_insert(Event("1", "1"))
database.event_insert(Event("2", "2"))
assert all(event.event == "2" for event in database.event_get(limit=1, offset=1))
assert all(event.event == "1" for event in database.event_get(limit=1, offset=1))
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_logs_insert_get_pagination(database: SQLite, package_ahriman: Package)
"""
database.logs_insert(LogRecordId(package_ahriman.base, "1"), 42.0, "message 1")
database.logs_insert(LogRecordId(package_ahriman.base, "1"), 43.0, "message 2")
assert database.logs_get(package_ahriman.base, 1, 1) == [(43.0, "message 2")]
assert database.logs_get(package_ahriman.base, 1, 1) == [(42.0, "message 1")]


def test_logs_insert_get_multi(database: SQLite, package_ahriman: Package) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def test_get(client: TestClient) -> None:
assert not response_schema.validate(json, many=True)

events = [Event.from_json(event) for event in json]
assert events == [event1, event2]
assert events == [event2, event1]


async def test_get_with_pagination(client: TestClient) -> None:
Expand All @@ -61,7 +61,7 @@ async def test_get_with_pagination(client: TestClient) -> None:
json = await response.json()
assert not response_schema.validate(json, many=True)

assert [Event.from_json(event) for event in json] == [event2]
assert [Event.from_json(event) for event in json] == [event1]


async def test_get_bad_request(client: TestClient) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async def test_get_with_pagination(client: TestClient, package_ahriman: Package)

logs = await response.json()
assert not response_schema.validate(logs)
assert logs == [{"created": 43.0, "message": "message 2"}]
assert logs == [{"created": 42.0, "message": "message 1"}]


async def test_get_bad_request(client: TestClient, package_ahriman: Package) -> None:
Expand Down

0 comments on commit 4c4c9b2

Please sign in to comment.