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"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"sync" "sync"
"time"
"git.cryptic.systems/fh-trier/go-flucky/pkg/internal/errutils" "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) 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 { func (fl *FileLogger) LogHumidities(humidities []*types.Humidity) error {
countLogfiles := len(fl.LogFiles[LogHumidity]) countLogfiles := len(fl.LogFiles[LogHumidity])
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
@ -76,79 +154,36 @@ func (fl *FileLogger) LogHumidities(humidities []*types.Humidity) error {
} }
func (fl *FileLogger) LogTemperatures(temperatures []*types.Temperature) 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] { for _, logfile := range fl.LogFiles[LogTemperature] {
go func(temps []*types.Temperature, lf string) {
defer wg.Done()
if _, err := os.Stat(lf); os.IsNotExist(err) { logTemperatures := []*types.Temperature{}
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()
}
// open logfile // open logfile
f, err := os.Open(lf) f, err := os.OpenFile(logfile, os.O_RDWR, os.ModePerm)
if err != nil { if err != nil {
errChan <- err return fmt.Errorf("Can not open file %v: %v", logfile, err)
return
} }
defer f.Close()
// decode file content into temperatures // if logfile already exists, read temperatures from logfile
logTemperatures := []*types.Temperature{} if _, err := os.Stat(logfile); os.IsExist(err) {
decoder := json.NewDecoder(f) decoder := json.NewDecoder(f)
err = decoder.Decode(&logTemperatures) err = decoder.Decode(&logTemperatures)
if err != nil { if err != nil {
errChan <- err return fmt.Errorf("Can not decode temperatures from reader")
return }
} }
// close file // append new temperatures with exsisting humidities from logfile
f.Close() logTemperatures = append(logTemperatures, temperatures...)
// append new humidities with exsisting humidities from logfile // encode temperatures
logTemperatures = append(logTemperatures, temps...)
// create new logfile
f, err = os.Create(lf)
if err != nil {
errChan <- err
return
}
// convert humidities into json file
encoder := json.NewEncoder(f) encoder := json.NewEncoder(f)
encoder.SetIndent("", " ") encoder.SetIndent("", " ")
err = encoder.Encode(logTemperatures) err = encoder.Encode(logTemperatures)
if err != nil { if err != nil {
errChan <- err return fmt.Errorf("Can not encode temperatures for the writer")
return
} }
f.Close()
}(temperatures, logfile)
}
wg.Wait()
errList := errutils.CollectErrors(errChan)
err := errutils.FormatErrors(errList)
if err != nil {
return err
} }
return nil return nil

View File

@ -1,10 +1,14 @@
package logger package logger
import ( import (
"time"
"git.cryptic.systems/fh-trier/go-flucky/pkg/types" "git.cryptic.systems/fh-trier/go-flucky/pkg/types"
) )
type Logger interface { 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 LogHumidities(humidities []*types.Humidity) error
LogTemperatures(temperatures []*types.Temperature) error LogTemperatures(temperatures []*types.Temperature) error
} }