feat: add events rotation trigger to remove old events

This commit is contained in:
arcanis committed 2026-11-26 18:12:27 +02:00
1 parent c70640c492
commit 1fcb4cdb84
25 files changed
+427 -5

No files matched your search

@@ -31,6 +31,7 @@ from ahriman.web.schemas.error_schema import ErrorSchema
from ahriman.web.schemas.event_bus_filter_schema import EventBusFilterSchema
from ahriman.web.schemas.event_schema import EventSchema
from ahriman.web.schemas.event_search_schema import EventSearchSchema
from ahriman.web.schemas.events_rotate_schema import EventsRotateSchema
from ahriman.web.schemas.file_schema import FileSchema
from ahriman.web.schemas.hold_schema import HoldSchema
from ahriman.web.schemas.info_schema import InfoSchema
@@ -0,0 +1,30 @@
#
# Copyright (c) 2021-2026 ahriman team.
#
# This file is part of ahriman
# (see https://github.com/arcan1s/ahriman).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from ahriman.web.apispec import Schema, fields
class EventsRotateSchema(Schema):
"""
request events rotate schema
"""
keep_last_records = fields.Integer(metadata={
"description": "Keep the specified amount of events for each object",
})
@@ -23,7 +23,7 @@ from typing import ClassVar
from ahriman.models.event import Event
from ahriman.models.user_access import UserAccess
from ahriman.web.apispec.decorators import apidocs
from ahriman.web.schemas import EventSchema, EventSearchSchema
from ahriman.web.schemas import EventSchema, EventSearchSchema, EventsRotateSchema
from ahriman.web.views.base import BaseView
@@ -32,13 +32,40 @@ class EventsView(BaseView):
audit log view
Attributes:
DELETE_PERMISSION(UserAccess): (class attribute) delete permissions of self
GET_PERMISSION(UserAccess): (class attribute) get permissions of self
POST_PERMISSION(UserAccess): (class attribute) post permissions of self
"""
GET_PERMISSION = POST_PERMISSION = UserAccess.FULL # type: ClassVar[UserAccess]
DELETE_PERMISSION = GET_PERMISSION = POST_PERMISSION = UserAccess.FULL # type: ClassVar[UserAccess]
ROUTES = ["/api/v1/events"]
@apidocs(
tags=["Audit log"],
summary="Rotate audit events",
description="Remove older events from the selected repository",
permission=DELETE_PERMISSION,
error_400_enabled=True,
error_404_description="Repository is unknown",
query_schema=EventsRotateSchema,
)
async def delete(self) -> None:
"""
rotate events in storage
Raises:
HTTPBadRequest: if bad data is supplied
HTTPNoContent: on success response
"""
try:
keep_last_records = int(self.request.query.get("keep_last_records", 0))
except Exception as ex:
raise HTTPBadRequest(reason=str(ex))
await self.service().events_rotate(keep_last_records)
raise HTTPNoContent
@apidocs(
tags=["Audit log"],
summary="Get events",
@@ -53,7 +80,10 @@ class EventsView(BaseView):
get events list
Returns:
Response: 200 with workers list on success
Response: 200 with events list on success
Raises:
HTTPBadRequest: if bad data is supplied
"""
limit, offset = self.page()
event = self.request.query.get("event") or None
@@ -0,0 +1 @@
# schema testing goes in view class tests
@@ -11,7 +11,7 @@ async def test_get_permission() -> None:
"""
must return correct permission for the request
"""
for method in ("GET", "POST"):
for method in ("GET", "POST", "DELETE"):
request = pytest.helpers.request("", "", method)
assert await EventsView.get_permission(request) == UserAccess.FULL
@@ -23,6 +23,41 @@ def test_routes() -> None:
assert EventsView.ROUTES == ["/api/v1/events"]
async def test_delete(client: TestClient) -> None:
"""
must rotate events keeping last record for each object
"""
events = [
Event("event1", "object1", created=1),
Event("event2", "object1", created=2),
Event("event3", "object2", created=3),
Event("event4", "object2", created=4),
]
for event in events:
await client.post("/api/v1/events", json=event.view())
request_schema = pytest.helpers.schema_request(EventsView.delete, location="querystring")
payload = {"keep_last_records": 1}
assert not request_schema.validate(payload)
response = await client.delete("/api/v1/events", params=payload)
assert response.status == 204
response = await client.get("/api/v1/events")
assert [Event.from_json(event) for event in await response.json()] == [events[1], events[3]]
async def test_delete_bad_request(client: TestClient) -> None:
"""
must return bad request for invalid query parameters
"""
response_schema = pytest.helpers.schema_response(EventsView.delete, code=400)
response = await client.delete("/api/v1/events", params={"keep_last_records": "invalid"})
assert response.status == 400
assert not response_schema.validate(await response.json())
async def test_get(client: TestClient) -> None:
"""
must return all events