mirror of
https://github.com/ansible/awx.git
synced 2026-03-19 18:07:33 -02:30
support --granularity=0d to delete all facts.
Still respect --older_than
This commit is contained in:
@@ -30,20 +30,32 @@ class CleanupFacts(object):
|
|||||||
# pivot -= granularity
|
# pivot -= granularity
|
||||||
# group by host
|
# group by host
|
||||||
def cleanup(self, older_than_abs, granularity):
|
def cleanup(self, older_than_abs, granularity):
|
||||||
|
flag_delete_all = False
|
||||||
fact_oldest = FactVersion.objects.all().order_by('timestamp').first()
|
fact_oldest = FactVersion.objects.all().order_by('timestamp').first()
|
||||||
if not fact_oldest:
|
if not fact_oldest:
|
||||||
return 0
|
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
|
total = 0
|
||||||
date_pivot = older_than_abs
|
date_pivot = older_than_abs
|
||||||
while date_pivot > fact_oldest.timestamp:
|
while date_pivot > fact_oldest.timestamp:
|
||||||
date_pivot_next = date_pivot - granularity
|
date_pivot_next = date_pivot - granularity
|
||||||
kv = {
|
kv = {
|
||||||
'timestamp__lte': date_pivot,
|
'timestamp__lte': date_pivot
|
||||||
'timestamp__gt': date_pivot_next,
|
|
||||||
}
|
}
|
||||||
|
if not flag_delete_all:
|
||||||
|
kv['timestamp__gt'] = date_pivot_next
|
||||||
|
|
||||||
version_objs = FactVersion.objects.filter(**kv).order_by('-timestamp')
|
version_objs = FactVersion.objects.filter(**kv).order_by('-timestamp')
|
||||||
|
|
||||||
|
if flag_delete_all:
|
||||||
|
total = version_objs.delete()
|
||||||
|
break
|
||||||
|
|
||||||
# Transform array -> {host_id} = [<fact_version>, <fact_version>, ...]
|
# Transform array -> {host_id} = [<fact_version>, <fact_version>, ...]
|
||||||
# TODO: If this set gets large then we can use mongo to transform the data set for us.
|
# TODO: If this set gets large then we can use mongo to transform the data set for us.
|
||||||
host_ids = {}
|
host_ids = {}
|
||||||
@@ -66,6 +78,7 @@ class CleanupFacts(object):
|
|||||||
total += count
|
total += count
|
||||||
|
|
||||||
date_pivot = date_pivot_next
|
date_pivot = date_pivot_next
|
||||||
|
|
||||||
return total
|
return total
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|||||||
@@ -28,6 +28,12 @@ class CleanupFactsCommandFunctionalTest(BaseCommandMixin, BaseTest, MongoDBRequi
|
|||||||
result, stdout, stderr = self.run_command('cleanup_facts', granularity='1w',older_than='5d')
|
result, stdout, stderr = self.run_command('cleanup_facts', granularity='1w',older_than='5d')
|
||||||
self.assertEqual(stdout, 'Deleted 0 facts.\n')
|
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):
|
def test_invoke_params_required(self):
|
||||||
result, stdout, stderr = self.run_command('cleanup_facts')
|
result, stdout, stderr = self.run_command('cleanup_facts')
|
||||||
self.assertIsInstance(result, CommandError)
|
self.assertIsInstance(result, CommandError)
|
||||||
|
|||||||
Reference in New Issue
Block a user