Add the pruneflips management command.

This commit is contained in:
Pēteris Caune 2019-07-20 12:25:58 +03:00
parent b37d908879
commit c0d808271e
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
3 changed files with 27 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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