diff --git a/awx/main/management/commands/cleanup_facts.py b/awx/main/management/commands/cleanup_facts.py index d127b52c52..81387636b6 100644 --- a/awx/main/management/commands/cleanup_facts.py +++ b/awx/main/management/commands/cleanup_facts.py @@ -30,20 +30,32 @@ class CleanupFacts(object): # pivot -= granularity # group by host def cleanup(self, older_than_abs, granularity): + flag_delete_all = False fact_oldest = FactVersion.objects.all().order_by('timestamp').first() if not fact_oldest: return 0 + # Special case, granularity=0x where x is d, w, or y + # The intent is to delete all facts < older_than_abs + if granularity == relativedelta(): + flag_delete_all = True + total = 0 date_pivot = older_than_abs while date_pivot > fact_oldest.timestamp: date_pivot_next = date_pivot - granularity kv = { - 'timestamp__lte': date_pivot, - 'timestamp__gt': date_pivot_next, + 'timestamp__lte': date_pivot } + if not flag_delete_all: + kv['timestamp__gt'] = date_pivot_next + version_objs = FactVersion.objects.filter(**kv).order_by('-timestamp') + if flag_delete_all: + total = version_objs.delete() + break + # Transform array -> {host_id} = [, , ...] # TODO: If this set gets large then we can use mongo to transform the data set for us. host_ids = {} @@ -66,6 +78,7 @@ class CleanupFacts(object): total += count date_pivot = date_pivot_next + return total ''' diff --git a/awx/main/tests/commands/cleanup_facts.py b/awx/main/tests/commands/cleanup_facts.py index c10f5cd90b..8d02310a2d 100644 --- a/awx/main/tests/commands/cleanup_facts.py +++ b/awx/main/tests/commands/cleanup_facts.py @@ -28,6 +28,12 @@ class CleanupFactsCommandFunctionalTest(BaseCommandMixin, BaseTest, MongoDBRequi result, stdout, stderr = self.run_command('cleanup_facts', granularity='1w',older_than='5d') self.assertEqual(stdout, 'Deleted 0 facts.\n') + def test_invoke_all_deleted(self): + self.create_hosts_and_facts(datetime(year=2015, day=2, month=1, microsecond=0), 10, 20) + + result, stdout, stderr = self.run_command('cleanup_facts', granularity='0d', older_than='0d') + self.assertEqual(stdout, 'Deleted 200 facts.\n') + def test_invoke_params_required(self): result, stdout, stderr = self.run_command('cleanup_facts') self.assertIsInstance(result, CommandError)