fix: use repository instead db package

This commit is contained in:
Markus Pesch 2020-05-21 20:07:32 +02:00
parent 8f1c7b10f7
commit d0e238e64a
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
2 changed files with 10 additions and 8 deletions

View File

@ -8,7 +8,7 @@ import (
"syscall" "syscall"
"github.com/volker-raschek/flucky/pkg/config" "github.com/volker-raschek/flucky/pkg/config"
"github.com/volker-raschek/flucky/pkg/repository/db" "github.com/volker-raschek/flucky/pkg/repository"
"github.com/volker-raschek/flucky/pkg/sensor" "github.com/volker-raschek/flucky/pkg/sensor"
"github.com/volker-raschek/flucky/pkg/types" "github.com/volker-raschek/flucky/pkg/types"
"github.com/volker-raschek/go-logger/pkg/logger" "github.com/volker-raschek/go-logger/pkg/logger"
@ -37,7 +37,7 @@ func Start(cnf *config.Config, flogger logger.Logger) error {
return err return err
} }
backend, err := db.New(storageEndpointURL, flogger) repo, err := repository.New(storageEndpointURL, flogger)
if err != nil { if err != nil {
return err return err
} }
@ -49,18 +49,18 @@ func Start(cnf *config.Config, flogger logger.Logger) error {
parentCtx := context.Background() parentCtx := context.Background()
// Insert device if not exist // Insert device if not exist
device, _ := backend.SelectDevice(parentCtx, cnf.Device.ID) device, _ := repo.GetDevice(cnf.Device.ID)
if device == nil { if device == nil {
if err := backend.InsertDevices(parentCtx, cnf.Device); err != nil { if err := repo.AddDevices(cnf.Device); err != nil {
return err return err
} }
} }
// Insert sensors if not exist // Insert sensors if not exist
for _, cnfSensor := range cnf.Sensors { for _, cnfSensor := range cnf.Sensors {
sensor, _ := backend.SelectSensor(parentCtx, cnfSensor.ID) sensor, _ := repo.GetSensor(cnfSensor.ID)
if sensor == nil { if sensor == nil {
if err := backend.InsertSensors(parentCtx, cnfSensor); err != nil { if err := repo.AddSensors(cnfSensor); err != nil {
return err return err
} }
} }
@ -97,7 +97,7 @@ func Start(cnf *config.Config, flogger logger.Logger) error {
if cap(measuredValues) == len(measuredValues) { if cap(measuredValues) == len(measuredValues) {
flogger.Debug("Flush cache") flogger.Debug("Flush cache")
err := backend.InsertMeasuredValues(ctx, measuredValues...) err := repo.AddMeasuredValues(measuredValues...)
if err != nil { if err != nil {
flogger.Error("%v", err) flogger.Error("%v", err)
} }
@ -108,7 +108,7 @@ func Start(cnf *config.Config, flogger logger.Logger) error {
cancel() cancel()
close(measuredValueChannel) close(measuredValueChannel)
err := backend.InsertMeasuredValues(ctx, measuredValues...) err := repo.AddMeasuredValues(measuredValues...)
if err != nil { if err != nil {
flogger.Error("%v", err) flogger.Error("%v", err)
} }

View File

@ -85,6 +85,7 @@ func New(storageEndpoint *url.URL, flogger logger.Logger) (Database, error) {
case "sqlite3": case "sqlite3":
// Create directory where the db file will be created if not exists.
if _, err := os.Stat(filepath.Dir(storageEndpoint.Path)); os.IsNotExist(err) { if _, err := os.Stat(filepath.Dir(storageEndpoint.Path)); os.IsNotExist(err) {
err := os.MkdirAll(filepath.Dir(storageEndpoint.Path), 0755) err := os.MkdirAll(filepath.Dir(storageEndpoint.Path), 0755)
if err != nil { if err != nil {
@ -108,6 +109,7 @@ func New(storageEndpoint *url.URL, flogger logger.Logger) (Database, error) {
return nil, fmt.Errorf("Unsupported database scheme: %v", storageEndpoint.Scheme) return nil, fmt.Errorf("Unsupported database scheme: %v", storageEndpoint.Scheme)
} }
// Initialize database scheme if not exists
err = database.Scheme(context.Background()) err = database.Scheme(context.Background())
if err != nil { if err != nil {
return nil, err return nil, err