visit
Now if we had the autonomy and agency to reclaim this lost time and spend it more productively, would we? This was a question I looked to answer myself. A key and compelling feature of video games, mobile games, and social media is tracking. Whether it be gold coins, gems, experience, likes, or views, these platforms track our accomplishments through arbitrary metrics that often have no intrinsic value. Yet we still keep playing, swiping, and scrolling. I decided to start applying tracking metrics to areas of my life I wish to improve, exercise was one of these.
I chose to implement a PostgreSQL database to track the personal performance of my exercise. I am not so vain as to claim to be an athlete, but I complete a 5km run each day. I do this to maintain sufficient vitamin D from its natural source (the sun), cardio and general fitness, and mental wellbeing. One way I gamify the process is by creating an artificial scarcity of rewards. I only allow myself to listen to my while I am running. Nowadays, most mobile phones or smartwatches have step tracking functionality. This can be used to approximate the distance for each run. If not, you can use to measure your runs. Now finally, onto the technical side of things.First, we create a database to store the analytics for our exercises in. This can be done through the terminal on a Linux system using the createdb command:
createdb exercise
psql exercise
Then we create a table to store the run data in. This table has four fields; an id as a primary key, date stores when we ran, distance is the length of the run in kilometres and time is how long it took in minutes.
create table runs (
id serial primary key,
date date not null,
distance integer not null,
time integer not null
);
We use the serial to create an auto-incrementing integer value sequence for our primary key. In Laymon's terms, a range of numbers; 1,2,3,4,... up to the length of our table.
We can add data to the table once we complete a run. Let's take the following example of a 5 km run that took 32 minutes on the 27th of January 2021. Notice we do not have to specify the id, this is done automatically with the serial macro.
insert into runs
(date,distance,time)
values ('2021-01-27',5,32);
select * from runs;
In terms of gamification, we can set milestones or achievements for ourselves. Say, I want to run 100km this year. The query below displays the total distance we have run over the year 2021.
select sum(distance) from runs
where date <= '2021-01-01' and date <= '2021-12-31';
We can store that query as a view to monitor our progress towards this milestone.
create view goal
select sum(distance) from runs
where date <= '2021-01-01' and date <= '2021-12-31';
Now, it is always important to store backups. We cannot rely on this information being safely stored on a single local machine forever. I have chosen to use the pg_dump command-line utility and git to create backups on the cloud for recovery.
First, we create initialize git in the repository.
git init
git remote add origin <remote-origin>
#! /bin/bash
TODAY=`date --rfc-3339=date`
pg_dump exercise > exercise.dump
git add -A
git commit -m "Backup: ${TODAY}"
git push origin main
On Linux systems, we must change the permissions so that our bash script is executable. The
chmod
command below modifies the permissions such that it can be run on the system by the current user.chmod u+x backup.sh
./backup.sh
Previously published at