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

@@ -106,3 +106,33 @@ class EventOperations(Operations):
})
return self.with_connection(run, commit=True)
def events_rotate(self, keep_last_records: int, repository_id: RepositoryId | None = None) -> None:
"""
rotate events in storage, keeping only the last N records for each object
Args:
keep_last_records(int): number of last records to keep
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
"""
repository_id = repository_id or self._repository_id
def run(connection: Connection) -> None:
connection.execute(
"""
delete from auditlog
where rowid in (
select rowid from (
select rowid, row_number() over (
partition by object_id order by created desc, rowid desc
) as rn
from auditlog
where repository = :repository
)
where rn > :keep_last_records
)
""",
{"keep_last_records": keep_last_records, "repository": repository_id.id},
)
self.with_connection(run, commit=True)
@@ -18,4 +18,5 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from ahriman.core.housekeeping.archive_rotation_trigger import ArchiveRotationTrigger
from ahriman.core.housekeeping.events_rotation_trigger import EventsRotationTrigger
from ahriman.core.housekeeping.logs_rotation_trigger import LogsRotationTrigger
@@ -0,0 +1,85 @@
#
# 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.core import context
from ahriman.core.configuration import Configuration
from ahriman.core.status import Client
from ahriman.core.triggers import Trigger
from ahriman.models.package import Package
from ahriman.models.repository_id import RepositoryId
from ahriman.models.result import Result
class EventsRotationTrigger(Trigger):
"""
rotate events after build processes
Attributes:
keep_last_records(int): number of last records to keep
"""
CONFIGURATION_SCHEMA = {
"events-rotation": {
"type": "dict",
"schema": {
"keep_last_events": {
"type": "integer",
"required": True,
"coerce": "integer",
"min": 0,
},
},
},
}
def __init__(self, repository_id: RepositoryId, configuration: Configuration) -> None:
"""
Args:
repository_id(RepositoryId): repository unique identifier
configuration(Configuration): configuration instance
"""
Trigger.__init__(self, repository_id, configuration)
section = next(iter(self.configuration_sections(configuration)))
self.keep_last_records = configuration.getint(section, "keep_last_events")
@classmethod
def configuration_sections(cls, configuration: Configuration) -> list[str]:
"""
extract configuration sections from configuration
Args:
configuration(Configuration): configuration instance
Returns:
list[str]: read configuration sections belong to this trigger
"""
return list(cls.CONFIGURATION_SCHEMA.keys())
def on_result(self, result: Result, packages: list[Package]) -> None:
"""
run trigger
Args:
result(Result): build result
packages(list[Package]): list of all available packages
"""
ctx = context.get()
reporter = ctx.get(Client)
reporter.events_rotate(self.keep_last_records)
@@ -118,6 +118,18 @@ class Client:
"""
raise NotImplementedError
def events_rotate(self, keep_last_records: int) -> None:
"""
remove older events from storage
Args:
keep_last_records(int): number of last records to keep
Raises:
NotImplementedError: not implemented method
"""
raise NotImplementedError
def logs_rotate(self, keep_last_records: int) -> None:
"""
remove older logs from storage
@@ -75,6 +75,15 @@ class LocalClient(Client):
"""
return self.database.event_get(event, object_id, from_date, to_date, limit, offset, self.repository_id)
def events_rotate(self, keep_last_records: int) -> None:
"""
remove older events from storage
Args:
keep_last_records(int): number of last records to keep
"""
self.database.events_rotate(keep_last_records, self.repository_id)
def logs_rotate(self, keep_last_records: int) -> None:
"""
remove older logs from storage
@@ -90,6 +90,15 @@ class Watcher(LazyLogging):
"""
return self.client.event_get(event, object_id, from_date, to_date, limit, offset)
async def events_rotate(self, keep_last_records: int) -> None:
"""
remove older events from storage
Args:
keep_last_records(int): number of last records to keep
"""
self.client.events_rotate(keep_last_records)
async def load(self) -> None:
"""
load packages from local database
@@ -218,6 +218,18 @@ class WebClient(Client, SyncAhrimanClient):
return []
def events_rotate(self, keep_last_records: int) -> None:
"""
remove older events from storage
Args:
keep_last_records(int): number of last records to keep
"""
query = self.repository_id.query() + [("keep_last_records", str(keep_last_records))]
with contextlib.suppress(Exception):
self.make_request("DELETE", self._events_url(), params=query)
def logs_rotate(self, keep_last_records: int) -> None:
"""
remove older logs from storage