From b008492b146fe5a589ef6c7f33854914c6b57cc5 Mon Sep 17 00:00:00 2001 From: Andrew Melton Date: Wed, 10 Apr 2013 16:37:41 -0400 Subject: [PATCH] Only set delete field when deleted_at set on notification --- stacktach/views.py | 11 +++++++---- tests/unit/test_stacktach.py | 3 --- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/stacktach/views.py b/stacktach/views.py index 96c0712..a61eacf 100644 --- a/stacktach/views.py +++ b/stacktach/views.py @@ -290,8 +290,6 @@ def _process_exists(raw, body): launched_range = (launched_at, launched_at+1) usage = STACKDB.get_instance_usage(instance=instance_id, launched_at__range=launched_range) - delete = STACKDB.get_instance_delete(instance=instance_id, - launched_at__range=launched_range) values = {} values['message_id'] = body['message_id'] values['instance'] = instance_id @@ -303,14 +301,19 @@ def _process_exists(raw, body): values['instance_type_id'] = payload['instance_type_id'] if usage: values['usage'] = usage - if delete: - values['delete'] = delete values['raw'] = raw deleted_at = payload.get('deleted_at') if deleted_at and deleted_at != '': + # We only want to pre-populate the 'delete' if we know this is in fact + # an exist event for a deleted instance. Otherwise, there is a + # chance we may populate it for a previous period's exist. + delete = STACKDB.get_instance_delete(instance=instance_id, + launched_at__range=launched_range) deleted_at = utils.str_time_to_unix(deleted_at) values['deleted_at'] = deleted_at + if delete: + values['delete'] = delete exists = STACKDB.create_instance_exists(**values) STACKDB.save(exists) diff --git a/tests/unit/test_stacktach.py b/tests/unit/test_stacktach.py index 851e00d..6a93bc1 100644 --- a/tests/unit/test_stacktach.py +++ b/tests/unit/test_stacktach.py @@ -629,9 +629,6 @@ class StacktackUsageParsingTestCase(unittest.TestCase): views.STACKDB.get_instance_usage(instance=INSTANCE_ID_1, launched_at__range=launched_range)\ .AndReturn(usage) - views.STACKDB.get_instance_delete(instance=INSTANCE_ID_1, - launched_at__range=launched_range)\ - .AndReturn(None) exists_values = { 'message_id': MESSAGE_ID_1, 'instance': INSTANCE_ID_1,