Bring the begin/end checking before the storage module
In storage module, the parameters(begin and end) of the function get_tenants and get_total is datetime.datetime format and always needed. So they should be checked before storage module. Change-Id: I54e788086b36da3a03ab5640c77988185a4ab690
This commit is contained in:
parent
cd994fb0c6
commit
b3e6efa30e
|
@ -24,6 +24,7 @@ from wsme import types as wtypes
|
|||
import wsmeext.pecan as wsme_pecan
|
||||
|
||||
from cloudkitty.common import policy
|
||||
from cloudkitty import utils as ck_utils
|
||||
|
||||
|
||||
class ReportController(rest.RestController):
|
||||
|
@ -44,6 +45,12 @@ class ReportController(rest.RestController):
|
|||
|
||||
"""
|
||||
policy.enforce(pecan.request.context, 'report:list_tenants', {})
|
||||
|
||||
if not begin:
|
||||
begin = ck_utils.get_month_start()
|
||||
if not end:
|
||||
end = ck_utils.get_next_month()
|
||||
|
||||
storage = pecan.request.storage_backend
|
||||
tenants = storage.get_tenants(begin, end)
|
||||
return tenants
|
||||
|
@ -58,6 +65,12 @@ class ReportController(rest.RestController):
|
|||
|
||||
"""
|
||||
policy.enforce(pecan.request.context, 'report:get_total', {})
|
||||
|
||||
if not begin:
|
||||
begin = ck_utils.get_month_start()
|
||||
if not end:
|
||||
end = ck_utils.get_next_month()
|
||||
|
||||
storage = pecan.request.storage_backend
|
||||
# FIXME(sheeprine): We should filter on user id.
|
||||
# Use keystone token information by default but make it overridable and
|
||||
|
|
|
@ -23,6 +23,7 @@ from oslo_utils import importutils as i_utils
|
|||
from cloudkitty import config # noqa
|
||||
from cloudkitty import service
|
||||
from cloudkitty import storage
|
||||
from cloudkitty import utils as ck_utils
|
||||
from cloudkitty import write_orchestrator
|
||||
|
||||
CONF = cfg.CONF
|
||||
|
@ -49,6 +50,10 @@ class DBCommand(object):
|
|||
|
||||
def generate(self):
|
||||
if not CONF.command.tenant:
|
||||
if not CONF.command.begin:
|
||||
CONF.command.begin = ck_utils.get_month_start()
|
||||
if not CONF.command.end:
|
||||
CONF.command.end = ck_utils.get_next_month()
|
||||
tenants = self._storage.get_tenants(CONF.command.begin,
|
||||
CONF.command.end)
|
||||
else:
|
||||
|
@ -64,6 +69,10 @@ class DBCommand(object):
|
|||
wo.process()
|
||||
|
||||
def tenants_list(self):
|
||||
if not CONF.command.begin:
|
||||
CONF.command.begin = ck_utils.get_month_start()
|
||||
if not CONF.command.end:
|
||||
CONF.command.end = ck_utils.get_next_month()
|
||||
tenants = self._storage.get_tenants(CONF.command.begin,
|
||||
CONF.command.end)
|
||||
print('Tenant list:')
|
||||
|
|
|
@ -178,7 +178,7 @@ class BaseStorage(object):
|
|||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_total(self, begin=None, end=None, tenant_id=None, service=None):
|
||||
def get_total(self, begin, end, tenant_id=None, service=None):
|
||||
"""Return the current total.
|
||||
|
||||
:param begin: When to start filtering.
|
||||
|
@ -192,7 +192,7 @@ class BaseStorage(object):
|
|||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def get_tenants(self, begin=None, end=None):
|
||||
def get_tenants(self, begin, end):
|
||||
"""Return the list of rated tenants.
|
||||
|
||||
:param begin: When to start filtering.
|
||||
|
|
|
@ -258,12 +258,8 @@ class GnocchiStorage(storage.BaseStorage):
|
|||
# gnocchi always returns measures ordered by timestamp
|
||||
return ck_utils.dt2ts(dateutil.parser.parse(r[-1][0]))
|
||||
|
||||
def get_total(self, begin=None, end=None, tenant_id=None, service=None):
|
||||
def get_total(self, begin, end, tenant_id=None, service=None):
|
||||
# Get total rate in timeframe from gnocchi
|
||||
if not begin:
|
||||
begin = ck_utils.get_month_start()
|
||||
if not end:
|
||||
end = ck_utils.get_next_month()
|
||||
metric = "total.cost"
|
||||
if service:
|
||||
metric = service + ".cost"
|
||||
|
@ -282,11 +278,7 @@ class GnocchiStorage(storage.BaseStorage):
|
|||
return sum([measure[2] for measure in r])
|
||||
return 0
|
||||
|
||||
def get_tenants(self, begin=None, end=None):
|
||||
if not begin:
|
||||
begin = ck_utils.get_month_start()
|
||||
if not end:
|
||||
end = ck_utils.get_next_month()
|
||||
def get_tenants(self, begin, end):
|
||||
# We need to pass a query to force a post in gnocchi client metric
|
||||
# aggregation, so we use one that always meets
|
||||
query = {'=': {'type': 'cloudkitty_state'}}
|
||||
|
|
|
@ -83,13 +83,7 @@ class SQLAlchemyStorage(storage.BaseStorage):
|
|||
if r:
|
||||
return ck_utils.dt2ts(r.begin)
|
||||
|
||||
def get_total(self, begin=None, end=None, tenant_id=None, service=None):
|
||||
# Boundary calculation
|
||||
if not begin:
|
||||
begin = ck_utils.get_month_start()
|
||||
if not end:
|
||||
end = ck_utils.get_next_month()
|
||||
|
||||
def get_total(self, begin, end, tenant_id=None, service=None):
|
||||
session = db.get_session()
|
||||
q = session.query(
|
||||
sqlalchemy.func.sum(self.frame_model.rate).label('rate'))
|
||||
|
@ -105,13 +99,7 @@ class SQLAlchemyStorage(storage.BaseStorage):
|
|||
rate = q.scalar()
|
||||
return rate
|
||||
|
||||
def get_tenants(self, begin=None, end=None):
|
||||
# Boundary calculation
|
||||
if not begin:
|
||||
begin = ck_utils.get_month_start()
|
||||
if not end:
|
||||
end = ck_utils.get_next_month()
|
||||
|
||||
def get_tenants(self, begin, end):
|
||||
session = db.get_session()
|
||||
q = utils.model_query(
|
||||
self.frame_model,
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
#
|
||||
import copy
|
||||
|
||||
import mock
|
||||
import sqlalchemy
|
||||
import testscenarios
|
||||
|
||||
|
@ -335,14 +334,6 @@ class StorageTest(tests.TestCase):
|
|||
service='compute')
|
||||
self.assertEqual(0.84, total)
|
||||
|
||||
@mock.patch.object(ck_utils, 'utcnow',
|
||||
return_value=ck_utils.ts2dt(samples.INITIAL_TIMESTAMP))
|
||||
def test_get_total_no_filter(self, patch_utcnow_mock):
|
||||
self.insert_data()
|
||||
total = self.storage.get_total()
|
||||
self.assertEqual(1.9473999999999998, total)
|
||||
self.assertEqual(2, patch_utcnow_mock.call_count)
|
||||
|
||||
# Tenants
|
||||
def test_get_empty_tenant_with_nothing_in_storage(self):
|
||||
tenants = self.storage.get_tenants(
|
||||
|
|
Loading…
Reference in New Issue