Skip to content

Commit

Permalink
Added methods:
Browse files Browse the repository at this point in the history
 - trigger: search, get_metrics, get_throttling
 - notification fetch paginated
  • Loading branch information
Andrey Kolkov committed Feb 3, 2021
1 parent 90f83e5 commit f289bde
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
17 changes: 15 additions & 2 deletions moira_client/models/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
58 changes: 57 additions & 1 deletion moira_client/models/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f289bde

Please sign in to comment.