Providing json dict to usage parsing
This commit is contained in:
parent
b84f439d04
commit
8dba9e2e8b
@ -216,12 +216,11 @@ INSTANCE_EVENT = {
|
||||
}
|
||||
|
||||
|
||||
def _process_usage_for_new_launch(raw):
|
||||
notif = json.loads(raw.json)
|
||||
payload = notif[1]['payload']
|
||||
def _process_usage_for_new_launch(raw, body):
|
||||
payload = body['payload']
|
||||
values = {}
|
||||
values['instance'] = payload['instance_id']
|
||||
values['request_id'] = notif[1]['_context_request_id']
|
||||
values['request_id'] = body['_context_request_id']
|
||||
|
||||
(usage, new) = STACKDB.get_or_create_instance_usage(**values)
|
||||
|
||||
@ -231,11 +230,10 @@ def _process_usage_for_new_launch(raw):
|
||||
STACKDB.save(usage)
|
||||
|
||||
|
||||
def _process_usage_for_updates(raw):
|
||||
notif = json.loads(raw.json)
|
||||
payload = notif[1]['payload']
|
||||
def _process_usage_for_updates(raw, body):
|
||||
payload = body['payload']
|
||||
instance_id = payload['instance_id']
|
||||
request_id = notif[1]['_context_request_id']
|
||||
request_id = body['_context_request_id']
|
||||
(usage, new) = STACKDB.get_or_create_instance_usage(instance=instance_id,
|
||||
request_id=request_id)
|
||||
|
||||
@ -252,9 +250,8 @@ def _process_usage_for_updates(raw):
|
||||
STACKDB.save(usage)
|
||||
|
||||
|
||||
def _process_delete(raw):
|
||||
notif = json.loads(raw.json)
|
||||
payload = notif[1]['payload']
|
||||
def _process_delete(raw, body):
|
||||
payload = body['payload']
|
||||
instance_id = payload['instance_id']
|
||||
deleted_at = utils.str_time_to_unix(payload['deleted_at'])
|
||||
values = {
|
||||
@ -272,9 +269,8 @@ def _process_delete(raw):
|
||||
STACKDB.save(delete)
|
||||
|
||||
|
||||
def _process_exists(raw):
|
||||
notif = json.loads(raw.json)
|
||||
payload = notif[1]['payload']
|
||||
def _process_exists(raw, body):
|
||||
payload = body['payload']
|
||||
instance_id = payload['instance_id']
|
||||
launched_at = utils.str_time_to_unix(payload['launched_at'])
|
||||
launched_range = (launched_at, launched_at+1)
|
||||
@ -283,7 +279,7 @@ def _process_exists(raw):
|
||||
delete = STACKDB.get_instance_delete(instance=instance_id,
|
||||
launched_at__range=launched_range)
|
||||
values = {}
|
||||
values['message_id'] = notif[1]['message_id']
|
||||
values['message_id'] = body['message_id']
|
||||
values['instance'] = instance_id
|
||||
values['launched_at'] = launched_at
|
||||
values['instance_type_id'] = payload['instance_type_id']
|
||||
@ -315,12 +311,12 @@ USAGE_PROCESS_MAPPING = {
|
||||
}
|
||||
|
||||
|
||||
def aggregate_usage(raw):
|
||||
def aggregate_usage(raw, body):
|
||||
if not raw.instance:
|
||||
return
|
||||
|
||||
if raw.event in USAGE_PROCESS_MAPPING:
|
||||
USAGE_PROCESS_MAPPING[raw.event](raw)
|
||||
USAGE_PROCESS_MAPPING[raw.event](raw, body)
|
||||
|
||||
|
||||
def process_raw_data(deployment, args, json_args):
|
||||
@ -347,7 +343,7 @@ def process_raw_data(deployment, args, json_args):
|
||||
STACKDB.save(record)
|
||||
|
||||
aggregate_lifecycle(record)
|
||||
aggregate_usage(record)
|
||||
aggregate_usage(record, body)
|
||||
return record
|
||||
|
||||
|
||||
|
@ -172,7 +172,7 @@ class StacktachRawParsingTestCase(unittest.TestCase):
|
||||
self.mox.StubOutWithMock(views, "aggregate_lifecycle")
|
||||
views.aggregate_lifecycle(raw)
|
||||
self.mox.StubOutWithMock(views, "aggregate_usage")
|
||||
views.aggregate_usage(raw)
|
||||
views.aggregate_usage(raw, dict)
|
||||
self.mox.ReplayAll()
|
||||
views.process_raw_data(deployment, args, json_args)
|
||||
self.mox.VerifyAll()
|
||||
@ -201,7 +201,7 @@ class StacktachRawParsingTestCase(unittest.TestCase):
|
||||
self.mox.StubOutWithMock(views, "aggregate_lifecycle")
|
||||
views.aggregate_lifecycle(raw)
|
||||
self.mox.StubOutWithMock(views, "aggregate_usage")
|
||||
views.aggregate_usage(raw)
|
||||
views.aggregate_usage(raw, dict)
|
||||
self.mox.ReplayAll()
|
||||
views.process_raw_data(deployment, args, json_args)
|
||||
self.mox.VerifyAll()
|
||||
@ -415,7 +415,7 @@ class StacktackUsageParsingTestCase(unittest.TestCase):
|
||||
.AndReturn((usage, True))
|
||||
views.STACKDB.save(usage)
|
||||
self.mox.ReplayAll()
|
||||
views._process_usage_for_new_launch(raw)
|
||||
views._process_usage_for_new_launch(raw, notif[1])
|
||||
self.assertEquals(usage.instance_type_id, '1')
|
||||
self.mox.VerifyAll()
|
||||
|
||||
@ -439,7 +439,7 @@ class StacktackUsageParsingTestCase(unittest.TestCase):
|
||||
views.STACKDB.save(usage)
|
||||
self.mox.ReplayAll()
|
||||
|
||||
views._process_usage_for_updates(raw)
|
||||
views._process_usage_for_updates(raw, notif[1])
|
||||
self.assertEqual(usage.instance, INSTANCE_ID_1)
|
||||
self.assertEqual(usage.request_id, REQUEST_ID_1)
|
||||
self.assertEqual(usage.instance_type_id, '1')
|
||||
@ -465,7 +465,7 @@ class StacktackUsageParsingTestCase(unittest.TestCase):
|
||||
views.STACKDB.save(usage)
|
||||
self.mox.ReplayAll()
|
||||
|
||||
views._process_usage_for_updates(raw)
|
||||
views._process_usage_for_updates(raw, notif[1])
|
||||
self.assertEqual(usage.instance, INSTANCE_ID_1)
|
||||
self.assertEqual(usage.request_id, REQUEST_ID_1)
|
||||
self.assertEqual(usage.instance_type_id, '1')
|
||||
@ -490,7 +490,7 @@ class StacktackUsageParsingTestCase(unittest.TestCase):
|
||||
views.STACKDB.save(usage)
|
||||
self.mox.ReplayAll()
|
||||
|
||||
views._process_usage_for_updates(raw)
|
||||
views._process_usage_for_updates(raw, notif[1])
|
||||
self.assertEqual(usage.instance, INSTANCE_ID_1)
|
||||
self.assertEqual(usage.request_id, REQUEST_ID_1)
|
||||
self.assertEqual(usage.instance_type_id, '2')
|
||||
@ -520,7 +520,7 @@ class StacktackUsageParsingTestCase(unittest.TestCase):
|
||||
views.STACKDB.save(delete)
|
||||
self.mox.ReplayAll()
|
||||
|
||||
views._process_delete(raw)
|
||||
views._process_delete(raw, notif[1])
|
||||
self.assertEqual(delete.instance, INSTANCE_ID_1)
|
||||
self.assertEqual(delete.launched_at, launch_decimal)
|
||||
self.assertEqual(delete.deleted_at, delete_decimal)
|
||||
@ -545,7 +545,7 @@ class StacktackUsageParsingTestCase(unittest.TestCase):
|
||||
views.STACKDB.save(delete)
|
||||
self.mox.ReplayAll()
|
||||
|
||||
views._process_delete(raw)
|
||||
views._process_delete(raw, notif[1])
|
||||
self.assertEqual(delete.instance, INSTANCE_ID_1)
|
||||
self.assertEqual(delete.deleted_at, delete_decimal)
|
||||
self.mox.VerifyAll()
|
||||
@ -580,7 +580,7 @@ class StacktackUsageParsingTestCase(unittest.TestCase):
|
||||
views.STACKDB.create_instance_exists(**exists_values).AndReturn(exists)
|
||||
views.STACKDB.save(exists)
|
||||
self.mox.ReplayAll()
|
||||
views._process_exists(raw)
|
||||
views._process_exists(raw, notif[1])
|
||||
self.mox.VerifyAll()
|
||||
|
||||
def test_process_exists_with_deleted_at(self):
|
||||
@ -619,6 +619,6 @@ class StacktackUsageParsingTestCase(unittest.TestCase):
|
||||
views.STACKDB.create_instance_exists(**exists_values).AndReturn(exists)
|
||||
views.STACKDB.save(exists)
|
||||
self.mox.ReplayAll()
|
||||
views._process_exists(raw)
|
||||
views._process_exists(raw, notif[1])
|
||||
self.mox.VerifyAll()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user