From 59dc6a1aa49e3050d4a757a5e474791166e297eb Mon Sep 17 00:00:00 2001 From: Ildiko Vancsa Date: Sat, 22 Feb 2014 17:17:38 +0100 Subject: [PATCH] Remove code duplication Part 3 Remove code duplication from impl_mongodb and impl_db2 as both of these drivers use pymongo. This is a multiple step process as there are several differences between the implementation of these drivers. The third step is about to move the alarm history related functions into pymongo_base. Alarm change functions were not implemented in DB2, the expectation is that the MongoDB implementation will work on DB2 also. Change-Id: Icff92ac31829669481b512d5504dcc7e2668eb07 --- ceilometer/storage/impl_mongodb.py | 62 ------------------------------ ceilometer/storage/pymongo_base.py | 62 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 62 deletions(-) diff --git a/ceilometer/storage/impl_mongodb.py b/ceilometer/storage/impl_mongodb.py index 365bc367e..e01e4b700 100644 --- a/ceilometer/storage/impl_mongodb.py +++ b/ceilometer/storage/impl_mongodb.py @@ -758,68 +758,6 @@ class Connection(pymongo_base.Connection): (g, result['groupby'][g]) for g in groupby) if groupby else None) return models.Statistics(**stats_args) - def get_alarm_changes(self, alarm_id, on_behalf_of, - user=None, project=None, type=None, - start_timestamp=None, start_timestamp_op=None, - end_timestamp=None, end_timestamp_op=None): - """Yields list of AlarmChanges describing alarm history - - Changes are always sorted in reverse order of occurrence, given - the importance of currency. - - Segregation for non-administrative users is done on the basis - of the on_behalf_of parameter. This allows such users to have - visibility on both the changes initiated by themselves directly - (generally creation, rule changes, or deletion) and also on those - changes initiated on their behalf by the alarming service (state - transitions after alarm thresholds are crossed). - - :param alarm_id: ID of alarm to return changes for - :param on_behalf_of: ID of tenant to scope changes query (None for - administrative user, indicating all projects) - :param user: Optional ID of user to return changes for - :param project: Optional ID of project to return changes for - :project type: Optional change type - :param start_timestamp: Optional modified timestamp start range - :param start_timestamp_op: Optional timestamp start range operation - :param end_timestamp: Optional modified timestamp end range - :param end_timestamp_op: Optional timestamp end range operation - """ - q = dict(alarm_id=alarm_id) - if on_behalf_of is not None: - q['on_behalf_of'] = on_behalf_of - if user is not None: - q['user_id'] = user - if project is not None: - q['project_id'] = project - if type is not None: - q['type'] = type - if start_timestamp or end_timestamp: - ts_range = pymongo_base.make_timestamp_range(start_timestamp, - end_timestamp, - start_timestamp_op, - end_timestamp_op) - if ts_range: - q['timestamp'] = ts_range - - return self._retrieve_alarm_changes(q, - [("timestamp", - pymongo.DESCENDING)], - None) - - def record_alarm_change(self, alarm_change): - """Record alarm change event. - """ - self.db.alarm_history.insert(alarm_change) - - def query_alarm_history(self, filter_expr=None, orderby=None, limit=None): - """Return an iterable of model.AlarmChange objects. - """ - return self._retrieve_data(filter_expr, - orderby, - limit, - models.AlarmChange) - def get_capabilities(self): """Return an dictionary representing the capabilities of this driver. """ diff --git a/ceilometer/storage/pymongo_base.py b/ceilometer/storage/pymongo_base.py index 9556d58cb..d0abc55b4 100644 --- a/ceilometer/storage/pymongo_base.py +++ b/ceilometer/storage/pymongo_base.py @@ -218,6 +218,11 @@ class Connection(base.Connection): """ self.db.alarm.remove({'alarm_id': alarm_id}) + def record_alarm_change(self, alarm_change): + """Record alarm change event. + """ + self.db.alarm_history.insert(alarm_change) + def get_samples(self, sample_filter, limit=None): """Return an iterable of model.Sample instances. @@ -260,6 +265,55 @@ class Connection(base.Connection): return self._retrieve_alarms(q, [], None) + def get_alarm_changes(self, alarm_id, on_behalf_of, + user=None, project=None, type=None, + start_timestamp=None, start_timestamp_op=None, + end_timestamp=None, end_timestamp_op=None): + """Yields list of AlarmChanges describing alarm history + + Changes are always sorted in reverse order of occurrence, given + the importance of currency. + + Segregation for non-administrative users is done on the basis + of the on_behalf_of parameter. This allows such users to have + visibility on both the changes initiated by themselves directly + (generally creation, rule changes, or deletion) and also on those + changes initiated on their behalf by the alarming service (state + transitions after alarm thresholds are crossed). + + :param alarm_id: ID of alarm to return changes for + :param on_behalf_of: ID of tenant to scope changes query (None for + administrative user, indicating all projects) + :param user: Optional ID of user to return changes for + :param project: Optional ID of project to return changes for + :project type: Optional change type + :param start_timestamp: Optional modified timestamp start range + :param start_timestamp_op: Optional timestamp start range operation + :param end_timestamp: Optional modified timestamp end range + :param end_timestamp_op: Optional timestamp end range operation + """ + q = dict(alarm_id=alarm_id) + if on_behalf_of is not None: + q['on_behalf_of'] = on_behalf_of + if user is not None: + q['user_id'] = user + if project is not None: + q['project_id'] = project + if type is not None: + q['type'] = type + if start_timestamp or end_timestamp: + ts_range = make_timestamp_range(start_timestamp, + end_timestamp, + start_timestamp_op, + end_timestamp_op) + if ts_range: + q['timestamp'] = ts_range + + return self._retrieve_alarm_changes(q, + [("timestamp", + pymongo.DESCENDING)], + None) + def query_samples(self, filter_expr=None, orderby=None, limit=None): return self._retrieve_data(filter_expr, orderby, limit, models.Meter) @@ -268,6 +322,14 @@ class Connection(base.Connection): """ return self._retrieve_data(filter_expr, orderby, limit, models.Alarm) + def query_alarm_history(self, filter_expr=None, orderby=None, limit=None): + """Return an iterable of model.AlarmChange objects. + """ + return self._retrieve_data(filter_expr, + orderby, + limit, + models.AlarmChange) + def _retrieve_data(self, filter_expr, orderby, limit, model): if limit == 0: return []