fix(pkg/logfile): use one instead of several logfiles for all measured values
This commit is contained in:
parent
072a902376
commit
3dae3e38be
@ -32,11 +32,10 @@ var rootCmd = &cobra.Command{
|
|||||||
|
|
||||||
cnf := config.Configuration{
|
cnf := config.Configuration{
|
||||||
Device: &types.Device{
|
Device: &types.Device{
|
||||||
DeviceID: uuid.NewV4().String(),
|
DeviceID: uuid.NewV4().String(),
|
||||||
DeviceName: hostname,
|
DeviceName: hostname,
|
||||||
HumidityLogfile: "/var/log/flucky/humidity.json",
|
Logfile: "/var/log/flucky/logfile.json",
|
||||||
TemperatureLogfile: "/var/log/flucky/temperature.json",
|
CreationDate: time.Now(),
|
||||||
CreationDate: time.Now(),
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ var listTemperatureCmd = &cobra.Command{
|
|||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logfile := logfile.New(cnf.Device.TemperatureLogfile)
|
logfile := logfile.New(cnf.Device.Logfile)
|
||||||
|
|
||||||
measuredValues, err := logfile.Read()
|
measuredValues, err := logfile.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -50,7 +50,7 @@ var readTemperatureCmd = &cobra.Command{
|
|||||||
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
||||||
|
|
||||||
if logs {
|
if logs {
|
||||||
logfile := logfile.New(cnf.Device.TemperatureLogfile)
|
logfile := logfile.New(cnf.Device.Logfile)
|
||||||
err := logfile.Append(compression, measuredValues)
|
err := logfile.Append(compression, measuredValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rgbled.Error(rgbLEDs)
|
rgbled.Error(rgbLEDs)
|
||||||
|
@ -33,7 +33,7 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
childContext, cancel := context.WithCancel(ctx)
|
childContext, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
logfile := logfile.New(cnf.Device.TemperatureLogfile)
|
logfile := logfile.New(cnf.Device.Logfile)
|
||||||
|
|
||||||
measuredValuesCache := make([]types.MeasuredValue, 0)
|
measuredValuesCache := make([]types.MeasuredValue, 0)
|
||||||
|
|
||||||
|
@ -185,6 +185,8 @@ func (jl *jsonLogfile) Write(measuredValues []types.MeasuredValue) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
writeCreationDate(measuredValues)
|
||||||
|
|
||||||
f, err := os.Create(jl.logfile)
|
f, err := os.Create(jl.logfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%v %v: %v", errorLogfileCreate, jl.logfile, err)
|
return fmt.Errorf("%v %v: %v", errorLogfileCreate, jl.logfile, err)
|
||||||
|
@ -3,39 +3,13 @@ package logfile
|
|||||||
import (
|
import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"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}$")
|
// 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}$")
|
||||||
var timeFormat = time.RFC3339
|
var timeFormat = time.RFC3339
|
||||||
|
|
||||||
// // AppendTemperatures with temperature values from a logfile. As additional
|
|
||||||
// // option it's possible to compress the temperature data.
|
|
||||||
// func AppendTemperatures(logfile Logfile, compression bool, temperatures []*types.Temperature) error {
|
|
||||||
|
|
||||||
// allTemperatures := make([]*types.Temperature, 0)
|
|
||||||
|
|
||||||
// if _, err := os.Stat(logfile.GetLogfile()); err == nil {
|
|
||||||
// temperaturesFromLogfile, err := logfile.ReadTemperatures()
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// allTemperatures = append(allTemperatures, temperaturesFromLogfile...)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// allTemperatures = append(allTemperatures, temperatures...)
|
|
||||||
|
|
||||||
// if compression {
|
|
||||||
// allTemperatures = CompressTemperature(allTemperatures)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// err := logfile.WriteTemperatures(allTemperatures)
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
// CompressTemperature compresses the temperatures from an array. It is checked
|
// CompressTemperature compresses the temperatures from an array. It is checked
|
||||||
// whether the measured temperature of a value corresponds to that of the
|
// whether the measured temperature of a value corresponds to that of the
|
||||||
// predecessor. If this is the case, the current value is discarded and the
|
// predecessor. If this is the case, the current value is discarded and the
|
||||||
@ -91,10 +65,10 @@ func New(logfile string) Logfile {
|
|||||||
return &jsonLogfile{
|
return &jsonLogfile{
|
||||||
logfile: logfile,
|
logfile: logfile,
|
||||||
}
|
}
|
||||||
// case ".xml":
|
// case ".xml":
|
||||||
// return &xmlLogfile{
|
// return &xmlLogfile{
|
||||||
// logfile: logfile,
|
// logfile: logfile,
|
||||||
// }
|
// }
|
||||||
default:
|
default:
|
||||||
return &jsonLogfile{
|
return &jsonLogfile{
|
||||||
logfile: logfile,
|
logfile: logfile,
|
||||||
@ -103,56 +77,11 @@ func New(logfile string) Logfile {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// // SplittTemperatures into multiple arrays. The Size can be defined by
|
func writeCreationDate(measuredValues []types.MeasuredValue) {
|
||||||
// // temperatureSplitBy parameter.
|
now := time.Now()
|
||||||
// func SplittTemperatures(temperatures []*types.Temperature, templeratureSplitBy int) [][]*types.Temperature {
|
for _, measuredValue := range measuredValues {
|
||||||
// splittedTemperatures := make([][]*types.Temperature, 0)
|
if measuredValue.GetCreationDate() == nil {
|
||||||
// newTemperatures := make([]*types.Temperature, 0)
|
measuredValue.SetCreationDate(&now)
|
||||||
// for _, temperature := range temperatures {
|
}
|
||||||
// if len(newTemperatures) == templeratureSplitBy {
|
}
|
||||||
// splittedTemperatures = append(splittedTemperatures, newTemperatures)
|
}
|
||||||
// newTemperatures = make([]*types.Temperature, 0)
|
|
||||||
// }
|
|
||||||
// newTemperatures = append(newTemperatures, temperature)
|
|
||||||
// }
|
|
||||||
// splittedTemperatures = append(splittedTemperatures, newTemperatures)
|
|
||||||
// return splittedTemperatures
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // SortTemperatures by TemperatureFromDate
|
|
||||||
// func SortTemperatures(temperatures []*types.Temperature) {
|
|
||||||
// sort.SliceStable(temperatures, func(i int, j int) bool {
|
|
||||||
// return temperatures[i].TemperatureFromDate.Before(temperatures[j].TemperatureFromDate)
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // ValidateTemperatures Checks if the temperature data is valid.
|
|
||||||
// // - Check the temperature id (uuid)
|
|
||||||
// // - Checks whether the time specifications are historically in a sequence.
|
|
||||||
// // - Check the sensor id (uuid)
|
|
||||||
// func ValidateTemperatures(temperatures []*types.Temperature) error {
|
|
||||||
// for _, temperature := range temperatures {
|
|
||||||
// if !validUUID.MatchString(temperature.TemperatureID) {
|
|
||||||
// return errorNoValidTemperatureID
|
|
||||||
// } else if temperature.TemperatureValue == 0 {
|
|
||||||
// return errorNoValidMesuredValue
|
|
||||||
// } else if temperature.TemperatureFromDate.After(temperature.TemperatureTillDate) {
|
|
||||||
// return errorNoValidTimePeriods
|
|
||||||
// } else if !validUUID.MatchString(temperature.SensorID) {
|
|
||||||
// return errorNoValidSensorID
|
|
||||||
// } else if temperature.CreationDate.After(*temperature.UpdateDate) && temperature.UpdateDate != nil {
|
|
||||||
// return errorNoValidTimePeriods
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func writeCreationDate(temperatures []*types.Temperature) {
|
|
||||||
// now := time.Now()
|
|
||||||
// for _, temperature := range temperatures {
|
|
||||||
// if temperature.CreationDate == nil {
|
|
||||||
// temperature.CreationDate = &now
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
@ -4,11 +4,10 @@ import "time"
|
|||||||
|
|
||||||
// Device ...
|
// Device ...
|
||||||
type Device struct {
|
type Device struct {
|
||||||
DeviceID string `json:"device_id" xml:"device_id"`
|
DeviceID string `json:"device_id" xml:"device_id"`
|
||||||
DeviceName string `json:"device_name" xml:"device_name"`
|
DeviceName string `json:"device_name" xml:"device_name"`
|
||||||
DeviceLocation *string `json:"device_location" xml:"device_location"`
|
DeviceLocation *string `json:"device_location" xml:"device_location"`
|
||||||
DeviceLastContact time.Time `json:"device_last_contact" xml:"device_last_contact"`
|
DeviceLastContact time.Time `json:"device_last_contact" xml:"device_last_contact"`
|
||||||
HumidityLogfile string `json:"humidity_logfile" xml:"humidity_logfile"`
|
Logfile string `json:"logfile" xml:"logfile"`
|
||||||
TemperatureLogfile string `json:"temperature_logfile" xml:"temperature_logfile"`
|
CreationDate time.Time `json:"creation_date" xml:"creation_date"`
|
||||||
CreationDate time.Time `json:"creation_date" xml:"creation_date"`
|
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,10 @@ type Humidity struct {
|
|||||||
UpdateDate *time.Time `json:"update_date" xml:"update_date"`
|
UpdateDate *time.Time `json:"update_date" xml:"update_date"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Humidity) GetCreationDate() *time.Time {
|
||||||
|
return h.CreationDate
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Humidity) GetID() string {
|
func (h *Humidity) GetID() string {
|
||||||
return h.HumidityID
|
return h.HumidityID
|
||||||
}
|
}
|
||||||
@ -24,3 +28,15 @@ func (h *Humidity) GetSensorID() string {
|
|||||||
func (h *Humidity) GetValue() float64 {
|
func (h *Humidity) GetValue() float64 {
|
||||||
return h.HumidityValue
|
return h.HumidityValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Humidity) SetTillDate(date time.Time) {
|
||||||
|
h.HumidityTillDate = date
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Humidity) SetCreationDate(date *time.Time) {
|
||||||
|
h.CreationDate = date
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Humidity) SetUpdateDate(date *time.Time) {
|
||||||
|
h.UpdateDate = date
|
||||||
|
}
|
||||||
|
@ -1,7 +1,13 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
type MeasuredValue interface {
|
type MeasuredValue interface {
|
||||||
GetID() string
|
GetID() string
|
||||||
|
GetCreationDate() *time.Time
|
||||||
GetSensorID() string
|
GetSensorID() string
|
||||||
GetValue() float64
|
GetValue() float64
|
||||||
|
SetCreationDate(date *time.Time)
|
||||||
|
SetTillDate(date time.Time)
|
||||||
|
SetUpdateDate(date *time.Time)
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,10 @@ type Temperature struct {
|
|||||||
UpdateDate *time.Time `json:"update_date" xml:"update_date"`
|
UpdateDate *time.Time `json:"update_date" xml:"update_date"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Temperature) GetCreationDate() *time.Time {
|
||||||
|
return t.CreationDate
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Temperature) GetID() string {
|
func (t *Temperature) GetID() string {
|
||||||
return t.TemperatureID
|
return t.TemperatureID
|
||||||
}
|
}
|
||||||
@ -24,3 +28,15 @@ func (t *Temperature) GetSensorID() string {
|
|||||||
func (t *Temperature) GetValue() float64 {
|
func (t *Temperature) GetValue() float64 {
|
||||||
return t.TemperatureValue
|
return t.TemperatureValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Temperature) SetTillDate(date time.Time) {
|
||||||
|
t.TemperatureTillDate = date
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Temperature) SetCreationDate(date *time.Time) {
|
||||||
|
t.CreationDate = date
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Temperature) SetUpdateDate(date *time.Time) {
|
||||||
|
t.UpdateDate = date
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user