Skip to content

Commit

Permalink
Add quirks support
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Hanselmann <[email protected]>
  • Loading branch information
hansmi committed Sep 24, 2022
1 parent 56b62b7 commit 01beff5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
23 changes: 12 additions & 11 deletions luxws-exporter/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
18 changes: 12 additions & 6 deletions luxws-exporter/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
}
})
}
}
Expand Down
5 changes: 5 additions & 0 deletions luxws-exporter/quirks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

type quirks struct {
missingSuppliedHeat bool
}

0 comments on commit 01beff5

Please sign in to comment.