Skip to content

Commit

Permalink
Add elevation_gain metric for components
Browse files Browse the repository at this point in the history
Co-authored-by: Tomas Janousek <tomi@nomi.cz>
  • Loading branch information
gangitano-gsom and liskin committed Nov 2, 2023
1 parent ab3c874 commit 48a31f5
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ For a real life example, take a look at [my own rules.yaml](https://github.com/l
--rules FILENAME Rules configuration (bikes, components, ...) [default:
/home/user/.config/strava_gear/rules.yaml]
--csv FILENAME Load activities from CSV instead of the strava-offline database (columns: name,
gear_id, start_date, moving_time, distance)
gear_id, start_date, moving_time, distance, total_elevation_gain)
--strava-database PATH Location of the strava-offline database [default:
/home/user/.local/share/strava_offline/strava.sqlite]
-o, --output FILENAME Output file [default: -]
Expand All @@ -292,6 +292,7 @@ For a real life example, take a look at [my own rules.yaml](https://github.com/l
--show-name / --hide-name Show long component names [default: show-name]
--show-first-last / --hide-first-last
Show first/last usage of components [default: show-first-last]
--show-vert-m / --hide-vert-m Show vertical meters (elevation gain) [default: hide-vert-m]
--help Show this message and exit.
<!-- end include -->

Expand Down
14 changes: 12 additions & 2 deletions src/strava_gear/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
'--csv', type=click.File('r'),
help="""
Load activities from CSV instead of the strava-offline database
(columns: name, gear_id, start_date, moving_time, distance)
(columns: name, gear_id, start_date, moving_time, distance, total_elevation_gain)
""")
@click.option(
'--strava-database', type=click.Path(path_type=Path), # type: ignore [type-var] # debian typeshed compat
Expand All @@ -49,6 +49,9 @@
@click.option(
'--show-first-last/--hide-first-last', default=True, show_default=True,
help="Show first/last usage of components")
@click.option(
'--show-vert-m/--hide-vert-m', default=False, show_default=True,
help="Show vertical meters (elevation gain)")
def main(
rules_input: TextIO,
csv: Optional[TextIO],
Expand All @@ -58,14 +61,21 @@ def main(
tablefmt: str,
show_name: bool,
show_first_last: bool,
show_vert_m: bool,
):
if csv:
aliases, activities = read_input_csv(csv)
else:
aliases, activities = read_strava_offline(strava_database)
rules = read_rules(rules_input, aliases=aliases)
res = apply_rules(rules, activities)
reports[report](res, output=output, tablefmt=tablefmt, show_name=show_name, show_first_last=show_first_last)
reports[report](
res,
output=output, tablefmt=tablefmt,
show_name=show_name,
show_first_last=show_first_last,
show_vert_m=show_vert_m,
)
warn_unknown_bikes(rules, activities)


Expand Down
1 change: 1 addition & 0 deletions src/strava_gear/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def usage_for_activity(activity: Dict, rule: Rule) -> Usage:
return Usage.from_activity(
components=component_map.values(),
distance=activity['distance'],
elevation_gain=activity['total_elevation_gain'],
time=activity['moving_time'],
ts=activity['start_date'])

Expand Down
14 changes: 13 additions & 1 deletion src/strava_gear/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Component:
ident: ComponentId
name: ComponentName
distance: Meters = Meters(0.0)
elevation_gain: Meters = Meters(0.0)
time: Seconds = Seconds(0.0)
firstlast: FirstLast = FirstLast()
assignment: Optional[ComponentAssignment] = None
Expand All @@ -95,6 +96,7 @@ def add_usage(self, usage: Usage) -> Component:
return replace(
self,
distance=Meters(self.distance + usage.distances.get(self.ident, 0)),
elevation_gain=Meters(self.elevation_gain + usage.elevation_gains.get(self.ident, 0)),
time=Seconds(self.time + usage.times.get(self.ident, 0)),
firstlast=self.firstlast + usage.firstlasts.get(self.ident, FirstLast()))

Expand Down Expand Up @@ -164,14 +166,22 @@ def all_rule_bike_ids(self) -> Set[BikeId]:
@dataclass
class Usage:
distances: Dict[ComponentId, float] = field(default_factory=lambda: defaultdict(float))
elevation_gains: Dict[ComponentId, float] = field(default_factory=lambda: defaultdict(float))
times: Dict[ComponentId, float] = field(default_factory=lambda: defaultdict(float))
firstlasts: Dict[ComponentId, FirstLast] = field(default_factory=lambda: defaultdict(FirstLast))

@staticmethod
def from_activity(components: Iterable[ComponentId], distance: float, time: float, ts: datetime):
def from_activity(
components: Iterable[ComponentId],
distance: float,
elevation_gain: float,
time: float,
ts: datetime,
):
fl = FirstLast.from_ts(ts)
return Usage(
distances={c: distance for c in components},
elevation_gains={c: elevation_gain for c in components},
times={c: time for c in components},
firstlasts={c: fl for c in components})

Expand All @@ -180,6 +190,8 @@ def __iadd__(self, other) -> Usage:
return NotImplemented
for k, d in other.distances.items():
self.distances[k] += d
for k, d in other.elevation_gains.items():
self.elevation_gains[k] += d
for k, t in other.times.items():
self.times[k] += t
for k, fl in other.firstlasts.items():
Expand Down
4 changes: 3 additions & 1 deletion src/strava_gear/input/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'start_date',
'moving_time',
'distance',
'total_elevation_gain',
}


Expand All @@ -26,7 +27,7 @@ def read_input_csv(inp) -> Tuple[Dict[BikeName, BikeId], List[Dict]]:
sqlite3 ~/.local/share/strava_offline/strava.sqlite \
".mode csv" \
".headers on" \
"SELECT name, gear_id, start_date, moving_time, distance FROM activity" \
"SELECT name, gear_id, start_date, moving_time, distance, total_elevation_gain FROM activity" \
>activities.csv
"""
activities: List[Dict] = []
Expand All @@ -36,6 +37,7 @@ def read_input_csv(inp) -> Tuple[Dict[BikeName, BikeId], List[Dict]]:
**r,
'moving_time': int(r['moving_time']),
'distance': float(r['distance']),
'total_elevation_gain': float(r['total_elevation_gain']),
'start_date': parse_datetime(r['start_date']),
})

Expand Down
6 changes: 5 additions & 1 deletion src/strava_gear/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
from .data import Result


def report(f, res: Result, output, tablefmt: str, show_name: bool, show_first_last: bool):
def report(f, res: Result, output, tablefmt: str, show_name: bool, show_first_last: bool, show_vert_m: bool):
def cols(d: Dict) -> Dict:
if not show_name:
del d["name"]
if not show_first_last:
del d["first … last"]
if not show_vert_m:
del d["vert m"]
return d

table = [cols(d) for d in f(res)]
Expand All @@ -38,6 +40,7 @@ def report_components(res: Result) -> Iterator[Dict]:
"id": c.ident,
"name": c.name,
"km": c.distance / 1000,
"vert m": c.elevation_gain,
"hour": c.time / 3600,
"first … last": c.firstlast,
}
Expand All @@ -61,6 +64,7 @@ def sort_key(c: Component):
"id": c.ident,
"name": c.name,
"km": c.distance / 1000,
"vert m": c.elevation_gain,
"hour": c.time / 3600,
"first … last": c.firstlast,
}
Expand Down
24 changes: 12 additions & 12 deletions tests/csv.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ Rules:
Bikes report:

$ strava-gear <<END
> name,gear_id,start_date,moving_time,distance
> Ride 1,road,2022-01-01,3600,1000
> Ride 2,road,2023-01-01,3600,1000
> Ride 3,road,2023-02-01,3600,1000
> name,gear_id,start_date,moving_time,distance,total_elevation_gain
> Ride 1,road,2022-01-01,3600,1000,10
> Ride 2,road,2023-01-01,3600,1000,10
> Ride 3,road,2023-02-01,3600,1000,10
> END
bike,role,id,name,km,hour,first … last
road,chain,c3,c3,0.0,0.0,never

Components report:

$ strava-gear --report components <<END
> name,gear_id,start_date,moving_time,distance
> Ride 1,road,2022-01-01,3600,1000
> Ride 2,road,2023-01-01,3600,1000
> Ride 3,road,2023-02-01,3600,1000
> name,gear_id,start_date,moving_time,distance,total_elevation_gain
> Ride 1,road,2022-01-01,3600,1000,10
> Ride 2,road,2023-01-01,3600,1000,10
> Ride 3,road,2023-02-01,3600,1000,10
> END
id,name,km,hour,first … last
c3,c3,0.0,0.0,never
Expand All @@ -51,10 +51,10 @@ Components report:
VirtualRide virtual hashtag:

$ strava-gear --report components <<END
> name,gear_id,start_date,moving_time,distance,type
> Ride 1,road,2022-01-01,3600,1000,Ride
> Ride 2,road,2023-01-01,3600,1000,Ride
> Ride 4,road,2023-02-01,3600,1000,VirtualRide
> name,gear_id,start_date,moving_time,distance,total_elevation_gain,type
> Ride 1,road,2022-01-01,3600,1000,10,Ride
> Ride 2,road,2023-01-01,3600,1000,10,Ride
> Ride 4,road,2023-02-01,3600,1000,10,VirtualRide
> END
id,name,km,hour,first … last
c3,c3,0.0,0.0,never
Expand Down
3 changes: 2 additions & 1 deletion tests/readme/cmdline.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
--rules FILENAME Rules configuration (bikes, components, ...) [default:
/home/user/.config/strava_gear/rules.yaml]
--csv FILENAME Load activities from CSV instead of the strava-offline database (columns: name,
gear_id, start_date, moving_time, distance)
gear_id, start_date, moving_time, distance, total_elevation_gain)
--strava-database PATH Location of the strava-offline database [default:
/home/user/.local/share/strava_offline/strava.sqlite]
-o, --output FILENAME Output file [default: -]
Expand All @@ -20,4 +20,5 @@
--show-name / --hide-name Show long component names [default: show-name]
--show-first-last / --hide-first-last
Show first/last usage of components [default: show-first-last]
--show-vert-m / --hide-vert-m Show vertical meters (elevation gain) [default: hide-vert-m]
--help Show this message and exit.

0 comments on commit 48a31f5

Please sign in to comment.