From d977fa9175bac6e57e6783adefc203f303ce526d Mon Sep 17 00:00:00 2001 From: blazSmehov Date: Wed, 31 Dec 2025 14:56:17 +0100 Subject: [PATCH] feat: clean db on get values from server --- cmd/location/main.go | 54 +++++++++++++------------- cmd/server/main.go | 4 -- internal/pkg/apiclient/updatedb.go | 49 +++++++++++++++++------ internal/pkg/service/beacon_service.go | 3 +- 4 files changed, 65 insertions(+), 45 deletions(-) diff --git a/cmd/location/main.go b/cmd/location/main.go index befed92..4894339 100644 --- a/cmd/location/main.go +++ b/cmd/location/main.go @@ -132,34 +132,32 @@ func getLikelyLocations(appState *appcontext.AppState, writer *kafka.Writer) { if beacon.LocationConfidence == settings.LocationConfidence && beacon.PreviousConfidentLocation != bestLocName { beacon.LocationConfidence = 0 - - // Why do I need this if I am sending entire structure anyways? who knows - js, err := json.Marshal(model.LocationChange{ - Method: "LocationChange", - BeaconRef: beacon, - Name: beacon.Name, - PreviousLocation: beacon.PreviousConfidentLocation, - NewLocation: bestLocName, - Timestamp: time.Now().Unix(), - }) - - if err != nil { - eMsg := fmt.Sprintf("Error in marshaling: %v", err) - slog.Error(eMsg) - beacon.PreviousConfidentLocation = bestLocName - beacon.PreviousLocation = bestLocName - appState.UpdateBeacon(beacon.ID, beacon) - continue - } - - msg := kafka.Message{ - Value: js, - } - - err = writer.WriteMessages(context.Background(), msg) - if err != nil { - fmt.Println("Error in sending Kafka message") - } + // js, err := json.Marshal(model.LocationChange{ + // Method: "LocationChange", + // BeaconRef: beacon, + // Name: beacon.Name, + // PreviousLocation: beacon.PreviousConfidentLocation, + // NewLocation: bestLocName, + // Timestamp: time.Now().Unix(), + // }) + + // if err != nil { + // eMsg := fmt.Sprintf("Error in marshaling: %v", err) + // slog.Error(eMsg) + // beacon.PreviousConfidentLocation = bestLocName + // beacon.PreviousLocation = bestLocName + // appState.UpdateBeacon(beacon.ID, beacon) + // continue + // } + + // msg := kafka.Message{ + // Value: js, + // } + + // err = writer.WriteMessages(context.Background(), msg) + // if err != nil { + // fmt.Println("Error in sending Kafka message") + // } } beacon.PreviousLocation = bestLocName diff --git a/cmd/server/main.go b/cmd/server/main.go index b74d5e4..62e1169 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -145,10 +145,6 @@ eventLoop: fmt.Printf("Error in saving decoder event for beacon: %s\n", id) continue } - // if err := service.EventToBeaconService(msg, appState, ctx); err != nil { - // eMsg := fmt.Sprintf("Error in writing event change to beacon: %v\n", err) - // slog.Error(eMsg) - // } } } diff --git a/internal/pkg/apiclient/updatedb.go b/internal/pkg/apiclient/updatedb.go index e54a648..a39cb5a 100644 --- a/internal/pkg/apiclient/updatedb.go +++ b/internal/pkg/apiclient/updatedb.go @@ -24,38 +24,65 @@ func UpdateDB(db *gorm.DB, ctx context.Context, cfg *config.Config, writer *kafk if err != nil { return err } + trackers, err := GetTrackers(token, client) if err != nil { fmt.Printf("Error in getting trackers: %+v\n", err) - } + } else { + if err := controller.SendKafkaMessage(writer, &model.ApiUpdate{Method: "DELETE", MAC: "all"}, ctx); err != nil { + fmt.Printf("Error in sending delete all from lookup message: %v", err) + } - if err := controller.SendKafkaMessage(writer, &model.ApiUpdate{Method: "DELETE", MAC: "all"}, ctx); err != nil { - fmt.Printf("Error in sending delete all from lookup message: %v", err) - } + for _, v := range trackers { + apiUpdate := model.ApiUpdate{ + Method: "POST", + ID: v.ID, + MAC: v.MAC, + } - for _, v := range trackers { - apiUpdate := model.ApiUpdate{ - Method: "POST", - ID: v.ID, - MAC: v.MAC, + if err := controller.SendKafkaMessage(writer, &apiUpdate, ctx); err != nil { + fmt.Printf("Error in sending POST kafka message: %v", err) + } } - if err := controller.SendKafkaMessage(writer, &apiUpdate, ctx); err != nil { - fmt.Printf("Error in sending POST kafka message: %v", err) + var ids []string + for _, t := range trackers { + ids = append(ids, t.ID) } + db.Where("id NOT IN ?", ids).Delete(&model.Tracker{}) } gateways, err := GetGateways(token, client) if err != nil { fmt.Printf("Error in getting gateways: %+v\n", err) + } else { + var ids []string + for _, g := range gateways { + ids = append(ids, g.ID) + } + db.Where("id NOT IN ?", ids).Delete(&model.Gateway{}) } + zones, err := GetZones(token, client) if err != nil { fmt.Printf("Error in getting zones: %+v\n", err) + } else { + var ids []string + for _, z := range zones { + ids = append(ids, z.ID) + } + db.Where("id NOT IN ?", ids).Delete(&model.Zone{}) } + trackerZones, err := GetTrackerZones(token, client) if err != nil { fmt.Printf("Error in getting tracker zones: %+v\n", err) + } else { + var ids []string + for _, tz := range trackerZones { + ids = append(ids, tz.ID) + } + db.Where("id NOT IN ?", ids).Delete(&model.TrackerZones{}) } db.Clauses(clause.OnConflict{ diff --git a/internal/pkg/service/beacon_service.go b/internal/pkg/service/beacon_service.go index 7cf732a..4a3bfa4 100644 --- a/internal/pkg/service/beacon_service.go +++ b/internal/pkg/service/beacon_service.go @@ -15,6 +15,7 @@ import ( func LocationToBeaconService(msg model.HTTPLocation, db *gorm.DB, writer *kafka.Writer, ctx context.Context) { if msg.ID == "" { + fmt.Println("empty ID") return } @@ -23,8 +24,6 @@ func LocationToBeaconService(msg model.HTTPLocation, db *gorm.DB, writer *kafka. return } - fmt.Printf("zones: %v\n", zones) - var allowedZones []string for _, z := range zones { allowedZones = append(allowedZones, z.ZoneList...)