Skip to content

Commit

Permalink
fix(alerts): add alert log admin, fixes some issues with script. (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
ollz272 authored Apr 30, 2023
1 parent 96bb784 commit 3e86a80
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
25 changes: 24 additions & 1 deletion apps/alerts/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import admin

from alerts.models import Alert
from alerts.models import Alert, AlertLog


# Register your models here.
Expand All @@ -27,3 +27,26 @@ class AlertAdmin(admin.ModelAdmin):
},
),
]


@admin.register(AlertLog)
class AlertLogAdmin(admin.ModelAdmin):
list_filter = ["user", "addressed"]
readonly_fields = ["created", "updated"]
list_display = ["alert", "created", "updated", "addressed"]

fieldsets = [
(
"Alert Log Info",
{
"fields": ["user", "alert", "addressed"],
},
),
(
"Meta Data",
{
"classes": ["collapse"],
"fields": ["created", "updated"],
},
),
]
17 changes: 11 additions & 6 deletions apps/alerts/management/commands/scan_for_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ def handle(self, *args, **options):
alert_logs = []
# TODO have a way of automatically resolving logs if condition is "unmet"
for alert in Alert.objects.all().select_related("user", "plant", "sensor"):
if alert.lower_threshold and alert.latest_data_point <= alert.latest_data_point.data:
if alert.alert_logs.order_by("created").addressed is True:
logger.info(f"Condition met for alert {alert}")
self.stdout.write(f"Checking condition on {alert}")
if alert.lower_threshold and alert.latest_data_point.data <= alert.lower_threshold:
latest_log = alert.alert_logs.order_by("-created").first()
if not latest_log or latest_log.addressed:
self.stdout.write(f"Condition met for alert {alert}")
alert_logs.append(AlertLog(user=alert.user, alert=alert))
elif alert.lower_threshold and alert.latest_data_point >= alert.latest_data_point.data:
if alert.alert_logs.order_by("created").addressed is True:
logger.info(f"Condition met for alert {alert}")
elif alert.upper_threshold and alert.latest_data_point.data >= alert.upper_threshold:
latest_log = alert.alert_logs.order_by("-created").first()
if not latest_log or latest_log.addressed:
self.stdout.write(f"Condition met for alert {alert}")
alert_logs.append(AlertLog(user=alert.user, alert=alert))
else:
self.stdout.write(f"No conditions met for {alert}")

AlertLog.objects.bulk_create(alert_logs)
2 changes: 1 addition & 1 deletion apps/alerts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Alert(models.Model):

@cached_property
def latest_data_point(self):
return self.sensor.data_points.order_by("time").first()
return self.sensor.plant_data.order_by("time").first()

def __str__(self):
return self.name
Expand Down

0 comments on commit 3e86a80

Please sign in to comment.