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

fix: change verbosity of api key purge mgmt command #6758

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ def add_arguments(self, parser):
parser.add_argument('-n', '--dry-run', action='store_true', default=False,
help="Don't delete events, just show what would be done")


def handle(self, *args, **options):
keep_days = options['keep_days']
dry_run = options['dry_run']
verbosity = options.get("verbosity", 1)

def _format_count(count, unit='day'):
return '{} {}{}'.format(count, unit, ('' if count == 1 else 's'))

if keep_days < 0:
raise CommandError('Negative keep_days not allowed ({} was specified)'.format(keep_days))

self.stdout.write('purge_old_personal_api_key_events: Finding events older than {}\n'.format(_format_count(keep_days)))
if dry_run:
self.stdout.write('Dry run requested, records will not be deleted\n')
self.stdout.flush()
if verbosity > 1:
self.stdout.write('purge_old_personal_api_key_events: Finding events older than {}\n'.format(_format_count(keep_days)))
if dry_run:
self.stdout.write('Dry run requested, records will not be deleted\n')
self.stdout.flush()

now = timezone.now()
old_events = PersonApiKeyEvent.objects.filter(
Expand All @@ -41,7 +44,8 @@ def _format_count(count, unit='day'):
stats = old_events.aggregate(Min('time'), Max('time'))
old_count = old_events.count()
if old_count == 0:
self.stdout.write('No events older than {} found\n'.format(_format_count(keep_days)))
if verbosity > 1:
self.stdout.write('No events older than {} found\n'.format(_format_count(keep_days)))
return

oldest_date = stats['time__min']
Expand All @@ -50,10 +54,11 @@ def _format_count(count, unit='day'):
newest_ago = now - newest_date

action_fmt = 'Would delete {}\n' if dry_run else 'Deleting {}\n'
self.stdout.write(action_fmt.format(_format_count(old_count, 'event')))
self.stdout.write(' Oldest at {} ({} ago)\n'.format(oldest_date, _format_count(oldest_ago.days)))
self.stdout.write(' Most recent at {} ({} ago)\n'.format(newest_date, _format_count(newest_ago.days)))
self.stdout.flush()
if verbosity > 1:
self.stdout.write(action_fmt.format(_format_count(old_count, 'event')))
self.stdout.write(' Oldest at {} ({} ago)\n'.format(oldest_date, _format_count(oldest_ago.days)))
self.stdout.write(' Most recent at {} ({} ago)\n'.format(newest_date, _format_count(newest_ago.days)))
self.stdout.flush()

if not dry_run:
old_events.delete()
10 changes: 5 additions & 5 deletions ietf/person/management/commands/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,26 @@ def test_purge_old_personal_api_key_events(self):
num_recent_events = len(recent_events)

# call with dry run
output = self._call_command('purge_old_personal_api_key_events', str(keep_days), '--dry-run')
output = self._call_command('purge_old_personal_api_key_events', str(keep_days), '--dry-run', '-v2')
self._assert_purge_dry_run_results(output, num_old_events, old_events + recent_events)

# call for real
output = self._call_command('purge_old_personal_api_key_events', str(keep_days))
output = self._call_command('purge_old_personal_api_key_events', str(keep_days), '-v2')
self._assert_purge_results(output, num_old_events, recent_events)
self.assertEqual(PersonEvent.objects.count(), personevents_before + num_recent_events,
'PersonEvents were not cleaned up properly')

# repeat - there should be nothing left to delete
output = self._call_command('purge_old_personal_api_key_events', '--dry-run', str(keep_days))
output = self._call_command('purge_old_personal_api_key_events', '--dry-run', str(keep_days), '-v2')
self._assert_purge_dry_run_results(output, 0, recent_events)

output = self._call_command('purge_old_personal_api_key_events', str(keep_days))
output = self._call_command('purge_old_personal_api_key_events', str(keep_days), '-v2')
self._assert_purge_results(output, 0, recent_events)
self.assertEqual(PersonEvent.objects.count(), personevents_before + num_recent_events,
'PersonEvents were not cleaned up properly')

# and now delete the remaining events
output = self._call_command('purge_old_personal_api_key_events', '0')
output = self._call_command('purge_old_personal_api_key_events', '0', '-v2')
self._assert_purge_results(output, num_recent_events, [])
self.assertEqual(PersonEvent.objects.count(), personevents_before,
'PersonEvents were not cleaned up properly')
Expand Down
Loading