Skip to content

Commit

Permalink
Add prune-alerts command
Browse files Browse the repository at this point in the history
Combined with the new notifier queue in line#94 add a prune command so we
can avoid having our database grow too much
  • Loading branch information
kfdm committed Sep 25, 2018
1 parent d2f65e4 commit cedc156
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions promgen/management/commands/prune-alerts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) 2018 LINE Corporation
# These sources are released under the terms of the MIT license: see LICENSE

import datetime

from django.core.management.base import BaseCommand
from django.utils import timezone
from promgen import models


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('--days', type=int, default=30)
parser.add_argument('--force', dest='dryrun', action='store_false')

def success(self, message, *args):
self.stdout.write(self.style.SUCCESS(message % args))

def handle(self, days, dryrun, verbosity, **options):
cutoff = timezone.now() - datetime.timedelta(days=days)

if verbosity > 1:
self.success('Removing alerts before %s (%d days)', cutoff, days)

alerts = models.Alert.objects.filter(created__lt=cutoff)

if dryrun:
self.success('Would have removed %d alerts', alerts.count())
return

count, objects = alerts.delete()

if verbosity > 1:
self.success('Removed %d Alerts', count)

0 comments on commit cedc156

Please sign in to comment.