From 89467f19b929b742c35ae14d89abb3ca816ec435 Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Tue, 11 Aug 2026 20:36:48 +0200 Subject: feat: add interactive ride profile --- web/server.go | 7 +- web/server_test.go | 48 +++++++++++ web/templates.templ | 225 +++++++++++++++++++++++++++++++++++++++++++++++++ web/templates_templ.go | 58 +++++++------ web/views.go | 81 +++++++++++++++++- 5 files changed, 392 insertions(+), 27 deletions(-) (limited to 'web') diff --git a/web/server.go b/web/server.go index 46fe66b..ee769f0 100644 --- a/web/server.go +++ b/web/server.go @@ -18,6 +18,7 @@ import ( "time" "github.com/martinlehoux/biking_home/config" + "github.com/martinlehoux/biking_home/mountain_pass" "github.com/martinlehoux/biking_home/ride" "github.com/martinlehoux/biking_home/rides" "github.com/martinlehoux/biking_home/strava" @@ -103,8 +104,12 @@ func (s *Server) handleRide(w http.ResponseWriter, r *http.Request) { }), w) return } + passes, err := mountain_pass.LoadMountainPasses(s.db) + if err != nil { + slog.Warn("Failed to load mountain passes for ride detail", "ride_id", id, "error", err) + } slog.Info("Loaded ride detail", "ride_id", id) - kcore.RenderPage(r.Context(), RideDetailPage(buildRideDetailView(item, parsed)), w) + kcore.RenderPage(r.Context(), RideDetailPage(buildRideDetailView(item, parsed, passes)), w) } func (s *Server) listRides(rideSort RideSort) ([]rides.Ride, error) { diff --git a/web/server_test.go b/web/server_test.go index 5964c5a..8e4c568 100644 --- a/web/server_test.go +++ b/web/server_test.go @@ -14,7 +14,10 @@ import ( "testing" "time" + "github.com/jftuga/geodist" "github.com/martinlehoux/biking_home/config" + "github.com/martinlehoux/biking_home/mountain_pass" + "github.com/martinlehoux/biking_home/ride" "github.com/martinlehoux/biking_home/rides" "github.com/martinlehoux/biking_home/strava" _ "github.com/mattn/go-sqlite3" @@ -47,6 +50,19 @@ func newWebTestServer(t *testing.T) (*Server, *sql.DB) { ) `) require.NoError(t, err) + _, err = db.Exec(` + create table mountain_passes ( + id integer primary key, + external_id text unique not null, + name text not null, + country_code text not null, + department_code text not null, + elevation integer not null, + latitude real, + longitude real + ) + `) + require.NoError(t, err) configPath := filepath.Join(t.TempDir(), "config.yaml") appConfig := config.Default() appConfig.Strava.ClientID = "123" @@ -142,7 +158,13 @@ func TestHandlerRendersRideDetailWithEmbeddedRoute(t *testing.T) { assert.Equal(t, http.StatusOK, response.Code) assert.Contains(t, body, "Detailed Ride") assert.Contains(t, body, `id="ride-route"`) + assert.Contains(t, body, `id="ride-profile"`) + assert.Contains(t, body, `id="ride-profile-chart"`) assert.Contains(t, body, `id="ride-map"`) + assert.Contains(t, body, "pointermove") + assert.Contains(t, body, "circleMarker") + assert.Contains(t, body, "crossing.passElevationM") + assert.Contains(t, body, "const labelY = plot.top - 6") assert.Contains(t, body, "leaflet@1.9.4/dist/leaflet.js") assert.Contains(t, body, "tile.openstreetmap.org/{z}/{x}/{y}.png") assert.Contains(t, body, `"type":"FeatureCollection"`) @@ -150,6 +172,32 @@ func TestHandlerRendersRideDetailWithEmbeddedRoute(t *testing.T) { assert.Contains(t, body, `"coordinates":[[5,43]`) } +func TestBuildRideProfileIncludesClimbsAndCrossings(t *testing.T) { + parsed := ride.FromColumns( + []float64{0, 1000, 2000}, + []float64{300, 447, 380}, + []geodist.Coord{{Lat: 43.61, Lon: 5.42}, {Lat: 43.62, Lon: 5.43}, {Lat: 43.63, Lon: 5.44}}, + make([]time.Time, 3), + ) + passes := []mountain_pass.MountainPass{{ + Name: "Pas de Magnan", + Elevation: 440, + Coord: &geodist.Coord{Lat: 43.62, Lon: 5.43}, + }} + + profile := buildRideProfile(parsed, passes) + + require.Len(t, profile.Points, 3) + assert.Equal(t, 1.0, profile.Points[1].DistanceKm) + assert.Equal(t, 447.0, profile.Points[1].ElevationM) + require.Len(t, profile.Climbs, 1) + assert.Equal(t, "Pas de Magnan", profile.Climbs[0].Name) + assert.Equal(t, 1.0, profile.Climbs[0].TopKm) + require.Len(t, profile.Crossings, 1) + assert.Equal(t, "Pas de Magnan", profile.Crossings[0].Name) + assert.Equal(t, 1.0, profile.Crossings[0].DistanceKm) +} + func TestHandlerRendersRideDetailWithoutRoute(t *testing.T) { server, db := newWebTestServer(t) item := rides.Ride{ diff --git a/web/templates.templ b/web/templates.templ index 78591e1..b1304a5 100644 --- a/web/templates.templ +++ b/web/templates.templ @@ -43,6 +43,14 @@ templ Layout(title string, content templ.Component) { .empty { padding: 2.5rem 1rem; text-align: center; color: #68746c; } .map-panel { padding: 0; overflow: hidden; } .ride-map { min-height: 24rem; height: min(65vh, 40rem); } + .profile-panel { padding-bottom: 1rem; } + .profile-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 1rem; flex-wrap: wrap; } + .profile-heading h2 { margin: 0; font-size: 1.2rem; } + .profile-heading p { margin: 0; color: #68746c; font-size: .9rem; } + .profile-chart { margin-top: 1rem; } + .profile-chart canvas { display: block; width: 100%; height: 22rem; border-radius: .65rem; background: #fbfcfa; outline: none; } + .profile-chart canvas:focus-visible { box-shadow: 0 0 0 .2rem #18794e66; } + .profile-hover { display: block; min-height: 1.4rem; margin: .7rem 0 0; color: #5b675f; font-size: .9rem; } dl { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; margin: 0; } dt { color: #68746c; font-size: .78rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; } dd { margin: .25rem 0 0; font-size: 1.2rem; font-weight: 700; } @@ -113,16 +121,32 @@ templ RideDetailContent(data RideDetailView) {
{ data.RouteError }
} if data.HasRoute { +
+
+

Elevation profile

+

Hover or focus the profile to inspect the ride at that distance.

+
+
+ +
+ Hover or focus the profile to inspect elevation. +
@templ.JSONScript("ride-route", data.Route) + @templ.JSONScript("ride-profile", data.Profile) } diff --git a/web/templates_templ.go b/web/templates_templ.go index e0fecf8..8c71cd4 100644 --- a/web/templates_templ.go +++ b/web/templates_templ.go @@ -42,7 +42,7 @@ func Layout(title string, content templ.Component) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" · biking_home
biking_home
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" · biking_home
biking_home
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -148,7 +148,7 @@ func RidesContent(items []RideView, headers []RideSortHeader) templ.Component { var templ_7745c5c3_Var7 string templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(header.AriaSort) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 82, Col: 72} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 90, Col: 72} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7)) if templ_7745c5c3_Err != nil { @@ -170,7 +170,7 @@ func RidesContent(items []RideView, headers []RideSortHeader) templ.Component { var templ_7745c5c3_Var9 string templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(header.Label) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 82, Col: 141} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 90, Col: 141} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9)) if templ_7745c5c3_Err != nil { @@ -183,7 +183,7 @@ func RidesContent(items []RideView, headers []RideSortHeader) templ.Component { var templ_7745c5c3_Var10 string templ_7745c5c3_Var10, templ_7745c5c3_Err = templ.JoinStringErrs(header.Indicator) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 82, Col: 209} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 90, Col: 209} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var10)) if templ_7745c5c3_Err != nil { @@ -215,7 +215,7 @@ func RidesContent(items []RideView, headers []RideSortHeader) templ.Component { var templ_7745c5c3_Var12 string templ_7745c5c3_Var12, templ_7745c5c3_Err = templ.JoinStringErrs(item.Name) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 87, Col: 79} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 95, Col: 79} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var12)) if templ_7745c5c3_Err != nil { @@ -228,7 +228,7 @@ func RidesContent(items []RideView, headers []RideSortHeader) templ.Component { var templ_7745c5c3_Var13 string templ_7745c5c3_Var13, templ_7745c5c3_Err = templ.JoinStringErrs(item.Type) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 87, Col: 117} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 95, Col: 117} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var13)) if templ_7745c5c3_Err != nil { @@ -241,7 +241,7 @@ func RidesContent(items []RideView, headers []RideSortHeader) templ.Component { var templ_7745c5c3_Var14 string templ_7745c5c3_Var14, templ_7745c5c3_Err = templ.JoinStringErrs(formatRideDate(item.StartDate)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 87, Col: 168} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 95, Col: 168} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var14)) if templ_7745c5c3_Err != nil { @@ -254,7 +254,7 @@ func RidesContent(items []RideView, headers []RideSortHeader) templ.Component { var templ_7745c5c3_Var15 string templ_7745c5c3_Var15, templ_7745c5c3_Err = templ.JoinStringErrs(formatDistance(item.DistanceM)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 87, Col: 227} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 95, Col: 227} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var15)) if templ_7745c5c3_Err != nil { @@ -267,7 +267,7 @@ func RidesContent(items []RideView, headers []RideSortHeader) templ.Component { var templ_7745c5c3_Var16 string templ_7745c5c3_Var16, templ_7745c5c3_Err = templ.JoinStringErrs(formatDuration(item.MovingTimeS)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 87, Col: 288} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 95, Col: 288} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var16)) if templ_7745c5c3_Err != nil { @@ -280,7 +280,7 @@ func RidesContent(items []RideView, headers []RideSortHeader) templ.Component { var templ_7745c5c3_Var17 string templ_7745c5c3_Var17, templ_7745c5c3_Err = templ.JoinStringErrs(formatElevation(item.TotalElevationGainM)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 87, Col: 358} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 95, Col: 358} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var17)) if templ_7745c5c3_Err != nil { @@ -293,7 +293,7 @@ func RidesContent(items []RideView, headers []RideSortHeader) templ.Component { var templ_7745c5c3_Var18 string templ_7745c5c3_Var18, templ_7745c5c3_Err = templ.JoinStringErrs(item.Cotacol) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 87, Col: 399} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 95, Col: 399} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var18)) if templ_7745c5c3_Err != nil { @@ -306,7 +306,7 @@ func RidesContent(items []RideView, headers []RideSortHeader) templ.Component { var templ_7745c5c3_Var19 string templ_7745c5c3_Var19, templ_7745c5c3_Err = templ.JoinStringErrs(item.CotacolPer100Km) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 87, Col: 448} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 95, Col: 448} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var19)) if templ_7745c5c3_Err != nil { @@ -387,7 +387,7 @@ func RideDetailContent(data RideDetailView) templ.Component { var templ_7745c5c3_Var22 string templ_7745c5c3_Var22, templ_7745c5c3_Err = templ.JoinStringErrs(data.Ride.Name) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 102, Col: 27} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 110, Col: 27} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var22)) if templ_7745c5c3_Err != nil { @@ -400,7 +400,7 @@ func RideDetailContent(data RideDetailView) templ.Component { var templ_7745c5c3_Var23 string templ_7745c5c3_Var23, templ_7745c5c3_Err = templ.JoinStringErrs(data.Ride.Type) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 102, Col: 66} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 110, Col: 66} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var23)) if templ_7745c5c3_Err != nil { @@ -413,7 +413,7 @@ func RideDetailContent(data RideDetailView) templ.Component { var templ_7745c5c3_Var24 string templ_7745c5c3_Var24, templ_7745c5c3_Err = templ.JoinStringErrs(formatRideDate(data.Ride.StartDate)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 102, Col: 109} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 110, Col: 109} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var24)) if templ_7745c5c3_Err != nil { @@ -426,7 +426,7 @@ func RideDetailContent(data RideDetailView) templ.Component { var templ_7745c5c3_Var25 string templ_7745c5c3_Var25, templ_7745c5c3_Err = templ.JoinStringErrs(formatDistance(data.Ride.DistanceM)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 107, Col: 66} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 115, Col: 66} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var25)) if templ_7745c5c3_Err != nil { @@ -439,7 +439,7 @@ func RideDetailContent(data RideDetailView) templ.Component { var templ_7745c5c3_Var26 string templ_7745c5c3_Var26, templ_7745c5c3_Err = templ.JoinStringErrs(formatDuration(data.Ride.MovingTimeS)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 108, Col: 71} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 116, Col: 71} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var26)) if templ_7745c5c3_Err != nil { @@ -452,7 +452,7 @@ func RideDetailContent(data RideDetailView) templ.Component { var templ_7745c5c3_Var27 string templ_7745c5c3_Var27, templ_7745c5c3_Err = templ.JoinStringErrs(formatElevation(data.Ride.TotalElevationGainM)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 109, Col: 78} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 117, Col: 78} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var27)) if templ_7745c5c3_Err != nil { @@ -470,7 +470,7 @@ func RideDetailContent(data RideDetailView) templ.Component { var templ_7745c5c3_Var28 string templ_7745c5c3_Var28, templ_7745c5c3_Err = templ.JoinStringErrs(data.RouteError) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 113, Col: 38} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 121, Col: 38} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var28)) if templ_7745c5c3_Err != nil { @@ -482,7 +482,7 @@ func RideDetailContent(data RideDetailView) templ.Component { } } if data.HasRoute { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Elevation profile

Hover or focus the profile to inspect the ride at that distance.

Hover or focus the profile to inspect elevation.
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -490,7 +490,15 @@ func RideDetailContent(data RideDetailView) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" ") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templ.JSONScript("ride-profile", data.Profile).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" ") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -561,7 +569,7 @@ func SyncContent(data SyncPageData) templ.Component { var templ_7745c5c3_Var31 string templ_7745c5c3_Var31, templ_7745c5c3_Err = templ.JoinStringErrs(data.Notice) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 153, Col: 52} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 378, Col: 52} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var31)) if templ_7745c5c3_Err != nil { @@ -585,7 +593,7 @@ func SyncContent(data SyncPageData) templ.Component { var templ_7745c5c3_Var32 string templ_7745c5c3_Var32, templ_7745c5c3_Err = templ.JoinStringErrs(data.Error) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 158, Col: 49} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 383, Col: 49} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var32)) if templ_7745c5c3_Err != nil { @@ -627,7 +635,7 @@ func SyncContent(data SyncPageData) templ.Component { var templ_7745c5c3_Var34 string templ_7745c5c3_Var34, templ_7745c5c3_Err = templ.JoinStringErrs(data.From) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 169, Col: 64} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 394, Col: 64} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var34)) if templ_7745c5c3_Err != nil { @@ -640,7 +648,7 @@ func SyncContent(data SyncPageData) templ.Component { var templ_7745c5c3_Var35 string templ_7745c5c3_Var35, templ_7745c5c3_Err = templ.JoinStringErrs(data.To) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 170, Col: 58} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `web/templates.templ`, Line: 395, Col: 58} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var35)) if templ_7745c5c3_Err != nil { diff --git a/web/views.go b/web/views.go index a644d1e..2ce9eb2 100644 --- a/web/views.go +++ b/web/views.go @@ -5,6 +5,7 @@ import ( "net/url" "time" + "github.com/martinlehoux/biking_home/mountain_pass" "github.com/martinlehoux/biking_home/ride" "github.com/martinlehoux/biking_home/rides" ) @@ -19,9 +20,42 @@ type RideDetailView struct { RideView Route GeoJSONFeatureCollection HasRoute bool + Profile RideProfile RouteError string } +type RideProfile struct { + Points []RideProfilePoint `json:"points"` + Climbs []RideProfileClimb `json:"climbs"` + Crossings []RideProfileCrossing `json:"crossings"` +} + +type RideProfilePoint struct { + DistanceKm float64 `json:"distanceKm"` + ElevationM float64 `json:"elevationM"` + Latitude float64 `json:"latitude"` + Longitude float64 `json:"longitude"` +} + +type RideProfileClimb struct { + StartKm float64 `json:"startKm"` + EndKm float64 `json:"endKm"` + TopKm float64 `json:"topKm"` + TopElevationM float64 `json:"topElevationM"` + Name string `json:"name"` + Score float64 `json:"score"` + Category string `json:"category"` +} + +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"` @@ -150,12 +184,13 @@ func rideDetailURL(id int64) string { return fmt.Sprintf("/rides/%d", id) } -func buildRideDetailView(item rides.Ride, parsed ride.Ride) RideDetailView { +func buildRideDetailView(item rides.Ride, parsed ride.Ride, passes []mountain_pass.MountainPass) RideDetailView { coordinates := make([][]float64, parsed.Len()) for i := 0; i < parsed.Len(); i++ { coordinate := parsed.Coord(i) coordinates[i] = []float64{coordinate.Lon, coordinate.Lat} } + profile := buildRideProfile(parsed, passes) return RideDetailView{ RideView: buildRideView(item), Route: GeoJSONFeatureCollection{ @@ -169,7 +204,51 @@ func buildRideDetailView(item rides.Ride, parsed ride.Ride) RideDetailView { }}, }, HasRoute: true, + Profile: profile, + } +} + +func buildRideProfile(parsed ride.Ride, passes []mountain_pass.MountainPass) RideProfile { + profile := RideProfile{ + Points: make([]RideProfilePoint, parsed.Len()), + Climbs: make([]RideProfileClimb, 0), + Crossings: make([]RideProfileCrossing, 0), + } + for i := 0; i < parsed.Len(); i++ { + coordinate := parsed.Coord(i) + profile.Points[i] = RideProfilePoint{ + DistanceKm: parsed.DistanceM(i) / 1000, + ElevationM: parsed.ElevationM(i), + Latitude: coordinate.Lat, + Longitude: coordinate.Lon, + } + } + climbs := parsed.AllClimbs() + for i := range climbs { + if matched, ok := mountain_pass.MatchClimb(climbs[i], passes, 300, 50); ok { + climbs[i].Name = matched.Name + } + profile.Climbs = append(profile.Climbs, RideProfileClimb{ + StartKm: climbs[i].StartDistanceM() / 1000, + EndKm: climbs[i].EndDistanceM() / 1000, + TopKm: climbs[i].TopDistanceM() / 1000, + TopElevationM: climbs[i].TopElevationM(), + Name: climbs[i].Name, + Score: climbs[i].Score(), + Category: ride.Category(climbs[i].Score()), + }) + } + for _, crossing := range mountain_pass.DetectCrossings(parsed, passes, 100, 25) { + 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 { -- cgit v1.2.3