fix(pkg/server): logging and save values into postgres
This commit is contained in:
parent
6f45c2957a
commit
671a3eb748
@ -2,12 +2,11 @@ package daemon
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
|
"github.com/go-flucky/flucky/cmd/internal"
|
||||||
"github.com/go-flucky/flucky/pkg/config"
|
"github.com/go-flucky/flucky/pkg/config"
|
||||||
"github.com/go-flucky/flucky/pkg/daemon"
|
"github.com/go-flucky/flucky/pkg/daemon"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/volker-raschek/go-logger/pkg/logger"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -40,14 +39,13 @@ func run(cmd *cobra.Command, args []string) error {
|
|||||||
return fmt.Errorf("No config file defined: %v", err)
|
return fmt.Errorf("No config file defined: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WARNING: Must set logger for daemon package
|
logLevel, err := cmd.Flags().GetString("loglevel")
|
||||||
// logLevel, err := cmd.Flags().GetString("loglevel")
|
if err != nil {
|
||||||
// if err != nil {
|
return fmt.Errorf("No loglevel defined: %v", err)
|
||||||
// return fmt.Errorf("No loglevel defined: %v", err)
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
//flogger := initializeLogger(logLevel)
|
flogger := internal.InitializeLogger(logLevel)
|
||||||
// daemon.SetLogLevel(flogger)
|
daemon.SetLogger(flogger)
|
||||||
|
|
||||||
cnf, err := config.Read(configFile)
|
cnf, err := config.Read(configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -56,21 +54,3 @@ func run(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
return daemon.Start(cnf, cachedMeasuredValues, compression, round)
|
return daemon.Start(cnf, cachedMeasuredValues, compression, round)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initializeLogger(logLevel string) logger.Logger {
|
|
||||||
log.Println(logLevel)
|
|
||||||
switch logLevel {
|
|
||||||
case "debug", "DEBUG":
|
|
||||||
return logger.NewDefaultLogger(logger.LogLevelDebug)
|
|
||||||
case "info", "INFO":
|
|
||||||
return logger.NewDefaultLogger(logger.LogLevelInfo)
|
|
||||||
case "warn", "WARN":
|
|
||||||
return logger.NewDefaultLogger(logger.LogLevelWarn)
|
|
||||||
case "error", "ERROR":
|
|
||||||
return logger.NewDefaultLogger(logger.LogLevelError)
|
|
||||||
case "fatal", "FATAL":
|
|
||||||
return logger.NewDefaultLogger(logger.LogLevelFatal)
|
|
||||||
default:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
106
cmd/db/db.go
Normal file
106
cmd/db/db.go
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
|
"github.com/Masterminds/semver"
|
||||||
|
"github.com/go-flucky/flucky/cmd/internal"
|
||||||
|
"github.com/go-flucky/flucky/pkg/config"
|
||||||
|
"github.com/go-flucky/flucky/pkg/storage/db"
|
||||||
|
"github.com/go-flucky/flucky/pkg/types"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
semanticVersion *semver.Version
|
||||||
|
)
|
||||||
|
|
||||||
|
// InitCmd initialize all convert subcommands
|
||||||
|
func InitCmd(cmd *cobra.Command, semver *semver.Version) error {
|
||||||
|
|
||||||
|
semanticVersion = semver
|
||||||
|
|
||||||
|
databaseCmd := &cobra.Command{
|
||||||
|
Use: "db",
|
||||||
|
Short: "Interagte with the database configured as storage endpoint",
|
||||||
|
}
|
||||||
|
|
||||||
|
updateDatabaseCmd := &cobra.Command{
|
||||||
|
Use: "init",
|
||||||
|
Short: "Initialize the database scheme for the version of the flucky binary",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
Example: "flucky db update",
|
||||||
|
RunE: initializeDatabase,
|
||||||
|
}
|
||||||
|
|
||||||
|
databaseCmd.AddCommand(updateDatabaseCmd)
|
||||||
|
cmd.AddCommand(databaseCmd)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func initializeDatabase(cmd *cobra.Command, args []string) error {
|
||||||
|
|
||||||
|
configFile, err := cmd.Flags().GetString("config")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("No config file defined")
|
||||||
|
}
|
||||||
|
|
||||||
|
cnf, err := config.Read(configFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
storageEndpointURL, err := url.Parse(cnf.StorageEndpoint)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failed to parse storage endpoint: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logLevel, err := cmd.Flags().GetString("loglevel")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("No loglevel defined: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
flogger := internal.InitializeLogger(logLevel)
|
||||||
|
|
||||||
|
database, err := db.New(storageEndpointURL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer database.Close()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
flogger.Debug("Initialize database scheme")
|
||||||
|
err = database.Schema(ctx, semanticVersion)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = database.SelectDeviceByID(ctx, cnf.Device.ID)
|
||||||
|
if err != nil {
|
||||||
|
flogger.Debug("Add device into database")
|
||||||
|
devices := []*types.Device{cnf.Device}
|
||||||
|
if err := database.InsertDevices(ctx, devices); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
flogger.Debug("Device already availabe in the database")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, sensor := range cnf.Sensors {
|
||||||
|
_, err = database.SelectSensorByID(ctx, sensor.ID)
|
||||||
|
if err != nil {
|
||||||
|
flogger.Debug("Add sensor %v into the database", sensor.FullName())
|
||||||
|
sensors := []*types.Sensor{sensor}
|
||||||
|
if err := database.InsertSensors(ctx, sensors); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
flogger.Debug("Sensor %v already available in the database", sensor.FullName())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
20
cmd/internal/internal.go
Normal file
20
cmd/internal/internal.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import "github.com/volker-raschek/go-logger/pkg/logger"
|
||||||
|
|
||||||
|
func InitializeLogger(logLevel string) logger.Logger {
|
||||||
|
switch logLevel {
|
||||||
|
case "debug", "DEBUG":
|
||||||
|
return logger.NewDefaultLogger(logger.LogLevelDebug)
|
||||||
|
case "info", "INFO":
|
||||||
|
return logger.NewDefaultLogger(logger.LogLevelInfo)
|
||||||
|
case "warn", "WARN":
|
||||||
|
return logger.NewDefaultLogger(logger.LogLevelWarn)
|
||||||
|
case "error", "ERROR":
|
||||||
|
return logger.NewDefaultLogger(logger.LogLevelError)
|
||||||
|
case "fatal", "FATAL":
|
||||||
|
return logger.NewDefaultLogger(logger.LogLevelFatal)
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/go-flucky/flucky/cmd/compression"
|
"github.com/go-flucky/flucky/cmd/compression"
|
||||||
"github.com/go-flucky/flucky/cmd/convert"
|
"github.com/go-flucky/flucky/cmd/convert"
|
||||||
"github.com/go-flucky/flucky/cmd/daemon"
|
"github.com/go-flucky/flucky/cmd/daemon"
|
||||||
|
"github.com/go-flucky/flucky/cmd/db"
|
||||||
"github.com/go-flucky/flucky/cmd/humidity"
|
"github.com/go-flucky/flucky/cmd/humidity"
|
||||||
"github.com/go-flucky/flucky/cmd/pressure"
|
"github.com/go-flucky/flucky/cmd/pressure"
|
||||||
"github.com/go-flucky/flucky/cmd/rgbled"
|
"github.com/go-flucky/flucky/cmd/rgbled"
|
||||||
@ -61,7 +62,12 @@ func Execute(version *semver.Version) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := rootCmd.Execute()
|
err := db.InitCmd(rootCmd, version)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = rootCmd.Execute()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,7 @@ flucky sensor add --i2c-bus 1 --i2c-address 0x76 wetter-station BME280`,
|
|||||||
Aliases: []string{"rm"},
|
Aliases: []string{"rm"},
|
||||||
Example: "flucky sensor remove outdoor",
|
Example: "flucky sensor remove outdoor",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
RunE: enableSensor,
|
RunE: removeSensor,
|
||||||
}
|
}
|
||||||
|
|
||||||
renameSensorCmd := &cobra.Command{
|
renameSensorCmd := &cobra.Command{
|
||||||
|
@ -86,7 +86,7 @@ func PrintSensors(cnf *config.Configuration, w io.Writer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if sensor.I2CAddress != nil {
|
if sensor.I2CAddress != nil {
|
||||||
fmt.Fprintf(tw, "%v\t", string(*sensor.I2CAddress))
|
fmt.Fprintf(tw, "%#v\t", *sensor.I2CAddress)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(tw, "\t")
|
fmt.Fprintf(tw, "\t")
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package daemon
|
package daemon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"context"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ func (cs *cacheStore) WriteToEndpoint() error {
|
|||||||
case "file":
|
case "file":
|
||||||
return cs.logfile()
|
return cs.logfile()
|
||||||
case "postgres":
|
case "postgres":
|
||||||
return fmt.Errorf("Not yet implemented to store values into a postgres database")
|
return cs.postgres()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -91,3 +91,8 @@ func (cs *cacheStore) logfile() error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cs *cacheStore) postgres() error {
|
||||||
|
ctx := context.Background()
|
||||||
|
return storage.Write(ctx, cs.cache, cs.URL)
|
||||||
|
}
|
||||||
|
@ -15,9 +15,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
flogger = logger.NewDefaultLogger(logger.LogLevelDebug)
|
flogger = logger.NewSilentLogger()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func SetLogger(logger logger.Logger) {
|
||||||
|
flogger = logger
|
||||||
|
}
|
||||||
|
|
||||||
// Start the daemon
|
// Start the daemon
|
||||||
func Start(cnf *config.Configuration, cacheSize uint, compression bool, round float64) error {
|
func Start(cnf *config.Configuration, cacheSize uint, compression bool, round float64) error {
|
||||||
|
|
||||||
@ -129,7 +133,10 @@ func Start(cnf *config.Configuration, cacheSize uint, compression bool, round fl
|
|||||||
flogger.Fatal("Received a fatal error: %v", fatal)
|
flogger.Fatal("Received a fatal error: %v", fatal)
|
||||||
case <-interruptChannel:
|
case <-interruptChannel:
|
||||||
flogger.Debug("Write %v cached values into storage endpoint", cache.Size())
|
flogger.Debug("Write %v cached values into storage endpoint", cache.Size())
|
||||||
cache.WriteToEndpoint()
|
err := cache.WriteToEndpoint()
|
||||||
|
if err != nil {
|
||||||
|
flogger.Error("%v", err)
|
||||||
|
}
|
||||||
flogger.Debug("Close context")
|
flogger.Debug("Close context")
|
||||||
cancel()
|
cancel()
|
||||||
flogger.Debug("Close channels")
|
flogger.Debug("Close channels")
|
||||||
|
Loading…
Reference in New Issue
Block a user