Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added management command to delete old pipeline errors #122

Open
wants to merge 4 commits into
base: develop-koa
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions figures/management/commands/delete_old_pipeline_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from __future__ import print_function
from __future__ import absolute_import
from textwrap import dedent

from django.core.management.base import BaseCommand
from django.utils.timezone import now, timedelta
from figures.models import PipelineError

class Command(BaseCommand):
'''Delete PipelineError records older than a specified number of days (default is 15 days).'''
help = dedent(__doc__).strip()

def add_arguments(self, parser):
'''Add command arguments.'''
parser.add_argument(
'--days',
type=int,
default=15,
help='Number of days to retain PipelineError records. Defaults to 15.',
)

def handle(self, *args, **options):
'''Handle the deletion process.'''
days = options['days']
cutoff_date = now() - timedelta(days=days)

print(f"Deleting PipelineError records older than {days} days in batches...")

batch_size = 1000
total_deleted = 0

while True:
old_records = PipelineError.objects.filter(created__lt=cutoff_date)[:batch_size]
if not old_records.exists():
break
ids_to_delete = list(old_records.values_list('id', flat=True))
PipelineError.objects.filter(id__in=ids_to_delete).delete()
total_deleted += len(ids_to_delete)
Comment on lines +36 to +38

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we replace this logic with a single query by deleting the records directly?

deleted, _ = old_records.delete()

print(f"Deleted {len(ids_to_delete)} records in this batch. Total deleted: {total_deleted}.")

print(f"Deletion complete. Total {total_deleted} PipelineError records older than {days} days deleted.")
print('Done.')