-
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a report-bikes-cummulative-distance command #14
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,31 @@ | ||
from io import StringIO | ||
import sqlite3 | ||
import csv | ||
from typing import Optional | ||
|
||
from tabulate import tabulate | ||
|
||
|
||
def tabulate_execute(db: sqlite3.Connection, sql: str, *params) -> str: | ||
def tabulate_execute(db: sqlite3.Connection, sql: str, *params, format='plain') -> str: | ||
# db.set_trace_callback(print) | ||
# sqlite3.enable_callback_tracebacks(True) | ||
table = (dict(row) for row in db.execute(sql, params)) | ||
return tabulate(table, headers='keys') | ||
if format == 'csv': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we turn this into a separate commit that adds configurable output formats to all reports, pls? You can steal some code from here: https://github.com/liskin/strava-gear/blob/0b6195ca6e08a8790998d2f0e756aba682a6d93c/src/strava_gear/cli.py#L42-L47 |
||
table = list(table) | ||
if len(table) == 0: | ||
return "" | ||
|
||
keys = list(table[0].keys()) | ||
stream = StringIO() | ||
writer = csv.DictWriter(stream, keys) | ||
writer.writeheader() | ||
for row in table: | ||
writer.writerow(row) | ||
|
||
return stream.getvalue() | ||
|
||
else: | ||
return tabulate(table, headers='keys', tablefmt=format) | ||
|
||
|
||
def yearly(db: sqlite3.Connection, year: int) -> str: | ||
|
@@ -34,6 +54,54 @@ | |
""", f"{year}-%") | ||
|
||
|
||
def bikes_cummulative_distance(db: sqlite3.Connection, resolution: str, output_format: str, bike: Optional[str], start_year: int, end_year) -> str: | ||
Check failure on line 57 in src/strava_offline/reports.py GitHub Actions / check (3.10)
Check failure on line 57 in src/strava_offline/reports.py GitHub Actions / check (3.9)
Check failure on line 57 in src/strava_offline/reports.py GitHub Actions / check (3.11)
Check failure on line 57 in src/strava_offline/reports.py GitHub Actions / check (3.12)
Check failure on line 57 in src/strava_offline/reports.py GitHub Actions / check (3.8)
Check failure on line 57 in src/strava_offline/reports.py GitHub Actions / check-distro (ubuntu:rolling)
Check failure on line 57 in src/strava_offline/reports.py GitHub Actions / check-distro (debian:oldstable)
Check failure on line 57 in src/strava_offline/reports.py GitHub Actions / check-distro (ubuntu:devel)
Check failure on line 57 in src/strava_offline/reports.py GitHub Actions / check-distro (debian:unstable)
Check failure on line 57 in src/strava_offline/reports.py GitHub Actions / check-distro (debian:stable)
|
||
sql_template = """ | ||
WITH | ||
daily AS ( | ||
SELECT | ||
b.name AS "Bike" | ||
,CASE | ||
WHEN ? == "day" THEN strftime("%Y-%m-%d", a.start_date) | ||
WHEN ? == "month" THEN strftime("%Y-%m-01", a.start_date) | ||
WHEN ? == "year" THEN strftime("%Y-01-01", a.start_date) | ||
END AS "Date" | ||
,CAST(SUM(a.distance) / 1000.0 AS DOUBLE) AS "Distance (km)" | ||
,CAST(SUM(a.moving_time) / 3600.0 AS DOUBLE) AS "Moving time (hour)" | ||
,CAST(SUM(a.total_elevation_gain) AS DOUBLE) AS "Total Elevation (m)" | ||
FROM activity a INNER JOIN bike b ON a.gear_id = b.id | ||
GROUP BY 1, 2 | ||
) | ||
|
||
,daily_with_cumsum AS ( | ||
SELECT | ||
* | ||
,SUM(`Distance (km)`) OVER (PARTITION BY Bike ORDER BY Date ASC) AS "Cummulative (km)" | ||
,SUM(`Moving time (hour)`) OVER (PARTITION BY Bike ORDER BY Date ASC) AS "Cummulative (hour)" | ||
,SUM(`Total Elevation (m)`) OVER (PARTITION BY Bike ORDER BY Date ASC) AS "Cummulative (elevation)" | ||
FROM daily | ||
) | ||
|
||
SELECT | ||
Bike | ||
,Date | ||
,printf("%.02f", `Distance (km)`) AS `Distance (km)` | ||
,printf("%.02f", `Cummulative (km)`) AS `Cummulative (km)` | ||
,printf("%.02f", `Total Elevation (m)`) AS `Total Elevation (m)` | ||
,printf("%.02f", `Cummulative (elevation)`) AS `Cummulative (elevation)` | ||
,printf("%.02f", `Moving Time (hour)`) AS `Moving Time (hour)` | ||
,printf("%.02f", `Cummulative (hour)`) AS `Cummulative (hour)` | ||
FROM daily_with_cumsum | ||
WHERE | ||
CAST(strftime("%Y", Date) AS INT) BETWEEN ? AND ? | ||
AND | ||
Bike LIKE ? | ||
ORDER BY Bike ASC, Date ASC | ||
""" | ||
|
||
bike = bike or "" | ||
return tabulate_execute(db, sql_template , resolution, resolution, resolution, start_year, end_year, f'{bike}%', format=output_format) | ||
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check (3.10)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check (3.10)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check (3.9)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check (3.9)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check (3.11)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check (3.11)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check (3.12)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check (3.12)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check (3.8)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check (3.8)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check-distro (ubuntu:rolling)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check-distro (ubuntu:rolling)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check-distro (debian:oldstable)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check-distro (debian:oldstable)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check-distro (ubuntu:devel)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check-distro (ubuntu:devel)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check-distro (debian:unstable)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check-distro (debian:unstable)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check-distro (debian:stable)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check-distro (debian:stable)
Check failure on line 102 in src/strava_offline/reports.py GitHub Actions / check-distro (ubuntu:latest)
|
||
|
||
|
||
def bikes(db: sqlite3.Connection) -> str: | ||
return tabulate_execute(db, """ | ||
SELECT | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's actually daily, isn't it? Also probably wanna mention "cummulative" here as well :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! I probably changed the default and completely forgot about the docstr