aboutsummaryrefslogtreecommitdiff
path: root/ride/store.go
diff options
context:
space:
mode:
Diffstat (limited to 'ride/store.go')
-rw-r--r--ride/store.go17
1 files changed, 11 insertions, 6 deletions
diff --git a/ride/store.go b/ride/store.go
index 9429acf..cc053ba 100644
--- a/ride/store.go
+++ b/ride/store.go
@@ -19,7 +19,7 @@ type StoredRide struct {
GPXPath string
FitPath string
Name string
- Type string
+ ExternalSportType string
StartDate time.Time
DistanceM float64
ExternalMovingTimeS int64
@@ -49,7 +49,7 @@ const (
SortCotacolKm SortColumn = "cotacol_100km"
)
-const columns = "id, external_id, gpx_path, fit_path, name, type, start_date, distance_m, external_moving_time_s, moving_time_s, moving_time_algo_version, elapsed_time_s, total_elevation_gain_m, average_speed_mps, cotacol_score, cotacol_algo_version, bike_type, ride_type, created_at, updated_at"
+const columns = "id, external_id, gpx_path, fit_path, name, external_sport_type, start_date, distance_m, external_moving_time_s, moving_time_s, moving_time_algo_version, elapsed_time_s, total_elevation_gain_m, average_speed_mps, cotacol_score, cotacol_algo_version, bike_type, ride_type, created_at, updated_at"
func Save(db *sql.DB, r StoredRide) error {
var cotacolScore any
@@ -83,13 +83,13 @@ func Save(db *sql.DB, r StoredRide) error {
return fmt.Errorf("invalid ride_type %q", r.RideType)
}
_, err := db.Exec(`
- INSERT INTO rides (external_id, gpx_path, fit_path, name, type, start_date, distance_m, external_moving_time_s, moving_time_s, moving_time_algo_version, elapsed_time_s, total_elevation_gain_m, average_speed_mps, cotacol_score, cotacol_algo_version, bike_type, ride_type)
+ INSERT INTO rides (external_id, gpx_path, fit_path, name, external_sport_type, start_date, distance_m, external_moving_time_s, moving_time_s, moving_time_algo_version, elapsed_time_s, total_elevation_gain_m, average_speed_mps, cotacol_score, cotacol_algo_version, bike_type, ride_type)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(external_id) DO UPDATE SET
gpx_path = excluded.gpx_path,
fit_path = excluded.fit_path,
name = excluded.name,
- type = excluded.type,
+ external_sport_type = excluded.external_sport_type,
start_date = excluded.start_date,
distance_m = excluded.distance_m,
external_moving_time_s = excluded.external_moving_time_s,
@@ -103,7 +103,7 @@ func Save(db *sql.DB, r StoredRide) error {
bike_type = CASE WHEN excluded.bike_type = '' THEN rides.bike_type ELSE excluded.bike_type END,
ride_type = CASE WHEN excluded.ride_type = '' THEN rides.ride_type ELSE excluded.ride_type END,
updated_at = strftime('%Y-%m-%dT%H:%M:%SZ', 'now')
- `, r.ExternalID, r.GPXPath, r.FitPath, r.Name, r.Type, r.StartDate.UTC().Format(time.RFC3339), r.DistanceM, r.ExternalMovingTimeS, movingTimeS, movingTimeAlgorithmVersion, r.ElapsedTimeS, r.TotalElevationGainM, r.AverageSpeedMps, cotacolScore, cotacolAlgorithmVersion, string(r.BikeType), string(r.RideType))
+ `, r.ExternalID, r.GPXPath, r.FitPath, r.Name, r.ExternalSportType, r.StartDate.UTC().Format(time.RFC3339), r.DistanceM, r.ExternalMovingTimeS, movingTimeS, movingTimeAlgorithmVersion, r.ElapsedTimeS, r.TotalElevationGainM, r.AverageSpeedMps, cotacolScore, cotacolAlgorithmVersion, string(r.BikeType), string(r.RideType))
return err
}
@@ -173,6 +173,11 @@ func Backfill(db *sql.DB, reporters ...func(current, total int)) (int, error) {
slog.Warn("Failed to stat FIT file", "ride", item.ExternalID, "file", fitPath, "error", err)
}
}
+ if item.RideType == "" {
+ if inferred, ok := BikeTypeForExternalSportType(item.ExternalSportType); ok {
+ item.BikeType = inferred
+ }
+ }
if err := Save(db, item); err != nil {
slog.Warn("Failed to backfill ride values", "ride", item.ExternalID, "file", item.GPXPath, "error", err)
continue
@@ -276,7 +281,7 @@ func scanRide(s scanner) (StoredRide, error) {
createdAt string
updatedAt string
)
- err := s.Scan(&ride.ID, &ride.ExternalID, &ride.GPXPath, &ride.FitPath, &ride.Name, &ride.Type, &startDate, &ride.DistanceM, &ride.ExternalMovingTimeS, &movingTimeS, &movingTimeAlgoVer, &ride.ElapsedTimeS, &ride.TotalElevationGainM, &ride.AverageSpeedMps, &cotacolScore, &cotacolAlgoVersion, &bikeType, &rideType, &createdAt, &updatedAt)
+ err := s.Scan(&ride.ID, &ride.ExternalID, &ride.GPXPath, &ride.FitPath, &ride.Name, &ride.ExternalSportType, &startDate, &ride.DistanceM, &ride.ExternalMovingTimeS, &movingTimeS, &movingTimeAlgoVer, &ride.ElapsedTimeS, &ride.TotalElevationGainM, &ride.AverageSpeedMps, &cotacolScore, &cotacolAlgoVersion, &bikeType, &rideType, &createdAt, &updatedAt)
if err != nil {
return StoredRide{}, err
}