# 03 Downtime Reporting

In this tutorial you build a dashboard for analyzing machine downtimes with a pie chart, timeline, and detail table:

<figure><img src="https://4261006941-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSNEuiyRRKwuqtIcaEt45%2Fuploads%2Fgit-blob-526ae91df106c74629f265bba54ea1fc8b64e850%2Fgrafana-downtime-hero-01.png?alt=media" alt=""><figcaption></figcaption></figure>

## What you will learn

* Query the downtimes API endpoint
* Calculate downtime duration with transformations
* Visualize downtimes with pie chart, bar gauge, and state timeline
* Set up value mappings for downtime categories

## Prerequisites

* [Query the ENLYZE API](https://docs.enlyze.com/en/integrations/grafana/advanced-api/02-api-queries) and [Variables](https://docs.enlyze.com/en/integrations/grafana/advanced-api/03-variables) completed
* Understanding of Infinity queries and transformations

***

## Query downtimes

| Setting       | Value                                                                        |
| ------------- | ---------------------------------------------------------------------------- |
| Type          | JSON                                                                         |
| Parser        | Backend                                                                      |
| Method        | GET                                                                          |
| URL           | `downtimes?machine=${machine}&start=${__from:date:iso}&end=${__to:date:iso}` |
| Root selector | see JSONata                                                                  |

The API returns downtimes with nested reason information. Use JSONata to extract the fields and fill in missing end times:

```
$.data.{
  "start": start,
  "end": end ? end : $now(),
  "category": reason.category ? reason.category : "UNKNOWN",
  "reason": reason.name
}
```

{% hint style="info" %}
When `end` is `null`, the downtime is still active. `$now()` replaces missing end times with the current time.
{% endhint %}

<figure><img src="https://4261006941-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSNEuiyRRKwuqtIcaEt45%2Fuploads%2Fgit-blob-cc43bf7b451fc14d65fc1c4b9398ab27459b2fbb%2Fgrafana-downtime-query-01.png?alt=media" alt=""><figcaption></figcaption></figure>

***

## Calculate downtime duration

You can calculate the duration from start and end times using transformations:

1. **Convert field type**: start and end to Time
2. **Add field from calculation**: Mode Binary, end - start to `duration_ms`
3. **Add field from calculation**: duration\_ms / 1000 to `duration`

Set the unit of the `duration` field to **Duration (d hh:mm:ss)** (`dtdhms`).

<figure><img src="https://4261006941-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSNEuiyRRKwuqtIcaEt45%2Fuploads%2Fgit-blob-9e1b861a2201de141d2acfa3f24248e0b71cf98a%2Fgrafana-downtime-duration-transform-01.png?alt=media" alt=""><figcaption></figcaption></figure>

***

## Pie chart by category

Visualize the distribution of downtime categories:

1. Use the base query with the duration calculation transformations
2. Add a **Group by** transformation: Group by `category`, Aggregate `duration` to Sum
3. Select the **Pie Chart** visualization

Set up **Value Mappings** to make the API values more readable:

| API value      | Display            | Color  |
| -------------- | ------------------ | ------ |
| PLANNED        | Planned downtime   | Blue   |
| UNPLANNED      | Unplanned (manual) | Orange |
| NOT\_SCHEDULED | Not scheduled      | Gray   |
| UNKNOWN        | No reason          | Yellow |

<figure><img src="https://4261006941-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSNEuiyRRKwuqtIcaEt45%2Fuploads%2Fgit-blob-87e0bb6b1a75c3ea650342d36787a67a570da746%2Fgrafana-downtime-pie-01.png?alt=media" alt=""><figcaption></figcaption></figure>

***

## Bar gauge by category

Show the duration per category as horizontal bars:

| Setting       | Value                 |
| ------------- | --------------------- |
| Visualization | Bar Gauge             |
| Orientation   | Horizontal            |
| Display mode  | Basic                 |
| Unit          | Duration (d hh:mm:ss) |

Use the same query and transformations as the pie chart.

<figure><img src="https://4261006941-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSNEuiyRRKwuqtIcaEt45%2Fuploads%2Fgit-blob-d42c596d446046f4fb2bb0d7327aa5bfcf640cdb%2Fgrafana-downtime-bargauge-01.png?alt=media" alt=""><figcaption></figcaption></figure>

***

## State timeline

Show when downtimes occurred over time:

| Setting       | Value          |
| ------------- | -------------- |
| Visualization | State timeline |
| Merge values  | On             |

Use the same value mappings as the pie chart so that the categories are color-coded consistently.

<figure><img src="https://4261006941-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSNEuiyRRKwuqtIcaEt45%2Fuploads%2Fgit-blob-8dfc0e6a538e55c97afb9931594aaa31e36a4eae%2Fgrafana-downtime-timeline-01.png?alt=media" alt=""><figcaption></figcaption></figure>

***

## Detail table

Show individual downtime events with all details:

| Column   | Type      | Notes          |
| -------- | --------- | -------------- |
| start    | Timestamp | dateTimeAsIso  |
| end      | Timestamp | dateTimeAsIso  |
| duration | Number    | Unit: dtdhms   |
| category | String    | Value Mappings |
| reason   | String    | -              |

<figure><img src="https://4261006941-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSNEuiyRRKwuqtIcaEt45%2Fuploads%2Fgit-blob-e0e4bbe896f66ab8d430a6c09ab2a3be6d654ce2%2Fgrafana-downtime-table-01.png?alt=media" alt=""><figcaption></figcaption></figure>

***

## Tips

* **Downtime categories**: `PLANNED` (scheduled maintenance, changeovers), `UNPLANNED` (manually reported), `NOT_SCHEDULED` (no production scheduled). Use consistent colors across all panels.
* **Ongoing downtimes**: Replace `null` end times with `$now()` in JSONata so that the duration calculation works.
* **Empty data**: Check the time range. Downtimes are only available for periods when the machine was active.
* **Pareto analysis**: Filter for `UNPLANNED`, group by `reason`, and sort descending by `duration` to identify the most common downtime reasons.

***

## Next steps

* [**Shopfloor monitoring**](https://docs.enlyze.com/en/integrations/grafana/production-dashboards/01-shopfloor-monitoring) -- Build dashboards for large screens and mobile devices
