Skip to content

Commit 630c8a8

Browse files
authored
fix: allow enhanced fields (#113)
* chore: add .gitignnore * fix: allow enhanced fields
1 parent 5990a0b commit 630c8a8

File tree

5 files changed

+107
-24
lines changed

5 files changed

+107
-24
lines changed

src/wasm/activity-service/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode/settings.json

src/wasm/activity-service/activity/lap.go

+29-8
Original file line numberDiff line numberDiff line change
@@ -342,16 +342,27 @@ func (l *Lap) MarshalAppendJSON(b []byte) []byte {
342342
b = strconv.AppendUint(b, uint64(l.TotalCalories), 10)
343343
b = append(b, ',')
344344
}
345-
if l.AvgSpeed != basetype.Uint16Invalid {
345+
346+
avgSpeed := l.AvgSpeedScaled()
347+
if math.IsNaN(avgSpeed) {
348+
avgSpeed = l.EnhancedAvgSpeedScaled()
349+
}
350+
if !math.IsNaN(avgSpeed) {
346351
b = append(b, `"avgSpeed":`...)
347-
b = strconv.AppendFloat(b, l.AvgSpeedScaled(), 'g', -1, 64)
352+
b = strconv.AppendFloat(b, avgSpeed, 'g', -1, 64)
348353
b = append(b, ',')
349354
}
350-
if l.MaxSpeed != basetype.Uint16Invalid {
355+
356+
maxSpeed := l.MaxSpeedScaled()
357+
if math.IsNaN(maxSpeed) {
358+
maxSpeed = l.EnhancedMaxSpeedScaled()
359+
}
360+
if !math.IsNaN(maxSpeed) {
351361
b = append(b, `"maxSpeed":`...)
352-
b = strconv.AppendFloat(b, l.MaxSpeedScaled(), 'g', -1, 64)
362+
b = strconv.AppendFloat(b, maxSpeed, 'g', -1, 64)
353363
b = append(b, ',')
354364
}
365+
355366
if l.AvgHeartRate != basetype.Uint8Invalid {
356367
b = append(b, `"avgHeartRate":`...)
357368
b = strconv.AppendUint(b, uint64(l.AvgHeartRate), 10)
@@ -392,14 +403,24 @@ func (l *Lap) MarshalAppendJSON(b []byte) []byte {
392403
b = strconv.AppendInt(b, int64(l.MaxTemperature), 10)
393404
b = append(b, ',')
394405
}
395-
if l.AvgAltitude != basetype.Uint16Invalid {
406+
407+
avgAltitude := l.AvgAltitudeScaled()
408+
if math.IsNaN(avgAltitude) {
409+
avgAltitude = l.EnhancedAvgAltitudeScaled()
410+
}
411+
if !math.IsNaN(avgAltitude) {
396412
b = append(b, `"avgAltitude":`...)
397-
b = strconv.AppendFloat(b, l.AvgAltitudeScaled(), 'g', -1, 64)
413+
b = strconv.AppendFloat(b, avgAltitude, 'g', -1, 64)
398414
b = append(b, ',')
399415
}
400-
if l.MaxAltitude != basetype.Uint16Invalid {
416+
417+
maxAltitude := l.MaxAltitudeScaled()
418+
if math.IsNaN(maxAltitude) {
419+
maxAltitude = l.EnhancedMaxAltitudeScaled()
420+
}
421+
if !math.IsNaN(maxAltitude) {
401422
b = append(b, `"maxAltitude":`...)
402-
b = strconv.AppendFloat(b, l.MaxAltitudeScaled(), 'g', -1, 64)
423+
b = strconv.AppendFloat(b, maxAltitude, 'g', -1, 64)
403424
b = append(b, ',')
404425
}
405426

src/wasm/activity-service/activity/preprocessor.go

+29-4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ func (p *Preprocessor) AggregateByTimestamp(records []Record) []Record {
101101
rec.Altitude = next.Altitude
102102
}
103103

104+
if rec.EnhancedAltitude != basetype.Uint32Invalid && next.EnhancedAltitude != basetype.Uint32Invalid {
105+
rec.EnhancedAltitude = uint32((uint64(rec.EnhancedAltitude) + uint64(next.EnhancedAltitude)) / 2)
106+
} else if next.EnhancedAltitude != basetype.Uint32Invalid {
107+
rec.EnhancedAltitude = next.EnhancedAltitude
108+
}
109+
104110
if rec.Cadence != basetype.Uint8Invalid && next.Cadence != basetype.Uint8Invalid {
105111
rec.Cadence = uint8((uint16(rec.Cadence) + uint16(next.Cadence)) / 2)
106112
} else if next.Cadence != basetype.Uint8Invalid {
@@ -113,6 +119,12 @@ func (p *Preprocessor) AggregateByTimestamp(records []Record) []Record {
113119
rec.Speed = next.Speed
114120
}
115121

122+
if rec.EnhancedSpeed != basetype.Uint32Invalid && next.Speed != basetype.Uint16Invalid {
123+
rec.EnhancedSpeed = uint32((uint64(rec.EnhancedSpeed) + uint64(next.EnhancedSpeed)) / 2)
124+
} else if next.EnhancedSpeed != basetype.Uint32Invalid {
125+
rec.EnhancedSpeed = next.EnhancedSpeed
126+
}
127+
116128
if rec.Distance != basetype.Uint32Invalid && next.Distance != basetype.Uint32Invalid {
117129
rec.Distance = uint32((uint64(rec.Distance) + uint64(next.Distance)) / 2)
118130
} else if next.Distance != basetype.Uint32Invalid {
@@ -186,8 +198,11 @@ func (p *Preprocessor) SmoothingElevation(records []Record) {
186198
// Copy altitude value
187199
for i := range records {
188200
rec := &records[i]
189-
if rec.Altitude != basetype.Uint16Invalid {
201+
switch {
202+
case rec.Altitude != basetype.Uint16Invalid:
190203
rec.SmoothedAltitude = rec.AltitudeScaled()
204+
case rec.EnhancedAltitude != basetype.Uint32Invalid:
205+
rec.SmoothedAltitude = rec.EnhancedAltitudeScaled()
191206
}
192207
}
193208

@@ -226,6 +241,9 @@ func (p *Preprocessor) CalculateGrade(records []Record) {
226241
if math.IsNaN(altitude) {
227242
altitude = rec.AltitudeScaled()
228243
}
244+
if math.IsNaN(altitude) {
245+
altitude = rec.EnhancedAltitudeScaled()
246+
}
229247

230248
if rec.Distance == basetype.Uint32Invalid || math.IsNaN(altitude) {
231249
continue
@@ -239,6 +257,9 @@ func (p *Preprocessor) CalculateGrade(records []Record) {
239257
if math.IsNaN(nextAltitude) {
240258
nextAltitude = next.AltitudeScaled()
241259
}
260+
if math.IsNaN(nextAltitude) {
261+
nextAltitude = next.EnhancedAltitudeScaled()
262+
}
242263

243264
if next.Distance == basetype.Uint32Invalid || math.IsNaN(nextAltitude) {
244265
continue
@@ -273,11 +294,15 @@ func (p *Preprocessor) CalculatePace(sport typedef.Sport, records []Record) {
273294
continue
274295
}
275296

276-
if !IsConsideredMoving(sport, rec.SpeedScaled()) {
297+
speed := rec.SpeedScaled()
298+
if math.IsNaN(speed) {
299+
speed = rec.EnhancedSpeedScaled()
300+
}
301+
if !IsConsideredMoving(sport, speed) {
277302
continue
278303
}
279304

280-
if rec.Speed == basetype.Uint16Invalid {
305+
if math.IsNaN(speed) {
281306
pointDistance := rec.DistanceScaled() - prev.DistanceScaled()
282307
elapsed := rec.Timestamp.Sub(prev.Timestamp).Seconds()
283308
pointDistanceInKM := pointDistance / 1000
@@ -286,7 +311,7 @@ func (p *Preprocessor) CalculatePace(sport typedef.Sport, records []Record) {
286311
}
287312
rec.Pace = elapsed / pointDistanceInKM
288313
} else {
289-
speedkph := rec.SpeedScaled() * 3.6
314+
speedkph := speed * 3.6
290315
rec.Pace = (1 / speedkph) * 60 * 60
291316
}
292317
}

src/wasm/activity-service/activity/record.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,20 @@ func (r *Record) MarshalAppendJSON(b []byte) []byte {
7070
b = strconv.AppendFloat(b, r.DistanceScaled(), 'g', -1, 64)
7171
b = append(b, ',')
7272
}
73-
if !math.IsNaN(r.SmoothedAltitude) {
73+
74+
altitude := r.SmoothedAltitude
75+
if math.IsNaN(altitude) {
76+
altitude = r.AltitudeScaled()
77+
}
78+
if math.IsNaN(altitude) {
79+
altitude = r.EnhancedAltitudeScaled()
80+
}
81+
if !math.IsNaN(altitude) {
7482
b = append(b, `"altitude":`...)
75-
b = strconv.AppendFloat(b, r.SmoothedAltitude, 'g', -1, 64)
83+
b = strconv.AppendFloat(b, altitude, 'g', -1, 64)
7684
b = append(b, ',')
7785
}
86+
7887
if r.HeartRate != basetype.Uint8Invalid {
7988
b = append(b, `"heartRate":`...)
8089
b = strconv.AppendUint(b, uint64(r.HeartRate), 10)
@@ -85,11 +94,17 @@ func (r *Record) MarshalAppendJSON(b []byte) []byte {
8594
b = strconv.AppendUint(b, uint64(r.Cadence), 10)
8695
b = append(b, ',')
8796
}
88-
if r.Speed != basetype.Uint16Invalid {
97+
98+
speed := r.SpeedScaled()
99+
if math.IsNaN(speed) {
100+
speed = r.EnhancedAltitudeScaled()
101+
}
102+
if !math.IsNaN(speed) {
89103
b = append(b, `"speed":`...)
90-
b = strconv.AppendFloat(b, r.SpeedScaled(), 'g', -1, 64)
104+
b = strconv.AppendFloat(b, speed, 'g', -1, 64)
91105
b = append(b, ',')
92106
}
107+
93108
if r.Power != basetype.Uint16Invalid {
94109
b = append(b, `"power":`...)
95110
b = strconv.AppendUint(b, uint64(r.Power), 10)

src/wasm/activity-service/activity/session.go

+29-8
Original file line numberDiff line numberDiff line change
@@ -452,16 +452,27 @@ func (s *Session) MarshalAppendJSON(b []byte) []byte {
452452
b = strconv.AppendUint(b, uint64(s.TotalCalories), 10)
453453
b = append(b, ',')
454454
}
455-
if s.AvgSpeed != basetype.Uint16Invalid {
455+
456+
avgSpeed := s.AvgSpeedScaled()
457+
if math.IsNaN(avgSpeed) {
458+
avgSpeed = s.EnhancedAvgSpeedScaled()
459+
}
460+
if !math.IsNaN(avgSpeed) {
456461
b = append(b, `"avgSpeed":`...)
457-
b = strconv.AppendFloat(b, s.AvgSpeedScaled(), 'g', -1, 64)
462+
b = strconv.AppendFloat(b, avgSpeed, 'g', -1, 64)
458463
b = append(b, ',')
459464
}
460-
if s.MaxSpeed != basetype.Uint16Invalid {
465+
466+
maxSpeed := s.MaxSpeedScaled()
467+
if math.IsNaN(maxSpeed) {
468+
maxSpeed = s.EnhancedMaxSpeedScaled()
469+
}
470+
if !math.IsNaN(maxSpeed) {
461471
b = append(b, `"maxSpeed":`...)
462-
b = strconv.AppendFloat(b, s.MaxSpeedScaled(), 'g', -1, 64)
472+
b = strconv.AppendFloat(b, maxSpeed, 'g', -1, 64)
463473
b = append(b, ',')
464474
}
475+
465476
if s.AvgHeartRate != basetype.Uint8Invalid {
466477
b = append(b, `"avgHeartRate":`...)
467478
b = strconv.AppendUint(b, uint64(s.AvgHeartRate), 10)
@@ -502,14 +513,24 @@ func (s *Session) MarshalAppendJSON(b []byte) []byte {
502513
b = strconv.AppendInt(b, int64(s.MaxTemperature), 10)
503514
b = append(b, ',')
504515
}
505-
if s.AvgAltitude != basetype.Uint16Invalid {
516+
517+
avgAltitude := s.AvgAltitudeScaled()
518+
if math.IsNaN(avgAltitude) {
519+
avgAltitude = s.EnhancedAvgAltitudeScaled()
520+
}
521+
if !math.IsNaN(avgAltitude) {
506522
b = append(b, `"avgAltitude":`...)
507-
b = strconv.AppendFloat(b, s.AvgAltitudeScaled(), 'g', -1, 64)
523+
b = strconv.AppendFloat(b, avgAltitude, 'g', -1, 64)
508524
b = append(b, ',')
509525
}
510-
if s.MaxAltitude != basetype.Uint16Invalid {
526+
527+
maxAltitude := s.MaxAltitudeScaled()
528+
if math.IsNaN(maxAltitude) {
529+
maxAltitude = s.EnhancedMaxAltitudeScaled()
530+
}
531+
if !math.IsNaN(maxAltitude) {
511532
b = append(b, `"maxAltitude":`...)
512-
b = strconv.AppendFloat(b, s.MaxAltitudeScaled(), 'g', -1, 64)
533+
b = strconv.AppendFloat(b, maxAltitude, 'g', -1, 64)
513534
b = append(b, ',')
514535
}
515536

0 commit comments

Comments
 (0)