diff --git a/src/ecodash/database.go b/src/ecodash/database.go index 0c44e35..f580b15 100644 --- a/src/ecodash/database.go +++ b/src/ecodash/database.go @@ -75,7 +75,7 @@ func (config *Config) refreshCacheFromPast(pastTime time.Time) error { } defer stmtIgnore.Close() - for key, day := range greenEnergyPercentage { + for key, day := range historyPolledSmartEnergySummation { var stmt *sql.Stmt if greenEnergyPercentage[key].Value != 0 && historyPolledSmartEnergySummation[key].Value != 0 { stmt = stmtReplace diff --git a/src/ecodash/http.go b/src/ecodash/http.go index 7c32f2a..7fbd1b3 100644 --- a/src/ecodash/http.go +++ b/src/ecodash/http.go @@ -155,7 +155,7 @@ func (config *Config) saveAdminForm(c *fiber.Ctx) error { } func averageExcludingCurrentDay(data []float32) float32 { - if len(data) == 0 { + if len(data) <= 1 { return 0 } data = data[:len(data)-1] diff --git a/src/tools/tools.go b/src/tools/tools.go index 9d819c1..3bac1b1 100644 --- a/src/tools/tools.go +++ b/src/tools/tools.go @@ -17,8 +17,12 @@ func Hash(toHash string) string { func TemplateDivide(num1, num2 float32) template.HTML { division := float64(num1 / num2) + if math.IsNaN(division) || division == 0 { + return "0" + } + powerOfTen := int(math.Floor(math.Log10(division))) - if (powerOfTen >= -2 && powerOfTen <= 2) || division == 0 { + if powerOfTen >= -2 && powerOfTen <= 2 { // #nosec G203 // We're only printing floats return template.HTML(strconv.FormatFloat(math.Round(division*100)/100, 'f', -1, 64)) }