refactor state to recorder, finish implementing
This commit is contained in:
@ -20,19 +20,24 @@ import (
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/provider/awn"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/provider/wunderground"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
|
||||
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather/recorder"
|
||||
)
|
||||
|
||||
const defUpdatesToKeep = 120
|
||||
|
||||
type AmbientWeather struct {
|
||||
// These providers implement support for the update sent
|
||||
// when either "AmbientWeather" or "Wunderground" are selected
|
||||
// in the "Custom" section of the AWNet app, or the web UI
|
||||
// of an Ambient WeatherHub
|
||||
Config *config.AmbientLocalExporterConfig
|
||||
awnProvider provider.AmbientProvider
|
||||
wuProvider provider.AmbientProvider
|
||||
appCtx context.Context
|
||||
metrics *weather.WeatherMetrics
|
||||
l *zerolog.Logger
|
||||
Config *config.AmbientLocalExporterConfig
|
||||
awnProvider provider.AmbientProvider
|
||||
wuProvider provider.AmbientProvider
|
||||
weatherState *recorder.WeatherRecorder
|
||||
appCtx context.Context
|
||||
metrics *weather.WeatherMetrics
|
||||
l *zerolog.Logger
|
||||
*sync.RWMutex
|
||||
}
|
||||
|
||||
func New(appCtx context.Context, awConfig *config.AmbientLocalExporterConfig) *AmbientWeather {
|
||||
@ -44,11 +49,33 @@ func New(appCtx context.Context, awConfig *config.AmbientLocalExporterConfig) *A
|
||||
|
||||
// Initialize with defaults, set logger from context
|
||||
func (aw *AmbientWeather) Init() *AmbientWeather {
|
||||
tracer := otel.GetTracer(aw.appCtx, "ambientWeather")
|
||||
_, span := tracer.Start(aw.appCtx, "ambientWeather.init",
|
||||
trace.WithAttributes(
|
||||
attribute.String("name", aw.Config.Name),
|
||||
attribute.Bool("grpcEnabled", aw.Config.GRPCEnabled()),
|
||||
attribute.Bool("httpEnabled", aw.Config.HTTPEnabled()),
|
||||
attribute.Int("weatherStations", len(aw.Config.WeatherStations)),
|
||||
))
|
||||
defer span.End()
|
||||
|
||||
aw.awnProvider = &awn.AWNProvider{}
|
||||
aw.wuProvider = &wunderground.WUProvider{}
|
||||
aw.l = zerolog.Ctx(aw.appCtx)
|
||||
|
||||
updatesToKeep := defUpdatesToKeep
|
||||
if aw.Config.UpdatesToKeep != nil && *aw.Config.UpdatesToKeep > 0 {
|
||||
updatesToKeep = *aw.Config.UpdatesToKeep
|
||||
}
|
||||
span.SetAttributes(attribute.Int("updatesToKeep", updatesToKeep))
|
||||
|
||||
aw.weatherState = recorder.NewWeatherRecorder(&recorder.Opts{
|
||||
Ctx: aw.appCtx,
|
||||
KeepLast: updatesToKeep,
|
||||
})
|
||||
|
||||
aw.l.Trace().Any("awConfig", aw.Config).Send()
|
||||
span.SetStatus(codes.Ok, "")
|
||||
return aw
|
||||
}
|
||||
|
||||
@ -78,6 +105,9 @@ func (aw *AmbientWeather) handleProviderRequest(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
aw.Lock()
|
||||
defer aw.Unlock()
|
||||
|
||||
l := zerolog.Ctx(aw.appCtx)
|
||||
tracer := otel.GetTracer(aw.appCtx, p.Name()+".http.handler")
|
||||
|
||||
@ -110,6 +140,9 @@ func (aw *AmbientWeather) handleProviderRequest(
|
||||
updateSpan.SetAttributes(attribute.String("stationName", update.StationConfig.Name))
|
||||
}
|
||||
|
||||
// Record state
|
||||
aw.weatherState.Set(ctx, update)
|
||||
|
||||
// Update metrics
|
||||
aw.metricsUpdate(ctx, p, update)
|
||||
|
||||
@ -242,3 +275,10 @@ func (aw *AmbientWeather) enrichStation(update *weather.WeatherUpdate) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (aw *AmbientWeather) GetState() *recorder.WeatherRecorder {
|
||||
aw.RLock()
|
||||
defer aw.RUnlock()
|
||||
|
||||
return aw.weatherState
|
||||
}
|
||||
|
Reference in New Issue
Block a user