소스 검색

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

master
Blaz Smehov 1 주 전
부모
커밋
7c6e18f3f0
2개의 변경된 파일66개의 추가작업 그리고 8개의 파일을 삭제
  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 파일 보기

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


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


// AppState provides centralized access to application state // AppState provides centralized access to application state
type AppState struct { 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 { 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 // NewAppState creates a new application context AppState with default values
func NewAppState() *AppState { func NewAppState() *AppState {
return &AppState{ return &AppState{
startTime: time.Now(),
beacons: BeaconsList{ beacons: BeaconsList{
Beacons: make(map[string]Beacon), Beacons: make(map[string]Beacon),
}, },
@@ -46,9 +54,60 @@ func NewAppState() *AppState {
beaconsLookup: BeaconsLookup{ beaconsLookup: BeaconsLookup{
Lookup: make(map[string]string), 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 // GetBeacons returns thread-safe access to beacons list
func (m *AppState) GetBeacons() *BeaconsList { func (m *AppState) GetBeacons() *BeaconsList {
m.beacons.Lock.RLock() m.beacons.Lock.RLock()


internal/pkg/model/health.go → internal/pkg/common/appcontext/health.go 파일 보기

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


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


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


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


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


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


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

불러오는 중...
취소
저장