-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Collect 15min energy metrics #23185
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
Open
andig
wants to merge
13
commits into
master
Choose a base branch
from
chore/metrics
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Collect 15min energy metrics #23185
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
258017a
Collect metrics
andig 8a27bcc
wip
andig a7727f4
wip
andig e9079d6
Merge branch 'master' into chore/metrics
andig 25bd75a
Add entity ids
andig af5d9b9
Ensure home entity exists
andig 306af2c
wip
andig 1a4103e
Merge branch 'master' into chore/metrics
andig 3db952f
wip
andig b94f89e
wip
andig 9a35483
Add entity groups
andig 774d491
wip
andig 960b7ea
Add pos/neg energy
andig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package metrics | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/benbjohnson/clock" | ||
"github.com/samber/lo" | ||
) | ||
|
||
type Accumulator struct { | ||
clock clock.Clock | ||
updated time.Time | ||
posMeter, negMeter *float64 // kWh | ||
Pos float64 `json:"pos"` // kWh | ||
Neg float64 `json:"neg"` // kWh | ||
} | ||
|
||
func WithClock(clock clock.Clock) func(*Accumulator) { | ||
return func(m *Accumulator) { | ||
m.clock = clock | ||
} | ||
} | ||
|
||
func NewAccumulator(opt ...func(*Accumulator)) *Accumulator { | ||
m := &Accumulator{clock: clock.New()} | ||
for _, o := range opt { | ||
o(m) | ||
} | ||
return m | ||
} | ||
|
||
func (m *Accumulator) Updated() time.Time { | ||
return m.updated | ||
} | ||
|
||
func (m *Accumulator) String() string { | ||
b := new(bytes.Buffer) | ||
fmt.Fprintf(b, "Accumulated: %.3fkWh pos, %.3fkWh neg, updated: %v", m.Pos, m.Neg, m.updated.Truncate(time.Second)) | ||
if m.posMeter != nil || m.negMeter != nil { | ||
fmt.Fprintf(b, " meter: ") | ||
if m.posMeter != nil { | ||
fmt.Fprintf(b, " %.3fkWh pos", *m.posMeter) | ||
} | ||
if m.negMeter != nil { | ||
fmt.Fprintf(b, " %.3fkWh pos", *m.negMeter) | ||
} | ||
} | ||
return b.String() | ||
} | ||
|
||
// PosEnergy returns the accumulated energy in kWh | ||
func (m *Accumulator) PosEnergy() float64 { | ||
return m.Pos | ||
} | ||
|
||
// NegEnergy returns the accumulated energy in kWh | ||
func (m *Accumulator) NegEnergy() float64 { | ||
return m.Neg | ||
} | ||
|
||
// AddPosMeterTotal adds the difference to the last total meter value in kWh | ||
func (m *Accumulator) AddPosMeterTotal(v float64) { | ||
defer func() { | ||
m.updated = m.clock.Now() | ||
m.posMeter = lo.ToPtr(v) | ||
}() | ||
|
||
if m.posMeter == nil { | ||
return | ||
} | ||
|
||
m.Pos += v - *m.posMeter | ||
} | ||
|
||
// AddNegMeterTotal adds the difference to the last total meter value in kWh | ||
func (m *Accumulator) AddNegMeterTotal(v float64) { | ||
defer func() { | ||
m.updated = m.clock.Now() | ||
m.negMeter = lo.ToPtr(v) | ||
}() | ||
|
||
if m.negMeter == nil { | ||
return | ||
} | ||
|
||
m.Neg += v - *m.negMeter | ||
} | ||
|
||
// AddPosEnergy adds the given energy in kWh to the positive meter | ||
func (m *Accumulator) AddPosEnergy(v float64) { | ||
defer func() { m.updated = m.clock.Now() }() | ||
|
||
if m.updated.IsZero() { | ||
return | ||
} | ||
|
||
m.Pos += v | ||
} | ||
|
||
// AddNegEnergy adds the given energy in kWh to the negative meter | ||
func (m *Accumulator) AddNegEnergy(v float64) { | ||
defer func() { m.updated = m.clock.Now() }() | ||
|
||
if m.updated.IsZero() { | ||
return | ||
} | ||
|
||
m.Neg += v | ||
} | ||
|
||
// AddPower adds the given power in W, calculating the energy based on the time since the last update | ||
func (m *Accumulator) AddPower(v float64) { | ||
if v >= 0 { | ||
m.AddPosEnergy(v * m.clock.Since(m.updated).Hours() / 1e3) | ||
} else { | ||
m.AddNegEnergy(-v * m.clock.Since(m.updated).Hours() / 1e3) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package metrics | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/evcc-io/evcc/server/db" | ||
) | ||
|
||
const ( | ||
SlotDuration = 15 * time.Minute | ||
|
||
// groups | ||
Virtual = "virtual" | ||
Grid = "grid" | ||
PV = "pv" | ||
|
||
// meters | ||
Home = "home" // virtual home meter | ||
) | ||
|
||
type Collector struct { | ||
entity entity | ||
accu *Accumulator | ||
started time.Time | ||
} | ||
|
||
func NewCollector(group, name string, opt ...func(*Accumulator)) (*Collector, error) { | ||
entity, err := createEntity(group, name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Collector{ | ||
entity: entity, | ||
accu: NewAccumulator(opt...), | ||
}, nil | ||
} | ||
|
||
func createEntity(group, name string) (entity, error) { | ||
entity := entity{ | ||
Group: group, | ||
Name: name, | ||
} | ||
|
||
if err := db.Instance.Where(&entity).FirstOrCreate(&entity).Error; err != nil { | ||
return entity, err | ||
} | ||
|
||
return entity, nil | ||
} | ||
|
||
func (c *Collector) process(fun func()) error { | ||
andig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
now := c.accu.clock.Now() | ||
|
||
if c.accu.updated.IsZero() { | ||
c.started = now | ||
} | ||
|
||
fun() | ||
|
||
if slotStart := now.Truncate(SlotDuration); slotStart.After(c.started) { | ||
// full slot completed | ||
if slotStart.Sub(c.started) == SlotDuration { | ||
if err := c.persist(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
c.started = slotStart | ||
c.accu.Pos = 0 | ||
c.accu.Neg = 0 | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Collector) persist() error { | ||
return persist(c.entity, c.started, c.accu.PosEnergy(), c.accu.NegEnergy()) | ||
} | ||
|
||
func (c *Collector) Profile(from time.Time) (*[96]float64, error) { | ||
return profile(c.entity, from) | ||
} | ||
|
||
func (c *Collector) AddPosEnergy(v float64) error { | ||
return c.process(func() { | ||
c.accu.AddPosEnergy(v) | ||
}) | ||
} | ||
|
||
func (c *Collector) AddNegEnergy(v float64) error { | ||
return c.process(func() { | ||
c.accu.AddNegEnergy(v) | ||
}) | ||
} | ||
|
||
func (c *Collector) AddPosMeterTotal(v float64) error { | ||
return c.process(func() { | ||
c.accu.AddPosMeterTotal(v) | ||
}) | ||
} | ||
|
||
func (c *Collector) AddNegMeterTotal(v float64) error { | ||
return c.process(func() { | ||
c.accu.AddNegMeterTotal(v) | ||
}) | ||
} | ||
|
||
func (c *Collector) AddPower(v float64) error { | ||
return c.process(func() { | ||
c.accu.AddPower(v) | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package metrics | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/benbjohnson/clock" | ||
"github.com/evcc-io/evcc/server/db" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCollectorAddPower(t *testing.T) { | ||
clock := clock.NewMock() | ||
|
||
require.NoError(t, db.NewInstance("sqlite", ":memory:")) | ||
Init() | ||
|
||
col, err := NewCollector("foo", "foo", WithClock(clock)) | ||
require.NoError(t, err) | ||
require.True(t, col.accu.updated.IsZero()) | ||
|
||
clock.Add(5 * time.Minute) | ||
require.NoError(t, col.AddPower(1e3)) | ||
require.False(t, col.accu.updated.IsZero()) | ||
|
||
clock.Add(5 * time.Minute) | ||
require.NoError(t, col.AddPower(1e3)) | ||
require.Equal(t, 1e3*5/60/1e3, col.accu.PosEnergy()) // kWh | ||
|
||
clock.Add(5 * time.Minute) | ||
require.NoError(t, col.AddPower(1e3)) | ||
require.Equal(t, 0.0, col.accu.PosEnergy()) // accumulator reset after 15 minutes | ||
|
||
clock.Add(15 * time.Minute) | ||
require.NoError(t, col.AddPower(1e3)) | ||
require.Equal(t, 0.0, col.accu.PosEnergy()) // accumulator reset after 15 minutes | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming: Pos/Neg ist sehr abstrakt/technisch. Um hier ne saubere Datenbasis zu bekommen werden wir ja auch bei den den Netz-/Batteriezählern das jeweils andere Meter hinzufügen müssen (zumindest mittelfristig). Wie würden wir denn das nennen (
energy[In|Out]
,import|export
,energy & energyReverse
)? Ich fänd gut, wenn wir das Naming dann hier bei den Metriken "kompatibel" haben.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Es ist auch eine Datenbanktabelle! Das Naming entspricht 1:1 der evcc Logik der Energieflüsse. Ich wollte da keine neuen Begriffe erfinden. Import/Export sind mindestens bei der Batterie zweideutig.
Was wir aktuell übrigens (leider) nicht können ist hier mit Zählerständen zu agieren da wir die für 2-Richtungszähler mangels APIs nicht kennen sondern immer nur eine Richtung ausgeprägt haben...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Zwei Einwürfe:
Aktuell erwartetes Energy-Register-Mapping: