From 3f8c4b60a691e78e8dfd96be3b61f09d5689e826 Mon Sep 17 00:00:00 2001 From: blazSmehov Date: Thu, 7 May 2026 11:30:27 +0200 Subject: [PATCH] fix: alerts --- internal/app/server/events.go | 2 +- internal/pkg/apiclient/data.go | 1 - internal/pkg/service/beacon_service.go | 13 +++++----- scripts/api/alert_status.sh | 36 +++++++++++++++++++------- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/internal/app/server/events.go b/internal/app/server/events.go index d21617b..e5dc17b 100644 --- a/internal/app/server/events.go +++ b/internal/app/server/events.go @@ -56,7 +56,7 @@ func RunEventLoop(ctx context.Context, a *ServerApp) { continue } if msg.Type == "Eddystone" && msg.Battery < 3000 { - service.SendAlert(id, "BatteryLow", a.KafkaManager.GetWriter("alert"), ctx, a.DB) + service.SendAlert(id, "BatteryLow", "", a.KafkaManager.GetWriter("alert"), ctx, a.DB) } case <-beaconTicker.C: var list []model.Tracker diff --git a/internal/pkg/apiclient/data.go b/internal/pkg/apiclient/data.go index 75b9d22..c371852 100644 --- a/internal/pkg/apiclient/data.go +++ b/internal/pkg/apiclient/data.go @@ -36,7 +36,6 @@ func GetGateways(token string, client *http.Client, cfg *config.Config) ([]model if err != nil { return []model.Gateway{}, err } - fmt.Printf("Gateways raw response: %s\n", body) var i []model.Gateway err = json.NewDecoder(bytes.NewReader(body)).Decode(&i) diff --git a/internal/pkg/service/beacon_service.go b/internal/pkg/service/beacon_service.go index 3f542ba..c3c4c48 100644 --- a/internal/pkg/service/beacon_service.go +++ b/internal/pkg/service/beacon_service.go @@ -108,7 +108,7 @@ func LocationToBeaconService(msg model.HTTPLocation, db *gorm.DB, writer *kafka. return } - sendRestrictedZoneAlert(gw.ID, msg.ID, writer, ctx, allowedZones, db) + sendRestrictedZoneAlert(gw.Zone, msg.ID, writer, ctx, allowedZones, db) } func LocationToBeaconServiceAI(msg model.HTTPLocation, db *gorm.DB, writer *kafka.Writer, ctx context.Context) { @@ -165,10 +165,10 @@ func LocationToBeaconServiceAI(msg model.HTTPLocation, db *gorm.DB, writer *kafk return } - sendRestrictedZoneAlert(gw.ID, tracker.ID, writer, ctx, allowedZones, db) + sendRestrictedZoneAlert(gw.Zone, tracker.ID, writer, ctx, allowedZones, db) } -func SendAlert(trackerId, alertType string, writer *kafka.Writer, ctx context.Context, db *gorm.DB) { +func SendAlert(trackerId, alertType, zone string, writer *kafka.Writer, ctx context.Context, db *gorm.DB) { var existingAlert model.Alert result := db.Select("status").Where("tracker_id = ? AND type = ?", trackerId, alertType).Order("timestamp DESC").Limit(1).Find(&existingAlert) @@ -179,6 +179,7 @@ func SendAlert(trackerId, alertType string, writer *kafka.Writer, ctx context.Co Type: alertType, Status: "new", Timestamp: time.Now(), + Zone: zone, } if err := InsertAlert(alert, db, ctx); err != nil { @@ -208,9 +209,9 @@ func SendAlert(trackerId, alertType string, writer *kafka.Writer, ctx context.Co } } -func sendRestrictedZoneAlert(gwId, trackerId string, writer *kafka.Writer, ctx context.Context, allowedZones []string, db *gorm.DB) { - if len(allowedZones) != 0 && !slices.Contains(allowedZones, gwId) { - SendAlert(trackerId, "Restricted zone", writer, ctx, db) +func sendRestrictedZoneAlert(gwZone, trackerId string, writer *kafka.Writer, ctx context.Context, allowedZones []string, db *gorm.DB) { + if len(allowedZones) != 0 && !slices.Contains(allowedZones, gwZone) { + SendAlert(trackerId, "Restricted zone", gwZone, writer, ctx, db) } } diff --git a/scripts/api/alert_status.sh b/scripts/api/alert_status.sh index e214370..8c61f34 100755 --- a/scripts/api/alert_status.sh +++ b/scripts/api/alert_status.sh @@ -1,17 +1,35 @@ #!/bin/bash -# PATCH alert status by alert id. -# Usage: ./alert_status.sh [status] -# alert_id - required, the alert id (e.g. from GET /reslevis/alerts) -# status - optional, default: resolved -# Example: ./alert_status.sh abc-123 resolved -# BASE_URL=http://host:port ./alert_status.sh abc-123 acknowledged +# Find the first "Restricted zone" alert, update its status and operator, +# then list alerts before and after. +# Usage: ./alert_status.sh [status] [operator] +# status - optional, default: resolved +# operator - optional, default: admin +# Example: ./alert_status.sh acknowledged "John Doe" +# BASE_URL=http://host:port ./alert_status.sh resolved admin SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" . "${SCRIPT_DIR}/../_common.sh" -ALERT_ID="${1:?Usage: $0 [status]}" -STATUS="${2:-resolved}" +STATUS="${1:-resolved}" +OPERATOR="${2:-admin}" +echo "=== Alerts before update ===" +ALERTS=$(curl -s -X GET "${BASE_URL}/reslevis/alerts?limit=100&offset=0") +echo "$ALERTS" | jq '.' + +ALERT_ID=$(echo "$ALERTS" | jq -r '[.[] | select(.type == "Restricted zone")] | first | .id') + +if [ -z "$ALERT_ID" ] || [ "$ALERT_ID" = "null" ]; then + echo "No alert with type 'Restricted zone' found." + exit 1 +fi + +echo "" +echo "=== Updating alert ${ALERT_ID} (status=${STATUS}, operator=${OPERATOR}) ===" curl -s -X PATCH "${BASE_URL}/reslevis/alerts/${ALERT_ID}" \ -H "Content-Type: application/json" \ - -d "{\"status\": \"${STATUS}\"}" + -d "{\"status\": \"${STATUS}\", \"operator\": \"${OPERATOR}\"}" +echo "" + echo "" +echo "=== Alerts after update ===" +curl -s -X GET "${BASE_URL}/reslevis/alerts?limit=100&offset=0" | jq '.'