Pass objects instead of IDs to notify method

This commit is contained in:
Alex DeBrie 2016-03-15 15:02:50 +00:00
parent e91610f4b4
commit 04eb37a7f2
6 changed files with 9 additions and 12 deletions

View File

@ -37,7 +37,7 @@ class BaseDestination(object):
def configuration_schema(cls):
return {}
def notify(self, alert_id, query_id, user_id, new_state, app, host, options):
def notify(self, alert, query, user, new_state, app, host, options):
raise NotImplementedError()
@classmethod

View File

@ -15,13 +15,11 @@ class Email(BaseDestination):
def icon(cls):
return 'fa-envelope'
def notify(self, alert_id, query_id, user_id, new_state, app, host, options):
user = models.User.get_by_id(user_id)
def notify(self, alert, query, user, new_state, app, host, options):
recipients = [user.email]
alert = models.Alert.get_by_id(alert_id)
html = """
Check <a href="{host}/alerts/{alert_id}">alert</a> / check <a href="{host}/queries/{query_id}">query</a>.
""".format(host=host, alert_id=alert.id, query_id=query_id)
""".format(host=host, alert_id=alert.id, query_id=query.id)
logging.debug("Notifying: %s", recipients)
try:

View File

@ -23,9 +23,9 @@ class Slack(BaseDestination):
def icon(cls):
return 'fa-slack'
def notify(self, alert_id, query_id, user_id, new_state, app, host, options):
def notify(self, alert, query, user, new_state, app, host, options):
msg = "Check <{host}/alerts/{alert_id}|alert> / check <{host}/queries/{query_id}|query>".format(
host=host, alert_id=alert_id, query_id=query_id)
host=host, alert_id=alert.id, query_id=query.id)
payload = {'text': msg}
try:
resp = requests.post(options.get('url'), data=json.dumps(payload))

View File

@ -32,8 +32,7 @@ class Webhook(BaseDestination):
def icon(cls):
return 'fa-bolt'
def notify(self, alert_id, query_id, user_id, new_state, app, host, options):
alert = models.Alert.get_by_id(alert_id)
def notify(self, alert, query, user, new_state, app, host, options):
try:
data = {
'event': 'alert_state_change',

View File

@ -1091,10 +1091,10 @@ class NotificationDestination(BelongsToOrgMixin, BaseModel):
return notification_destinations
def notify(self, alert_id, query_id, user_id, new_state, app, host):
def notify(self, alert, query, user, new_state, app, host):
schema = get_configuration_schema_for_destination_type(self.type)
self.options.set_schema(schema)
return self.destination.notify(alert_id, query_id, user_id, new_state,
return self.destination.notify(alert, query, user, new_state,
app, host, self.options)

View File

@ -364,7 +364,7 @@ def check_alerts_for_query(self, query_id):
for subscription in alert.subscriptions:
try:
subscription.destination.notify(alert.id, query.id, subscription.user.id, new_state, app, host)
subscription.destination.notify(alert, query, subscription.user, new_state, app, host)
except Exception as e:
logger.warn("Exception: {}".format(e))