Screen Time Tracker

Screen Time Tracker is a Grafana dashboard for the data that ActivityWatch records on a Mac: which app is in front, which site is open in the browser, and whether anyone is at the keyboard. It shows when the computer was in use, the top apps and sites for any time range, and a timeline of apps in which every app has its own colour. It is a personal setup and is not published as a package.

How the data gets to Grafana

ActivityWatch is an open-source time tracker. Its watchers record the focused app and window title, the current tab in each browser, and AFK status (whether the keyboard and mouse have been idle), and its server keeps all of this in a SQLite file.

Every 30 minutes a launchd job copies that file into Grafana’s data folder with SQLite’s .backup command and then rebuilds a second database of corrected totals, described below. Grafana runs in Docker and reads both files through the SQLite data source plugin. The dashboard opens on the last 24 hours and refreshes every 5 minutes. A separate alerter sends a push notification through ntfy if no new data has arrived for 48 hours.

The panels

  • AFK Status is a timeline across the top: green when someone was using the computer and red when it was idle.
  • Top Apps and Top Sites are bar charts of the five apps and five websites with the most minutes in the selected range. Sites are grouped by domain, with www. removed.
  • Active Time is the total hours marked as not AFK.
  • App Timeline shows which app was in front at each moment, as a coloured strip with a legend of app names.
The ActivityWatch dashboard in Grafana over 24 hours: an AFK Status strip in green and red, bar charts of the top five apps and top five sites in minutes, an Active Time total of 5.2 hours, and an App Timeline strip with a different colour for each app.
An earlier version of the dashboard, over 24 hours. Top Apps and Top Sites now count only time spent at the computer.

Counting only the time you were there

ActivityWatch’s window watcher keeps recording whichever window is in front when nobody is at the computer, so adding up its raw durations gives close to 24 hours a day. The extra time is not spread evenly. Measured on this setup’s data, Finder came out about 22 times too high, iTerm2 about 7 times and VMware about 2 times, which changed the order of the top apps. The browser watchers have the same problem: each browser reports its current tab even when it is in the background, and Chrome and Firefox report at the same time, so browsing time could add up to more than 24 hours in a day.

A Python script, rollups.py, fixes this after each copy. It cuts every window event down to the parts that fall inside not-AFK periods, and every tab event down to the parts where its own browser was the app in front and someone was at the computer. It stores the clipped start and end times rather than daily totals, so the panels give exact figures for any time range Grafana asks for. A full rebuild reads about 1.8 million events. Top Apps and Top Sites read from this database.

A colour for every app

The App Timeline gets its colours from a list in the dashboard file that pairs each app name with a colour. Instead of writing that list by hand for hundreds of apps, a short Python script writes it. The Docker Compose file runs the script in its own container first, and Grafana only starts after it has finished.

The script reads every app name ActivityWatch has recorded and gives each one a colour from an MD5 hash of its name. The hue is the hash multiplied by 137.508 degrees, the golden angle, modulo 360. Other bits of the same hash pick one of four saturations (60 to 90%) and one of three lightnesses (40, 50 or 60%). The same app always gets the same colour, and a new app gets a colour the next time the stack starts.

All self-hosted →