summaryrefslogtreecommitdiff
path: root/web/views.go
blob: f6e84d5136d8823173338cadd50498bf64b7eae0 (plain) (blame)
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
package web

import (
	"fmt"
	"net/url"
	"time"

	"github.com/martinlehoux/biking_home/ride"
	"github.com/martinlehoux/biking_home/rides"
)

type RideView struct {
	rides.Ride
	Cotacol         string
	CotacolPer100Km string
}

type RideDetailView struct {
	RideView
	Route      GeoJSONFeatureCollection
	HasRoute   bool
	RouteError string
}

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"
	rideSortElevation  = "elevation"
	rideSortCotacol    = "cotacol"
	rideSortCotacolKm  = "cotacol_100km"
)

var rideSortColumns = map[string]bool{
	rideSortName:       true,
	rideSortStarted:    true,
	rideSortDistance:   true,
	rideSortMovingTime: 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() (rides.SortColumn, bool) {
	columns := map[string]rides.SortColumn{
		rideSortName:       rides.SortName,
		rideSortStarted:    rides.SortStartDate,
		rideSortDistance:   rides.SortDistance,
		rideSortMovingTime: rides.SortMovingTime,
		rideSortElevation:  rides.SortElevation,
		rideSortCotacol:    rides.SortCotacol,
		rideSortCotacolKm:  rides.SortCotacolKm,
	}
	column, found := columns[s.Column]
	return column, found
}

func rideSortHeaders(current RideSort) []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: 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 := url.Values{}
		query.Set("sort", column.key)
		if descending {
			query.Set("dir", "desc")
		} else {
			query.Set("dir", "asc")
		}
		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 rideDetailURL(id int64) string {
	return fmt.Sprintf("/rides/%d", id)
}

func buildRideDetailView(item rides.Ride, parsed ride.Ride) RideDetailView {
	coordinates := make([][]float64, parsed.Len())
	for i := 0; i < parsed.Len(); i++ {
		coordinate := parsed.Coord(i)
		coordinates[i] = []float64{coordinate.Lon, coordinate.Lat}
	}
	return RideDetailView{
		RideView: buildRideView(item),
		Route: GeoJSONFeatureCollection{
			Type: "FeatureCollection",
			Features: []GeoJSONFeature{{
				Type: "Feature",
				Geometry: GeoJSONGeometry{
					Type:        "LineString",
					Coordinates: coordinates,
				},
			}},
		},
		HasRoute: true,
	}
}

type SyncPageData struct {
	From    string
	To      string
	Error   string
	Notice  string
	HasAuth bool
}

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 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(score float64) string {
	return fmt.Sprintf("%.1f", score)
}

func formatCotacolPer100Km(score, distanceM float64) string {
	distanceKm := distanceM / 1000
	if distanceKm <= 0 {
		return "-"
	}
	return fmt.Sprintf("%.1f", score*100/distanceKm)
}