Finishing unordered-deletes, modifying db queries for low resolution timestamps from Nova

This commit is contained in:
Andrew Melton 2013-02-01 11:07:13 -05:00
parent 1d8e18e865
commit 05b4cb38dd
4 changed files with 51 additions and 9 deletions

View File

@ -1,43 +1,71 @@
import models
def get_or_create_deployment(name):
return models.Deployment.objects.get_or_create(name=name)
def create_rawdata(**kwargs):
return models.RawData(**kwargs)
def create_lifecycle(**kwargs):
return models.Lifecycle(**kwargs)
def find_lifecycles(**kwargs):
return models.Lifecycle.objects.select_related().filter(**kwargs)
def create_timing(**kwargs):
return models.Timing(**kwargs)
def find_timings(**kwargs):
return models.Timing.objects.select_related().filter(**kwargs)
def create_request_tracker(**kwargs):
return models.RequestTracker(**kwargs)
def find_request_trackers(**kwargs):
return models.RequestTracker.objects.filter(**kwargs)
def create_instance_usage(**kwargs):
return models.InstanceUsage(**kwargs)
def get_or_create_instance_usage(**kwargs):
return models.InstanceUsage.objects.get_or_create(**kwargs)
def get_instance_usage(**kwargs):
return models.InstanceUsage.objects.get(**kwargs)
usage = None
try:
usage = models.InstanceUsage.objects.get(**kwargs)
except models.InstanceUsage.DoesNotExist:
pass
return usage
def create_instance_delete(**kwargs):
return models.InstanceDeletes(**kwargs)
def get_instance_delete(**kwargs):
delete = None
try:
delete = models.InstanceDeletes.objects.get(**kwargs)
except models.InstanceDeletes.DoesNotExist:
pass
return delete
def create_instance_exists(**kwargs):
return models.InstanceExists(**kwargs)
def save(obj):
obj.save()

View File

@ -103,10 +103,8 @@ class InstanceExists(models.Model):
]
instance = models.CharField(max_length=50, null=True,
blank=True, db_index=True)
#launched_at = models.IntegerField(null=True, db_index=True)
launched_at = models.DecimalField(null=True, max_digits=20,
decimal_places=6)
#deleted_at = models.IntegerField(null=True, db_index=True)
deleted_at = models.DecimalField(null=True, max_digits=20,
decimal_places=6)
message_id = models.CharField(max_length=50, null=True,
@ -120,6 +118,7 @@ class InstanceExists(models.Model):
default=PENDING)
raw = models.ForeignKey(RawData, related_name='+', null=True)
usage = models.ForeignKey(InstanceUsage, related_name='+', null=True)
delete = models.ForeignKey(InstanceDeletes, related_name='+', null=True)
class Timing(models.Model):

View File

@ -270,16 +270,21 @@ def _process_exists(raw):
notif = json.loads(raw.json)
payload = notif[1]['payload']
instance_id = payload['instance_id']
launched_at = payload['launched_at']
launched_at = str_time_to_unix(launched_at)
launched_at = str_time_to_unix(payload['launched_at'])
launched_range = (launched_at, launched_at+1)
usage = STACKDB.get_instance_usage(instance=instance_id,
launched_at=launched_at)
launched_at__range=launched_range)
delete = STACKDB.get_instance_delete(instance=instance_id,
launched_at__range=launched_range)
values = {}
values['message_id'] = notif[1]['message_id']
values['instance'] = instance_id
values['launched_at'] = launched_at
values['instance_type_id'] = payload['instance_type_id']
values['usage'] = usage
if usage:
values['usage'] = usage
if delete:
values['delete'] = delete
values['raw'] = raw
deleted_at = payload.get('deleted_at')

View File

@ -537,9 +537,13 @@ class StacktackUsageParsingTestCase(unittest.TestCase):
raw = utils.create_raw(self.mox, current_decimal, event=event,
json_str=json_str)
usage = self.mox.CreateMockAnything()
launched_range = (launch_decimal, launch_decimal+1)
views.STACKDB.get_instance_usage(instance=INSTANCE_ID_1,
launched_at=launch_decimal)\
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,
@ -569,9 +573,14 @@ class StacktackUsageParsingTestCase(unittest.TestCase):
raw = utils.create_raw(self.mox, current_decimal, event=event,
json_str=json_str)
usage = self.mox.CreateMockAnything()
launched_range = (launch_decimal, launch_decimal+1)
views.STACKDB.get_instance_usage(instance=INSTANCE_ID_1,
launched_at=launch_decimal)\
launched_at__range=launched_range)\
.AndReturn(usage)
delete = self.mox.CreateMockAnything()
views.STACKDB.get_instance_delete(instance=INSTANCE_ID_1,
launched_at__range=launched_range)\
.AndReturn(delete)
exists_values = {
'message_id': MESSAGE_ID_1,
'instance': INSTANCE_ID_1,
@ -579,6 +588,7 @@ class StacktackUsageParsingTestCase(unittest.TestCase):
'deleted_at': deleted_decimal,
'instance_type_id': '1',
'usage': usage,
'delete': delete,
'raw': raw,
}
exists = self.mox.CreateMockAnything()