# How to over-engineer using Jexia (iOS Shortcut Edition)

This article is an explanation of how I built an iOS shortcut to show my server status, which was stored in a Jexia dataset, in an iOS notification that could be set-up to run when I connect to my local network or simply placed my phone on charge.

## #The Idea

I didn’t set out wanting to make this, I was just playing around with iOS Shortcuts after re-downloading the app and was experimenting with what I could do. I was also coincidentally working on the Jexia SDK and was looking for a small use-case where I could send and fetch data from Jexia using my new wrapper. I had also just brought a Raspberry Pi and was looking for a small use-case for this before I started my bigger projects. Hence this totally over-engineered project was started.

## #Sending System Information

Sending the data from the Raspberry Pi was very simple, I had done all of the complicated work within the Jexia SDK, such as managing access tokens and connecting to the API, so all I had to do was collect the system data. For this, I used Shirou’s gopsutil package. It all came together in this small raspberry-pi-health repository. This was then built using the correct Pi architecture and that was it. The Pi was now sending data.

## #Returning the JSON over a URL

So, I now had a place where the data was stored, I now needed a way to fetch and display the data from my dataset. Well, this was also really simple as the Jexia SDK handles all the heavy lifting. All I had to do was cook up a test-health-server project, a small Go server that would return an array of JSON object when a GET method was sent to /health. Easy.

The JSON object looked like below:

{
"created_at": "2020-07-10T19:23:50.085006Z",
"memory.active": 5088960
}


It has everything we need, the time at which the data was sent to the dataset, the number of running processes and the active (in current use) memory in bytes.

## #Creating the shortcut

The shortcut actually took the longest time to create, as I said, this was not my main intention and the original just returned the number of objects within the array, which is not very useful, especially when you hit the limit of 1000 objects being sent per request. So, I decided I wanted a useful notification.

It turned out looking like below, and I’m really happy with it.

### #Getting the JSON data

The first step is to fetch the data from the endpoint. As I was testing this locally, the URL is http://192.168.0.3/health, this returned an array of objects following the same format as the JSON object listed in the Returning the JSON over a URL section.

Next, as the shortcut app has quite a flexible programming language of its own, for lack of a better phrase, I was able to count the number of items sent from the URL with this simple function and pass it to the next.

I then checked if the number was greater than or equal to one, if it was, I continued and started getting the data from the returned JSON, if not, I showed an error notification.

Next, I collected the first item, (the newest) from the JSON array and passed this to the next function.

### #Formatting the time

So, now I had the JSON object, it was time to start collecting the values. First, I collected the sting value of created_at from the given JSON and started to edit this to match the ISO 8601 Apple wants, the endpoint was already sending this format but Apple would prefer it in the format: 2020-07-10 19:23:50+00:00 instead of 2020-07-10T19:23:50.085006Z.

To correct the date value, I first replaced the T with a space. In the image below, the single empty circle has an empty space in, the whitespace after in the next input is just the line-wrapping of the variable.

After this, I then used a regular expression to match everything after, and including the first ., and replaced it with +00:00. Such expression looks like \..*\$. In the case of the timestamp above, it changes from 2020-07-10 19:23:50.085006Z to 2020-07-10 19:23:50+00:00. If I didn’t do this, the correct date would be returned, but at 12:00, meaning the “Updated x ago” would just tell me the time since lunch, not exactly what I wanted.

The next step is to get the time difference between now and the date I just formatted. Well, Apple though of everything a lot, and I could do this in a simple function.

### #Getting the memory usage

The next value is the used memory, or memory.active, so I needed to fetch this from the JSON object.

As this is given in bytes, I needed to convert to gigabytes. To do this, following the JEDEC memory standards I divided by 10242.

After this, because I didn’t want a long decimal number, I rounded to the hundredths, showing in a nice 0.00 format.

### #Getting the process count

As the JSON already returns a normal integer, I could just read that and pass it straight into the notification.

Again, as I decided I wanted the format “There are currently x processes running, using x GB of memory. Updated x ago.”, I simply created a notification function and dropped the correct variable at each appropriate x.

### #Error check

As I created an if statement right at the beginning, I needed to close this with a nice notification telling me something went wrong.

## #Result

After all that hard work, I finally had a lovely notification, that I can call at any time, or set up an automation to call it when I get home.

Although the shortcut won’t work without some tweaking for your environment, you’re welcome to use it.