th7/db/adapter.go

55 lines
1.1 KiB
Go
Raw Permalink Normal View History

2022-11-12 16:22:55 +00:00
package db
import (
"errors"
2023-11-23 20:46:54 +00:00
"strings"
"th7/data/config"
2022-11-12 16:22:55 +00:00
"th7/ports"
"time"
2022-11-12 16:22:55 +00:00
)
2023-12-05 17:22:12 +00:00
const (
DB_LOGGER_DEFAULT_DUR = 300
)
func NewAdapter(corePort ports.CorePort, cfg config.Config) (ports.DBPort, error) {
2022-11-12 16:22:55 +00:00
var duration time.Duration
var adapter ports.DBPort
var err error
2023-12-05 17:22:12 +00:00
// if `freq' is not present in the DB map, use default of 5 minutes (300 sec)
if _, ok := cfg.DB["freq"]; !ok {
duration = time.Duration(DB_LOGGER_DEFAULT_DUR) * time.Second
} else {
duration = time.Duration(cfg.DB["freq"].(int64)) * time.Second
}
// if no DB is specified, or nolog=true then use a dummy db adapter
2023-12-05 17:22:12 +00:00
if _, ok := cfg.DB["type"]; !ok || cfg.Board.NoLog {
2023-11-25 10:37:02 +00:00
adapter, _ = NewDummyAdapter(cfg)
go startLoggingProcess(adapter, corePort, duration)
return adapter, nil
2022-11-12 16:22:55 +00:00
}
2023-11-23 20:46:54 +00:00
db := cfg.DB["type"].(string)
2023-11-23 20:46:54 +00:00
switch strings.ToLower(db) {
2022-11-12 16:22:55 +00:00
case "sqlite3":
adapter, err = NewSQLite3Adapter(cfg)
2023-11-23 20:46:54 +00:00
case "influxdb":
adapter, err = NewInfluxDBAdapter(cfg)
default:
2023-11-25 10:37:02 +00:00
return &DummyAdapter{cfg: cfg}, errors.New("unknown db type")
}
if err != nil {
2023-11-25 10:37:02 +00:00
return &DummyAdapter{cfg: cfg}, err
2022-11-12 16:22:55 +00:00
}
go startLoggingProcess(adapter, corePort, duration)
return adapter, nil
2022-11-12 16:22:55 +00:00
}