Explorar el Código

feat: finalize services Health status

master
Blaz Smehov hace 1 semana
padre
commit
f7e4f76933
Se han modificado 12 ficheros con 39 adiciones y 12 borrados
  1. BIN
      bridge
  2. BIN
      decoder
  3. +0
    -2
      internal/app/decoder/app.go
  4. +3
    -3
      internal/app/server/events.go
  5. +2
    -0
      internal/app/server/routes.go
  6. +26
    -1
      internal/pkg/common/appcontext/context.go
  7. +0
    -1
      internal/pkg/common/appcontext/health.go
  8. +6
    -1
      internal/pkg/common/utils/beacons.go
  9. +1
    -1
      internal/pkg/database/database.go
  10. +1
    -3
      internal/pkg/decoder/process.go
  11. BIN
      location
  12. BIN
      server

BIN
bridge Ver fichero


BIN
decoder Ver fichero


+ 0
- 2
internal/app/decoder/app.go Ver fichero

@@ -2,7 +2,6 @@ package decoder

import (
"context"
"fmt"
"log/slog"
"sync"
"time"
@@ -84,7 +83,6 @@ func (a *DecoderApp) Run(ctx context.Context) {
continue
}
case msg := <-a.ChRaw:
fmt.Println("msg: ", msg)
decoder.ProcessIncoming(msg, a.AppState, a.KafkaManager.GetWriter("alertbeacons"), a.ParserRegistry)
case msg := <-a.ChParser:
switch msg.ID {


+ 3
- 3
internal/app/server/events.go Ver fichero

@@ -23,11 +23,11 @@ func RunEventLoop(ctx context.Context, a *ServerApp) {
case <-ctx.Done():
return
case msg := <-a.ChHealthLocation:
slog.Info("location health", "health", msg)
a.AppState.UpdateLocationHealth(msg)
case msg := <-a.ChHealthDecoder:
slog.Info("decoder health", "health", msg)
a.AppState.UpdateDecoderHealth(msg)
case msg := <-a.ChHealthBridge:
slog.Info("bridge health", "health", msg)
a.AppState.UpdateBridgeHealth(msg)
case msg := <-a.ChLoc:
switch msg.Method {
case "Standard":


+ 2
- 0
internal/app/server/routes.go Ver fichero

@@ -59,6 +59,8 @@ func (a *ServerApp) RegisterRoutes() http.Handler {
// Tracks
r.HandleFunc("/reslevis/getTracks/{id}", controller.TracksListController(a.DB, a.ctx)).Methods("GET")

r.HandleFunc("/reslevis/health", controller.HealthController(a.AppState, a.ctx)).Methods("GET")

chain := middleware.Recovery(middleware.Logging(middleware.RequestID(middleware.CORS(nil, nil, nil)(r))))
return chain
}

+ 26
- 1
internal/pkg/common/appcontext/context.go Ver fichero

@@ -4,6 +4,7 @@ import (
"fmt"
"log/slog"
"os"
"sync"
"time"

"github.com/AFASystems/presence/internal/pkg/kafkaclient"
@@ -18,6 +19,7 @@ type AppState struct {
beaconsLookup BeaconsLookup
health Health
startTime time.Time
mu sync.RWMutex
}

func getEnv(key, def string) string {
@@ -52,7 +54,6 @@ func NewAppState() *AppState {
Lookup: make(map[string]string),
},
health: Health{
ID: "1",
Location: LocationHealth{
BaseHealth: BaseHealth{
Uptime: 0,
@@ -81,6 +82,30 @@ func NewAppState() *AppState {
}
}

func (m *AppState) GetHealth() Health {
m.mu.RLock()
defer m.mu.RUnlock()
return m.health
}

func (m *AppState) UpdateLocationHealth(h LocationHealth) {
m.mu.Lock()
m.health.Location = h
m.mu.Unlock()
}

func (m *AppState) UpdateDecoderHealth(h DecoderHealth) {
m.mu.Lock()
m.health.Decoder = h
m.mu.Unlock()
}

func (m *AppState) UpdateBridgeHealth(h BridgeHealth) {
m.mu.Lock()
m.health.Bridge = h
m.mu.Unlock()
}

func (m *AppState) GetLocationHealth(k *kafkaclient.KafkaManager) ([]byte, error) {
m.health.Location.GetUptime(m.startTime)
m.health.Location.GetActiveReaders(k)


+ 0
- 1
internal/pkg/common/appcontext/health.go Ver fichero

@@ -29,7 +29,6 @@ type BridgeHealth struct {
}

type Health struct {
ID string `json:"id" gorm:"primaryKey"`
Location LocationHealth `gorm:"embedded;embeddedPrefix:loc_"`
Decoder DecoderHealth `gorm:"embedded;embeddedPrefix:dec_"`
Bridge BridgeHealth `gorm:"embedded;embeddedPrefix:brg_"`


+ 6
- 1
internal/pkg/common/utils/beacons.go Ver fichero

@@ -1,6 +1,8 @@
package utils

import (
"fmt"

"github.com/AFASystems/presence/internal/pkg/common/appcontext"
"github.com/AFASystems/presence/internal/pkg/model"
)
@@ -38,16 +40,18 @@ func RemoveFlagBytes(b []byte) []byte {
}

// Generate event based on the Beacon type
func LoopADStructures(b []byte, i [][2]int, id string, parserRegistry *model.ParserRegistry) appcontext.BeaconEvent {
func LoopADStructures(b []byte, i [][2]int, id string, parserRegistry *model.ParserRegistry, beacon string) appcontext.BeaconEvent {
be := appcontext.BeaconEvent{}
for _, r := range i {
ad := b[r[0]:r[1]]
if !isValidADStructure(ad) {
fmt.Println("invalid ad structure: ", beacon)
break
}
for name, parser := range parserRegistry.ParserList {
if parser.CanParse(ad) {
event, ok := parser.Parse(name, ad)
fmt.Println("parser can parse: ", name)
if ok {
event.ID = id
event.Name = id
@@ -55,6 +59,7 @@ func LoopADStructures(b []byte, i [][2]int, id string, parserRegistry *model.Par
}
}
}
// fmt.Println("no parser can parse: ", beacon)
}

return be


+ 1
- 1
internal/pkg/database/database.go Ver fichero

@@ -26,7 +26,7 @@ func Connect(cfg *config.Config) (*gorm.DB, error) {
return nil, err
}

if err := db.AutoMigrate(&model.Gateway{}, model.Zone{}, model.TrackerZones{}, model.Tracker{}, model.Config{}, appcontext.Settings{}, model.Tracks{}, &model.Alert{}, appcontext.Health{}); err != nil {
if err := db.AutoMigrate(&model.Gateway{}, model.Zone{}, model.TrackerZones{}, model.Tracker{}, model.Config{}, appcontext.Settings{}, model.Tracks{}, &model.Alert{}); err != nil {
return nil, err
}



+ 1
- 3
internal/pkg/decoder/process.go Ver fichero

@@ -30,8 +30,6 @@ func DecodeBeacon(adv appcontext.BeaconAdvertisement, appState *appcontext.AppSt
return nil
}

fmt.Println("beacon: ", beacon)

b, err := hex.DecodeString(beacon)
if err != nil {
return err
@@ -39,7 +37,7 @@ func DecodeBeacon(adv appcontext.BeaconAdvertisement, appState *appcontext.AppSt

b = utils.RemoveFlagBytes(b)
indices := utils.ParseADFast(b)
event := utils.LoopADStructures(b, indices, id, registry)
event := utils.LoopADStructures(b, indices, id, registry, beacon)

if event.ID == "" {
return nil


BIN
location Ver fichero


BIN
server Ver fichero


Cargando…
Cancelar
Guardar