Skip to content

Commit 43fd8fb

Browse files
committed
Refactor _build_user_prompt function in enrich_trip.py to simplify unit system handling
- Removed parameters for unit distance and temperature, standardizing to kilometers and Celsius. - Updated user prompt messages to reflect the new fixed unit system, enhancing clarity and consistency.
1 parent 69df748 commit 43fd8fb

2 files changed

Lines changed: 60 additions & 14 deletions

File tree

app/tasks/enrich_trip.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,10 @@
114114
]
115115

116116

117-
def _build_user_prompt(trip: Trip, unit_distance: str, unit_temperature: str) -> str:
118-
dist_label = "miles" if unit_distance == "MI" else "kilometers"
119-
elev_label = "feet" if unit_distance == "MI" else "meters"
120-
temp_label = "Fahrenheit" if unit_temperature == "F" else "Celsius"
121-
117+
def _build_user_prompt(trip: Trip) -> str:
122118
lines = [
123119
f"Trail/Location: {trip.location}",
124-
f"Unit system: distance in {dist_label}, elevation in {elev_label}, temperature in {temp_label}",
120+
"Unit system: distance in kilometers, elevation in meters, temperature in Celsius",
125121
"",
126122
]
127123

@@ -136,12 +132,12 @@ def _build_user_prompt(trip: Trip, unit_distance: str, unit_temperature: str) ->
136132
lines.append("Current field status:")
137133

138134
field_labels = {
139-
"distance": f"Distance ({dist_label})",
140-
"daily_elevation_gain": f"Daily elevation gain ({elev_label})",
135+
"distance": "Distance (kilometers)",
136+
"daily_elevation_gain": "Daily elevation gain (meters)",
141137
"terrain": "Terrain type",
142138
"pace": "Pace",
143-
"temp_min": f"Temperature min ({temp_label})",
144-
"temp_max": f"Temperature max ({temp_label})",
139+
"temp_min": "Temperature min (Celsius)",
140+
"temp_max": "Temperature max (Celsius)",
145141
"temp_category": "Temperature category",
146142
"trail_system": "Trail system",
147143
}
@@ -188,13 +184,10 @@ def enrich_trip(self, trip_id: int):
188184
trip.enrich_status = "processing"
189185
session.commit()
190186

191-
unit_distance = user.unit_distance or "MI"
192-
unit_temperature = user.unit_temperature or "F"
193-
194187
logger.info("Enriching trip %s: %s", trip_id, trip.location)
195188

196189
try:
197-
user_prompt = _build_user_prompt(trip, unit_distance, unit_temperature)
190+
user_prompt = _build_user_prompt(trip)
198191
response = ai_complete(
199192
system=SYSTEM_PROMPT,
200193
user=user_prompt,

migrations/canonical_units.sql

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
-- Canonical Units Migration
2+
-- Converts existing trip and hiker profile data to canonical metric units.
3+
-- All numeric trip fields will be stored as: km, meters, °C
4+
-- All hiker profile fields will be stored as: kg, cm
5+
--
6+
-- IMPORTANT: Run this migration BEFORE deploying the new frontend code.
7+
-- The new frontend expects all DB values to be in metric.
8+
--
9+
-- This migration is idempotent for metric users (multiplying by 1 effectively).
10+
-- For imperial users, it converts their data to metric.
11+
12+
BEGIN;
13+
14+
-- Step 1: Convert trip distance (miles -> km) and elevation (feet -> meters)
15+
-- for users whose unit_distance is 'MI'
16+
UPDATE trip
17+
SET
18+
distance = ROUND(CAST(distance * 1.60934 AS NUMERIC), 2),
19+
daily_elevation_gain = ROUND(CAST(daily_elevation_gain * 0.3048 AS NUMERIC), 0)
20+
FROM "user"
21+
WHERE trip.user_id = "user".id
22+
AND "user".unit_distance = 'MI'
23+
AND (trip.distance IS NOT NULL OR trip.daily_elevation_gain IS NOT NULL);
24+
25+
-- Step 2: Convert trip temperatures (°F -> °C)
26+
-- for users whose unit_temperature is 'F' or NULL (default is F)
27+
UPDATE trip
28+
SET
29+
temp_min = ROUND(CAST((temp_min - 32) * 5.0 / 9.0 AS NUMERIC), 0),
30+
temp_max = ROUND(CAST((temp_max - 32) * 5.0 / 9.0 AS NUMERIC), 0)
31+
FROM "user"
32+
WHERE trip.user_id = "user".id
33+
AND (COALESCE("user".unit_temperature, 'F') = 'F')
34+
AND (trip.temp_min IS NOT NULL OR trip.temp_max IS NOT NULL);
35+
36+
-- Step 3: Convert hiker profile weight (lb -> kg) and height (in -> cm)
37+
-- for users whose unit_weight is 'IMPERIAL'
38+
UPDATE hikerprofile
39+
SET
40+
weight = ROUND(CAST(weight * 0.453592 AS NUMERIC), 1),
41+
height = ROUND(CAST(height * 2.54 AS NUMERIC), 1)
42+
FROM "user"
43+
WHERE hikerprofile.user_id = "user".id
44+
AND "user".unit_weight = 'IMPERIAL'
45+
AND (hikerprofile.weight IS NOT NULL OR hikerprofile.height IS NOT NULL);
46+
47+
-- Step 4: Set unit_temperature for users who have it NULL/empty
48+
-- so the new settings UI has a known starting value
49+
UPDATE "user"
50+
SET unit_temperature = 'F'
51+
WHERE unit_temperature IS NULL OR unit_temperature = '';
52+
53+
COMMIT;

0 commit comments

Comments
 (0)