package logfile import ( "path/filepath" "time" "github.com/go-flucky/flucky/pkg/types" ) // 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}$") var timeFormat = time.RFC3339 // 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 Compression(measuredValues []types) []*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 // } // 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 // into the correct format. func New(logfile string) Logfile { ext := filepath.Join(logfile) switch ext { // case ".csv": // return &csvLogfile{ // logfile: logfile, // } case ".json": return &jsonLogfile{ logfile: logfile, } // case ".xml": // return &xmlLogfile{ // logfile: logfile, // } default: return &jsonLogfile{ logfile: logfile, } } } func writeCreationDate(measuredValues []types.MeasuredValue) { now := time.Now() for _, measuredValue := range measuredValues { if measuredValue.GetCreationDate() == nil { measuredValue.SetCreationDate(&now) } } }