Просмотр исходного кода

feat: clean db on get values from server

master
Blaz Smehov 1 месяц назад
Родитель
Сommit
d977fa9175
4 измененных файлов: 65 добавлений и 45 удалений
  1. +26
    -28
      cmd/location/main.go
  2. +0
    -4
      cmd/server/main.go
  3. +38
    -11
      internal/pkg/apiclient/updatedb.go
  4. +1
    -2
      internal/pkg/service/beacon_service.go

+ 26
- 28
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


+ 0
- 4
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)
// }
}
}



+ 38
- 11
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{


+ 1
- 2
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...)


Загрузка…
Отмена
Сохранить