Skip to content

Commit

Permalink
Add top hundred tables
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnScolaro committed Dec 31, 2023
1 parent 92f41fa commit b0f2645
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 4 deletions.
4 changes: 4 additions & 0 deletions backend/active_statistics/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
personal_bests_tab,
polyline_grid_tab,
polyline_overlay_tab,
top_100_longest_rides_tab,
top_100_longest_runs_tab,
)
from active_statistics.tabs.tab_group import TabGroup
from active_statistics.tabs.tabs import Tab
Expand Down Expand Up @@ -48,6 +50,8 @@
min_and_max_elevation_activities_tab,
general_trivia_tab,
flagged_activities_tab,
top_100_longest_runs_tab,
top_100_longest_rides_tab,
],
),
TabGroup(
Expand Down
24 changes: 24 additions & 0 deletions backend/active_statistics/gui/tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
pace_timeline,
personal_bests,
)
from active_statistics.statistics.tables import top_hundred
from active_statistics.statistics.tables.flagged_activities import (
flagged_activities_table,
)
Expand Down Expand Up @@ -130,6 +131,29 @@
table_function=flagged_activities_table,
)

top_100_longest_runs_tab = TableTab(
name="Top 100 Longest Runs",
description="Shows the top 100 longest runs you've ever done.",
table_function=top_hundred.get_top_hundred_table_function(
"Run",
"distance",
"Distance (km)",
top_hundred.distance_conversion_function_to_km,
),
detailed=False,
)

top_100_longest_rides_tab = TableTab(
name="Top 100 Longest Rides",
description="Shows the top 100 longest rides you've ever done.",
table_function=top_hundred.get_top_hundred_table_function(
"Ride",
"distance",
"Distance (km)",
top_hundred.distance_conversion_function_to_km,
),
detailed=False,
)

polyline_overlay_tab = ImageTab(
name="Polyline Overlay",
Expand Down
63 changes: 63 additions & 0 deletions backend/active_statistics/statistics/tables/top_hundred.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
The idea is that this is a generic "top 100's" table creator for all Strava
activities. I can simply say I want "a table of the top 100 longest runs" or
"shortest runs" or "most kudosed" or "hottest" activities or whatever, and this
code can handle getting that data, and displaying it later in a table.
"""

from typing import Any, Callable, Iterator

import pandas as pd
from active_statistics.statistics.utils.strava_links import get_activity_url, get_link
from stravalib import unithelper as uh
from stravalib.model import Activity, ActivityType


def get_top_hundred_table_function(
activity_type: ActivityType,
attribute_name: str,
attribute_column_name: str,
attribute_conversion_function: Callable[[Any], Any],
) -> Callable[[Iterator[Activity]], pd.DataFrame]:
def top_hundred_table_function(activities: Iterator[Activity]) -> pd.DataFrame:
activity_names = []
attribute_column = []
activity_links = []

for activity in activities:
if activity.type == activity_type:
activity_names.append(activity.name)
attribute_column.append(
attribute_conversion_function(getattr(activity, attribute_name))
)
activity_links.append(get_link(get_activity_url(activity.id)))

# Create a DataFrame with the specified attribute as a column
data = {
"Activity Name": activity_names,
attribute_column_name: attribute_column,
"Activity Link": activity_links,
}
df = pd.DataFrame(data)

# Sort the df
df = df.sort_values(by=[attribute_column_name], ascending=False)

# Only keep top 100
df = df.head(100)

# Add a rank column as the first column
df.insert(0, "rank", df.reset_index().index + 1)

# Shorten the values
df[attribute_column_name] = df[attribute_column_name].apply(
lambda x: f"{x:.2f}"
)

return df

return top_hundred_table_function


def distance_conversion_function_to_km(quantity: uh.Quantity) -> float:
return float(uh.kilometers(quantity).magnitude)
18 changes: 14 additions & 4 deletions backend/active_statistics/tabs/table_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,20 @@ def has_column_headings(self):

def get_columns(self, df: pd.DataFrame) -> list[dict[str, str]]:
column_types = self.get_table_column_types()
return [
{"column_name": col, "column_type": column_types.get(col, "string")}
for col in df.columns
]

# Default column types are "string" for all columns UNLESS there is a
# single instance of a LinkCell in that column.
cols: list[dict[str, str]] = []
for col in df.columns:
col_type = "string"
if any(isinstance(obj, LinkCell) for obj in df[col]):
col_type = "link"

cols.append(
{"column_name": col, "column_type": column_types.get(col, col_type)}
)

return cols

def get_table_data(self, activities: Iterator[Activity]) -> dict[str, Any]:
df = self.get_table_dataframe(activities)
Expand Down
41 changes: 41 additions & 0 deletions backend/tests/test_tables/test_top_hundred_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from active_statistics.statistics.tables import top_hundred


class TestTopHundredLongestRuns:
def test_cumulative_time_spent(self, some_basic_runs_and_rides) -> None:
table_function = top_hundred.get_top_hundred_table_function(
"Run",
"distance",
"Distance (km)",
top_hundred.distance_conversion_function_to_km,
)
table_function(some_basic_runs_and_rides)

def test_no_data(self, no_activities_at_all) -> None:
table_function = top_hundred.get_top_hundred_table_function(
"Run",
"distance",
"Distance (km)",
top_hundred.distance_conversion_function_to_km,
)
table_function(no_activities_at_all)


class TestTopHundredLongestRides:
def test_cumulative_time_spent(self, some_basic_runs_and_rides) -> None:
table_function = top_hundred.get_top_hundred_table_function(
"Ride",
"distance",
"Distance (km)",
top_hundred.distance_conversion_function_to_km,
)
table_function(some_basic_runs_and_rides)

def test_no_data(self, no_activities_at_all) -> None:
table_function = top_hundred.get_top_hundred_table_function(
"Ride",
"distance",
"Distance (km)",
top_hundred.distance_conversion_function_to_km,
)
table_function(no_activities_at_all)

0 comments on commit b0f2645

Please sign in to comment.