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.
This commit is contained in:
MassiveBox 2022-12-07 17:54:46 +01:00
parent 6dc8fa3750
commit e9125b783c
6 changed files with 56 additions and 33 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ config.json
cache.json
go.sum
.idea
database.db

View file

@ -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)
_, 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 saving cached data to file -" + err.Error())
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
}

View file

@ -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
}

1
go.mod
View file

@ -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
)

View file

@ -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)

View file

@ -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()