UPDATE
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
# Generated by Django 5.1.15 on 2026-04-28 23:30
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("notifications", "0001_initial"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="farmnotification",
|
||||
name="endpoint",
|
||||
field=models.CharField(blank=True, default="", max_length=64),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="farmnotification",
|
||||
name="payload",
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="farmnotification",
|
||||
name="source_alert_id",
|
||||
field=models.CharField(blank=True, db_index=True, default="", max_length=255),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="farmnotification",
|
||||
name="source_metric_type",
|
||||
field=models.CharField(blank=True, default="", max_length=255),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="farmnotification",
|
||||
name="suggested_action",
|
||||
field=models.TextField(blank=True, default=""),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="farmnotification",
|
||||
name="updated_at",
|
||||
field=models.DateTimeField(auto_now=True, default=None),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
||||
@@ -13,9 +13,15 @@ class FarmNotification(models.Model):
|
||||
title = models.CharField(max_length=255)
|
||||
message = models.TextField()
|
||||
level = models.CharField(max_length=32, default="info")
|
||||
endpoint = models.CharField(max_length=64, blank=True, default="")
|
||||
suggested_action = models.TextField(blank=True, default="")
|
||||
source_alert_id = models.CharField(max_length=255, blank=True, default="", db_index=True)
|
||||
source_metric_type = models.CharField(max_length=255, blank=True, default="")
|
||||
payload = models.JSONField(default=dict, blank=True)
|
||||
is_read = models.BooleanField(default=False)
|
||||
metadata = models.JSONField(default=dict, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
db_table = "farm_notifications"
|
||||
|
||||
@@ -4,19 +4,27 @@ from .models import FarmNotification
|
||||
|
||||
|
||||
class FarmNotificationSerializer(serializers.ModelSerializer):
|
||||
id = serializers.IntegerField(read_only=True)
|
||||
farm_uuid = serializers.UUIDField(source="farm.farm_uuid", read_only=True)
|
||||
since_id = serializers.IntegerField(source="id", read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = FarmNotification
|
||||
fields = [
|
||||
"id",
|
||||
"uuid",
|
||||
"farm_uuid",
|
||||
"since_id",
|
||||
"endpoint",
|
||||
"title",
|
||||
"message",
|
||||
"level",
|
||||
"suggested_action",
|
||||
"source_alert_id",
|
||||
"source_metric_type",
|
||||
"payload",
|
||||
"is_read",
|
||||
"metadata",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
]
|
||||
|
||||
@@ -2,6 +2,7 @@ import time
|
||||
|
||||
from django.db import OperationalError, ProgrammingError
|
||||
from django.db.models import Case, IntegerField, QuerySet, Value, When
|
||||
from django.utils import timezone
|
||||
|
||||
|
||||
from farm_hub.models import FarmHub
|
||||
@@ -13,7 +14,19 @@ DEFAULT_POLL_TIMEOUT_SECONDS = 15
|
||||
DEFAULT_POLL_INTERVAL_SECONDS = 1
|
||||
|
||||
|
||||
def create_notification_for_farm_uuid(*, farm_uuid, title, message, level="info", metadata=None):
|
||||
def create_notification_for_farm_uuid(
|
||||
*,
|
||||
farm_uuid,
|
||||
title,
|
||||
message,
|
||||
level="info",
|
||||
endpoint="",
|
||||
suggested_action="",
|
||||
source_alert_id="",
|
||||
source_metric_type="",
|
||||
payload=None,
|
||||
metadata=None,
|
||||
):
|
||||
farm = FarmHub.objects.filter(farm_uuid=farm_uuid).first()
|
||||
if farm is None:
|
||||
raise ValueError("Farm not found.")
|
||||
@@ -24,12 +37,25 @@ def create_notification_for_farm_uuid(*, farm_uuid, title, message, level="info"
|
||||
title=title,
|
||||
message=message,
|
||||
level=level,
|
||||
endpoint=endpoint,
|
||||
suggested_action=suggested_action,
|
||||
source_alert_id=source_alert_id,
|
||||
source_metric_type=source_metric_type,
|
||||
payload=payload or {},
|
||||
metadata=metadata or {},
|
||||
)
|
||||
except (ProgrammingError, OperationalError) as exc:
|
||||
raise ValueError("Notifications table is not migrated.") from exc
|
||||
|
||||
|
||||
def get_recent_notifications_for_farm(*, farm: FarmHub, since_days=3, limit=10) -> QuerySet[FarmNotification]:
|
||||
try:
|
||||
since = timezone.now() - timezone.timedelta(days=max(since_days, 0))
|
||||
return FarmNotification.objects.filter(farm=farm, created_at__gte=since).order_by("-created_at", "-id")[:limit]
|
||||
except (ProgrammingError, OperationalError) as exc:
|
||||
raise ValueError("Notifications table is not migrated.") from exc
|
||||
|
||||
|
||||
def get_notifications_for_farm(*, farm: FarmHub, since_id=None) -> QuerySet[FarmNotification]:
|
||||
try:
|
||||
queryset = FarmNotification.objects.filter(farm=farm)
|
||||
|
||||
Reference in New Issue
Block a user