package appcontext import ( "encoding/json" "time" "github.com/AFASystems/presence/internal/pkg/kafkaclient" ) // ServiceStatus represents the health of an external service (e.g. Kafka, database). type ServiceStatus struct { Status string `json:"status"` // "up", "down", "unknown" Message string `json:"message,omitempty"` } type BaseHealth struct { Uptime time.Duration `json:"uptime"` ActiveReaders []string `json:"activeReaders" gorm:"type:jsonb"` ActiveWriters []string `json:"activeWriters" gorm:"type:jsonb"` ActiveBeacons []string `json:"activeBeacons" gorm:"type:jsonb"` } type DecoderHealth struct { BaseHealth // current active configs ? dont know yet how } type LocationHealth struct { BaseHealth ActiveSettings []Settings `json:"activeSettings" gorm:"type:jsonb"` } type BridgeHealth struct { BaseHealth } type Health struct { Location LocationHealth `json:"location" gorm:"embedded;embeddedPrefix:loc_"` Decoder DecoderHealth `json:"decoder" gorm:"embedded;embeddedPrefix:dec_"` Bridge BridgeHealth `json:"bridge" gorm:"embedded;embeddedPrefix:brg_"` Kafka ServiceStatus `json:"kafka"` Database ServiceStatus `json:"database"` } func (b *BaseHealth) GetUptime(startTime time.Time) { b.Uptime = time.Since(startTime) } func (b *BaseHealth) GetActiveReaders(m *kafkaclient.KafkaManager) { b.ActiveReaders = m.GetReaders() } func (b *BaseHealth) GetActiveWriters(m *kafkaclient.KafkaManager) { b.ActiveWriters = m.GetWriters() } func (b *BaseHealth) GetActiveBeacons(m *AppState) { beacons := m.GetAllBeacons() for beacon := range beacons { b.ActiveBeacons = append(b.ActiveBeacons, beacon) } } func (d *DecoderHealth) Marshal() ([]byte, error) { return json.Marshal(d) } func (l *LocationHealth) Marshal() ([]byte, error) { return json.Marshal(l) } func (b *BridgeHealth) Marshal() ([]byte, error) { return json.Marshal(b) }