Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support newer version of openstack databases schema #642

Merged
merged 1 commit into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/ralph_scrooge/plugins/collect/_openstack_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def _get_dates_from_to(self, date):
)
return date_from, date_to

def get_usages(self, date, connection_string):
def get_usages(self, date, connection_string, query_config=None):
"""
Return usages from OpenStack for given date.

Expand All @@ -140,12 +140,17 @@ def get_usages(self, date, connection_string):
:return: list of usages
:rtype: list
"""
query_config = query_config or {}
date_from, date_to = self._get_dates_from_to(date)
engine = create_engine(connection_string)
connection = engine.connect()
default_db = connection_string.split('/')[-1]
params = dict(
from_ts=self._format_date(date_from),
to_ts=self._format_date(date_to),
instances_db=query_config.get('instances_db', default_db),
flavors_db=query_config.get('flavors_db', default_db),
flavors_table=query_config.get('flavors_table', 'instance_types')
)
query = self.query.format(**params)
return connection.execute(query)
Expand Down Expand Up @@ -202,8 +207,9 @@ def run_plugin(self, sites, today, **kwargs):
new = total = 0
for site in sites:
logger.info(
"Processing {} {}".format(
"Processing {} {}.{}".format(
self.plugin_name,
site.get('USAGE_PREFIX', ''),
site['WAREHOUSE'],
)
)
Expand All @@ -215,7 +221,9 @@ def run_plugin(self, sites, today, **kwargs):
))
# default warehouse from fixtures
warehouse = Warehouse.objects.get(pk=1)
usages = self.get_usages(today, site['CONNECTION'])
usages = self.get_usages(
today, site['CONNECTION'], site.get('QUERY_CONFIG', {})
)
site_new, site_total = self.save_usages(
usages,
today,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ class NovaInstancePlugin(OpenStackBasePlugin):
), '{to_ts}') as DATETIME),
CAST(GREATEST(instances.created_at, '{from_ts}') as DATETIME)
)) / (60.0 * 60)) as work_time,
instance_types.name AS metric_name
FROM instances
LEFT JOIN instance_types ON instances.instance_type_id=instance_types.id
{flavors_table}.name AS metric_name
FROM {instances_db}.instances
LEFT JOIN {flavors_db}.{flavors_table}
ON instances.instance_type_id={flavors_table}.id
WHERE
instances.created_at <= '{to_ts}' and
(instances.deleted_at IS NULL OR instances.deleted_at >= '{from_ts}')
GROUP BY instances.project_id, instance_type_id;
GROUP BY instances.project_id, instances.instance_type_id;
"""

def _format_date(self, d):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def test_run_plugin(
get_usages_mock.assert_any_call(
self.today,
CEILOMETER_SETTINGS[0]['CONNECTION'],
{}
)
self.assertEquals(get_usages_mock.call_count, 2)
save_usages_mock.assert_any_call(
Expand Down