2019-06-13 21:21:41 +00:00
|
|
|
package logfile
|
|
|
|
|
|
|
|
import (
|
2019-06-26 21:16:26 +00:00
|
|
|
"path/filepath"
|
2019-06-26 21:36:01 +00:00
|
|
|
|
2019-08-20 19:37:45 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/internal/format"
|
2019-06-26 21:36:01 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
2019-06-13 21:21:41 +00:00
|
|
|
)
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
// 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}$")
|
2019-06-30 12:34:13 +00:00
|
|
|
|
2019-07-14 17:06:26 +00:00
|
|
|
// Append adds an array of several measured values to a logfile
|
2019-10-11 10:36:00 +00:00
|
|
|
func Append(logfile Logfile, measuredValues []*types.MeasuredValue) error {
|
2019-06-30 12:34:13 +00:00
|
|
|
|
|
|
|
allMeasuredValues, err := logfile.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
allMeasuredValues = append(allMeasuredValues, measuredValues...)
|
|
|
|
|
|
|
|
err = logfile.Write(allMeasuredValues)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-21 09:48:44 +00:00
|
|
|
|
2019-06-21 12:31:56 +00:00
|
|
|
// New returns a log file with basic functions for reading and writing data. The
|
|
|
|
// file extension of the logfile is taken into account to format the logfile
|
2019-06-19 17:18:01 +00:00
|
|
|
// into the correct format.
|
2019-06-26 21:16:26 +00:00
|
|
|
func New(logfile string) Logfile {
|
2019-06-25 20:22:34 +00:00
|
|
|
|
2019-06-28 11:04:04 +00:00
|
|
|
ext := filepath.Ext(logfile)
|
2019-06-26 21:16:26 +00:00
|
|
|
|
|
|
|
switch ext {
|
2019-06-28 11:04:04 +00:00
|
|
|
case ".csv":
|
|
|
|
return &csvLogfile{
|
|
|
|
logfile: logfile,
|
|
|
|
}
|
2019-06-26 21:16:26 +00:00
|
|
|
case ".json":
|
|
|
|
return &jsonLogfile{
|
|
|
|
logfile: logfile,
|
|
|
|
}
|
2019-07-03 16:46:19 +00:00
|
|
|
case ".xml":
|
|
|
|
return &xmlLogfile{
|
|
|
|
logfile: logfile,
|
|
|
|
}
|
2019-06-26 21:16:26 +00:00
|
|
|
default:
|
|
|
|
return &jsonLogfile{
|
|
|
|
logfile: logfile,
|
|
|
|
}
|
2019-06-15 13:45:35 +00:00
|
|
|
}
|
2019-06-26 21:16:26 +00:00
|
|
|
|
2019-06-15 13:45:35 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 19:37:45 +00:00
|
|
|
func writeCreationDate(measuredValues []*types.MeasuredValue) error {
|
2019-10-11 10:36:00 +00:00
|
|
|
now := format.FormatedTime()
|
2019-06-26 21:36:01 +00:00
|
|
|
for _, measuredValue := range measuredValues {
|
2019-10-11 10:36:00 +00:00
|
|
|
if measuredValue.CreationDate == nil {
|
|
|
|
measuredValue.CreationDate = &now
|
|
|
|
}
|
2019-06-26 21:36:01 +00:00
|
|
|
}
|
2019-08-20 19:37:45 +00:00
|
|
|
return nil
|
2019-06-26 21:36:01 +00:00
|
|
|
}
|