fix: file logger

This commit is contained in:
Markus Pesch 2019-03-04 17:45:46 +01:00
parent a243021c70
commit 453bf8e440
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
2 changed files with 103 additions and 64 deletions

View File

@ -6,8 +6,8 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"
"git.cryptic.systems/fh-trier/go-flucky/pkg/internal/errutils"
@ -23,6 +23,84 @@ func (fl *FileLogger) AddLogFile(logfile string, logValue LogValue) {
fl.LogFiles[logValue] = append(fl.LogFiles[logValue], logfile)
}
func (fl *FileLogger) GetHumidities(fromDate *time.Time, tillDate *time.Time, sensorNameOrUUID *[]string) ([]*types.Humidity, error) {
return nil, nil
}
func (fl *FileLogger) GetTemperatures(from *time.Time, till *time.Time, sensorIDs []string) ([]*types.Temperature, error) {
cachedTemperatures := []*types.Temperature{}
// iterate over all configured temperature logfiles
for _, logfile := range fl.LogFiles[LogTemperature] {
// skip logfile if not exists
if _, err := os.Stat(logfile); os.IsNotExist(err) {
return nil, fmt.Errorf("Logfile %v not exists", logfile)
}
loggedTemperatures := []*types.Temperature{}
f, err := os.Open(logfile)
if err != nil {
return nil, fmt.Errorf("Can not open %v: %v", logfile, err)
}
defer f.Close()
decoder := json.NewDecoder(f)
err = decoder.Decode(&loggedTemperatures)
if err != nil {
return nil, fmt.Errorf("Can not decode temperatures from logfile %v: %v", logfile, err)
}
// sort all redundant values out
for _, loggedTemperature := range loggedTemperatures {
found := false
for _, cachedTemperature := range cachedTemperatures {
if cachedTemperature.TemperatureID == loggedTemperature.TemperatureID {
found = true
break
}
}
if !found {
cachedTemperatures = append(cachedTemperatures, loggedTemperature)
}
}
}
// // sort out all temperatures which are not mesured by sensor id
// for i, cachedTemperature := range cachedTemperatures {
// found := false
// for _, sensorID := range sensorIDs {
// if cachedTemperature.SensorID == sensorID {
// found = true
// break
// }
// }
// if !found {
// cachedTemperatures = append(cachedTemperatures[:i], cachedTemperatures[i+1:]...)
// }
// }
// // sort out all temperatures which are before from date
// if from != nil {
// for i, cachedTemperature := range cachedTemperatures {
// if cachedTemperature.TemperatureDate.Before(*from) {
// cachedTemperatures = append(cachedTemperatures[:i], cachedTemperatures[i+1:]...)
// }
// }
// }
// // sort out all temperatures which are after till date
// if till != nil {
// for i, cachedTemperature := range cachedTemperatures {
// if cachedTemperature.TemperatureDate.After(*till) {
// cachedTemperatures = append(cachedTemperatures[:i], cachedTemperatures[i+1:]...)
// }
// }
// }
return cachedTemperatures, nil
}
func (fl *FileLogger) LogHumidities(humidities []*types.Humidity) error {
countLogfiles := len(fl.LogFiles[LogHumidity])
wg := sync.WaitGroup{}
@ -76,79 +154,36 @@ func (fl *FileLogger) LogHumidities(humidities []*types.Humidity) error {
}
func (fl *FileLogger) LogTemperatures(temperatures []*types.Temperature) error {
countLogfiles := len(fl.LogFiles[LogTemperature])
wg := sync.WaitGroup{}
wg.Add(countLogfiles)
errChan := make(chan error)
for _, logfile := range fl.LogFiles[LogTemperature] {
go func(temps []*types.Temperature, lf string) {
defer wg.Done()
if _, err := os.Stat(lf); os.IsNotExist(err) {
err := os.MkdirAll(filepath.Dir(lf), os.ModeDir)
if err != nil {
errChan <- fmt.Errorf("Can not create directory: %v", err)
return
}
f, err := os.Create(lf)
if err != nil {
errChan <- fmt.Errorf("Can not create log file: %v", err)
return
}
f.Close()
}
logTemperatures := []*types.Temperature{}
// open logfile
f, err := os.Open(lf)
f, err := os.OpenFile(logfile, os.O_RDWR, os.ModePerm)
if err != nil {
errChan <- err
return
return fmt.Errorf("Can not open file %v: %v", logfile, err)
}
defer f.Close()
// decode file content into temperatures
logTemperatures := []*types.Temperature{}
// if logfile already exists, read temperatures from logfile
if _, err := os.Stat(logfile); os.IsExist(err) {
decoder := json.NewDecoder(f)
err = decoder.Decode(&logTemperatures)
if err != nil {
errChan <- err
return
return fmt.Errorf("Can not decode temperatures from reader")
}
}
// close file
f.Close()
// append new temperatures with exsisting humidities from logfile
logTemperatures = append(logTemperatures, temperatures...)
// append new humidities with exsisting humidities from logfile
logTemperatures = append(logTemperatures, temps...)
// create new logfile
f, err = os.Create(lf)
if err != nil {
errChan <- err
return
}
// convert humidities into json file
// encode temperatures
encoder := json.NewEncoder(f)
encoder.SetIndent("", " ")
err = encoder.Encode(logTemperatures)
if err != nil {
errChan <- err
return
return fmt.Errorf("Can not encode temperatures for the writer")
}
f.Close()
}(temperatures, logfile)
}
wg.Wait()
errList := errutils.CollectErrors(errChan)
err := errutils.FormatErrors(errList)
if err != nil {
return err
}
return nil

View File

@ -1,10 +1,14 @@
package logger
import (
"time"
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
)
type Logger interface {
GetHumidities(fromDate *time.Time, tillDate *time.Time, sensorNameOrUUID *[]string) ([]*types.Humidity, error)
GetTemperatures(fromDate *time.Time, tillDate *time.Time, sensorNameOrUUID *[]string) ([]*types.Temperature, error)
LogHumidities(humidities []*types.Humidity) error
LogTemperatures(temperatures []*types.Temperature) error
}