From e9e4cf680b5684b06fbacc9439ad7451a571b57a Mon Sep 17 00:00:00 2001 From: nhnn Date: Thu, 25 Jul 2024 20:34:28 +0300 Subject: [PATCH] feat: add option to filter logs by unit or slice --- selfprivacy_api/graphql/queries/logs.py | 6 +++++- selfprivacy_api/utils/systemd_journal.py | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/selfprivacy_api/graphql/queries/logs.py b/selfprivacy_api/graphql/queries/logs.py index cf8fe21..4f76115 100644 --- a/selfprivacy_api/graphql/queries/logs.py +++ b/selfprivacy_api/graphql/queries/logs.py @@ -75,6 +75,10 @@ class Logs: up_cursor: str | None = None, # All entries returned will be greater than this cursor. Sets lower bound on results. down_cursor: str | None = None, + # All entries will be from a specific systemd slice + filterBySlice: str | None = None, + # All entries will be from a specific systemd unit + filterByUnit: str | None = None, ) -> PaginatedEntries: if limit > 50: raise Exception("You can't fetch more than 50 entries via single request.") @@ -82,7 +86,7 @@ class Logs: list( map( lambda x: LogEntry(x), - get_paginated_logs(limit, up_cursor, down_cursor), + get_paginated_logs(limit, up_cursor, down_cursor, filterBySlice, filterByUnit), ) ) ) diff --git a/selfprivacy_api/utils/systemd_journal.py b/selfprivacy_api/utils/systemd_journal.py index 48e97b8..cbb953f 100644 --- a/selfprivacy_api/utils/systemd_journal.py +++ b/selfprivacy_api/utils/systemd_journal.py @@ -24,9 +24,18 @@ def get_paginated_logs( up_cursor: str | None = None, # All entries returned will be greater than this cursor. Sets lower bound on results. down_cursor: str | None = None, + # All entries will be from a specific systemd slice + filterBySlice: str | None = None, + # All entries will be from a specific systemd unit + filterByUnit: str | None = None, ): j = journal.Reader() + if filterBySlice: + j.add_match("_SYSTEMD_SLICE=" + filterBySlice) + if filterByUnit: + j.add_match("_SYSTEMD_UNIT=" + filterByUnit) + if up_cursor is None and down_cursor is None: j.seek_tail()