|
|
|
@@ -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() |
|
|
|
|