From 16b96130c73885287ab6d88db27111f5ab732351 Mon Sep 17 00:00:00 2001 From: ben_29 Date: Mon, 3 Jun 2024 21:38:33 +0800 Subject: [PATCH] feat: elevation gain - fix --- run_page/db_updater.py | 7 ++++++- run_page/generator/__init__.py | 2 ++ src/components/RunTable/RunRow.tsx | 2 +- src/components/RunTable/index.tsx | 4 +++- src/utils/utils.ts | 2 +- 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/run_page/db_updater.py b/run_page/db_updater.py index 570ed5aea7d..9777be486d2 100644 --- a/run_page/db_updater.py +++ b/run_page/db_updater.py @@ -1,6 +1,9 @@ from generator.db import init_db, Activity from config import SQL_FILE import sqlalchemy +from sqlalchemy import text +from config import GPX_FOLDER, JSON_FILE, SQL_FILE +from utils import make_activities_file def add_column_elevation_gain(session): @@ -11,10 +14,12 @@ def add_column_elevation_gain(session): print("column elevation_gain already added, skipping") except sqlalchemy.exc.OperationalError: sql_statement = 'alter TABLE "activities" add column elevation_gain Float after average_heartrate' - session.execute(statement=sql_statement) + session.execute(text(sql_statement)) print("column elevation_gain added successfully") if __name__ == "__main__": session = init_db(SQL_FILE) add_column_elevation_gain(session) + # regenerate activities + make_activities_file(SQL_FILE, GPX_FOLDER, JSON_FILE) diff --git a/run_page/generator/__init__.py b/run_page/generator/__init__.py index 9c798718a6f..fcc9e1b63c9 100644 --- a/run_page/generator/__init__.py +++ b/run_page/generator/__init__.py @@ -65,6 +65,8 @@ def sync(self, force): continue if IGNORE_BEFORE_SAVING: activity.summary_polyline = filter_out(activity.summary_polyline) + # strava use total_elevation_gain as elevation_gain + activity.elevation_gain = activity.total_elevation_gain created = update_or_create_activity(self.session, activity) if created: sys.stdout.write("+") diff --git a/src/components/RunTable/RunRow.tsx b/src/components/RunTable/RunRow.tsx index 77d69fef9ac..5f05ea2ebf0 100644 --- a/src/components/RunTable/RunRow.tsx +++ b/src/components/RunTable/RunRow.tsx @@ -11,7 +11,7 @@ interface IRunRowProperties { const RunRow = ({ elementIndex, locateActivity, run, runIndex, setRunIndex }: IRunRowProperties) => { const distance = (run.distance / 1000.0).toFixed(2); - const elevation_gain = run.elevation_gain.toFixed(0); + const elevation_gain = run.elevation_gain?.toFixed(0); const paceParts = run.average_speed ? formatPace(run.average_speed) : null; const heartRate = run.average_heartrate; const runTime = formatRunTime(run.moving_time); diff --git a/src/components/RunTable/index.tsx b/src/components/RunTable/index.tsx index 1929c921a34..2dc83413345 100644 --- a/src/components/RunTable/index.tsx +++ b/src/components/RunTable/index.tsx @@ -31,7 +31,9 @@ const RunTable = ({ const sortKMFunc: SortFunc = (a, b) => sortFuncInfo === 'KM' ? a.distance - b.distance : b.distance - a.distance; const sortElevationGainFunc: SortFunc = (a, b) => - sortFuncInfo === 'Elevation Gain' ? a.elevation_gain - b.elevation_gain : b.elevation_gain - a.elevation_gain; + sortFuncInfo === 'Elevation Gain' + ? (a.elevation_gain ?? 0) - (b.elevation_gain ?? 0) + : (b.elevation_gain ?? 0) - (a.elevation_gain ?? 0); const sortPaceFunc: SortFunc = (a, b) => sortFuncInfo === 'Pace' ? a.average_speed - b.average_speed diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 3237189643a..503e66ac7ab 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -22,7 +22,7 @@ export interface Activity { location_country?: string | null; summary_polyline?: string | null; average_heartrate?: number | null; - elevation_gain: number; + elevation_gain: number | null; average_speed: number; streak: number; }