From 879378dc4f5d2e98bc959de064ab04edf325b94c Mon Sep 17 00:00:00 2001 From: Michael Geers Date: Wed, 8 Jan 2025 22:40:54 +0100 Subject: [PATCH] Stats: fix this year start (#18133) --- core/stats.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/stats.go b/core/stats.go index ea4a082e0..d224e357e 100644 --- a/core/stats.go +++ b/core/stats.go @@ -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)