From f289bde4254f173ca97e406f85cb092631ee2782 Mon Sep 17 00:00:00 2001 From: Andrey Kolkov Date: Wed, 3 Feb 2021 12:06:33 +0400 Subject: [PATCH] Added methods: - trigger: search, get_metrics, get_throttling - notification fetch paginated --- moira_client/models/notification.py | 17 ++++++++- moira_client/models/trigger.py | 58 ++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/moira_client/models/notification.py b/moira_client/models/notification.py index da64fba..239f6a6 100644 --- a/moira_client/models/notification.py +++ b/moira_client/models/notification.py @@ -11,11 +11,24 @@ def fetch_all(self): Returns all notifications :return: list of dict + :raises: ResponseStructureError + """ + result = self.fetch(start=0, end=-1) + return result + + def fetch(self, start, end): + """ + Gets a paginated list of notifications + :return: list of dict + + :param start + :param end + :raises: ResponseStructureError """ params = { - 'start': 0, - 'end': -1 + 'start': start, + 'end': end } result = self._client.get(self._full_path(), params=params) if 'list' not in result: diff --git a/moira_client/models/trigger.py b/moira_client/models/trigger.py index 99856cb..ba6cb8a 100644 --- a/moira_client/models/trigger.py +++ b/moira_client/models/trigger.py @@ -262,6 +262,25 @@ def check_exists(self): set(self.tags) == set(trigger.tags): return trigger + def get_metrics(self, _from, to): + """ + Get metrics associated with certain trigger + + :param _from: The start period of metrics to get. Example : -1hour + :param to: The end period of metrics to get. Example : now + + :return: Metrics for trigger + """ + try: + params = { + 'from': _from, + 'to': to, + } + result = self._client.get('trigger/' + self.id + '/metrics', params=params) + return result + except InvalidJSONError: + return [] + def delete_metric(self, metric_name): """ Deletes metric from last check and all trigger pattern metrics @@ -333,6 +352,29 @@ def fetch_by_id(self, trigger_id): elif not 'trigger_id' in result: raise ResponseStructureError("invalid api response", result) + def search(self, only_problems, page, text): + """ + Search triggers + + :param only_problems: Restricts the result to errors only. Example: false + :param page: Defines the number of the displayed page. E.g, page=2 would display the 2nd page. Example: 1 + :param text: Query to perform a search for. Example: cpu + + :return: matching triggers list + + :raises: ResponseStructureError + """ + params = { + 'onlyProblems': only_problems, + 'page': page, + 'text': text, + } + result = self._client.get(self._full_path(), params=params) + if 'list' not in result: + raise ResponseStructureError("list doesn't exist in response", result) + + return result + def delete(self, trigger_id): """ Delete trigger by trigger id @@ -346,6 +388,21 @@ def delete(self, trigger_id): except InvalidJSONError: return True + def get_throttling(self, trigger_id): + """ + Get a trigger with its throttling i.e its next allowed message time + + :param trigger_id: str trigger id + :return: trigger throttle value or None + """ + try: + result = self._client.get(self._full_path(trigger_id + '/throttling')) + if 'throttling' in result: + return result['throttling'] + return None + except InvalidJSONError: + return None + def reset_throttling(self, trigger_id): """ Resets throttling by trigger id @@ -385,7 +442,6 @@ def remove_metric(self, trigger_id, metric): except InvalidJSONError: return False - def is_exist(self, trigger): """ Check whether trigger exists or not