# Calculate MTTR for jenkins builds

**URL:** https://discuss.elastic.co/t/calculate-mttr-for-jenkins-builds/327067
**Category:** Elasticsearch
**Tags:** transforms
**Created:** [March 6, 2023, 10:47am UTC](https://discuss.elastic.co/t/calculate-mttr-for-jenkins-builds/327067 "2023-03-06T10:47:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![khuongdp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/khuongdp/32/118078_2.png) [@khuongdp](https://discuss.elastic.co/u/khuongdp)
#### Post date: [March 6, 2023, 10:47am UTC](https://discuss.elastic.co/t/calculate-mttr-for-jenkins-builds/327067/1 "2023-03-06T10:47:33Z")

</div>

Hi

I would like to calculate MTTR for some jenkins builds. I have these fields in multiple documents:

Using Elasticsearch 8.3.0

input :

```auto
name, type [type1|type2], status [failure|success], buildDateTime

```

Output (something like this):

```auto
only for type=type2, some-name, duration from failure to success

```

What I have done to far:

- Create a transform
  - group by terms(name)
  - group by terms(type)
  - group by terms(status)
  - aggregations mix(buildDateTime)
  - aggregations max(buildDateTime)

- Start the transform
- Edit the destination index in the Data Views
  - Add field
  - Set format to "Duration"
  - Set value

```auto
long d = ChronoUnit.MILLIS.between(doc['buildDateTime.min'].value, doc['buildDateTime.max'].value);

emit(d)

```

- Add a lens type visualization in my dashboard and add the fields

When I look at the graph it does not look right. What am I missing?

---

<div class="post-metadata">

### Author: ![przemekwitek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/przemekwitek/32/79526_2.png) [@przemekwitek](https://discuss.elastic.co/u/przemekwitek)
#### Post date: [March 6, 2023, 12:55pm UTC](https://discuss.elastic.co/t/calculate-mttr-for-jenkins-builds/327067/2 "2023-03-06T12:55:51Z")

</div>

Hi,

> When I look at the graph it does not look right. What am I missing?

Unfortunately, I cannot see the graph so I can't tell if it looks right or not.

One suggestion though, might be to calculate `d` (duration) in the transform itself, here is how you do it:

```auto
{
  ...
  "aggregations": {
    "buildDateTime_min": {
      "min": {
        "field": "buildDateTime"
      }
    },
    "buildDateTime_max": {
      "max": {
        "field": "buildDateTime"
      }
    },
    "buildDateTime_duration": {
      "bucket_script": {
        "buckets_path": {
          "time_min": "buildDateTime_min",   
          "time_max": "buildDateTime_max"    
        },
        "script": "params.time_max - params.time_min"
      }
    }
  }
  ...
}

```

Then you can visualize the duration which is available in the destination index as `buildDateTime_duration` field.

You can read more about `bucket_script` pipeline aggregation [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html).

---

<div class="post-metadata">

### Author: ![khuongdp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/khuongdp/32/118078_2.png) [@khuongdp](https://discuss.elastic.co/u/khuongdp)
#### Post date: [March 6, 2023, 1:44pm UTC](https://discuss.elastic.co/t/calculate-mttr-for-jenkins-builds/327067/3 "2023-03-06T13:44:20Z")

</div>

I think I have using the wrong way to calculate the duration. How can i model this in a transform (only for duration from FAILURE to SUCCESS?

**Input:**  
name1, type2, 2023-1-1, FAILURE,  
name1, type2, 2023-1-3, SUCCESS

**Output:**  
name1, type2, 2 days

**Input:**  
name1, type2, 2023-2-1, SUCCESS  
name1, type2, 2023-2-2, FAILURE,  
name1, type2, 2023-2-3, FAILURE,  
name1, type2, 2023-2-4, FAILURE,  
name1, type2, 2023-2-5, SUCCESS

**Output:**  
name1, type2, 3 days

MTTR will then be (2+3)/2  
name1, type2, 2.5 days

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [April 3, 2023, 1:45pm UTC](https://discuss.elastic.co/t/calculate-mttr-for-jenkins-builds/327067/4 "2023-04-03T13:45:03Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
