From 368efe2c6366b521ca84a921b2ccd06318ffe13b Mon Sep 17 00:00:00 2001 From: Jennifer Taylor Date: Fri, 8 May 2020 18:22:31 +0000 Subject: [PATCH] Auto-prune event log entries after some time. --- TODO.md | 1 - bemani/data/mysql/network.py | 8 ++++++++ bemani/frontend/templates/admin/settings.html | 2 ++ bemani/utils/scheduler.py | 9 ++++++++- config/server.yaml | 3 +++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/TODO.md b/TODO.md index 68cbf9f..618c7bb 100644 --- a/TODO.md +++ b/TODO.md @@ -8,4 +8,3 @@ Things that I have not gotten around to doing. - Make the frontend work better on mobile. It works well enough, but it could be a much better experience. - Support for DanEvo. I meant to do this but my DanEvo ended up in storage before I could tackle it, so the only thing that exists at the moment is a rudimentary music DB parser. - Figure out phase/unlock/etc bits for some older IIDX and Pop'n Music versions and hook them up to the Arcade panel to allow switching events. - - Auto-prune events in the event log after some configured date. Currently it grows indefinitely which is fine for small hobby setups but could be a problem with lots of events. diff --git a/bemani/data/mysql/network.py b/bemani/data/mysql/network.py index ad3c4a0..beea5a6 100644 --- a/bemani/data/mysql/network.py +++ b/bemani/data/mysql/network.py @@ -298,3 +298,11 @@ class NetworkData(BaseData): ), ) return events + + def delete_events(self, oldest_event_ts: int) -> None: + """ + Given a timestamp of the oldset event we should keep around, delete + all events older than this timestamp. + """ + sql = "DELETE FROM audit WHERE timestamp < :ts" + self.execute(sql, {'ts': oldest_event_ts}) diff --git a/bemani/frontend/templates/admin/settings.html b/bemani/frontend/templates/admin/settings.html index 8d68620..9821198 100644 --- a/bemani/frontend/templates/admin/settings.html +++ b/bemani/frontend/templates/admin/settings.html @@ -56,6 +56,8 @@
{{ 'yes' if config['paseli']['enabled'] else 'no' }} (can be overridden by arcade settings)
Infinite PASELI Enabled
{{ 'yes' if config['paseli']['infinite'] else 'no' }} (can be overridden by arcade settings)
+
Event Log Preservation Duration
+
{{ (config['event_log_duration']|string + ' seconds') if config['event_log_duration'] else 'infinite' }}
{% endblock %} diff --git a/bemani/utils/scheduler.py b/bemani/utils/scheduler.py index e63e825..6fed111 100644 --- a/bemani/utils/scheduler.py +++ b/bemani/utils/scheduler.py @@ -18,7 +18,7 @@ from bemani.frontend.ddr import DDRCache from bemani.frontend.sdvx import SoundVoltexCache from bemani.frontend.reflec import ReflecBeatCache from bemani.frontend.museca import MusecaCache -from bemani.common import GameConstants +from bemani.common import GameConstants, Time from bemani.data import Data @@ -61,6 +61,13 @@ def run_scheduled_work(config: Dict[str, Any]) -> None: for cache in enabled_caches: cache.preload(data, config) # type: ignore + # Now, possibly delete old log entries + keep_duration = config.get('event_log_duration', 0) + if keep_duration > 0: + # Calculate timestamp of events we should delete + oldest_event = Time.now() - keep_duration + data.local.network.delete_events(oldest_event) + if __name__ == '__main__': parser = argparse.ArgumentParser(description="A scheduler for work that needs to be done periodically.") diff --git a/config/server.yaml b/config/server.yaml index da52536..91a1dad 100644 --- a/config/server.yaml +++ b/config/server.yaml @@ -57,3 +57,6 @@ name: 'e-AMUSEMENT Network' email: 'nobody@nowhere.com' # Cache DIR, should point somewhere other than /tmp for production instances cache_dir: '/tmp' +# Number of seconds to preserve event logs before deleting them. +# Set to zero to disable deleting logs. +event_log_duration: 2592000