Skip to content

Commit

Permalink
Hide retired bikes by default
Browse files Browse the repository at this point in the history
  • Loading branch information
liskin committed May 13, 2024
1 parent f10ceb2 commit 9346936
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ For a real life example, take a look at [my own rules.yaml](https://github.com/l
--show-first-last / --hide-first-last
Show first/last usage of components [default: show-first-last]
--show-vert / --hide-vert Show vertical (elevation gain) [default: hide-vert]
--show-retired / --hide-retired
Show retired bikes (on Strava) [default: hide-retired]
--units [metric|imperial] Show data in metric or imperial [default: metric]
--date-start ISO8601 Filter activities: start at or after the specified date(time)
--date-end ISO8601 Filter activities: start before the specified date(time)
Expand Down
5 changes: 5 additions & 0 deletions src/strava_gear/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def get_metavar(self, _param):
@click.option(
'--show-vert/--hide-vert', default=False, show_default=True,
help="Show vertical (elevation gain)")
@click.option(
'--show-retired/--hide-retired', default=False, show_default=True,
help="Show retired bikes (on Strava)")
@click.option(
'--units', type=click.Choice([u.name.lower() for u in Units]), default=Units.METRIC.name.lower(), show_default=True,
callback=lambda _ctx, _param, v: Units[v.upper()], # TODO: drop when Python 3.11 is the oldest supported
Expand All @@ -91,6 +94,7 @@ def cli(
show_name: bool,
show_first_last: bool,
show_vert: bool,
show_retired: bool,
units: Units,
date_start: Optional[datetime],
date_end: Optional[datetime]
Expand All @@ -108,6 +112,7 @@ def cli(
show_name=show_name,
show_first_last=show_first_last,
show_vert=show_vert,
show_bike=lambda b: show_retired or not input.bike_retired(b),
units=units,
)
warn_unknown_bikes(rules, activities)
Expand Down
2 changes: 2 additions & 0 deletions src/strava_gear/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from datetime import timezone
from functools import total_ordering
from itertools import chain
from typing import Callable
from typing import Dict
from typing import Iterable
from typing import Iterator
Expand Down Expand Up @@ -38,6 +39,7 @@
class Input:
activities: List[Dict]
aliases: Dict[BikeName, BikeId] = field(default_factory=dict)
bike_retired: Callable[[BikeId], bool] = lambda _: False


@total_ordering
Expand Down
7 changes: 7 additions & 0 deletions src/strava_gear/input/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ def read_strava_offline(db_filename: Union[str, PathLike]) -> Input:
assert essential_columns <= set(r.keys())
activities.append({**r, 'start_date': parse_datetime(r['start_date'])})

# Strava doesn't return any retired bikes in /api/v3/athlete,
# which is where strava-offline gets its list of bikes from
# (if it ever does, there's an (undocumented) "retired" field
# in the SummaryGear record)
known_bike_ids = set(aliases.values())
bike_retired = lambda b: b not in known_bike_ids # noqa: E731

return Input(
activities=activities,
aliases=aliases,
Expand Down
17 changes: 13 additions & 4 deletions src/strava_gear/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from enum import Enum
from enum import auto
from functools import partial
from typing import Callable
from typing import Dict
from typing import Final
from typing import Iterator
Expand Down Expand Up @@ -31,6 +32,7 @@ def report(
show_name: bool,
show_first_last: bool,
show_vert: bool,
show_bike: Callable[[BikeId], bool],
units: Units,
):
def cols(d: Dict) -> Dict:
Expand All @@ -50,7 +52,7 @@ def cols(d: Dict) -> Dict:
del d["vert m"]
return d

table = [cols(d) for d in f(res)]
table = [cols(d) for d in f(res, show_bike=show_bike)]
if not table:
return

Expand All @@ -62,7 +64,7 @@ def cols(d: Dict) -> Dict:
print(tabulate(table, headers="keys", floatfmt=".1f", tablefmt=tablefmt), file=output)


def report_components(res: Result) -> Iterator[Dict]:
def report_components(res: Result, **_kwargs) -> Iterator[Dict]:
for c in sorted(res.components, key=lambda c: (c.firstlast, c.ident,)):
yield {
"id": c.ident,
Expand All @@ -76,14 +78,21 @@ def report_components(res: Result) -> Iterator[Dict]:
}


def report_bikes(res: Result) -> Iterator[Dict]:
def report_bikes(res: Result, show_bike: Callable[[BikeId], bool]) -> Iterator[Dict]:
bikes_firstlasts = bikes_firstlast(res)

def sort_key(c: Component):
assert c.assignment
return bikes_firstlasts[c.assignment.bike], c.assignment

for c in sorted((c for c in res.components if c.assignment), key=sort_key):
for c in sorted((
c
for c in res.components
if c.assignment
if show_bike(c.assignment.bike)
),
key=sort_key
):
assert c.assignment
yield {
"bike": res.bike_names.get(c.assignment.bike, c.assignment.bike),
Expand Down
2 changes: 2 additions & 0 deletions tests/readme/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
--show-first-last / --hide-first-last
Show first/last usage of components [default: show-first-last]
--show-vert / --hide-vert Show vertical (elevation gain) [default: hide-vert]
--show-retired / --hide-retired
Show retired bikes (on Strava) [default: hide-retired]
--units [metric|imperial] Show data in metric or imperial [default: metric]
--date-start ISO8601 Filter activities: start at or after the specified date(time)
--date-end ISO8601 Filter activities: start before the specified date(time)
Expand Down

0 comments on commit 9346936

Please sign in to comment.