|
1 | 1 | from flask import request |
2 | 2 | from funcy import project |
3 | 3 |
|
4 | | -from redash import models |
| 4 | +from redash import models, utils |
5 | 5 | from redash.handlers.base import ( |
6 | 6 | BaseResource, |
7 | 7 | get_object_or_404, |
|
14 | 14 | view_only, |
15 | 15 | ) |
16 | 16 | from redash.serializers import serialize_alert |
| 17 | +from redash.tasks.alerts import ( |
| 18 | + notify_subscriptions, |
| 19 | + should_notify, |
| 20 | +) |
17 | 21 |
|
18 | 22 |
|
19 | 23 | class AlertResource(BaseResource): |
@@ -43,6 +47,21 @@ def delete(self, alert_id): |
43 | 47 | models.db.session.commit() |
44 | 48 |
|
45 | 49 |
|
| 50 | +class AlertEvaluateResource(BaseResource): |
| 51 | + def post(self, alert_id): |
| 52 | + alert = get_object_or_404(models.Alert.get_by_id_and_org, alert_id, self.current_org) |
| 53 | + require_admin_or_owner(alert.user.id) |
| 54 | + |
| 55 | + new_state = alert.evaluate() |
| 56 | + if should_notify(alert, new_state): |
| 57 | + alert.state = new_state |
| 58 | + alert.last_triggered_at = utils.utcnow() |
| 59 | + models.db.session.commit() |
| 60 | + |
| 61 | + notify_subscriptions(alert, new_state, {}) |
| 62 | + self.record_event({"action": "evaluate", "object_id": alert.id, "object_type": "alert"}) |
| 63 | + |
| 64 | + |
46 | 65 | class AlertMuteResource(BaseResource): |
47 | 66 | def post(self, alert_id): |
48 | 67 | alert = get_object_or_404(models.Alert.get_by_id_and_org, alert_id, self.current_org) |
|
0 commit comments