From 01beff57673a2f2634538bd7980bb833760d2e71 Mon Sep 17 00:00:00 2001 From: Michael Hanselmann Date: Sat, 24 Sep 2022 10:05:28 +0200 Subject: [PATCH] Add quirks support Signed-off-by: Michael Hanselmann --- luxws-exporter/collector.go | 23 ++++++++++++----------- luxws-exporter/collector_test.go | 18 ++++++++++++------ luxws-exporter/quirks.go | 5 +++++ 3 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 luxws-exporter/quirks.go diff --git a/luxws-exporter/collector.go b/luxws-exporter/collector.go index b3435a8..f9f9789 100644 --- a/luxws-exporter/collector.go +++ b/luxws-exporter/collector.go @@ -29,7 +29,7 @@ func findContentItem(r *luxwsclient.ContentRoot, name string) (*luxwsclient.Cont return found, nil } -type contentCollectFunc func(chan<- prometheus.Metric, *luxwsclient.ContentRoot) error +type contentCollectFunc func(chan<- prometheus.Metric, *luxwsclient.ContentRoot, *quirks) error type collector struct { sem *semaphore.Weighted @@ -126,7 +126,7 @@ func (c *collector) parseValue(text string) (float64, string, error) { return c.terms.ParseMeasurement(text) } -func (c *collector) collectInfo(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot) error { +func (c *collector) collectInfo(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot, q *quirks) error { var swVersion, opMode, heatOutputUnit string var heatOutputValue float64 var hpType []string @@ -272,40 +272,41 @@ func (c *collector) collectTimetable(ch chan<- prometheus.Metric, desc *promethe return nil } -func (c *collector) collectTemperatures(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot) error { +func (c *collector) collectTemperatures(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot, _ *quirks) error { return c.collectMeasurements(ch, c.temperatureDesc, content, c.terms.NavTemperatures) } -func (c *collector) collectOperatingDuration(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot) error { +func (c *collector) collectOperatingDuration(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot, _ *quirks) error { return c.collectDurations(ch, c.operatingDurationDesc, content, c.terms.NavOpHours, c.terms.HoursImpulsesRe) } -func (c *collector) collectElapsedTime(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot) error { +func (c *collector) collectElapsedTime(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot, _ *quirks) error { return c.collectDurations(ch, c.elapsedDurationDesc, content, c.terms.NavElapsedTimes, nil) } -func (c *collector) collectInputs(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot) error { +func (c *collector) collectInputs(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot, _ *quirks) error { return c.collectMeasurements(ch, c.inputDesc, content, c.terms.NavInputs) } -func (c *collector) collectOutputs(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot) error { +func (c *collector) collectOutputs(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot, _ *quirks) error { return c.collectMeasurements(ch, c.outputDesc, content, c.terms.NavOutputs) } -func (c *collector) collectSuppliedHeat(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot) error { +func (c *collector) collectSuppliedHeat(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot, _ *quirks) error { return c.collectMeasurements(ch, c.suppliedHeatDesc, content, c.terms.NavHeatQuantity) } -func (c *collector) collectLatestError(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot) error { +func (c *collector) collectLatestError(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot, _ *quirks) error { return c.collectTimetable(ch, c.latestErrorDesc, content, c.terms.NavErrorMemory) } -func (c *collector) collectLatestSwitchOff(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot) error { +func (c *collector) collectLatestSwitchOff(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot, _ *quirks) error { return c.collectTimetable(ch, c.switchOffDesc, content, c.terms.NavSwitchOffs) } func (c *collector) collectAll(ch chan<- prometheus.Metric, content *luxwsclient.ContentRoot) error { var err error + var q quirks for _, fn := range []contentCollectFunc{ c.collectInfo, @@ -318,7 +319,7 @@ func (c *collector) collectAll(ch chan<- prometheus.Metric, content *luxwsclient c.collectLatestError, c.collectLatestSwitchOff, } { - multierr.AppendInto(&err, fn(ch, content)) + multierr.AppendInto(&err, fn(ch, content, &q)) } return err diff --git a/luxws-exporter/collector_test.go b/luxws-exporter/collector_test.go index 93e659b..99344e9 100644 --- a/luxws-exporter/collector_test.go +++ b/luxws-exporter/collector_test.go @@ -67,11 +67,13 @@ func TestCollectWebSocketParts(t *testing.T) { }) for _, tc := range []struct { - name string - fn contentCollectFunc - input *luxwsclient.ContentRoot - want string - wantErr error + name string + fn contentCollectFunc + input *luxwsclient.ContentRoot + quirks quirks + want string + wantErr error + wantQuirks quirks }{ { name: "info empty", @@ -451,10 +453,14 @@ luxws_latest_switchoff{reason="bbb"} 1585954800 a := &adapter{ c: c, collect: func(ch chan<- prometheus.Metric) error { - return tc.fn(ch, tc.input) + return tc.fn(ch, tc.input, &tc.quirks) }, } a.collectAndCompare(t, tc.want, tc.wantErr) + + if diff := cmp.Diff(tc.wantQuirks, tc.quirks, cmp.AllowUnexported(quirks{})); diff != "" { + t.Errorf("Quirks diff (-want +got):\n%s", diff) + } }) } } diff --git a/luxws-exporter/quirks.go b/luxws-exporter/quirks.go new file mode 100644 index 0000000..13ea3bb --- /dev/null +++ b/luxws-exporter/quirks.go @@ -0,0 +1,5 @@ +package main + +type quirks struct { + missingSuppliedHeat bool +}