Get last collection time from valid projects
If some projects are ignored, we should not take them into consideration when calculating the last collection time. Change-Id: I44393101fd655ab7dea542493c53245616ec3c4a
This commit is contained in:
parent
4961755e83
commit
efc880799b
|
@ -118,11 +118,11 @@ class CollectorService(service.Service):
|
||||||
return
|
return
|
||||||
|
|
||||||
projects = openstack.get_projects()
|
projects = openstack.get_projects()
|
||||||
project_ids = [p['id'] for p in projects]
|
|
||||||
valid_projects = filter_projects(projects)
|
valid_projects = filter_projects(projects)
|
||||||
|
project_ids = [p['id'] for p in valid_projects]
|
||||||
|
|
||||||
# For new created project, we use the earliest last collection time
|
# For new created project, we use the earliest last collection time
|
||||||
# among existing projects as the start time.
|
# among existing valid projects as the start time.
|
||||||
last_collect = db_api.get_last_collect(project_ids).last_collected
|
last_collect = db_api.get_last_collect(project_ids).last_collected
|
||||||
|
|
||||||
end = datetime.utcnow().replace(minute=0, second=0, microsecond=0)
|
end = datetime.utcnow().replace(minute=0, second=0, microsecond=0)
|
||||||
|
|
|
@ -108,17 +108,14 @@ class CollectorTest(base.DistilWithDbTestCase):
|
||||||
@mock.patch('distil.common.openstack.get_projects')
|
@mock.patch('distil.common.openstack.get_projects')
|
||||||
def test_last_collect_new_project(self, mock_get_projects, mock_cclient,
|
def test_last_collect_new_project(self, mock_get_projects, mock_cclient,
|
||||||
mock_collect_usage):
|
mock_collect_usage):
|
||||||
self.override_config('collector', include_tenants=['project_3'])
|
# Assume project_2 is a new project that doesn't exist in distil db.
|
||||||
|
|
||||||
# Assume project_3 is a new project that doesn't exist in distil db.
|
|
||||||
mock_get_projects.return_value = [
|
mock_get_projects.return_value = [
|
||||||
{'id': '111', 'name': 'project_1', 'description': ''},
|
{'id': '111', 'name': 'project_1', 'description': ''},
|
||||||
{'id': '222', 'name': 'project_2', 'description': ''},
|
{'id': '222', 'name': 'project_2', 'description': ''},
|
||||||
{'id': '333', 'name': 'project_3', 'description': ''},
|
|
||||||
]
|
]
|
||||||
|
|
||||||
# Insert 3 projects in the database, including one project which is
|
# Insert project_0 and project_1 in the database, project_0 is not in
|
||||||
# not in keystone.
|
# keystone anymore.
|
||||||
project_0_collect = datetime(2017, 5, 17, 19)
|
project_0_collect = datetime(2017, 5, 17, 19)
|
||||||
db_api.project_add(
|
db_api.project_add(
|
||||||
{
|
{
|
||||||
|
@ -137,22 +134,54 @@ class CollectorTest(base.DistilWithDbTestCase):
|
||||||
},
|
},
|
||||||
project_1_collect
|
project_1_collect
|
||||||
)
|
)
|
||||||
project_2_collect = datetime(2017, 5, 17, 21)
|
|
||||||
|
svc = collector.CollectorService()
|
||||||
|
svc.collect_usage()
|
||||||
|
|
||||||
|
self.assertEqual(2, mock_collect_usage.call_count)
|
||||||
|
mock_collect_usage.assert_called_with(
|
||||||
|
{'id': '222', 'name': 'project_2', 'description': ''},
|
||||||
|
[(project_1_collect, project_1_collect + timedelta(hours=1))]
|
||||||
|
)
|
||||||
|
|
||||||
|
@mock.patch(
|
||||||
|
'distil.collector.ceilometer.CeilometerCollector.collect_usage')
|
||||||
|
@mock.patch('distil.common.openstack.get_ceilometer_client')
|
||||||
|
@mock.patch('distil.common.openstack.get_projects')
|
||||||
|
def test_last_collect_ignore_project(self, mock_get_projects, mock_cclient,
|
||||||
|
mock_collect_usage):
|
||||||
|
self.override_config('collector', ignore_tenants=['project_2'])
|
||||||
|
|
||||||
|
mock_get_projects.return_value = [
|
||||||
|
{'id': '111', 'name': 'project_1', 'description': ''},
|
||||||
|
{'id': '222', 'name': 'project_2', 'description': ''},
|
||||||
|
]
|
||||||
|
|
||||||
|
project1_time = datetime(2017, 5, 17, 20)
|
||||||
|
db_api.project_add(
|
||||||
|
{
|
||||||
|
'id': '111',
|
||||||
|
'name': 'project_1',
|
||||||
|
'description': '',
|
||||||
|
},
|
||||||
|
project1_time
|
||||||
|
)
|
||||||
|
project2_time = datetime(2017, 5, 17, 19)
|
||||||
db_api.project_add(
|
db_api.project_add(
|
||||||
{
|
{
|
||||||
'id': '222',
|
'id': '222',
|
||||||
'name': 'project_2',
|
'name': 'project_2',
|
||||||
'description': '',
|
'description': '',
|
||||||
},
|
},
|
||||||
project_2_collect
|
project2_time
|
||||||
)
|
)
|
||||||
|
|
||||||
svc = collector.CollectorService()
|
svc = collector.CollectorService()
|
||||||
svc.collect_usage()
|
svc.collect_usage()
|
||||||
|
|
||||||
mock_collect_usage.assert_called_once_with(
|
mock_collect_usage.assert_called_once_with(
|
||||||
{'id': '333', 'name': 'project_3', 'description': ''},
|
{'id': '111', 'name': 'project_1', 'description': ''},
|
||||||
[(project_1_collect, project_1_collect + timedelta(hours=1))]
|
[(project1_time, project1_time + timedelta(hours=1))]
|
||||||
)
|
)
|
||||||
|
|
||||||
@mock.patch('distil.common.openstack.get_ceilometer_client')
|
@mock.patch('distil.common.openstack.get_ceilometer_client')
|
||||||
|
|
Loading…
Reference in New Issue