From e9125b783c920892ba7554d91f94352e4f40d925 Mon Sep 17 00:00:00 2001 From: MassiveBox Date: Wed, 7 Dec 2022 17:54:46 +0100 Subject: [PATCH] SQLite Initial Implementation This is the first, most basic implementation of a SQLite database for caching. Future commits will make it much more optimized and able to efficiently store data for periods longer than 8 days. --- .gitignore | 3 ++- cache.go | 60 ++++++++++++++++++++++++++++-------------------------- config.go | 19 +++++++++++++++++ go.mod | 1 + http.go | 4 ++-- main.go | 2 +- 6 files changed, 56 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 64ca34e..9ef1227 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ config.json cache.json go.sum -.idea \ No newline at end of file +.idea +database.db \ No newline at end of file diff --git a/cache.go b/cache.go index 82cb705..2f528b3 100644 --- a/cache.go +++ b/cache.go @@ -1,18 +1,16 @@ package main import ( - "encoding/json" "fmt" - "os" "time" ) type CacheEntry struct { - Date string `json:"date""` - GreenEnergyPercentage float32 `json:"green_energy_percentage"` - PolledSmartEnergySummation float32 `json:"polled_smart_energy_summation"` + Date int64 + GreenEnergyPercentage float32 + PolledSmartEnergySummation float32 } -type CacheFile []CacheEntry +type CacheData []CacheEntry func (config Config) updateCache() { @@ -36,37 +34,41 @@ func (config Config) updateCache() { return } - var cacheEntries []CacheEntry + _, err = config.db.Exec("DELETE FROM cache") + if err != nil { + fmt.Println("Error deleting previous records to cache: -", err.Error()) + return + } + for key, day := range greenEnergyPercentage { - cacheEntries = append(cacheEntries, CacheEntry{ - Date: day.DayTime.Format("02/01"), - GreenEnergyPercentage: greenEnergyPercentage[key].Value, - PolledSmartEnergySummation: historyPolledSmartEnergySummation[key].Value, - }) - } - - out, _ := json.Marshal(cacheEntries) - - err = os.WriteFile("cache.json", out, 0666) - if err != nil { - fmt.Println("Error saving cached data to file -" + err.Error()) + _, err := config.db.Exec("INSERT INTO cache(time,green_energy_percentage,energy_consumption) VALUES (?,?,?);", day.DayTime.Unix(), greenEnergyPercentage[key].Value, historyPolledSmartEnergySummation[key].Value) + if err != nil { + fmt.Println("Error adding record to cache: -", err.Error()) + return + } } } -func readCache() (CacheFile, error) { +func (config Config) readCache() (CacheData, error) { - data, err := os.ReadFile("cache.json") + rows, err := config.db.Query("SELECT time, green_energy_percentage, energy_consumption FROM cache") if err != nil { - return CacheFile{}, err + return CacheData{}, err + } + defer rows.Close() + var ret CacheData + + for rows.Next() { + var ( + date int64 + greenEnergyPercentage float32 + polledSmartEnergyConsumption float32 + ) + err = rows.Scan(&date, &greenEnergyPercentage, &polledSmartEnergyConsumption) + ret = append(ret, CacheEntry{date, greenEnergyPercentage, polledSmartEnergyConsumption}) } - var cache CacheFile - err = json.Unmarshal(data, &cache) - if err != nil { - return CacheFile{}, err - } - - return cache, nil + return ret, nil } diff --git a/config.go b/config.go index 94d439e..89b399e 100644 --- a/config.go +++ b/config.go @@ -2,10 +2,12 @@ package main import ( "crypto/sha256" + "database/sql" "encoding/json" "errors" "fmt" "github.com/gofiber/fiber/v2" + _ "github.com/mattn/go-sqlite3" "os" "regexp" "strings" @@ -13,6 +15,7 @@ import ( ) type Config struct { + db *sql.DB HomeAssistant HomeAssistant `json:"home_assistant"` Sensors Sensors `json:"sensors"` Administrator Administrator `json:"administrator"` @@ -94,6 +97,22 @@ func loadConfig() (config Config, err error, isFirstRun bool) { return Config{}, err, false } + db, err := sql.Open("sqlite3", "./database.db") + if err != nil { + return Config{}, err, false + } + conf.db = db + + _, err = db.Exec(`CREATE TABLE IF NOT EXISTS "cache" ( + "time" NUMERIC NOT NULL, + "green_energy_percentage" REAL NOT NULL, + "energy_consumption" REAL NOT NULL, + PRIMARY KEY("time") + );`) + if err != nil { + return Config{}, err, false + } + return conf, nil, false } diff --git a/go.mod b/go.mod index 7ef3761..e54d368 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.17 require ( github.com/gofiber/fiber/v2 v2.37.1 github.com/gofiber/template v1.7.1 + github.com/mattn/go-sqlite3 v1.14.16 github.com/robfig/cron/v3 v3.0.1 ) diff --git a/http.go b/http.go index b7bdbd2..5a54713 100644 --- a/http.go +++ b/http.go @@ -181,7 +181,7 @@ func (config Config) renderIndex(c *fiber.Ctx) error { }, "base") } - data, err := readCache() + data, err := config.readCache() if err != nil { return err } @@ -194,7 +194,7 @@ func (config Config) renderIndex(c *fiber.Ctx) error { ) for _, datum := range data { - labels = append(labels, datum.Date) + labels = append(labels, time.Unix(datum.Date, 0).Format("02/01")) greenEnergyPercents = append(greenEnergyPercents, datum.GreenEnergyPercentage) greenEnergyConsumptionAbsolute = append(greenEnergyConsumptionAbsolute, datum.GreenEnergyPercentage/100*datum.PolledSmartEnergySummation) energyConsumptions = append(energyConsumptions, datum.PolledSmartEnergySummation) diff --git a/main.go b/main.go index 7be94bf..cd30f5a 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,7 @@ func main() { cr := cron.New() _, err = cr.AddFunc("@hourly", config.updateCache) if err != nil { - return + log.Fatal(err) } cr.Start() config.updateCache()