1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
package web
import (
"fmt"
"net/url"
"time"
"github.com/martinlehoux/biking_home/climb"
"github.com/martinlehoux/biking_home/mountain_pass"
"github.com/martinlehoux/biking_home/official_climb"
"github.com/martinlehoux/biking_home/ride"
"github.com/martinlehoux/biking_home/rideanalysis"
"github.com/martinlehoux/biking_home/strava"
)
type RideView struct {
Ride ride.StoredRide
Cotacol string
CotacolPer100Km string
MovingTime string
}
type RidesPageData struct {
Items []RideView
Headers []RideSortHeader
Filters RideFilterView
Sort RideSort
}
type AnalyticsPageData struct {
Points []AnalyticsPoint
Filter RideFilterView
}
type AnalyticsPoint struct {
ID int64 `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
SpeedKmh float64 `json:"speedKmh"`
CotacolPer100Km float64 `json:"cotacolPer100Km"`
MovingHours float64 `json:"movingHours"`
RideType string `json:"rideType"`
BikeType string `json:"bikeType"`
Year int `json:"year"`
}
func buildAnalyticsPoints(items []ride.StoredRide) []AnalyticsPoint {
points := make([]AnalyticsPoint, 0, len(items))
for _, item := range items {
cotacol, ready := item.CotacolScore()
if !ready || item.CotacolAlgorithmVersion() != climb.CotacolAlgorithmVersion || item.DistanceM <= 0 {
continue
}
points = append(points, AnalyticsPoint{
ID: item.ID,
Name: item.Name,
URL: rideDetailURL(item.ID),
SpeedKmh: item.AverageSpeedMps * 3.6,
CotacolPer100Km: cotacol * 100 / (item.DistanceM / 1000),
MovingHours: float64(item.EffectiveMovingTimeS()) / 3600,
RideType: string(item.RideType),
BikeType: string(item.BikeType),
Year: item.StartDate.Year(),
})
}
return points
}
type RideDetailView struct {
RideView
Route GeoJSONFeatureCollection
Passes []RideMapPass
HasRoute bool
Profile RideProfile
RouteError string
ActionError string
Notice string
ExternalURL string
Preview bool
OfficialClimbFormURL string
}
type RideMapPass struct {
Name string `json:"name"`
ElevationM int `json:"elevationM"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Category string `json:"category"`
}
type RideProfile struct {
Points []RideProfilePoint `json:"points"`
Climbs []RideProfileClimb `json:"climbs"`
Crossings []RideProfileCrossing `json:"crossings"`
}
func (profile RideProfile) OfficialClimbs() []RideProfileClimb {
climbs := make([]RideProfileClimb, 0)
for _, climb := range profile.Climbs {
if climb.OfficialClimbID > 0 {
climbs = append(climbs, climb)
}
}
return climbs
}
func (profile RideProfile) UnmatchedClimbs() []RideProfileClimb {
climbs := make([]RideProfileClimb, 0)
for _, climb := range profile.Climbs {
if climb.OfficialClimbID == 0 {
climbs = append(climbs, climb)
}
}
return climbs
}
type RideProfilePoint struct {
Index int `json:"index"`
DistanceKm float64 `json:"distanceKm"`
ElevationM float64 `json:"elevationM"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
type RideProfileClimb struct {
Index int `json:"-"`
StartKm float64 `json:"startKm"`
EndKm float64 `json:"endKm"`
TopKm float64 `json:"topKm"`
TopElevationM float64 `json:"topElevationM"`
Name string `json:"name"`
Category string `json:"category"`
DistanceKm float64 `json:"distanceKm"`
SlopePercent float64 `json:"slopePercent"`
Cotacol float64 `json:"cotacol"`
OfficialClimbID int64 `json:"officialClimbId,omitempty"`
OfficialName string `json:"officialName,omitempty"`
StartIndex int `json:"startIndex"`
EndIndex int `json:"endIndex"`
}
type RideProfileCrossing struct {
DistanceKm float64 `json:"distanceKm"`
PassElevation float64 `json:"passElevationM"`
RideElevation float64 `json:"rideElevationM"`
DistanceToM float64 `json:"distanceToM"`
ElevationDiff float64 `json:"elevationDiffM"`
Name string `json:"name"`
}
type GeoJSONFeatureCollection struct {
Type string `json:"type"`
Features []GeoJSONFeature `json:"features"`
}
type GeoJSONFeature struct {
Type string `json:"type"`
Geometry GeoJSONGeometry `json:"geometry"`
Properties map[string]any `json:"properties,omitempty"`
}
type GeoJSONGeometry struct {
Type string `json:"type"`
Coordinates [][]float64 `json:"coordinates"`
}
type RideSort struct {
Column string
Descending bool
}
type RideSortHeader struct {
Label string
Class string
URL string
AriaSort string
Indicator string
}
const (
rideSortName = "name"
rideSortStarted = "started"
rideSortDistance = "distance"
rideSortMovingTime = "moving_time"
rideSortSpeed = "speed"
rideSortElevation = "elevation"
rideSortCotacol = "cotacol"
rideSortCotacolKm = "cotacol_100km"
)
var rideSortColumns = map[string]bool{
rideSortName: true,
rideSortStarted: true,
rideSortDistance: true,
rideSortMovingTime: true,
rideSortSpeed: true,
rideSortElevation: true,
rideSortCotacol: true,
rideSortCotacolKm: true,
}
func parseRideSort(query url.Values) RideSort {
column := query.Get("sort")
if !rideSortColumns[column] {
column = rideSortStarted
}
direction := query.Get("dir")
return RideSort{Column: column, Descending: direction != "asc"}
}
func (s RideSort) databaseColumn() (ride.SortColumn, bool) {
columns := map[string]ride.SortColumn{
rideSortName: ride.SortName,
rideSortStarted: ride.SortStartDate,
rideSortDistance: ride.SortDistance,
rideSortMovingTime: ride.SortMovingTime,
rideSortSpeed: ride.SortSpeed,
rideSortElevation: ride.SortElevation,
rideSortCotacol: ride.SortCotacol,
rideSortCotacolKm: ride.SortCotacolKm,
}
column, found := columns[s.Column]
return column, found
}
func rideSortHeaders(current RideSort) []RideSortHeader {
return rideSortHeadersWithFilters(current, RideFilters{})
}
func rideSortHeadersWithFilters(current RideSort, filters RideFilters) []RideSortHeader {
columns := []struct {
key string
label string
class string
}{
{key: rideSortName, label: "Ride"},
{key: rideSortStarted, label: "Started"},
{key: rideSortDistance, label: "Distance", class: "numeric"},
{key: rideSortMovingTime, label: "Moving time", class: "numeric"},
{key: rideSortSpeed, label: "Speed", class: "numeric"},
{key: rideSortElevation, label: "Elevation", class: "numeric"},
{key: rideSortCotacol, label: "Cotacol", class: "numeric"},
{key: rideSortCotacolKm, label: "Cotacol / 100 km", class: "numeric"},
}
headers := make([]RideSortHeader, 0, len(columns))
for _, column := range columns {
descending := false
active := current.Column == column.key
if active {
descending = !current.Descending
}
query := rideFilterQuery(filters)
query.Set("sort", column.key)
query.Set("dir", rideSortDirection(RideSort{Column: column.key, Descending: descending}))
headerClass := column.class
ariaSort := "none"
indicator := ""
if active {
headerClass += " active"
if current.Descending {
ariaSort = "descending"
indicator = "↓"
} else {
ariaSort = "ascending"
indicator = "↑"
}
}
headers = append(headers, RideSortHeader{
Label: column.label,
Class: headerClass,
URL: "/?" + query.Encode(),
AriaSort: ariaSort,
Indicator: indicator,
})
}
return headers
}
func rideSortDirection(sort RideSort) string {
if sort.Descending {
return "desc"
}
return "asc"
}
func rideDetailURL(id int64) string {
return fmt.Sprintf("/rides/%d", id)
}
func officialClimbCreateURL(id int64) string {
return fmt.Sprintf("/rides/%d/official-climbs", id)
}
func buildRideDetailView(view RideView, parsed ride.Ride, passes []mountain_pass.MountainPass, officialClimbs []official_climb.OfficialClimb, matchPolicy official_climb.MatchPolicy) RideDetailView {
coords := parsed.Coords
coordinates := make([][]float64, len(coords))
for i, coordinate := range coords {
coordinates[i] = []float64{coordinate.Lon, coordinate.Lat}
}
profile := buildRideProfile(parsed, passes, officialClimbs, matchPolicy)
detail := RideDetailView{
RideView: view,
Passes: buildRideMapPasses(passes),
ExternalURL: externalRideURL(view.Ride.ExternalID),
OfficialClimbFormURL: officialClimbCreateURL(view.Ride.ID),
HasRoute: true,
Route: GeoJSONFeatureCollection{
Type: "FeatureCollection",
Features: []GeoJSONFeature{{
Type: "Feature",
Geometry: GeoJSONGeometry{
Type: "LineString",
Coordinates: coordinates,
},
}},
},
Profile: profile,
}
return detail
}
func externalRideURL(externalID string) string {
const prefix = "strava:"
if len(externalID) > len(prefix) && externalID[:len(prefix)] == prefix {
id := externalID[len(prefix):]
if id != "" {
return "https://www.strava.com/activities/" + id
}
}
return ""
}
func buildRideMapPasses(passes []mountain_pass.MountainPass) []RideMapPass {
mapPasses := make([]RideMapPass, 0, len(passes))
for _, mountainPass := range passes {
if mountainPass.Coord == nil {
continue
}
mapPasses = append(mapPasses, RideMapPass{
Name: mountainPass.Name,
ElevationM: mountainPass.Elevation,
Latitude: mountainPass.Coord.Lat,
Longitude: mountainPass.Coord.Lon,
Category: mountainPass.Category,
})
}
return mapPasses
}
func buildRideProfile(parsed ride.Ride, passes []mountain_pass.MountainPass, officialClimbs []official_climb.OfficialClimb, matchPolicy official_climb.MatchPolicy) RideProfile {
coords := parsed.Coords
distances := parsed.Distances
elevations := parsed.Elevations
profile := RideProfile{
Points: make([]RideProfilePoint, len(coords)),
Climbs: make([]RideProfileClimb, 0),
Crossings: make([]RideProfileCrossing, 0),
}
for i, coordinate := range coords {
profile.Points[i] = RideProfilePoint{
Index: i,
DistanceKm: distances[i] / 1000,
ElevationM: elevations[i],
Latitude: coordinate.Lat,
Longitude: coordinate.Lon,
}
}
analysis := rideanalysis.Analyze(parsed, passes, officialClimbs, matchPolicy)
for _, analyzedClimb := range analysis.Climbs {
segment := analyzedClimb.Segment
topIndex, topElevationM := segment.Top()
climb := RideProfileClimb{
Index: len(profile.Climbs),
StartKm: segment.Distances[0] / 1000,
EndKm: segment.Distances[len(segment.Distances)-1] / 1000,
TopKm: segment.Distances[topIndex] / 1000,
TopElevationM: topElevationM,
Name: analyzedClimb.Name,
Category: analyzedClimb.Category,
DistanceKm: analyzedClimb.DistanceM / 1000,
SlopePercent: analyzedClimb.SlopePercent,
Cotacol: analyzedClimb.Cotacol,
OfficialClimbID: analyzedClimb.OfficialClimbID,
OfficialName: analyzedClimb.OfficialName,
StartIndex: analyzedClimb.Interval.Start,
EndIndex: analyzedClimb.Interval.End,
}
profile.Climbs = append(profile.Climbs, climb)
}
for _, crossing := range analysis.Crossings {
profile.Crossings = append(profile.Crossings, RideProfileCrossing{
DistanceKm: crossing.RideDistanceM / 1000,
PassElevation: float64(crossing.Pass.Elevation),
RideElevation: crossing.RideElevation,
DistanceToM: crossing.DistanceToM,
ElevationDiff: crossing.ElevationDiff,
Name: crossing.Pass.Name,
})
}
return profile
}
type SyncPageData struct {
From string
To string
Error string
Notice string
HasAuth bool
}
type RateLimitSnapshot struct {
Read strava.RateLimit `json:"read"`
Overall strava.RateLimit `json:"overall"`
}
type SyncProgress struct {
Total int `json:"total"`
Completed int `json:"completed"`
Imported int `json:"imported"`
Skipped int `json:"skipped"`
Updated int `json:"updated"`
RateLimit *RateLimitSnapshot `json:"rateLimit,omitempty"`
}
func formatRideDate(value time.Time) string {
return value.Local().Format("02 Jan 2006, 15:04")
}
func formatDistance(meters float64) string {
return fmt.Sprintf("%.1f km", meters/1000)
}
func formatSpeed(metersPerSecond float64) string {
return fmt.Sprintf("%.1f km/h", metersPerSecond*3.6)
}
func formatDuration(seconds int64) string {
hours := seconds / 3600
minutes := (seconds % 3600) / 60
return fmt.Sprintf("%dh %02dm", hours, minutes)
}
func formatElevation(meters float64) string {
return fmt.Sprintf("%.0f m", meters)
}
func formatCotacol(cotacol float64) string {
return fmt.Sprintf("%.1f", cotacol)
}
func formatCotacolPer100Km(cotacol, distanceM float64) string {
distanceKm := distanceM / 1000
if distanceKm <= 0 {
return "-"
}
return fmt.Sprintf("%.1f", cotacol*100/distanceKm)
}
func formatRouteIndex(index int) string {
return fmt.Sprintf("%d", index)
}
func formatClimbNumber(index int) string {
return fmt.Sprintf("%d", index+1)
}
func formatClimbCount(count int) string {
return fmt.Sprintf("%d", count)
}
func formatRideCount(count int) string {
return fmt.Sprintf("%d", count)
}
func formatProfileDistance(distanceKm float64) string {
return fmt.Sprintf("%.1f km", distanceKm)
}
func formatSlope(slopePercent float64) string {
return fmt.Sprintf("%.1f%%", slopePercent)
}
func officialProfileLabel(name string) string {
return "Elevation profile for " + name
}
func rideDetailColumnsClass(unmatchedClimbCount int) string {
if unmatchedClimbCount == 0 {
return "ride-detail-columns ride-detail-columns-single"
}
return "ride-detail-columns"
}
|