diff --git a/CHANGELOG.md b/CHANGELOG.md index b2fc4171..2573badc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ### Improvements - Show the number of downtimes and total downtime minutes in monthly reports (#104) - Show the number of downtimes and total downtime minutes in "Check Details" page +- Add the `pruneflips` management command ## 1.8.0 - 2019-07-08 diff --git a/README.md b/README.md index bac79895..cd92fd6a 100644 --- a/README.md +++ b/README.md @@ -269,7 +269,7 @@ There are separate Django management commands for each task: $ ./manage.py pruneusers ``` -* Remove old records fromt he `api_tokenbucket` table. The TokenBucket +* Remove old records from the `api_tokenbucket` table. The TokenBucket model is used for rate-limiting login attempts and similar operations. Any records older than one day can be safely removed. @@ -277,6 +277,15 @@ There are separate Django management commands for each task: $ ./manage.py prunetokenbucket ``` +* Remove old records from the `api_flip` table. The Flip + objects are used to track status changes of checks, and to calculate + downtime statistics month by month. Flip objects from more than 3 months + ago are not used and can be safely removed. + + ``` + $ ./manage.py pruneflips + ``` + When you first try these commands on your data, it is a good idea to test them on a copy of your database, not on the live database right away. In a production setup, you should also have regular, automated database diff --git a/hc/api/management/commands/pruneflips.py b/hc/api/management/commands/pruneflips.py new file mode 100644 index 00000000..faa45963 --- /dev/null +++ b/hc/api/management/commands/pruneflips.py @@ -0,0 +1,16 @@ +from django.core.management.base import BaseCommand + +from hc.api.models import Flip +from hc.lib.date import month_boundaries + + +class Command(BaseCommand): + help = "Prune old Flip objects." + + def handle(self, *args, **options): + threshold = min(month_boundaries(months=3)) + + q = Flip.objects.filter(created__lt=threshold) + n_pruned, _ = q.delete() + + return "Done! Pruned %d flips." % n_pruned