Skip to content

Commit

Permalink
GH-41: Compute and display moving time
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-ueding committed Aug 3, 2024
1 parent f893407 commit 0ac130e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions geo_activity_playground/core/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ActivityMeta(TypedDict):
equipment: str
id: int
kind: str
moving_time: datetime.timedelta
name: str
path: str
start_latitude: float
Expand Down
20 changes: 11 additions & 9 deletions geo_activity_playground/core/activity_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,20 @@ def read_activity(path: pathlib.Path) -> tuple[ActivityMeta, pd.DataFrame]:
if len(timeseries):
timeseries, changed = embellish_single_time_series(timeseries)

# Extract some meta data from the time series.
metadata["start"] = timeseries["time"].iloc[0]
metadata["elapsed_time"] = (
timeseries["time"].iloc[-1] - timeseries["time"].iloc[0]
)
metadata["distance_km"] = timeseries["distance_km"].iloc[-1]
if "calories" in timeseries.columns:
metadata["calories"] = timeseries["calories"].iloc[-1]

return metadata, timeseries


def compute_moving_time(time_series: pd.DataFrame) -> datetime.timedelta:
def moving_time(group) -> datetime.timedelta:
selection = group["speed"] > 1.0
time_diff = group["time"].diff().loc[selection]
return time_diff.sum()

return (
time_series.groupby("segment_id").apply(moving_time, include_groups=False).sum()
)


def read_fit_activity(path: pathlib.Path, open) -> tuple[ActivityMeta, pd.DataFrame]:
"""
{'timestamp': datetime.datetime(2023, 11, 11, 16, 29, 49, tzinfo=datetime.timezone.utc),
Expand Down
1 change: 1 addition & 0 deletions geo_activity_playground/core/cache_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def apply_cache_migrations() -> None:
delete_heatmap_cache,
delete_tile_visits,
delete_everything,
delete_activity_metadata,
]

for migration in migrations[cache_status["num_applied_migrations"] :]:
Expand Down
20 changes: 20 additions & 0 deletions geo_activity_playground/importers/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from geo_activity_playground.core.activities import ActivityMeta
from geo_activity_playground.core.activities import ActivityRepository
from geo_activity_playground.core.activity_parsers import ActivityParseError
from geo_activity_playground.core.activity_parsers import compute_moving_time
from geo_activity_playground.core.activity_parsers import read_activity
from geo_activity_playground.core.tasks import WorkTracker

Expand Down Expand Up @@ -72,6 +73,11 @@ def import_from_directory(
consider_for_achievements=True,
)
activity_meta.update(activity_meta_from_file)
activity_meta.update(
_get_metadata_from_timeseries(
pd.read_parquet(activity_stream_dir / f"{activity_id}.parquet")
)
)
activity_meta.update(_get_metadata_from_path(path, metadata_extraction_regexes))
activity_meta.update(kind_defaults.get(activity_meta["kind"], {}))
repository.add_activity(activity_meta)
Expand Down Expand Up @@ -130,3 +136,17 @@ def _get_metadata_from_path(
if m := re.search(regex, str(path.relative_to(ACTIVITY_DIR))):
return m.groupdict()
return {}


def _get_metadata_from_timeseries(timeseries: pd.DataFrame) -> ActivityMeta:
metadata = ActivityMeta()

# Extract some meta data from the time series.
metadata["start"] = timeseries["time"].iloc[0]
metadata["elapsed_time"] = timeseries["time"].iloc[-1] - timeseries["time"].iloc[0]
metadata["distance_km"] = timeseries["distance_km"].iloc[-1]
if "calories" in timeseries.columns:
metadata["calories"] = timeseries["calories"].iloc[-1]
metadata["moving_time"] = compute_moving_time(timeseries)

return metadata
1 change: 1 addition & 0 deletions geo_activity_playground/importers/strava_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def try_import_strava(repository: ActivityRepository) -> bool:
"elapsed_time": activity.elapsed_time,
"equipment": gear_names[activity.gear_id],
"calories": detailed_activity.calories,
"moving_time": activity.moving_time,
}
)
limit_exceeded = False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<dd>{{ activity.distance_km|round(1) }} km</dd>
<dt>Elapsed time</dt>
<dd>{{ activity.elapsed_time }}</dd>
<dt>Moving time</dt>
<dd>{{ activity.moving_time }}</dd>
<dt>Start time</dt>
<dd><a href="{{ url_for('activity.day', year=date.year, month=date.month, day=date.day) }}">{{ date }}</a>
{{ time }}
Expand Down

0 comments on commit 0ac130e

Please sign in to comment.