diff --git a/pkg/storage/logfile/csv.go b/pkg/storage/logfile/csv.go index e91df8c..6bd3a2b 100644 --- a/pkg/storage/logfile/csv.go +++ b/pkg/storage/logfile/csv.go @@ -112,8 +112,6 @@ func (cl *csvLogfile) Write(measuredValues []*types.MeasuredValue) error { } defer f.Close() - writeCreationDate(measuredValues) - w := csv.NewWriter(f) for _, measuredValue := range measuredValues { diff --git a/pkg/storage/logfile/json.go b/pkg/storage/logfile/json.go index d97f6f7..be19d4e 100644 --- a/pkg/storage/logfile/json.go +++ b/pkg/storage/logfile/json.go @@ -52,8 +52,6 @@ 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) diff --git a/pkg/storage/logfile/logfile.go b/pkg/storage/logfile/logfile.go index aca6727..9a5ff1b 100644 --- a/pkg/storage/logfile/logfile.go +++ b/pkg/storage/logfile/logfile.go @@ -2,9 +2,6 @@ package logfile import ( "path/filepath" - - "github.com/go-flucky/flucky/pkg/internal/format" - "github.com/go-flucky/flucky/pkg/types" ) // New returns a log file with basic functions for reading and writing data. The @@ -31,13 +28,3 @@ func New(logfile string) Logfile { } } } - -func writeCreationDate(measuredValues []*types.MeasuredValue) error { - now := format.FormatedTime() - for _, measuredValue := range measuredValues { - if measuredValue.CreationDate == nil { - measuredValue.CreationDate = &now - } - } - return nil -} diff --git a/pkg/storage/logfile/xml.go b/pkg/storage/logfile/xml.go index f7e2a8f..6fda3ba 100644 --- a/pkg/storage/logfile/xml.go +++ b/pkg/storage/logfile/xml.go @@ -60,8 +60,6 @@ func (xl *xmlLogfile) Write(measuredValues []*types.MeasuredValue) error { } defer f.Close() - writeCreationDate(measuredValues) - cachedMeasuredValues := new(MeasuredValues) for _, measuredValue := range measuredValues { diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index e83da3c..a3539be 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -106,24 +106,28 @@ func Round(measuredValues []*types.MeasuredValue, round float64) { // data. The scheme must be matched to a storage provider, if the scheme is not // implemented, the function returns an error func Write(ctx context.Context, measuredValues []*types.MeasuredValue, storageEndpoint *url.URL) error { + writeCreationDate(measuredValues) switch storageEndpoint.Scheme { case "file": measuredValueLogfile := logfile.New(storageEndpoint.Path) - storedMeasuredValues, err := measuredValueLogfile.Read() - if err != nil { - return err - } - storedMeasuredValues = append(storedMeasuredValues, measuredValues...) - return measuredValueLogfile.Write(storedMeasuredValues) + return measuredValueLogfile.Write(measuredValues) case "postgres": database, err := db.New(storageEndpoint) if err != nil { return err } defer database.Close() - if err := database.InsertMeasuredValues(ctx, measuredValues); err != nil { - return err + return database.InsertMeasuredValues(ctx, measuredValues) + default: + return fmt.Errorf("No supported scheme") + } +} + +func writeCreationDate(measuredValues []*types.MeasuredValue) { + now := format.FormatedTime() + for _, measuredValue := range measuredValues { + if measuredValue.CreationDate == nil { + measuredValue.CreationDate = &now } } - return fmt.Errorf("No supported scheme") }