Skip to content

Commit

Permalink
Database pruning docs, delete_custom_graphs flag rename
Browse files Browse the repository at this point in the history
  • Loading branch information
wielas committed Dec 11, 2023
1 parent 1bd8c4b commit 9932f49
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
31 changes: 31 additions & 0 deletions docs/functionality.rst
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,37 @@ Finally, the implementation of the scheduler in the FMD
is based on the appscheduler.schedulers.Background schedulers
about which you can read more `in the corresponding documentation page <apscheduler.schedulers>`_.


Adding database cleaning schedule
-----------------

As your application grows, so will the amount of data stored in the database.
To prevent it from growing too large, you can set up a schedule to clean the database.
Pruning is fully customizable, and can be set up to run as often as you want deleting data as old as you want through
your own cron schedule.

This can be done by adding the following code to your application:

.. code-block:: python
weeks_to_keep = 8
delete_custom_graph_data = True
pruning_cron_schedule = {'month':"*/2",
'day': 1,
'hour': 3,
'minute': 0,
'second': 0}
dashboard.add_database_pruning_schedule(weeks_to_keep, delete_custom_graph_data, **pruning_cron_schedule)
The first argument is how many weeks of data you want to keep - in this case we delete data older than the last two months.
Second argument indicates whether you want to delete custom graph data as well.
The third argument is the cron schedule, which in the presented case will
clean the database every 2 months, on the first day of the month at 3:00 AM.

That's it! Now you won't have to worry about the performance slowing down with time.

Telemetry
----------------------

Expand Down
6 changes: 3 additions & 3 deletions flask_monitoringdashboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ def add_graph(title, func, trigger="interval", **schedule):
custom_graph.add_background_job(func, graph_id, trigger, **schedule)


def add_database_pruning_schedule(weeks_to_keep, delete_custom_graphs=False, **schedule):
def add_database_pruning_schedule(weeks_to_keep, delete_custom_graph_data, **schedule):
"""
Add a scheduled job to prune the database of Request and optionally CustomGraph data older than the specified
:param weeks_to_keep: number of weeks to keep in the database
:param delete_custom_graphs: flag telling if CustomGraph data should be deleted as well
:param delete_custom_graph_data: flag telling if CustomGraph data should be deleted as well
:param schedule: dict containing cron schedule values
"""
from flask_monitoringdashboard.core import database_pruning

database_pruning.add_background_pruning_job(weeks_to_keep, delete_custom_graphs, **schedule)
database_pruning.add_background_pruning_job(weeks_to_keep, delete_custom_graph_data, **schedule)
8 changes: 4 additions & 4 deletions flask_monitoringdashboard/core/database_pruning.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from flask_monitoringdashboard.core.custom_graph import scheduler


def prune_database_older_than_weeks(weeks_to_keep, delete_custom_graphs):
def prune_database_older_than_weeks(weeks_to_keep, delete_custom_graph_data):
"""Prune the database of Request and optionally CustomGraph data older than the specified number of weeks"""
with session_scope() as session:
date_to_delete_from = datetime.utcnow() - timedelta(weeks=weeks_to_keep)
Expand All @@ -18,7 +18,7 @@ def prune_database_older_than_weeks(weeks_to_keep, delete_custom_graphs):
session.query(Request).filter(Request.time_requested < date_to_delete_from).delete()

# Prune CustomGraphData table by joining with CustomGraph to get the time_added
if delete_custom_graphs:
if delete_custom_graph_data:
old_graph_data = session.query(CustomGraphData) \
.join(CustomGraph, CustomGraph.graph_id == CustomGraphData.graph_id) \
.filter(CustomGraph.time_added < date_to_delete_from).all()
Expand All @@ -28,14 +28,14 @@ def prune_database_older_than_weeks(weeks_to_keep, delete_custom_graphs):
session.commit()


def add_background_pruning_job(weeks_to_keep, delete_custom_graphs, **schedule):
def add_background_pruning_job(weeks_to_keep, delete_custom_graph_data, **schedule):
"""Add a scheduled job to prune the database of Request and optionally CustomGraph data older than the specified
number of weeks"""

scheduler.add_job(
id='database_pruning_schedule',
func=prune_database_older_than_weeks,
args=[weeks_to_keep, delete_custom_graphs], # These are arguments passed to the prune function
args=[weeks_to_keep, delete_custom_graph_data], # These are arguments passed to the prune function
trigger='cron',
replace_existing=True, # This will replace an existing job
**schedule
Expand Down
10 changes: 5 additions & 5 deletions flask_monitoringdashboard/views/pruning.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ def prune_database_on_demand():
"""
data = request.json
weeks = data.get('age_threshold_weeks')
delete_custom_graphs = data.get('delete_custom_graphs')
delete_custom_graph_data = data.get('delete_custom_graph_data')

# Validation
if not isinstance(weeks, int) or weeks < 0:
return jsonify({'error': 'age_threshold_weeks must be a natural number'}), 400
if not isinstance(delete_custom_graphs, bool):
if not isinstance(delete_custom_graph_data, bool):
return jsonify({'error': 'delete_custom_graphs must be a boolean'}), 400

# Prune database
prune_database_older_than_weeks(weeks, delete_custom_graphs)
prune_database_older_than_weeks(weeks, delete_custom_graph_data)

# Post info to telemetry if enabled
post_data = {'age_threshold_weeks': weeks, 'delete_custom_graphs': delete_custom_graphs}
post_data = {'age_threshold_weeks': weeks, 'delete_custom_graphs': delete_custom_graph_data}
post_to_back_if_telemetry_enabled('DatabasePruning', **post_data)

return jsonify({'message': 'Database pruning complete'}), 200
Expand All @@ -49,7 +49,7 @@ def get_pruning_schedule():
'second': str(job.trigger.fields[7]),
'next_run_time': job.next_run_time,
'weeks_to_keep': job.args[0],
'delete_custom_graphs': job.args[1],
'delete_custom_graph_data': job.args[1],
})
else:
return jsonify({'error': 'No pruning schedule found'}), 404

0 comments on commit 9932f49

Please sign in to comment.