Skip to content

Commit

Permalink
Merge pull request #579 from praekeltfoundation/events-export-api
Browse files Browse the repository at this point in the history
Add delete historical management command
  • Loading branch information
erikh360 authored Aug 31, 2023
2 parents e1ea0be + 29067a6 commit 0d8973c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
38 changes: 38 additions & 0 deletions eventstore/management/commands/delete_historical_records.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import logging

from dateutil.relativedelta import relativedelta
from django.apps import apps
from django.core.management.base import BaseCommand
from django.utils import timezone

logger = logging.getLogger(__name__)

VALID_MODELS = {"Event", "Message"}


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
"model",
type=str,
help="Specify the model you want to delete the records for.(Event/Message)",
)
parser.add_argument(
"retention_period",
type=int,
help="Specify the retention period in months",
default=60,
)

def handle(self, *args, **options):
model_name = options["model"]
retention_period = options["retention_period"]

if model_name not in VALID_MODELS:
raise Exception("Invalid model specified")

model = apps.get_model("eventstore", model_name)

filter_date = timezone.now() - relativedelta(months=retention_period, hour=0)
count, _ = model.objects.filter(timestamp__lt=filter_date).delete()
logger.info(f"Deleted {count} {model_name.lower()}(s)")
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from io import StringIO

from dateutil.relativedelta import relativedelta
from django.core.management import call_command
from django.core.management.base import CommandError
from django.test import TestCase
from django.utils import timezone

from eventstore.models import Event, Message


class DeleteHistoricalRecordsTests(TestCase):
def call_command(self, *args, **kwargs):
out = StringIO()
call_command(
"delete_historical_records",
*args,
stdout=out,
stderr=StringIO(),
**kwargs,
)
return out.getvalue()

def create_record(self, model, id, timestamp):
record = model.objects.create(id=id)
record.timestamp = timestamp
record.save()

def test_missing_arguments(self):
self.assertRaises(CommandError, self.call_command)

def test_invalid_arguments(self):
self.assertRaises(Exception, self.call_command, "InvalidModel")

def test_delete_events(self):
running_month = timezone.now() - relativedelta(months=12, hour=12)

for i in range(12):
self.create_record(Event, i, running_month)
running_month = running_month + relativedelta(months=1)

self.call_command("Event", 6)

self.assertEqual(Event.objects.count(), 6)

def test_delete_messages(self):
running_month = timezone.now() - relativedelta(months=12, hour=12)

for i in range(12):
self.create_record(Message, i, running_month)
running_month = running_month + relativedelta(months=1)

self.call_command("Message", 6)

self.assertEqual(Message.objects.count(), 6)

0 comments on commit 0d8973c

Please sign in to comment.