small refactor
This commit is contained in:
		
							parent
							
								
									851c6d01ec
								
							
						
					
					
						commit
						6b496a39ee
					
				
					 1 changed files with 18 additions and 15 deletions
				
			
		
							
								
								
									
										33
									
								
								main.go
									
										
									
									
									
								
							
							
						
						
									
										33
									
								
								main.go
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -178,6 +178,17 @@ type Config struct {
 | 
			
		|||
var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
 | 
			
		||||
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
 | 
			
		||||
// If it fails, it falls back to using environment variables
 | 
			
		||||
func loadConfig() (Config, error) {
 | 
			
		||||
| 
						 | 
				
			
			@ -399,26 +410,18 @@ func migrateDB(db *sql.DB) {
 | 
			
		|||
	if err != nil {
 | 
			
		||||
		log.Fatal("Error performing database migrations: ", err)
 | 
			
		||||
	}
 | 
			
		||||
	timescaleEnabled := config.TimescaleDB
 | 
			
		||||
 | 
			
		||||
	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 {
 | 
			
		||||
	if config.TimescaleDB {
 | 
			
		||||
		if err := enableTimescaleHypertables(db); err != nil {
 | 
			
		||||
			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) {
 | 
			
		||||
	timeZone := config.TZ
 | 
			
		||||
	loc, _ := time.LoadLocation(timeZone)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue