kafsemo.org

“very interesting, but I don't agree with you”

Hullo!

Extracting stats from Apple HealthKit

Monday, 30 November 2026 (#)

I track my swimming with an Apple Watch. It’s useful just for counting lengths, and will also record times. The Fitness app will show times for an individual workout, but not if I’m getting faster. After tweaking my routine, what I want to know is quite specific: for workouts in a specific pool size, where I was only swimming a specific stroke, what does the distribution of length times do over time? The stock Fitness app is absolutely not going to show that. I need to write at least my own query.

Luckily, I can export all health data; unluckily, it arrives as a complex document, with absolutely all health information included for all time. Luckily, it’s a reasonably well-structured document that very much expects you to extract subsets.

Workouts are recorded as a structured entity, with several distinct times and durations. The HealthKit documentation describes the classes in the actual runtime; I’m assuming the XML representation is a simple transliteration:

<Workout workoutActivityType="HKWorkoutActivityTypeSwimming" duration="..." durationUnit="min" sourceName="..." sourceVersion="..." device="..." creationDate="..." startDate="..." endDate="...">
  <MetadataEntry key="HKLapLength" value="50 m"/>
...
  <WorkoutEvent type="HKWorkoutEventTypeSegment" date="..." duration="..." durationUnit="min">
...
  </WorkoutEvent>
  <WorkoutEvent type="HKWorkoutEventTypeLap" date="..." duration="..." durationUnit="min">
   <MetadataEntry key="HKSwimmingStrokeStyle" value="2"/>
  </WorkoutEvent>
...
  <WorkoutEvent type="HKWorkoutEventTypePause" date="..."/>
...
</Workout>

Specifically, a HKWorkout contains several HKWorkoutEvents. Some of those HKWorkoutEvents may be segments, indicated by an HKWorkoutEventType of segment, which will overlap the individual events that are Laps. Of course, multiple levels in this hierarchy have their own start, finish and duration. (Is ‘finish’ always ‘start’ + ‘duration’? Sometimes!) The schema has changed over time; laps used to be implicitly terminated by the next lap, or the end of the workout; in watch OS 4, they gained a duration.

I appreciate the goal here: a single data model to capture a diverse range of activities with enough generalisation to avoid rebuilding everything for each activity. However, my goal here is far simpler.

I don’t know exactly which timing the Fitness app surfaces in the UI, and I suspect that the XML representation rounding stamps to seconds is going to make it hard to reproduce exactly. However, by eye, the closest fit seemed to be using each HKWorkoutEventTypeLap’s start time, from its date attribute, finished by the workout’s end time.

I suspect that the first and last laps are miscounted, and any pauses are over-counted; but that should be lost in the noise.

That was enough to get me data to plot a chart of box plots:

Some later times are faster than earlier ones, but there’s a lot of noise

At this granularity there’s too much noise. Aggregating more coarsely looks a lot like this:

Four out of five boxplot show a clear descending trend, with a single outlier that’s significantly higher

That certainly looks like an improvement.

Code’s over here.

(Music: Madvillain, “All Caps”)

Browser XSLT going away

Friday, 6 November 2026 (#)

Following a Google proposal in 2025, XSLT is being removed from the web platform. As Mozilla say:

While it's important to not break existing web content, it's also important to prevent security vulnerabilities. XSLT has been in maintenance mode in browsers and has been an ongoing source of security issues.

I’m a little nostalgic for the xml-stylesheet processing instruction. At the same time, the frozen 1.0 version of XSLT ignores all the improvements in 2.0 and 3.0, leaving it fairly literally stuck in the ’90s.

Outside a reference in my modest proposal for XML+Data, I used it to present some RDF for a blog post.

Existing behaviour

Currently, a file served as data.rdf starts with:

<?xml-stylesheet type='text/xsl' href='data-presentation.xsl'?>

That means:

  1. A browser requesting the RDF file applies the transformation and presents it as HTML.
  2. For other clients, the underlying data is RDF/XML.

Preserving behaviour

I’d like to preserve this behaviour, ideally under the existing URL, with Apache. I created a type map that points to underlying files with an additional suffix:

URI: data.rdf.rdf
Content-type: application/rdf+xml

URI: data.rdf.html
Content-type: text/html

Serve that map under the existing URL:

RewriteRule ^data.rdf$ data.rdf.var

and process the file server-side so there’s an HTML version:

xsltproc data-presentation.xsl data.rdf >data.rdf.html

Now:

  1. A browser requesting that URL will announce that it prefers HTML and get the HTML version.
  2. A client that prefers RDF/XML, or has no opinion, will get the RDF.

Ironically, the graphical representation of the data is more interesting.

(Music: The Hives, “Bogus Operandi”)

Feeds migrating to https

Friday, 12 September 2025 (#)

Way back in 2013, post-Snowden, moving general web traffic to TLS became an increasingly good idea, then a norm.

A common practice (but not a requirement) is to give insecure requests a 301 Moved Permanently from http over to https. This let user agents update their stored URL and make subsequent requests over a secure channel. If that initial request is compromised then all bets are still off, but TOFU is often a good trade-off.

The simple polling script I use for subscribed feeds uses exactly that logic: any chain of permanent redirections is persisted. Looking back, across all feeds, what did that migration from http to https look like?

An area chart showing http feeds being replaced by https

Initial adoption was slow. It was 2009 before I had an https subscription, when http://labs.mozilla.com/feed/ (now a 404) redirected to https://mozillalabs.com/feed/ (now an SSL_ERROR_BAD_CERT_DOMAIN). Mid-2016, https reached 5% of my subscriptions. That’s right after Let’s Encrypt officially launched.

In 2019 the ratio was 50-50, and it continued to increase to around 80% currently. Ironically, older feeds dropping off the web completely rather than switching still helped that ratio. Still, 20% plaintext is not great, so I took a look at the remaining holdouts.

A few were using a temporary redirection, with 302 Found:

the client ought to continue to use the target URI for future requests.

and indeed I did, fetching the original insecure resource each time.

Others were sending an Upgrade header:

Upgrade: h2
Connection: Upgrade

I could make a secure h2 connection; but the library I’m using needs changes for that.

Some other feeds were dead enough that it was time to unsubscribe.

With those migrated across manually, I’m at 96% https. Holdouts? Almost exclusively sites that aren’t actively being maintained. It’s always fun to see a feed spring back to life, even if it’s currently:

<updated>2022-03-17T05:00:46Z</updated>

or even:

<pubDate>Sun, 09 Mar 2014 00:00:00 PST</pubDate>

so I’ll poll until they 410.

(Music: Blonde Redhead, “Sit Down for Dinner (Part 2)”)
Joseph Walton <joe@kafsemo.org>