From my Strava activites into one sparkline
With sal it’s now possible to fetch on demand all my personal Strava
activities from the past year. The next step is to transform this raw dataset into
a sparkline. But before diving into this, let’s see concretly how to install
and start using sal.
Setup walkthrough
sal’s README explains it all, let’s go
through this step by step.
-
On Strava, create an API Application.
-
Fetch
salfrom the GitHub repository’s release section or build it on your machine. -
Launch
sal. You’ll need a browser to complete the first launch as it has to fetch an authentication token and ask for your approval in doing so.You’ll need the
client_idand theclient_secretthat got generated on step 1 as well. They are available anytime on the settings page of your Strava profile.SAL_CLIENT_ID=<your_client_id> SAL_CLIENT_SECRET_ID=<your_client_secret_id> sal -
Under the default root directory for user-specific configuration, you’ll find the authentication details that
salwill use on further launches. Those are stored undersal/state.json. That will come handy later.
If everything is alright, that should be it, the past year of Strava activities are dumped in your terminal!
Raw data into a sparkline
That dataset is an array of activities. To transform that into a sparkline, that needs to go through a few steps of transformations.
The idea is to get the total distance per week and then to plot those 52 points on a line. Besides that, one or two other stats are computed so that we can generate the accompanying text.
The whole pipeline uses jq and m4. The bulk of the process is the following:
|
|
A whole bunch of things are taking place 🤠:
mapandselectfilters the dataset to retain only running and trailrunning activities- another
maptransforms each entries to pick only the distance and the date of the activity; the%g%Vdate format creates a key we’ll use to group activities of the same week. For example for today2608is%g%V group_byprepares the dataset so that we can make a total per week, it produces an array of activites per week, or more precisely per%g%V- the next
mapsums all the activites of each week - to ensure there’s no hole in the series, another array is created. It contains the 52 past weeks and an initial distance to zero
- next, it groups the 2 arrays in one, with the week as key
- for each week, it sums the
.distancekey, from each array; if I don’t run on a given week, this would ensure that I still have a datapoint for that week. At that stage, the dataset is made up of week keys and _distance values, as in[{"2607": 42}, ...]. - lines 10-11 normalize the
.distancevalues - lines 13-14 are the final touches to generate the 52 datapoints that we are
going to draw with svg. The
. * 3is for the width of a given week. So the whole sparkline would be approximately 160px wide. There was definitely a bit of playing around before I’d end up with a pleasing rendering activities.jsoncontains the output ofsal, the past 52 weeks of Strava activities
So SL_DATAPOINTS contains the datapoints, something like 0,6 3,3 6,5...,
and are included in a m4 template that I prepared.
More on this next step, in yet another upcoming and final post about this fun little side project.