Selaa lähdekoodia

chore: move health to app context to solve the circular dependency, appContext is now used to collect all the statistics

master
Blaz Smehov 1 viikko sitten
vanhempi
commit
7c6e18f3f0
2 muutettua tiedostoa jossa 66 lisäystä ja 8 poistoa
  1. +63
    -4
      internal/pkg/common/appcontext/context.go
  2. +3
    -4
      internal/pkg/common/appcontext/health.go

+ 63
- 4
internal/pkg/common/appcontext/context.go Näytä tiedosto

@@ -4,16 +4,23 @@ import (
"fmt"
"log/slog"
"os"
"sync"
"time"

"github.com/AFASystems/presence/internal/pkg/kafkaclient"
"github.com/mitchellh/mapstructure"
)

// AppState provides centralized access to application state
type AppState struct {
beacons BeaconsList
settings Settings
beaconEvents BeaconEventList
beaconsLookup BeaconsLookup
beacons BeaconsList
settings Settings
beaconEvents BeaconEventList
beaconsLookup BeaconsLookup
locationHealth LocationHealth
decoderHealth DecoderHealth
bridgeHealth BridgeHealth
startTime time.Time
}

func getEnv(key, def string) string {
@@ -26,6 +33,7 @@ func getEnv(key, def string) string {
// NewAppState creates a new application context AppState with default values
func NewAppState() *AppState {
return &AppState{
startTime: time.Now(),
beacons: BeaconsList{
Beacons: make(map[string]Beacon),
},
@@ -46,9 +54,60 @@ func NewAppState() *AppState {
beaconsLookup: BeaconsLookup{
Lookup: make(map[string]string),
},
locationHealth: LocationHealth{
BaseHealth: BaseHealth{
Lock: sync.RWMutex{},
Uptime: 0,
ActiveReaders: []string{},
ActiveWriters: []string{},
ActiveBeacons: []string{},
},
},
decoderHealth: DecoderHealth{
BaseHealth: BaseHealth{
Lock: sync.RWMutex{},
Uptime: 0,
ActiveReaders: []string{},
ActiveWriters: []string{},
ActiveBeacons: []string{},
},
},
bridgeHealth: BridgeHealth{
BaseHealth: BaseHealth{
Lock: sync.RWMutex{},
Uptime: 0,
ActiveReaders: []string{},
ActiveWriters: []string{},
ActiveBeacons: []string{},
},
},
}
}

func (m *AppState) GetLocationHealth(k *kafkaclient.KafkaManager) *LocationHealth {
m.locationHealth.GetUptime(m.startTime)
m.locationHealth.GetActiveReaders(k)
m.locationHealth.GetActiveWriters(k)
m.locationHealth.GetActiveBeacons(m)
return &m.locationHealth
}

func (m *AppState) GetDecoderHealth(k *kafkaclient.KafkaManager) *DecoderHealth {
m.decoderHealth.GetUptime(m.startTime)
m.decoderHealth.GetActiveReaders(k)
m.decoderHealth.GetActiveWriters(k)
m.decoderHealth.GetActiveBeacons(m)
return &m.decoderHealth
}

func (m *AppState) GetBridgeHealth(k *kafkaclient.KafkaManager) *BridgeHealth {
m.bridgeHealth.GetUptime(m.startTime)
m.bridgeHealth.GetActiveReaders(k)
m.bridgeHealth.GetActiveWriters(k)
m.bridgeHealth.GetActiveBeacons(m)
return &m.bridgeHealth
}

// GetBeacons returns thread-safe access to beacons list
func (m *AppState) GetBeacons() *BeaconsList {
m.beacons.Lock.RLock()


internal/pkg/model/health.go → internal/pkg/common/appcontext/health.go Näytä tiedosto

@@ -1,11 +1,10 @@
package model
package appcontext

import (
"encoding/json"
"sync"
"time"

"github.com/AFASystems/presence/internal/pkg/common/appcontext"
"github.com/AFASystems/presence/internal/pkg/kafkaclient"
)

@@ -24,7 +23,7 @@ type DecoderHealth struct {

type LocationHealth struct {
BaseHealth
ActiveSettings []appcontext.Settings `json:"activeSettings"`
ActiveSettings []Settings `json:"activeSettings"`
}

type BridgeHealth struct {
@@ -49,7 +48,7 @@ func (b *BaseHealth) GetActiveWriters(m *kafkaclient.KafkaManager) {
b.Lock.Unlock()
}

func (b *BaseHealth) GetActiveBeacons(m *appcontext.AppState) {
func (b *BaseHealth) GetActiveBeacons(m *AppState) {
b.Lock.Lock()
beacons := m.GetAllBeacons()
for beacon := range beacons {

Ladataan…
Peruuta
Tallenna