Work Out Your Workouts with Splunk

One hundred days. A nice round number marking the days since I joined Splunk, as well as the days since I decided to start improving my fitness levels again. Nothing drastic, the plan was to start cycling to work and doing short runs in and around a nearby park.

A few days in I thought “wouldn’t it be cool to track all of this in Splunk?”. That way I’d get useful statistics out of it, including trending to track progress along with some other stats I probably hadn't even thought of. Like many other cyclists and runners, I track my activities using Strava, which provides an API that can be used as the source for our data.

In 2018 my colleague Stephen Luedtke wrote a blog post, "Iron Splunk," about integrating Splunk with Strava. Since then Strava implemented OAuth 2.0 for accessing their API, requiring additional inputs to access the data so I had to come up with another way to make it work.

Given that I was keen to Splunk my fitness journey, I decided to take matters into my own hands and I created the Strava Add-On for Splunk, just in time to track those ambitious New Year’s health resolutions! The add-on was created using Splunk's Add-on Builder, using Python. 

Before you install the add-on, make sure you read the add-on’s details page on how to do the initial setup in order to authorise via OAuth 2.0, which only needs to be done once.

Once you’ve created the app in Strava and have the access code as per the instructions, it’s time to setup the add-on. For the required fields use the values you got from Strava, the starting timestamp is optional in case you only want to get data since a certain time.

That’s it, data will now be pulled from Strava which based on the amount of activities you have can take a while. Once done you’ll see all the events in Splunk and we're off to the races!

I created my own app with some stats that were relevant to me, here’s an example of some basic totals including a link switcher to toggle between duration and distance by week. The Strava API provides a wealth of details on each activity, the table at the bottom displays a few of them.

The last column called 'Pram' in that table might look a bit unusual. I was interested in tracking my speed over time running with a pram compared to running without one, as I often run with either one or both kids in a pram. It’s no surprise that running without a pram is quicker! Also very noticeable is the drop in speed towards the end of December, I blame the chocolate.

The lookup editor can be used to add custom notes to activities, however I wanted to keep everything dynamic and pull it in straight from Strava. The way that's done is by leveraging the description field in Strava to build the visualisation above, using either “1P” or “2P” in the description as the trigger for running with a single (1P) or double (2P) pram.

This is the corresponding SPL:

index=strava sourcetype=strava:activities type=Run
| eval avg_speed = round(average_speed * 3.6,1)
| eval pram_people = if(description=="1P","Single",if(description=="2P","Double","No Pram"))
| timechart span=1w@w1 max(avg_speed) AS avg_speed by pram_people
| trendline sma2(avg_speed) as trend
| fields "No Pram" Single Double

To visualise my activities, I decided to plot my movements on a map with runs in pink and rides in blue. This is using an HTML iframe and this script. You can either use the script to generate a one-time CSV, or use Splunk’s outputcsv command to generate one and schedule it to run regularly.

Last but not least, I thought it would be fun to get some random statistics from all the data. Some useful ones, others not so much but any of them can be used to set certain goals. A lot of the panels use dynamic captions to compare it to things that are easier to comprehend, like comparing calories burned to items that are consumed often.

The panels with the dynamic caption are using the <finalized> tags to display multiple fields, see below for an example which is the Most kudo’d activity panel. The big single value are the kudos, the caption shows the date and the name of the activity.

        <title>Most kudo'd activity</title>
            <set token="kudos_name">$$</set>
            <set token="kudos_date">$$</set>
            index=strava sourcetype=strava:activities type=*
            | eval date = strftime(_time,"%d-%m-%Y")
            | stats max(kudos_count) as kudos by name, id, date
            | head 1
            | fields kudos name date id
        <option name="underLabel">$kudos_date$: $kudos_name$</option>
        <option name="unit">kudos</option>

So if you’re using Strava and Splunk give the add-on a try and good luck with setting and smashing those fitness goals for 2020!

Patrick Peeters

Posted by