Stats: fix this year start (#18133)

This commit is contained in:
Michael Geers 2025-01-08 22:40:54 +01:00 • committed by GitHub
parent fc4d185439
commit 879378dc4f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,6 +7,7 @@ import (
"github.com/evcc-io/evcc/core/keys"
"github.com/evcc-io/evcc/server/db"
"github.com/evcc-io/evcc/util"
"github.com/jinzhu/now"
)
// publisher gives access to the site's publish function
@ -33,10 +34,10 @@ func (s *Stats) Update(p publisher) {
}
stats := map[string]map[string]float64{
"30d": s.calculate(30),
"365d": s.calculate(365),
"thisYear": s.calculate(time.Now().YearDay()),
"total": s.calculate(365 * 100), // 100 years
"30d": s.calculate(time.Now().AddDate(0, 0, -30)),
"365d": s.calculate(time.Now().AddDate(0, 0, -365)),
"thisYear": s.calculate(now.BeginningOfYear()),
"total": s.calculate(time.Time{}),
}
p.publish(keys.Statistics, stats)
@ -44,7 +45,7 @@ func (s *Stats) Update(p publisher) {
}
// calculate reads the stats for the last n-days
func (s *Stats) calculate(days int) map[string]float64 {
func (s *Stats) calculate(fromDate time.Time) map[string]float64 {
result := make(map[string]float64)
executeQuery := func(selectClause string, whereClause string, fromDate time.Time, dest interface{}) {
@ -60,7 +61,6 @@ func (s *Stats) calculate(days int) map[string]float64 {
}
}
fromDate := time.Now().AddDate(0, 0, -days)
var solarPercentage, chargedKWh, avgPrice, avgCo2 float64
executeQuery("SUM(charged_kwh * solar_percentage) / SUM(charged_kwh)", "AND solar_percentage IS NOT NULL", fromDate, &solarPercentage)
executeQuery("SUM(charged_kwh)", "AND solar_percentage IS NOT NULL", fromDate, &chargedKWh)