Consolidate battery metrics
This commit is contained in:
		
							
								
								
									
										6
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								main.go
									
									
									
									
									
								
							| @@ -16,10 +16,10 @@ func main() { | |||||||
| 	ctx, cncl := signal.NotifyContext(context.Background(), os.Kill, unix.SIGTERM) | 	ctx, cncl := signal.NotifyContext(context.Background(), os.Kill, unix.SIGTERM) | ||||||
| 	defer cncl() | 	defer cncl() | ||||||
|  |  | ||||||
|  | 	// Read config and environment, set up logging, load up | ||||||
|  | 	// an appCtx, and prepare ambient weather local exporter | ||||||
| 	ctx = app.MustSetupConfigAndLogging(ctx) | 	ctx = app.MustSetupConfigAndLogging(ctx) | ||||||
|  | 	aw := ambient.New(ctx).Init() | ||||||
| 	aw := ambient.New(ctx) |  | ||||||
| 	aw.MustInit() |  | ||||||
|  |  | ||||||
| 	awApp := app.App{ | 	awApp := app.App{ | ||||||
| 		AppContext: ctx, | 		AppContext: ctx, | ||||||
|   | |||||||
| @@ -37,10 +37,12 @@ func New(appCtx context.Context) *AmbientWeather { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func (aw *AmbientWeather) MustInit() { | // Initialize with defaults, set logger from context | ||||||
|  | func (aw *AmbientWeather) Init() *AmbientWeather { | ||||||
| 	aw.awnProvider = &awn.AWNProvider{} | 	aw.awnProvider = &awn.AWNProvider{} | ||||||
| 	aw.wuProvider = &wunderground.WUProvider{} | 	aw.wuProvider = &wunderground.WUProvider{} | ||||||
| 	aw.l = zerolog.Ctx(aw.appCtx) | 	aw.l = zerolog.Ctx(aw.appCtx) | ||||||
|  | 	return aw | ||||||
| } | } | ||||||
|  |  | ||||||
| func (aw *AmbientWeather) GetAWNHandlerFunc(appCtx context.Context) func(http.ResponseWriter, *http.Request) { | func (aw *AmbientWeather) GetAWNHandlerFunc(appCtx context.Context) func(http.ResponseWriter, *http.Request) { | ||||||
|   | |||||||
| @@ -39,32 +39,46 @@ func MapAwnUpdate(awnUpdate *AmbientWeatherUpdate) *weather.WeatherUpdate { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return &weather.WeatherUpdate{ | 	return &weather.WeatherUpdate{ | ||||||
| 		StationType:       awnUpdate.StationType, | 		StationType:    awnUpdate.StationType, | ||||||
| 		DateUTC:           &updateTime, | 		DateUTC:        &updateTime, | ||||||
| 		TempOutdoorF:      awnUpdate.TempF, | 		TempOutdoorF:   awnUpdate.TempF, | ||||||
| 		HumidityOudoor:    awnUpdate.Humidity, | 		HumidityOudoor: awnUpdate.Humidity, | ||||||
| 		WindSpeedMPH:      awnUpdate.WindGustMPH, | 		WindSpeedMPH:   awnUpdate.WindGustMPH, | ||||||
| 		WindGustMPH:       awnUpdate.WindGustMPH, | 		WindGustMPH:    awnUpdate.WindGustMPH, | ||||||
| 		MaxDailyGust:      awnUpdate.MaxDailyGust, | 		MaxDailyGust:   awnUpdate.MaxDailyGust, | ||||||
| 		WindDir:           awnUpdate.WindDir, | 		WindDir:        awnUpdate.WindDir, | ||||||
| 		WindDirAvg10m:     awnUpdate.WindDirAVG10m, | 		WindDirAvg10m:  awnUpdate.WindDirAVG10m, | ||||||
| 		UV:                awnUpdate.UV, | 		UV:             awnUpdate.UV, | ||||||
| 		SolarRadiation:    awnUpdate.SolarRadiation, | 		SolarRadiation: awnUpdate.SolarRadiation, | ||||||
| 		HourlyRainIn:      awnUpdate.HourlyRainIn, | 		HourlyRainIn:   awnUpdate.HourlyRainIn, | ||||||
| 		EventRainIn:       awnUpdate.EventRainIn, | 		EventRainIn:    awnUpdate.EventRainIn, | ||||||
| 		DailyRainIn:       awnUpdate.DailyRainIn, | 		DailyRainIn:    awnUpdate.DailyRainIn, | ||||||
| 		WeeklyRainIn:      awnUpdate.WeeklyRainIn, | 		WeeklyRainIn:   awnUpdate.WeeklyRainIn, | ||||||
| 		MonthlyRainIn:     awnUpdate.MonthlyRainIn, | 		MonthlyRainIn:  awnUpdate.MonthlyRainIn, | ||||||
| 		YearlyRainIn:      awnUpdate.YearlyRainIn, | 		YearlyRainIn:   awnUpdate.YearlyRainIn, | ||||||
| 		TotalRainIn:       awnUpdate.TotalRainIn, | 		TotalRainIn:    awnUpdate.TotalRainIn, | ||||||
| 		BattOutdoorSensor: awnUpdate.BattOut, | 		Batteries: []weather.BatteryStatus{ | ||||||
| 		BattIndoorSensor:  awnUpdate.BattIn, | 			{ | ||||||
| 		BattRainSensor:    awnUpdate.BattRain, | 				Component: "OutdoorSensor", | ||||||
| 		BattCO2Sensor:     awnUpdate.BattCO2, | 				Status:    awnUpdate.BattOut, | ||||||
| 		TempIndoorF:       awnUpdate.TempInF, | 			}, | ||||||
| 		HumidityIndoor:    awnUpdate.HumidityIn, | 			{ | ||||||
| 		BaromRelativeIn:   awnUpdate.BaromRelIn, | 				Component: "IndoorSensor", | ||||||
| 		BaromAbsoluteIn:   awnUpdate.BaromAbsIn, | 				Status:    awnUpdate.BattIn, | ||||||
|  | 			}, | ||||||
|  | 			{ | ||||||
|  | 				Component: "RainSensor", | ||||||
|  | 				Status:    awnUpdate.BattRain, | ||||||
|  | 			}, | ||||||
|  | 			{ | ||||||
|  | 				Component: "CO2Sensor", | ||||||
|  | 				Status:    awnUpdate.BattCO2, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 		TempIndoorF:     awnUpdate.TempInF, | ||||||
|  | 		HumidityIndoor:  awnUpdate.HumidityIn, | ||||||
|  | 		BaromRelativeIn: awnUpdate.BaromRelIn, | ||||||
|  | 		BaromAbsoluteIn: awnUpdate.BaromAbsIn, | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -12,31 +12,29 @@ import ( | |||||||
|  |  | ||||||
| type WeatherMetrics struct { | type WeatherMetrics struct { | ||||||
| 	// Weather Metrics | 	// Weather Metrics | ||||||
| 	TempOutdoorF      metric.Float64Gauge | 	TempOutdoorF    metric.Float64Gauge | ||||||
| 	TempIndoorF       metric.Float64Gauge | 	TempIndoorF     metric.Float64Gauge | ||||||
| 	HumidityOudoor    metric.Int64Gauge | 	HumidityOudoor  metric.Int64Gauge | ||||||
| 	HumidityIndoor    metric.Int64Gauge | 	HumidityIndoor  metric.Int64Gauge | ||||||
| 	WindSpeedMPH      metric.Float64Gauge | 	WindSpeedMPH    metric.Float64Gauge | ||||||
| 	WindGustMPH       metric.Float64Gauge | 	WindGustMPH     metric.Float64Gauge | ||||||
| 	MaxDailyGust      metric.Float64Gauge | 	MaxDailyGust    metric.Float64Gauge | ||||||
| 	WindDir           metric.Int64Gauge | 	WindDir         metric.Int64Gauge | ||||||
| 	WindDirAvg10m     metric.Int64Gauge | 	WindDirAvg10m   metric.Int64Gauge | ||||||
| 	UV                metric.Int64Gauge | 	UV              metric.Int64Gauge | ||||||
| 	SolarRadiation    metric.Float64Gauge | 	SolarRadiation  metric.Float64Gauge | ||||||
| 	HourlyRainIn      metric.Float64Gauge | 	HourlyRainIn    metric.Float64Gauge | ||||||
| 	EventRainIn       metric.Float64Gauge | 	EventRainIn     metric.Float64Gauge | ||||||
| 	DailyRainIn       metric.Float64Gauge | 	DailyRainIn     metric.Float64Gauge | ||||||
| 	WeeklyRainIn      metric.Float64Gauge | 	WeeklyRainIn    metric.Float64Gauge | ||||||
| 	MonthlyRainIn     metric.Float64Gauge | 	MonthlyRainIn   metric.Float64Gauge | ||||||
| 	YearlyRainIn      metric.Float64Gauge | 	YearlyRainIn    metric.Float64Gauge | ||||||
| 	TotalRainIn       metric.Float64Gauge | 	TotalRainIn     metric.Float64Gauge | ||||||
| 	BattOutdoorSensor metric.Int64Gauge | 	BatteryStatus   metric.Int64Gauge | ||||||
| 	BattIndoorSensor  metric.Int64Gauge | 	BaromRelativeIn metric.Float64Gauge | ||||||
| 	BattRainSensor    metric.Int64Gauge | 	BaromAbsoluteIn metric.Float64Gauge | ||||||
| 	BaromRelativeIn   metric.Float64Gauge | 	DewPointF       metric.Float64Gauge | ||||||
| 	BaromAbsoluteIn   metric.Float64Gauge | 	WindChillF      metric.Float64Gauge | ||||||
| 	DewPointF         metric.Float64Gauge |  | ||||||
| 	WindChillF        metric.Float64Gauge |  | ||||||
|  |  | ||||||
| 	// Internal Telemetry | 	// Internal Telemetry | ||||||
| 	UpdatesReceived metric.Int64Counter | 	UpdatesReceived metric.Int64Counter | ||||||
| @@ -90,12 +88,8 @@ func MustInitMetrics(appCtx context.Context) *WeatherMetrics { | |||||||
| 		metric.WithDescription("Yearly Rain in Inches")) | 		metric.WithDescription("Yearly Rain in Inches")) | ||||||
| 	wm.TotalRainIn, _ = wm.meter.Float64Gauge("weather_total_rain_in", | 	wm.TotalRainIn, _ = wm.meter.Float64Gauge("weather_total_rain_in", | ||||||
| 		metric.WithDescription("Total Rain in Inches")) | 		metric.WithDescription("Total Rain in Inches")) | ||||||
| 	wm.BattOutdoorSensor, _ = wm.meter.Int64Gauge("weather_batt_outdoor_sensor", | 	wm.BatteryStatus, _ = wm.meter.Int64Gauge("battery_status", | ||||||
| 		metric.WithDescription("Outdoor Equipment Battery")) | 		metric.WithDescription("Per-component battery status")) | ||||||
| 	wm.BattIndoorSensor, _ = wm.meter.Int64Gauge("weather_batt_indoor_sensor", |  | ||||||
| 		metric.WithDescription("Indoor Equipmenet Battery")) |  | ||||||
| 	wm.BattRainSensor, _ = wm.meter.Int64Gauge("weather_batt_rain_sensor", |  | ||||||
| 		metric.WithDescription("Rain Sensor Battery")) |  | ||||||
| 	wm.BaromRelativeIn, _ = wm.meter.Float64Gauge("weather_barometric_pressure_relative_in", | 	wm.BaromRelativeIn, _ = wm.meter.Float64Gauge("weather_barometric_pressure_relative_in", | ||||||
| 		metric.WithDescription("Relative Pressure in Inches of Mercury")) | 		metric.WithDescription("Relative Pressure in Inches of Mercury")) | ||||||
| 	wm.BaromAbsoluteIn, _ = wm.meter.Float64Gauge("weather_barometric_pressure_absolute_in", | 	wm.BaromAbsoluteIn, _ = wm.meter.Float64Gauge("weather_barometric_pressure_absolute_in", | ||||||
| @@ -136,13 +130,18 @@ func (wm *WeatherMetrics) Update(u *WeatherUpdate) { | |||||||
| 	wm.MonthlyRainIn.Record(wm.appCtx, u.MonthlyRainIn, metric.WithAttributeSet(attributes)) | 	wm.MonthlyRainIn.Record(wm.appCtx, u.MonthlyRainIn, metric.WithAttributeSet(attributes)) | ||||||
| 	wm.YearlyRainIn.Record(wm.appCtx, u.YearlyRainIn, metric.WithAttributeSet(attributes)) | 	wm.YearlyRainIn.Record(wm.appCtx, u.YearlyRainIn, metric.WithAttributeSet(attributes)) | ||||||
| 	wm.TotalRainIn.Record(wm.appCtx, u.TotalRainIn, metric.WithAttributeSet(attributes)) | 	wm.TotalRainIn.Record(wm.appCtx, u.TotalRainIn, metric.WithAttributeSet(attributes)) | ||||||
| 	wm.BattOutdoorSensor.Record(wm.appCtx, int64(u.BattOutdoorSensor), metric.WithAttributeSet(attributes)) |  | ||||||
| 	wm.BattIndoorSensor.Record(wm.appCtx, int64(u.BattIndoorSensor), metric.WithAttributeSet(attributes)) |  | ||||||
| 	wm.BattRainSensor.Record(wm.appCtx, int64(u.BattRainSensor), metric.WithAttributeSet(attributes)) |  | ||||||
| 	wm.BaromRelativeIn.Record(wm.appCtx, u.BaromRelativeIn, metric.WithAttributeSet(attributes)) | 	wm.BaromRelativeIn.Record(wm.appCtx, u.BaromRelativeIn, metric.WithAttributeSet(attributes)) | ||||||
| 	wm.BaromAbsoluteIn.Record(wm.appCtx, u.BaromAbsoluteIn, metric.WithAttributeSet(attributes)) | 	wm.BaromAbsoluteIn.Record(wm.appCtx, u.BaromAbsoluteIn, metric.WithAttributeSet(attributes)) | ||||||
| 	wm.DewPointF.Record(wm.appCtx, u.DewPointF, metric.WithAttributeSet(attributes)) | 	wm.DewPointF.Record(wm.appCtx, u.DewPointF, metric.WithAttributeSet(attributes)) | ||||||
| 	wm.WindChillF.Record(wm.appCtx, u.WindChillF, metric.WithAttributeSet(attributes)) | 	wm.WindChillF.Record(wm.appCtx, u.WindChillF, metric.WithAttributeSet(attributes)) | ||||||
|  |  | ||||||
|  | 	// Batteries | ||||||
|  | 	for _, battery := range u.Batteries { | ||||||
|  | 		wm.BatteryStatus.Record(wm.appCtx, int64(battery.Status), | ||||||
|  | 			metric.WithAttributeSet(attributes), | ||||||
|  | 			metric.WithAttributes(attribute.String("component", battery.Component)), | ||||||
|  | 		) | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	wm.UpdatesReceived.Add(wm.appCtx, 1) | 	wm.UpdatesReceived.Add(wm.appCtx, 1) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -7,34 +7,40 @@ import ( | |||||||
| // Stable intermediate struct containing superset of fields | // Stable intermediate struct containing superset of fields | ||||||
| // between AWN and Wunderground style updates from Ambient devices | // between AWN and Wunderground style updates from Ambient devices | ||||||
| type WeatherUpdate struct { | type WeatherUpdate struct { | ||||||
| 	DateUTC           *time.Time | 	DateUTC        *time.Time | ||||||
| 	StationType       string | 	StationType    string | ||||||
| 	TempOutdoorF      float64 | 	TempOutdoorF   float64 | ||||||
| 	TempIndoorF       float64 | 	TempIndoorF    float64 | ||||||
| 	HumidityOudoor    int | 	HumidityOudoor int | ||||||
| 	HumidityIndoor    int | 	HumidityIndoor int | ||||||
| 	WindSpeedMPH      float64 | 	WindSpeedMPH   float64 | ||||||
| 	WindGustMPH       float64 | 	WindGustMPH    float64 | ||||||
| 	MaxDailyGust      float64 | 	MaxDailyGust   float64 | ||||||
| 	WindDir           int | 	WindDir        int | ||||||
| 	WindDirAvg10m     int | 	WindDirAvg10m  int | ||||||
| 	UV                int | 	UV             int | ||||||
| 	SolarRadiation    float64 | 	SolarRadiation float64 | ||||||
| 	HourlyRainIn      float64 | 	HourlyRainIn   float64 | ||||||
| 	EventRainIn       float64 | 	EventRainIn    float64 | ||||||
| 	DailyRainIn       float64 | 	DailyRainIn    float64 | ||||||
| 	WeeklyRainIn      float64 | 	WeeklyRainIn   float64 | ||||||
| 	MonthlyRainIn     float64 | 	MonthlyRainIn  float64 | ||||||
| 	YearlyRainIn      float64 | 	YearlyRainIn   float64 | ||||||
| 	TotalRainIn       float64 | 	TotalRainIn    float64 | ||||||
| 	BattOutdoorSensor int | 	Batteries      []BatteryStatus | ||||||
| 	BattIndoorSensor  int | 	// BattOutdoorSensor int | ||||||
| 	BattRainSensor    int | 	// BattIndoorSensor  int | ||||||
| 	BattCO2Sensor     int | 	// BattRainSensor    int | ||||||
| 	BaromRelativeIn   float64 | 	// BattCO2Sensor     int | ||||||
| 	BaromAbsoluteIn   float64 | 	BaromRelativeIn float64 | ||||||
|  | 	BaromAbsoluteIn float64 | ||||||
| 	// These fields may be calculated | 	// These fields may be calculated | ||||||
| 	// if not otherwise set | 	// if not otherwise set | ||||||
| 	DewPointF  float64 | 	DewPointF  float64 | ||||||
| 	WindChillF float64 | 	WindChillF float64 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | type BatteryStatus struct { | ||||||
|  | 	Component string | ||||||
|  | 	Status    int | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user