Fix statistics and gorm upgrade (#15569)
This commit is contained in:
parent
aca405b373
commit
4a49b04819
4 changed files with 26 additions and 47 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/evcc-io/evcc/core/keys"
|
||||
|
|
@ -45,52 +46,30 @@ func (s *Stats) Update(p publisher) {
|
|||
func (s *Stats) calculate(days int) map[string]float64 {
|
||||
result := make(map[string]float64)
|
||||
|
||||
// Calculate the date from numberOfDays ago
|
||||
fromDate := time.Now().AddDate(0, 0, -days)
|
||||
|
||||
// Struct to hold the results
|
||||
var dbResult struct {
|
||||
SolarPercentage float64
|
||||
ChargedKWh float64
|
||||
AvgPrice float64
|
||||
AvgCo2 float64
|
||||
}
|
||||
|
||||
// Calculate solar_percentage and total_kwh
|
||||
if err := db.Instance.Raw(`
|
||||
SELECT SUM(charged_kwh * solar_percentage) / SUM(charged_kwh) AS SolarPercentage,
|
||||
SUM(charged_kwh) as ChargedKWh
|
||||
FROM sessions
|
||||
WHERE finished >= ?
|
||||
AND charged_kwh > 0
|
||||
AND solar_percentage IS NOT NULL`, fromDate).Scan(&dbResult).Error; err != nil {
|
||||
s.log.ERROR.Printf("error executing solar stats query: %v", err)
|
||||
}
|
||||
|
||||
// Calculate avg_price
|
||||
if err := db.Instance.Raw(`
|
||||
SELECT SUM(charged_kwh * price_per_kwh) / SUM(charged_kwh) AS AvgPrice
|
||||
FROM sessions
|
||||
WHERE finished >= ?
|
||||
AND charged_kwh > 0
|
||||
AND price_per_kwh IS NOT NULL`, fromDate).Scan(&dbResult).Error; err != nil {
|
||||
s.log.ERROR.Printf("error executing price stats query: %v", err)
|
||||
}
|
||||
|
||||
// Calculate avg_co2
|
||||
if err := db.Instance.Raw(`
|
||||
SELECT SUM(charged_kwh * co2_per_kwh) / SUM(charged_kwh) AS AvgCo2
|
||||
executeQuery := func(selectClause string, whereClause string, fromDate time.Time, dest interface{}) {
|
||||
query := fmt.Sprintf(`
|
||||
SELECT %s
|
||||
FROM sessions
|
||||
WHERE finished >= ?
|
||||
AND charged_kwh > 0
|
||||
AND co2_per_kwh IS NOT NULL`, fromDate).Scan(&dbResult).Error; err != nil {
|
||||
s.log.ERROR.Printf("error executing co2 stats query: %v", err)
|
||||
WHERE finished >= ?
|
||||
AND charged_kwh > 0
|
||||
%s`, selectClause, whereClause)
|
||||
|
||||
if err := db.Instance.Raw(query, fromDate).Scan(dest).Error; err != nil {
|
||||
s.log.ERROR.Printf("error executing query: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
result["solarPercentage"] = dbResult.SolarPercentage
|
||||
result["chargedKWh"] = dbResult.ChargedKWh
|
||||
result["avgPrice"] = dbResult.AvgPrice
|
||||
result["avgCo2"] = dbResult.AvgCo2
|
||||
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)
|
||||
executeQuery("SUM(charged_kwh * price_per_kwh) / SUM(charged_kwh)", "AND price_per_kwh IS NOT NULL", fromDate, &avgPrice)
|
||||
executeQuery("SUM(charged_kwh * co2_per_kwh) / SUM(charged_kwh)", "AND co2_per_kwh IS NOT NULL", fromDate, &avgCo2)
|
||||
|
||||
result["solarPercentage"] = solarPercentage
|
||||
result["chargedKWh"] = chargedKWh
|
||||
result["avgPrice"] = avgPrice
|
||||
result["avgCo2"] = avgCo2
|
||||
|
||||
return result
|
||||
}
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -105,7 +105,7 @@ require (
|
|||
google.golang.org/grpc v1.64.0
|
||||
google.golang.org/protobuf v1.34.2
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
gorm.io/gorm v1.25.10
|
||||
gorm.io/gorm v1.25.11
|
||||
)
|
||||
|
||||
require (
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -915,8 +915,8 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
|
||||
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
|
||||
gorm.io/gorm v1.25.11 h1:/Wfyg1B/je1hnDx3sMkX+gAlxrlZpn6X0BXRlwXlvHg=
|
||||
gorm.io/gorm v1.25.11/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
|
||||
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ test.describe("footer", async () => {
|
|||
});
|
||||
});
|
||||
|
||||
test.describe.skip("statistics values", async () => {
|
||||
test.describe("statistics values", async () => {
|
||||
test("last 30 days", async ({ page }) => {
|
||||
await page.goto("/");
|
||||
await page.getByTestId("savings-button").click();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue