From 87c075a7533e1a76b9f5ea38861e84e0b3cb93c4 Mon Sep 17 00:00:00 2001 From: Mukti Date: Sun, 8 Sep 2024 19:14:20 +0700 Subject: [PATCH] fix: aggregator to handle enhanced fields (#405) --- cmd/fitactivity/README.md | 6 +++--- cmd/fitactivity/aggregator/aggregator.go | 18 +++++++++--------- cmd/fitactivity/aggregator/aggregator_test.go | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/cmd/fitactivity/README.md b/cmd/fitactivity/README.md index 8a503ccc..10105aad 100644 --- a/cmd/fitactivity/README.md +++ b/cmd/fitactivity/README.md @@ -48,9 +48,9 @@ We will aggregate fields depends on the prefix and suffix of the field name: - prefix **'total**': sum of the two values. (e.g. **total_elapsed_time**) - prefix **'num**' and suffix **'s**': sum of the two values. (e.g. **num_splits**) -- prefix **'max**': max value between two values. (e.g. **max_heart_rate**) -- prefix **'min**': min value between two values. (e.g. **min_cadence**) -- prefix **'avg**': average of the two values. (e.g. **avg_temperature**) +- prefix **'max**' or **'enhanced_max'**: max value between two values. (e.g. **max_heart_rate**, **enhanced_max_respiration_rate**) +- prefix **'min**' or **'enhanced_min'**: min value between two values. (e.g. **min_cadence**, **enhanced_min_altitude**) +- prefix **'avg**' or **'enhanced_avg'**: average of the two values. (e.g. **avg_temperature**, **enhanced_avg_speed**) Otherwise, they will be assigned with value from the corresponding field only if they are invalid. diff --git a/cmd/fitactivity/aggregator/aggregator.go b/cmd/fitactivity/aggregator/aggregator.go index 91f6d016..8f5c932a 100644 --- a/cmd/fitactivity/aggregator/aggregator.go +++ b/cmd/fitactivity/aggregator/aggregator.go @@ -17,9 +17,9 @@ import ( // should be a pointer to a struct, otherwise, it panics. // The logic depends on the prefix of the Field's Name; If it starts with: // - "Total": sum of the two values. -// - "Max": max value between the two values. -// - "Min": min value between the two values. -// - "Avg": avg of the two values. +// - "Max" or "EnhancedMax": max value between the two values. +// - "Min" or "EnhancedMin": min value between the two values. +// - "Avg" or "EnhancedAvg": avg of the two values. // - "Num" and ends with "s": sum of the two values. // (e.g. NumSplits, NumLaps, NumSessions) // - Otherwise: fill with src's Value only if dst's Value is invalid. @@ -41,12 +41,12 @@ func Aggregate[T any](dst, src T) { sum(dv.Field(i), sv.Field(i)) // TotalElapsedTime, TotalCycles, etc. case strings.HasPrefix(f.Name, "Num") && strings.HasSuffix(f.Name, "s"): sum(dv.Field(i), sv.Field(i)) // NumSessions, NumLaps, NumSplits, etc. - case strings.HasPrefix(f.Name, "Max"): - max(dv.Field(i), sv.Field(i)) // MaxHeartRate, MaxCadence, etc. - case strings.HasPrefix(f.Name, "Min"): - min(dv.Field(i), sv.Field(i)) // MinHeartRate, MinCadence, etc. - case strings.HasPrefix(f.Name, "Avg"): - avg(dv.Field(i), sv.Field(i)) // AvgHeartRate, AvgCadence, etc. + case strings.HasPrefix(f.Name, "Max") || strings.HasPrefix(f.Name, "EnhancedMax"): + max(dv.Field(i), sv.Field(i)) // MaxHeartRate, MaxCadence, EnhancedMaxRespirationRate, etc. + case strings.HasPrefix(f.Name, "Min") || strings.HasPrefix(f.Name, "EnhancedMin"): + min(dv.Field(i), sv.Field(i)) // MinHeartRate, MinCadence, EnhancedAltitude, etc. + case strings.HasPrefix(f.Name, "Avg") || strings.HasPrefix(f.Name, "EnhancedAvg"): + avg(dv.Field(i), sv.Field(i)) // AvgHeartRate, AvgCadence, EnhancedAvgSpeed, etc.. default: fill(dv.Field(i), sv.Field(i)) // Timestamp, Sport, Event, etc. } diff --git a/cmd/fitactivity/aggregator/aggregator_test.go b/cmd/fitactivity/aggregator/aggregator_test.go index 481c044e..5c7ed456 100644 --- a/cmd/fitactivity/aggregator/aggregator_test.go +++ b/cmd/fitactivity/aggregator/aggregator_test.go @@ -47,18 +47,36 @@ func TestAggregate(t *testing.T) { src: mesgdef.NewSession(nil).SetMaxAltitude(2000), exp: mesgdef.NewSession(nil).SetMaxAltitude(2000), }, + { + name: "EnhancedMax", + dst: mesgdef.NewSession(nil).SetEnhancedMaxAltitude(1000), + src: mesgdef.NewSession(nil).SetEnhancedMaxAltitude(2000), + exp: mesgdef.NewSession(nil).SetEnhancedMaxAltitude(2000), + }, { name: "Min", dst: mesgdef.NewSession(nil).SetMinHeartRate(60), src: mesgdef.NewSession(nil).SetMinHeartRate(80), exp: mesgdef.NewSession(nil).SetMinHeartRate(60), }, + { + name: "EnhancedMin", + dst: mesgdef.NewSession(nil).SetEnhancedMinAltitude(60), + src: mesgdef.NewSession(nil).SetEnhancedMinAltitude(80), + exp: mesgdef.NewSession(nil).SetEnhancedMinAltitude(60), + }, { name: "Avg", dst: mesgdef.NewSession(nil).SetAvgTemperature(20), src: mesgdef.NewSession(nil).SetAvgTemperature(22), exp: mesgdef.NewSession(nil).SetAvgTemperature(21), }, + { + name: "EnhancedAvg", + dst: mesgdef.NewSession(nil).SetEnhancedAvgAltitude(60), + src: mesgdef.NewSession(nil).SetEnhancedAvgAltitude(80), + exp: mesgdef.NewSession(nil).SetEnhancedAvgAltitude(70), + }, { name: "Fill dst invalid", dst: mesgdef.NewSession(nil),