# Percentage of downtime

**URL:** <https://discuss.elastic.co/t/percentage-of-downtime/108354>\
**Category:** Kibana\
**Created:** [November 20, 2017, 9:53am UTC](https://discuss.elastic.co/t/percentage-of-downtime/108354 "2017-11-20T09:53:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![OucemaBellagha](https://avatars.discourse-cdn.com/v4/letter/o/5fc32e/32.png) [@OucemaBellagha](https://discuss.elastic.co/u/OucemaBellagha)\
**Post date:** [November 20, 2017, 9:53am UTC](https://discuss.elastic.co/t/percentage-of-downtime/108354/1 "2017-11-20T09:53:07Z")

</div>

Hi folks,

I'm using scripted fields to calculate my host downtime over a month statically using 43800 which is the number of minutes per month: 100 - ((doc['downtime'].value / 43800) \*100)

Now this is static and not accurate as the number of minutes is a variable for each month so is there another way to replace the number of minutes by casting the date type cause I have @timestamp field so any ideas how to solve this?

Thx,

---

<div class="post-metadata">

**Author:** ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)\
**Post date:** [November 20, 2017, 6:31pm UTC](https://discuss.elastic.co/t/percentage-of-downtime/108354/2 "2017-11-20T18:31:45Z")

</div>

Hello,  
I gave it a try and this should help you take everything in account to calculate the downtime. You need to switch the strings in the `return` statements to the results of your calculations, or to set the variable `number_of_total_minutes_per_month` to the specific value in each branch and then just use it after the whole `if/else` block to calculate your downtime.

```auto

if ([1, 3, 5, 7, 8, 10, 12].contains( doc['@timestamp'].date.monthOfYear ) )
	{
		return "long month"
}
else
	{ 
		if ( [4, 6, 9, 11].contains( doc['@timestamp'].date.monthOfYear) ) 
			{
				return "short month"
		}
		else
			{
				if ( doc['@timestamp'].date.isLeap )
					{
						return "longFebruary"
				}
				else
					{
						return "shortFebruary"
				}
		}
}

```

---

<div class="post-metadata">

**Author:** ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)\
**Post date:** [November 20, 2017, 6:46pm UTC](https://discuss.elastic.co/t/percentage-of-downtime/108354/3 "2017-11-20T18:46:54Z")

</div>

There is also a `date.dayOfMonth.getMaximumValue()`which should return the maximum number of days in the current month. in Joda.Time.MutableDateTime (which is used here), but I can't get it to work due to some conflict between java.Time and Joda.Time.

---

<div class="post-metadata">

**Author:** ![OucemaBellagha](https://avatars.discourse-cdn.com/v4/letter/o/5fc32e/32.png) [@OucemaBellagha](https://discuss.elastic.co/u/OucemaBellagha)\
**Post date:** [November 21, 2017, 11:59am UTC](https://discuss.elastic.co/t/percentage-of-downtime/108354/4 "2017-11-21T11:59:41Z")

</div>

Thanks for the suggestion and indeed it is not returning the correct percentage of each month for some reason:

```
int monthly_minutes = 0;
int total = 0;

if ([1, 3, 5, 7, 8, 10, 12].contains( doc['@timestamp'].date.monthOfYear ) )
	{
		monthly_minutes = 44640;
}
else
	{ 
		if ( [4, 6, 9, 11].contains( doc['@timestamp'].date.monthOfYear) ) 
			{
				monthly_minutes = 43200;
		}
		else
			{
				if ( doc['@timestamp'].date.isLeap )
					{
				monthly_minutes = 41760;
				}
				else
					{
				monthly_minutes = 40320;
				}
		}
}
total += (100 - (( doc["downtime"].value / monthly_minutes) *100));
return total;

```

And by the way I wanted to have something more dynamic similar to this which also throwing an exception:

`100 - ((doc['downtime'].value / ( Instant.ofEpochSecond(doc['@timestamp'].value).minusMonths(1).toEpochSecond() ) ) *100)`

Any ideas about possible time manipulations here?

---

<div class="post-metadata">

**Author:** ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)\
**Post date:** [November 21, 2017, 12:33pm UTC](https://discuss.elastic.co/t/percentage-of-downtime/108354/5 "2017-11-21T12:33:33Z")

</div>

Well, it depends on how you have your data structured. Is it a monthly document that contains the downtime? is it a daily document (in that case you would have to do a Sum aggregation on this scripted field)?  
Also, no need to use the operations with 100, just leave it as the result of the division and set the field formatting to Percentage.

As for your second script, I have a hard time following what everything there does, can you break it down a little more?

---

<div class="post-metadata">

**Author:** ![OucemaBellagha](https://avatars.discourse-cdn.com/v4/letter/o/5fc32e/32.png) [@OucemaBellagha](https://discuss.elastic.co/u/OucemaBellagha)\
**Post date:** [November 21, 2017, 1:02pm UTC](https://discuss.elastic.co/t/percentage-of-downtime/108354/6 "2017-11-21T13:02:51Z")

</div>

well here's the index I'm working on and you understand it right it's a monthly report based on documents run each 3 minute to check whether my URL is 200 or not, and gets a downtime field containing the downtime in minutes when the HTTP status is different than 200

```
{
  "template": "whatever-index-*",
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
      "whatever-index-": {
      "_source": {
      "enabled": true
      },
      "properties": {
        "@timestamp": {
          "type": "date",
          "format" : "epoch_second"
        },
        "uri": {
          "type": "keyword"
        },
        "http_status": {
          "type": "integer"
        },
        "downtime": {
          "type": "integer"
        }
      }
    }
  }
}

```

So for the first script u sent me the logic is there but when I add the total function, painless somehow doesn't return the correct number: simply my first function `100 - ((doc['downtime'].value / 43800) *100)` is run on lang:expression but the then when I try to run same function with lang:painless the result is different and I don't get the problem yet.

for the second script it's the same logic as for `100 - ((doc['downtime'].value / 43800) *100)` but here I replace 43800 by `( Instant.ofEpochSecond(doc['@timestamp'].value).minusMonths(1).toEpochSecond() )` which calculate minus one month in minute from the actual time which is also throwing an exception..

Ideas? suggestions?

---

<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:** [December 19, 2017, 1:03pm UTC](https://discuss.elastic.co/t/percentage-of-downtime/108354/7 "2017-12-19T13:03:09Z")

</div>

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