/ Perl6

Health Kit Data

In a recent update (Perhaps less recent then I think) Apple had added the ability to export the data saved in the iOS Health Application. This data includes all types of data in one large XML file.

When viewed on the phone you are limited by the screen size. (There is a landscape view) But if you have several years of data you may not be able to view it all at once.

So I set out to parse the data in the Health App export file, and use an external program to graph it. I found several data graphing solutions. There is even an Perl6 module (SVG::Plot) to do the same. However I truly wanted something I could have up and running with little fuss. I was pleased to come upon Googles solution. With plenty of examples and the use of an online javascript web IED I was able to quickly make charts and settled on it.

Now I was ready to throw my data in and make my web based chart. So the first thing I had to do was come up with a way to extract the weight data from the export.xml file and gather that data into a text file.

Perl6 to the rescue

So I needed to find where the weight data is located in the file, and quickly located it. The `HKQuantityTypeIdentifierBodyMass` marker is the line I was looking for. It has an startDate and value key pairs. The startDate is the date and time which corresponds to the value entry. The value is the weight in Lbs. So I now was faced with the task on developing some regular expressions to match the information I'm seeking. ![](/content/images/2017/03/Screen-Shot-2017-02-28-at-11.27.28-PM.png)

The image above shows those regex's which correspond to the locations in the file I need to match. There is the recordType, which identifies a line that contains an weight entry. This is important because different apps can add an entry to the database. In my case I used a Workflow script from the lock screen and the Health app itself. Both look a little different. So if the value of recordType is HKQuantityTypeIdentifierBodyMass I know I have a line with weight data.

dateRecord and data are the date and weight identify the date and weight information I needed. Once I found the data I need, Its time to use what I found and write/save it in a useful format.

The format needed by Google Charts line graph is: [new Date(2014, 6, 20), 250], So now the task to to take a matching line and parse it to the format needed.

The bit of code above is nothing more then a for loop processing a text file (export.xml) and looking for the HKQuantityTypeIdentifierBodyMass marker, then grabbing the weight and saving it a temporary variable $weight. Finally parsing the date info I can output the results in the needed format.

Running the code from the command line like this...

...will write all the matching data to the records.txt text file. From there just open the file and cut and paste all the line into the javascript code for the chart in the data.addRows([]) section.

The final results...

The chart below is generated from javascript, so it must be enabled for it to be displayed in your web browser. Of course I'm skipping over some of the basic details of adding the html `
` tags and all the placement of the javascript library file. I have also left out all the trial and error I did with other charting programs and API's. For now I'm happy with what I have and the amount of time I did it in.

This was a quick and dirty project to parse the data file and get the results into a useful state. Of course there are many ways to improve the code. Perhaps using an Perl6 Grammar would be better. Instead of hardcoding the data file, I could have provided a method to provide an file path, but this was not needed for this case.