From e563520c7765d4e638f8e65b8b8e4095fa2fc4cc Mon Sep 17 00:00:00 2001 From: Gagan Deep Date: Mon, 8 Aug 2022 15:23:15 +0530 Subject: [PATCH] [feature] Added setting to configure default retention policy Added setting to configure default retention policy for the timeseries database. --- README.rst | 11 +++++++++++ .../db/backends/influxdb/tests.py | 19 +++++++++++++++---- openwisp_monitoring/device/apps.py | 7 ++++++- openwisp_monitoring/device/settings.py | 1 + openwisp_monitoring/device/utils.py | 9 +++++++++ 5 files changed, 42 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 2792d49ee..cdb642dbf 100644 --- a/README.rst +++ b/README.rst @@ -942,6 +942,17 @@ if there's anything that is not working as intended. Settings -------- +``OPENWISP_MONITORING_DEFAULT_RETENTION_POLICY`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ++--------------+--------------------------+ +| **type**: | ``str`` | ++--------------+--------------------------+ +| **default**: | ``26280h0m0s`` (3 years) | ++--------------+--------------------------+ + +The default retention policy that applies to the timeseries data. + ``OPENWISP_MONITORING_SHORT_RETENTION_POLICY`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/openwisp_monitoring/db/backends/influxdb/tests.py b/openwisp_monitoring/db/backends/influxdb/tests.py index 847cc39e1..4ca7ad752 100644 --- a/openwisp_monitoring/db/backends/influxdb/tests.py +++ b/openwisp_monitoring/db/backends/influxdb/tests.py @@ -11,8 +11,16 @@ from pytz import timezone as tz from swapper import load_model -from openwisp_monitoring.device.settings import SHORT_RETENTION_POLICY -from openwisp_monitoring.device.utils import SHORT_RP, manage_short_retention_policy +from openwisp_monitoring.device.settings import ( + DEFAULT_RETENTION_POLICY, + SHORT_RETENTION_POLICY, +) +from openwisp_monitoring.device.utils import ( + DEFAULT_RP, + SHORT_RP, + manage_default_retention_policy, + manage_short_retention_policy, +) from openwisp_monitoring.monitoring.tests import TestMonitoringMixin from openwisp_monitoring.settings import MONITORING_TIMESERIES_RETRY_OPTIONS from openwisp_utils.tests import capture_stderr @@ -183,12 +191,15 @@ def test_get_query_30d(self): def test_retention_policy(self): manage_short_retention_policy() + manage_default_retention_policy() rp = timeseries_db.get_list_retention_policies() self.assertEqual(len(rp), 2) + self.assertEqual(rp[0]['name'], DEFAULT_RP) + self.assertEqual(rp[0]['default'], True) + self.assertEqual(rp[0]['duration'], DEFAULT_RETENTION_POLICY) self.assertEqual(rp[1]['name'], SHORT_RP) self.assertEqual(rp[1]['default'], False) - duration = SHORT_RETENTION_POLICY - self.assertEqual(rp[1]['duration'], duration) + self.assertEqual(rp[1]['duration'], SHORT_RETENTION_POLICY) def test_query_set(self): c = self._create_chart(configuration='histogram') diff --git a/openwisp_monitoring/device/apps.py b/openwisp_monitoring/device/apps.py index 510629de1..d7370942a 100644 --- a/openwisp_monitoring/device/apps.py +++ b/openwisp_monitoring/device/apps.py @@ -24,7 +24,11 @@ from ..utils import transaction_on_commit from . import settings as app_settings from .signals import device_metrics_received, health_status_changed -from .utils import get_device_cache_key, manage_short_retention_policy +from .utils import ( + get_device_cache_key, + manage_default_retention_policy, + manage_short_retention_policy, +) class DeviceMonitoringConfig(AppConfig): @@ -33,6 +37,7 @@ class DeviceMonitoringConfig(AppConfig): verbose_name = _('Device Monitoring') def ready(self): + manage_default_retention_policy() manage_short_retention_policy() self.connect_is_working_changed() self.connect_device_signals() diff --git a/openwisp_monitoring/device/settings.py b/openwisp_monitoring/device/settings.py index 8a8250552..d239e3eac 100644 --- a/openwisp_monitoring/device/settings.py +++ b/openwisp_monitoring/device/settings.py @@ -43,6 +43,7 @@ def get_health_status_labels(): SHORT_RETENTION_POLICY = get_settings_value('SHORT_RETENTION_POLICY', '24h0m0s') +DEFAULT_RETENTION_POLICY = get_settings_value('DEFAULT_RETENTION_POLICY', '26280h0m0s') CRITICAL_DEVICE_METRICS = get_critical_device_metrics() HEALTH_STATUS_LABELS = get_health_status_labels() AUTO_CLEAR_MANAGEMENT_IP = get_settings_value('AUTO_CLEAR_MANAGEMENT_IP', True) diff --git a/openwisp_monitoring/device/utils.py b/openwisp_monitoring/device/utils.py index 3814d97ff..151b62609 100644 --- a/openwisp_monitoring/device/utils.py +++ b/openwisp_monitoring/device/utils.py @@ -2,6 +2,7 @@ from . import settings as app_settings SHORT_RP = 'short' +DEFAULT_RP = 'autogen' def get_device_cache_key(device, context='react-to-updates'): @@ -14,3 +15,11 @@ def manage_short_retention_policy(): """ duration = app_settings.SHORT_RETENTION_POLICY timeseries_db.create_or_alter_retention_policy(SHORT_RP, duration) + + +def manage_default_retention_policy(): + """ + creates or updates the "default" retention policy + """ + duration = app_settings.DEFAULT_RETENTION_POLICY + timeseries_db.create_or_alter_retention_policy(DEFAULT_RP, duration)