Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggregate batteries stats #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions battery.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ package battery

import (
"fmt"
"math"
"strings"
)

Expand Down Expand Up @@ -154,3 +155,32 @@ func getAll(sg func() ([]*Battery, error)) ([]*Battery, error) {
func GetAll() ([]*Battery, error) {
return getAll(systemGetAll)
}

func summarize(sg func() ([]*Battery, error)) (*Battery, error) {
bs, err := sg()
if err != nil {
return nil, err
}
if len(bs) == 1 {
return bs[0], nil
}
res := new(Battery)
for _, battery := range bs {
res.Current += battery.Current
res.Full += battery.Full
res.ChargeRate += battery.ChargeRate
res.Design += battery.Design
res.Voltage = math.Max(res.Voltage, battery.Voltage)
res.DesignVoltage = math.Max(res.DesignVoltage, battery.DesignVoltage)
res.State = State(math.Max(float64(res.State), float64(battery.State)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given how the states are ordered, I think it actually makes sense. Sure, there are weird cases like 1 charging 1 discharging, but in such scenario I'd rather know about the discharging part, I think.

Maybe it would be worth introducing specific states for summarize? Not sure ATM.

What I think would be good is have some more test cases for this, e.g. that for Full and Discharging it actually returns Discharging, etc.

}
return res, nil
}

// Summarize returns aggregated information about all batteries in the system.
//
// If error != nil, it will be either ErrFatal or Errors.
// If error is of type Errors, it is guaranteed that length of both returned slices is the same and that i-th error coresponds with i-th battery structure.
func Summarize() (*Battery, error) {
return summarize(systemGetAll)
}
41 changes: 41 additions & 0 deletions battery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,44 @@ func ExampleGet_errors() {
fmt.Println("Got current battery capacity")
}
}

func TestSummarize(t *testing.T) {
cases := []struct {
batteriesIn []*Battery
batterieOut *Battery
}{{
[]*Battery{{Full: 1}, {Full: 3}},
&Battery{Full: 4},
}, {
[]*Battery{{Current: 3}, {Current: 2}},
&Battery{Current: 5},
}, {
[]*Battery{{Design: 3}, {Design: 1}},
&Battery{Design: 4},
}, {
[]*Battery{{ChargeRate: 3}, {ChargeRate: 1}},
&Battery{ChargeRate: 4},
}, {
[]*Battery{{Voltage: 220}, {Voltage: 110}},
&Battery{Voltage: 220},
}, {
[]*Battery{{DesignVoltage: 220}, {DesignVoltage: 110}},
&Battery{DesignVoltage: 220},
}, {
[]*Battery{{State: Empty}, {State: Charging}},
&Battery{State: Charging},
},
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say this bracket should go to the previous line, for consistency.


for i, c := range cases {
f := func() ([]*Battery, error) {
return c.batteriesIn, nil
}
batterie, _ := summarize(f)

if !reflect.DeepEqual(batterie, c.batterieOut) {
t.Errorf("%d: %v != %v", i, batterie, c.batterieOut)
}

}
}