From 60be387b978cb32a8367589051ff5065ca10b79b Mon Sep 17 00:00:00 2001 From: Manali Latkar Date: Fri, 21 Mar 2014 15:20:33 +0530 Subject: [PATCH] Adding host and deployment info to missing exists entries in the nova usage audit --- reports/nova_usage_audit.py | 17 ++++++++++++++--- stacktach/models.py | 13 ++++++++----- tests/unit/test_models.py | 7 +++---- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/reports/nova_usage_audit.py b/reports/nova_usage_audit.py index fc4dc9a..e6c76ea 100644 --- a/reports/nova_usage_audit.py +++ b/reports/nova_usage_audit.py @@ -29,6 +29,7 @@ sys.path.append(os.environ.get('STACKTACH_INSTALL_DIR', '/stacktach')) import usage_audit +from stacktach.models import InstanceUsage from stacktach import datetime_to_decimal as dt from stacktach import models from stacktach.reconciler import Reconciler @@ -116,14 +117,24 @@ def _audit_launches_to_exists(launches, exists, beginning): rec = reconciler.missing_exists_for_instance(*args) msg = "Couldn't find exists for launch (%s, %s)" msg = msg % (instance, expected['launched_at']) - fails.append(['Launch', expected['id'], msg, 'Y' if rec else 'N']) + launched_at = dt.dt_from_decimal(expected['launched_at']) + usage = InstanceUsage.find(instance, launched_at)[0] + host = usage.host() + deployment = usage.deployment() + fails.append(['Launch', expected['id'], msg, + 'Y' if rec else 'N', host, deployment]) else: rec = False if reconciler: args = (launches[0]['id'], beginning) rec = reconciler.missing_exists_for_instance(*args) msg = "No exists for instance (%s)" % instance - fails.append(['Launch', '-', msg, 'Y' if rec else 'N']) + launched_at = dt.dt_from_decimal(launches[0]['launched_at']) + usage = InstanceUsage.find(instance, launched_at)[0] + host = usage.host() + deployment = usage.deployment() + fails.append(['Launch', '-', msg, 'Y' if rec else 'N', host, + deployment]) return fails @@ -233,7 +244,7 @@ def store_results(start, end, summary, details): 'created': dt.dt_to_decimal(datetime.datetime.utcnow()), 'period_start': start, 'period_end': end, - 'version': 6, + 'version': 7, 'name': 'nova usage audit' } diff --git a/stacktach/models.py b/stacktach/models.py index f17f40f..6a4e0cc 100644 --- a/stacktach/models.py +++ b/stacktach/models.py @@ -175,11 +175,14 @@ class InstanceUsage(models.Model): rax_options = models.TextField(null=True, blank=True) def deployment(self): - raws = RawData.objects.filter(request_id=self.request_id) - if raws.count() == 0: - return False - raw = raws[0] - return raw.deployment + return self.latest_raw_for_request_id().deployment.name + + def latest_raw_for_request_id(self): + return RawData.objects.filter( + request_id=self.request_id).order_by('-id')[0] + + def host(self): + return self.latest_raw_for_request_id().host @staticmethod def find(instance, launched_at): diff --git a/tests/unit/test_models.py b/tests/unit/test_models.py index f2c7a03..b9928c4 100644 --- a/tests/unit/test_models.py +++ b/tests/unit/test_models.py @@ -20,12 +20,11 @@ from datetime import datetime import unittest -from django.db.models import Q import mox -from stacktach.models import RawData, GlanceRawData, GenericRawData, ImageDeletes, InstanceExists, ImageExists -from tests.unit.utils import IMAGE_UUID_1 -from stacktach import datetime_to_decimal as dt, models from stacktach.models import RawData, GlanceRawData, GenericRawData +from stacktach.models import ImageDeletes, InstanceExists, ImageExists +from tests.unit.utils import IMAGE_UUID_1 +from stacktach import datetime_to_decimal as dt from tests.unit import StacktachBaseTestCase