fix(cli/db): remove obsolete db subcommand

This commit is contained in:
Markus Pesch 2019-12-07 18:57:41 +01:00
parent dbef4f8241
commit b125bf432c
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
6 changed files with 32 additions and 79 deletions

View File

@ -36,13 +36,13 @@ var rootCmd = &cobra.Command{
return fmt.Errorf("Can not locate the hostname: %v", err) return fmt.Errorf("Can not locate the hostname: %v", err)
} }
// Time must be truncted for postgres // Time must be truncted for postgres. Postgres currently does not support
// Postgres currently does not support nanoseconds which is automatically // nanoseconds which is automatically include into the go time object
// include into the go time object
t := time.Now() t := time.Now()
l, _ := time.LoadLocation("Europe/Berlin") l, _ := time.LoadLocation("Europe/Berlin")
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), int(math.Round(float64(t.Nanosecond())/1000000)*1000000), l) t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), int(math.Round(float64(t.Nanosecond())/1000000)*1000000), l)
// Default configuration
cnf := config.Configuration{ cnf := config.Configuration{
Device: &types.Device{ Device: &types.Device{
DeviceID: uuid.NewV4().String(), DeviceID: uuid.NewV4().String(),
@ -71,7 +71,6 @@ func Execute(version *semver.Version) {
compression.InitCmd(rootCmd, &configFile, version) compression.InitCmd(rootCmd, &configFile, version)
convert.InitCmd(rootCmd, &configFile, version) convert.InitCmd(rootCmd, &configFile, version)
daemon.InitCmd(rootCmd, &configFile, version) daemon.InitCmd(rootCmd, &configFile, version)
// db.InitCmd(rootCmd, &configFile, version)
humidity.InitCmd(rootCmd, &configFile, version) humidity.InitCmd(rootCmd, &configFile, version)
pressure.InitCmd(rootCmd, &configFile, version) pressure.InitCmd(rootCmd, &configFile, version)
rgbled.InitCmd(rootCmd, &configFile, version) rgbled.InitCmd(rootCmd, &configFile, version)

View File

@ -1,56 +0,0 @@
package db
import (
"github.com/Masterminds/semver"
"github.com/spf13/cobra"
)
var (
configFile *string
version *semver.Version
)
var dbCmd = &cobra.Command{
Use: "db",
Short: "Operates with the configured database",
Run: func(cmd *cobra.Command, args []string) {
// // read configuration
// cnf, err := config.Read(*configFile)
// if err != nil {
// log.Fatalln(err)
// }
// postgresDB, err := database.New(cnf.DatabaseSettings)
// if err != nil {
// log.Fatalf("%v", err)
// }
// ctx := context.Background()
// devices := []*types.Device{
// &types.Device{
// DeviceID: "1684df26-bc72-4435-a4f9-74b24bdb286c",
// DeviceName: "raspberr-pi",
// },
// &types.Device{
// DeviceID: "1684df26-bc72-4435-a4f9-74b24bdb286c",
// DeviceName: "raspberr-pi",
// },
// }
// if err := postgresDB.InsertDevices(ctx, devices); err != nil {
// log.Fatalln(err)
// }
},
}
// Execute a
func InitCmd(cmd *cobra.Command, cnfFile *string, sversion *semver.Version) {
configFile = cnfFile
version = sversion
cmd.AddCommand(dbCmd)
}

View File

@ -15,8 +15,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var logs bool
var readTemperatureCmd = &cobra.Command{ var readTemperatureCmd = &cobra.Command{
Use: "read", Use: "read",
Short: "Reading temperature values from different or specified sensors by arguments", Short: "Reading temperature values from different or specified sensors by arguments",
@ -38,7 +36,7 @@ var readTemperatureCmd = &cobra.Command{
} }
if len(sensors) == 0 { if len(sensors) == 0 {
return log.Fatalln("No sensors found, specified or configured")
} }
ctx := context.Background() ctx := context.Background()
@ -47,12 +45,20 @@ var readTemperatureCmd = &cobra.Command{
log.Fatalln(err) log.Fatalln(err)
} }
storage.Round(measuredValues, round)
measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypeTemperature, measuredValues) measuredValues = types.SelectMeasuredValues(types.MeasuredValueTypeTemperature, measuredValues)
// print temperatures on stdout // print temperatures on stdout
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout) cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
// Save the new measured values, if desired
if logs { if logs {
if compression {
measuredValues = storage.Compression(measuredValues)
}
storageEndpoint, err := cnf.GetStorageEndpointURL() storageEndpoint, err := cnf.GetStorageEndpointURL()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)

View File

@ -10,6 +10,7 @@ import (
var ( var (
compression bool compression bool
configFile *string configFile *string
logs bool
round float64 round float64
version *semver.Version version *semver.Version
@ -21,7 +22,7 @@ var temperatureCmd = &cobra.Command{
Example: fmt.Sprintf("flucky temperature read\nflucky temperature read outdoor"), Example: fmt.Sprintf("flucky temperature read\nflucky temperature read outdoor"),
} }
// Execute a // InitCmd initialize the subcommand
func InitCmd(cmd *cobra.Command, cnfFile *string, sversion *semver.Version) { func InitCmd(cmd *cobra.Command, cnfFile *string, sversion *semver.Version) {
configFile = cnfFile configFile = cnfFile
version = sversion version = sversion

View File

@ -7,24 +7,17 @@ import (
"github.com/go-flucky/flucky/pkg/types" "github.com/go-flucky/flucky/pkg/types"
) )
// var validUUID = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
// Append adds an array of several measured values to a logfile // Append adds an array of several measured values to a logfile
func Append(logfile Logfile, measuredValues []*types.MeasuredValue) error { func Append(logfile Logfile, measuredValues []*types.MeasuredValue) error {
allMeasuredValues, err := logfile.Read() allMeasuredValues, err := logfile.Read()
if err != nil { if err != nil {
return err return err
} }
allMeasuredValues = append(allMeasuredValues, measuredValues...) allMeasuredValues = append(allMeasuredValues, measuredValues...)
err = logfile.Write(allMeasuredValues) err = logfile.Write(allMeasuredValues)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
@ -32,9 +25,7 @@ func Append(logfile Logfile, measuredValues []*types.MeasuredValue) error {
// file extension of the logfile is taken into account to format the logfile // file extension of the logfile is taken into account to format the logfile
// into the correct format. // into the correct format.
func New(logfile string) Logfile { func New(logfile string) Logfile {
ext := filepath.Ext(logfile) ext := filepath.Ext(logfile)
switch ext { switch ext {
case ".csv": case ".csv":
return &csvLogfile{ return &csvLogfile{
@ -53,7 +44,6 @@ func New(logfile string) Logfile {
logfile: logfile, logfile: logfile,
} }
} }
} }
func writeCreationDate(measuredValues []*types.MeasuredValue) error { func writeCreationDate(measuredValues []*types.MeasuredValue) error {

View File

@ -31,6 +31,13 @@ func Compression(measuredValues []*types.MeasuredValue) []*types.MeasuredValue {
now := format.FormatedTime() now := format.FormatedTime()
for _, measuredValue := range measuredValues { for _, measuredValue := range measuredValues {
// If the sensor id does not exist in the map, a new map is initialized,
// which can assume measured value types as the key. Behind this key there
// is a pointer which refers to a measured value in the memory. This new map
// is added to the map "lastMeasuredValuesBySensors" under the sensor ID.
// This makes it possible to store one measured value per measured value
// type per sensor.
if _, ok := lastMeasuredValuesBySensors[measuredValue.SensorID]; !ok { if _, ok := lastMeasuredValuesBySensors[measuredValue.SensorID]; !ok {
lastMeasuredValuesBySensors[measuredValue.SensorID] = make(map[types.MeasuredValueType]*types.MeasuredValue, 0) lastMeasuredValuesBySensors[measuredValue.SensorID] = make(map[types.MeasuredValueType]*types.MeasuredValue, 0)
} }
@ -93,21 +100,27 @@ func Round(measuredValues []*types.MeasuredValue, round float64) {
} }
} }
// Write measured values to the given storage endpoint url. The scheme must be // Write measured values to the given storage endpoint url. If the storage
// matched to a provider, if the scheme is not implemented, the function // provider defined to a file, the data will be overwritten. If a database
// returns an error // provider is used, the data is simply added without deleting the existing
// data. The scheme must be matched to a storage provider, if the scheme is not
// implemented, the function returns an error
func Write(ctx context.Context, measuredValues []*types.MeasuredValue, storageEndpoint *url.URL) error { func Write(ctx context.Context, measuredValues []*types.MeasuredValue, storageEndpoint *url.URL) error {
switch storageEndpoint.Scheme { switch storageEndpoint.Scheme {
case "file": case "file":
measuredValueLogfile := logfile.New(storageEndpoint.Path) measuredValueLogfile := logfile.New(storageEndpoint.Path)
return measuredValueLogfile.Write(measuredValues) storedMeasuredValues, err := measuredValueLogfile.Read()
if err != nil {
return err
}
storedMeasuredValues = append(storedMeasuredValues, measuredValues...)
return measuredValueLogfile.Write(storedMeasuredValues)
case "postgres": case "postgres":
database, err := db.New(storageEndpoint) database, err := db.New(storageEndpoint)
if err != nil { if err != nil {
return err return err
} }
defer database.Close() defer database.Close()
if err := database.InsertMeasuredValues(ctx, measuredValues); err != nil { if err := database.InsertMeasuredValues(ctx, measuredValues); err != nil {
return err return err
} }