fix(pkg/logfile): use one instead of several logfiles for all measured values

This commit is contained in:
2019-06-26 23:36:01 +02:00
parent 072a902376
commit 3dae3e38be
10 changed files with 67 additions and 100 deletions

View File

@ -185,6 +185,8 @@ func (jl *jsonLogfile) Write(measuredValues []types.MeasuredValue) error {
}
}
writeCreationDate(measuredValues)
f, err := os.Create(jl.logfile)
if err != nil {
return fmt.Errorf("%v %v: %v", errorLogfileCreate, jl.logfile, err)

View File

@ -3,39 +3,13 @@ package logfile
import (
"path/filepath"
"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 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
// 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
@ -91,10 +65,10 @@ func New(logfile string) Logfile {
return &jsonLogfile{
logfile: logfile,
}
// case ".xml":
// return &xmlLogfile{
// logfile: logfile,
// }
// case ".xml":
// return &xmlLogfile{
// logfile: logfile,
// }
default:
return &jsonLogfile{
logfile: logfile,
@ -103,56 +77,11 @@ func New(logfile string) Logfile {
}
// // SplittTemperatures into multiple arrays. The Size can be defined by
// // temperatureSplitBy parameter.
// func SplittTemperatures(temperatures []*types.Temperature, templeratureSplitBy int) [][]*types.Temperature {
// splittedTemperatures := make([][]*types.Temperature, 0)
// newTemperatures := make([]*types.Temperature, 0)
// 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
// }
// }
// }
func writeCreationDate(measuredValues []types.MeasuredValue) {
now := time.Now()
for _, measuredValue := range measuredValues {
if measuredValue.GetCreationDate() == nil {
measuredValue.SetCreationDate(&now)
}
}
}