2019-06-13 21:21:41 +00:00
|
|
|
package logfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-06-14 19:17:06 +00:00
|
|
|
"sort"
|
|
|
|
"time"
|
2019-06-13 21:21:41 +00:00
|
|
|
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
2019-06-13 21:21:41 +00:00
|
|
|
)
|
|
|
|
|
2019-06-14 19:17:06 +00:00
|
|
|
// 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
|
|
|
|
// validity date of the predecessor value is set to that of the current value.
|
|
|
|
// No information is lost as a result. The validity period of the measured value
|
|
|
|
// is thereby exclusively increased.
|
|
|
|
func CompressTemperature(temperatures []*types.Temperature) []*types.Temperature {
|
|
|
|
compressedTemperatures := make([]*types.Temperature, 0)
|
|
|
|
lastTemperatureBySensors := make(map[string]*types.Temperature, 0)
|
|
|
|
|
|
|
|
// Sort all measured temperatures beforehand by the starting validity date to
|
|
|
|
// avoid errors when compressing the temperatures.
|
|
|
|
SortTemperatures(temperatures)
|
|
|
|
|
|
|
|
for _, temperature := range temperatures {
|
|
|
|
if lastTemperatureBySensor, ok := lastTemperatureBySensors[temperature.SensorID]; ok {
|
|
|
|
if lastTemperatureBySensor.TemperatureValue == temperature.TemperatureValue {
|
|
|
|
|
|
|
|
lastTemperatureBySensors[temperature.SensorID].TemperatureTillDate = temperature.TemperatureTillDate
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
lastTemperatureBySensors[temperature.SensorID].UpdateDate = &now
|
|
|
|
} else {
|
|
|
|
compressedTemperatures = append(compressedTemperatures, lastTemperatureBySensors[temperature.SensorID])
|
|
|
|
lastTemperatureBySensors[temperature.SensorID] = temperature
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lastTemperatureBySensors[temperature.SensorID] = temperature
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy all remaining entries from the map into the array
|
|
|
|
for _, lastTemperatureBySensor := range lastTemperatureBySensors {
|
|
|
|
compressedTemperatures = append(compressedTemperatures, lastTemperatureBySensor)
|
|
|
|
}
|
|
|
|
|
|
|
|
return compressedTemperatures
|
|
|
|
}
|
|
|
|
|
2019-06-15 10:12:57 +00:00
|
|
|
// ReadTemperatures from a file and returns an array with temperatures
|
2019-06-14 19:17:06 +00:00
|
|
|
func ReadTemperatures(temperatureLogfile string) ([]*types.Temperature, error) {
|
|
|
|
|
|
|
|
if _, err := os.Stat(temperatureLogfile); os.IsNotExist(err) {
|
|
|
|
return nil, fmt.Errorf("Can not find temperature logfile %v", temperatureLogfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
temperatures := make([]*types.Temperature, 0)
|
|
|
|
|
|
|
|
f, err := os.Open(temperatureLogfile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Can not open temperature logfile %v", temperatureLogfile)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
temperatures, err = ReadTemperaturesCustom(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Can not read temperatures from logfile %v", temperatureLogfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
return temperatures, nil
|
|
|
|
}
|
|
|
|
|
2019-06-15 13:49:21 +00:00
|
|
|
// ReadTemperaturesChannel reads temperatures from a channel until it is closed
|
|
|
|
// and returns the temperature.
|
2019-06-15 13:45:35 +00:00
|
|
|
func ReadTemperaturesChannel(temperatureChannel <-chan *types.Temperature) []*types.Temperature {
|
|
|
|
temperatures := make([]*types.Temperature, 0)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case temperature, more := <-temperatureChannel:
|
|
|
|
if more {
|
|
|
|
temperatures = append(temperatures, temperature)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return temperatures
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 10:12:57 +00:00
|
|
|
// ReadTemperaturesCustom from a custom reader and returns an array with
|
|
|
|
// temperatures
|
2019-06-14 19:17:06 +00:00
|
|
|
func ReadTemperaturesCustom(r io.Reader) ([]*types.Temperature, error) {
|
|
|
|
|
|
|
|
temperatures := make([]*types.Temperature, 0)
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(r)
|
|
|
|
err := decoder.Decode(&temperatures)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Can not decode temperatures from reader: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return temperatures, nil
|
|
|
|
}
|
|
|
|
|
2019-06-15 10:12:57 +00:00
|
|
|
// SplittTemperatures into multiple arrays. The Size can be defined by
|
|
|
|
// temperatureSplitBy parameter.
|
|
|
|
func SplittTemperatures(temperatures []*types.Temperature, templeratureSplitBy int) [][]*types.Temperature {
|
2019-06-14 19:17:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-06-15 10:12:57 +00:00
|
|
|
// SortTemperatures by TemperatureFromDate
|
2019-06-14 19:17:06 +00:00
|
|
|
func SortTemperatures(temperatures []*types.Temperature) {
|
|
|
|
sort.SliceStable(temperatures, func(i int, j int) bool {
|
|
|
|
return temperatures[i].TemperatureFromDate.Before(temperatures[j].TemperatureFromDate)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-15 10:12:57 +00:00
|
|
|
// WriteTemperatures encode temperatures into json and write it into a file.
|
|
|
|
// Compression can be enabled over a bolean parameter
|
2019-06-14 19:17:06 +00:00
|
|
|
func WriteTemperatures(temperatures []*types.Temperature, temperatureLogfile string, compression bool) error {
|
2019-06-13 21:21:41 +00:00
|
|
|
|
|
|
|
allTemperatures := make([]*types.Temperature, 0)
|
|
|
|
|
|
|
|
if _, err := os.Stat(temperatureLogfile); os.IsNotExist(err) {
|
|
|
|
err := os.MkdirAll(filepath.Dir(temperatureLogfile), 0755)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can not create directory %v to write temperatures into the logfile", filepath.Dir(temperatureLogfile))
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(temperatureLogfile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can not create file %v: %v", temperatureLogfile, err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
f, err := os.Open(temperatureLogfile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can not open file %v: %v", temperatureLogfile, err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
savedTemperatures, err := ReadTemperaturesCustom(f)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can not read temperatures from logfile %v: %v", temperatureLogfile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
allTemperatures = append(allTemperatures, savedTemperatures...)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(temperatureLogfile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can not create file %v: %v", temperatureLogfile, err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
allTemperatures = append(allTemperatures, temperatures...)
|
|
|
|
|
2019-06-14 19:17:06 +00:00
|
|
|
err = WriteTemperaturesCustom(allTemperatures, f, compression)
|
2019-06-13 21:21:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can not write temperatures to logfile %v: %v", temperatureLogfile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-15 10:12:57 +00:00
|
|
|
// WriteTemperaturesCustom encode temperatures into json and write it into
|
|
|
|
// custom writer. Compression can be enabled over a bolean parameter
|
2019-06-14 19:17:06 +00:00
|
|
|
func WriteTemperaturesCustom(temperatures []*types.Temperature, w io.Writer, compression bool) error {
|
|
|
|
|
|
|
|
if compression {
|
|
|
|
temperatures = CompressTemperature(temperatures)
|
|
|
|
}
|
|
|
|
|
|
|
|
writeCreationDate(temperatures)
|
|
|
|
|
2019-06-13 21:21:41 +00:00
|
|
|
jsonEncoder := json.NewEncoder(w)
|
2019-06-15 13:45:35 +00:00
|
|
|
jsonEncoder.SetIndent("", " ")
|
2019-06-13 21:21:41 +00:00
|
|
|
err := jsonEncoder.Encode(temperatures)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Can not encode temperatures: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-14 19:17:06 +00:00
|
|
|
func writeCreationDate(temperatures []*types.Temperature) {
|
|
|
|
now := time.Now()
|
|
|
|
for _, temperature := range temperatures {
|
2019-06-15 10:04:14 +00:00
|
|
|
if temperature.CreationDate == nil {
|
|
|
|
temperature.CreationDate = &now
|
|
|
|
}
|
2019-06-13 21:21:41 +00:00
|
|
|
}
|
|
|
|
}
|