small refactor

This commit is contained in:
Pieter Hollander 2025-10-04 01:56:08 +02:00
parent 851c6d01ec
commit 6b496a39ee
Signed by: pieter
SSH key fingerprint: SHA256:HbX+9cBXsop9SuvL+mELd29sK+7DehFfdVweFVDtMSg

33
main.go
View file

@ -178,6 +178,17 @@ type Config struct {
var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil)) var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
var config Config var config Config
const timescaleHypertableSQL = `
-- CREATE EXTENSION IF NOT EXISTS timescaledb;
SELECT create_hypertable('opendtu_log', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE);
SELECT create_hypertable('opendtu_inverters', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE);
SELECT create_hypertable('opendtu_inverters_ac', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE);
SELECT create_hypertable('opendtu_inverters_dc', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE);
SELECT create_hypertable('opendtu_inverters_inv', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE);
SELECT create_hypertable('opendtu_events', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE);
SELECT create_hypertable('opendtu_hints', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE);
`
// LoadConfig attempts to read the configuration from options.json // LoadConfig attempts to read the configuration from options.json
// If it fails, it falls back to using environment variables // If it fails, it falls back to using environment variables
func loadConfig() (Config, error) { func loadConfig() (Config, error) {
@ -399,26 +410,18 @@ func migrateDB(db *sql.DB) {
if err != nil { if err != nil {
log.Fatal("Error performing database migrations: ", err) log.Fatal("Error performing database migrations: ", err)
} }
timescaleEnabled := config.TimescaleDB if config.TimescaleDB {
if err := enableTimescaleHypertables(db); err != nil {
enableTimescaleDB := `
-- CREATE EXTENSION IF NOT EXISTS timescaledb;
SELECT create_hypertable('opendtu_log', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE);
SELECT create_hypertable('opendtu_inverters', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE);
SELECT create_hypertable('opendtu_inverters_ac', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE);
SELECT create_hypertable('opendtu_inverters_dc', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE);
SELECT create_hypertable('opendtu_inverters_inv', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE);
SELECT create_hypertable('opendtu_events', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE);
SELECT create_hypertable('opendtu_hints', 'timestamp', if_not_exists => TRUE, migrate_data => TRUE);
`
if timescaleEnabled {
_, err := db.Exec(enableTimescaleDB)
if err != nil {
log.Fatal("Error enabling TimescaleDB: ", err) log.Fatal("Error enabling TimescaleDB: ", err)
} }
} }
} }
func enableTimescaleHypertables(db *sql.DB) error {
_, err := db.Exec(timescaleHypertableSQL)
return err
}
func insertLiveData(db *sql.DB, inverter Inverter, total Total, hints Hints) { func insertLiveData(db *sql.DB, inverter Inverter, total Total, hints Hints) {
timeZone := config.TZ timeZone := config.TZ
loc, _ := time.LoadLocation(timeZone) loc, _ := time.LoadLocation(timeZone)