1000 kilometres in 2024


BASE_URL="https://intervals.icu/api/v1/athlete"
OLDEST="2024-01-01"
NEWEST="2024-12-31"

# Fetch activities
activities=$(curl -s -u API_KEY:$API_KEY \
    "$BASE_URL/$INTERVALS_UID/activities?oldest=$OLDEST&newest=$NEWEST")

# CSV header
echo "date,cumulative_distance"

# Initialise cumulative distance and iterate through each date
cumulative_distance=0
current_date=$(date -I -d "$OLDEST")
end_date=$(date -I -d "$NEWEST + 1 day")

while [ $current_date != $end_date ]; do
    # Calculate total distance for the current day
    day_distance=$(echo "$activities" | \
        jq --arg date "$current_date" '
        map(select(.type == "Run" and 
                   (.start_date_local | startswith($date)))) | 
        map(.distance) | 
        add // 0')  # return day distance (or return 0 if false or null)

    # Add day distance to cumulative distance
    cumulative_distance=$(echo "[$cumulative_distance, $day_distance]" | jq 'add')
    
    # Date and cumulative distance up until current date
    echo "$current_date,$cumulative_distance"
    
    # Move to the next day
    current_date=$(date -I -d "$current_date + 1 day")
done
date,cumulative_distance
2024-01-01,0
2024-01-02,6919
2024-01-03,6919
2024-01-04,10034
2024-01-05,10034
...
2024-12-27,958647.54
2024-12-28,973656.54
2024-12-29,973656.54
2024-12-30,980075.54
2024-12-31,1000088.54

set terminal pngcairo size 1024,768 font "iA Writer Quattro V"
set output 'cumulative_distance_graph.png'

# Set data file separator and time-related settings
set datafile separator ","
set xdata time
set timefmt "%Y-%m-%d"
set format x "%B"
set ylabel "Cumulative Running Distance in 2024"
set grid

# Customize axis and range
set xtics rotate by -45
set yrange [0:1150]
set xrange ['2024-01-01':*]

# Set margins
set lmargin 16
set rmargin 10
set tmargin 5
set bmargin 6

# Mark specific races with arrows and labels
set arrow from '2024-06-01',0 to '2024-06-01',1150 nohead lw 1 dashtype 3 linecolor "gray30"  # Stockholm marathon
set label "🇸🇪" at '2024-06-03',950

set arrow from '2024-09-15',0 to '2024-09-15',1150 nohead lw 1 dashtype 3 linecolor "gray30"  # In Flanders Fields marathon
set label "🇧🇪" at '2024-09-17',950

# Plot cumulative distance and 1000 km goal with markers and styles
plot "cumulative_distance.csv" using 1:($2/1000) with lines title "Cumulative Distance (km)" lw 2 linecolor rgb "orange-red", \
     0 with lines title "Marathons" lw 1 dashtype 3 linecolor "black", \
     1000 with lines notitle "1000 km" lw 1 dashtype 2 linecolor "black"