# Painless calculation based on time interval

**URL:** <https://discuss.elastic.co/t/painless-calculation-based-on-time-interval/99532>\
**Category:** Kibana\
**Created:** [September 6, 2017, 8:18am UTC](https://discuss.elastic.co/t/painless-calculation-based-on-time-interval/99532 "2017-09-06T08:18:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaharmor](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@shaharmor](https://discuss.elastic.co/u/shaharmor)\
**Post date:** [September 6, 2017, 8:18am UTC](https://discuss.elastic.co/t/painless-calculation-based-on-time-interval/99532/1 "2017-09-06T08:18:17Z")

</div>

Hi,

In the Visual Time Series Builder, when using the `calculation` aggregation, is it possible to reference the current time interval that kibana selected?

For example, when search for the last 24h and the time interval is 10m, to have something like `params.time_interval`equal 600000 (10m in milliseconds) or something similar that gets updated when the interval changes.

Thanks

---

<div class="post-metadata">

**Author:** ![Brandon\_Kobel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brandon_kobel/32/14829_2.png) [@Brandon\_Kobel](https://discuss.elastic.co/u/Brandon_Kobel)\
**Post date:** [September 6, 2017, 12:29pm UTC](https://discuss.elastic.co/t/painless-calculation-based-on-time-interval/99532/2 "2017-09-06T12:29:47Z")

</div>

@shaharmor referencing the interval directly in the calculation script isn't currently possible. Would you mind describing what you were intending to accomplish by doing so, so we can see if there is an alternative approach?

---

<div class="post-metadata">

**Author:** ![shaharmor](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@shaharmor](https://discuss.elastic.co/u/shaharmor)\
**Post date:** [September 6, 2017, 12:55pm UTC](https://discuss.elastic.co/t/painless-calculation-based-on-time-interval/99532/3 "2017-09-06T12:55:52Z")

</div>

Sure.

We are collecting the amount of bytes downloaded on a per 1m interval in a doc that looks like this:

```auto
{
  "timestamp": 1504702131146
  "bytes": 1000
}

```

And we want to display the average gbps (gigabit per **second** ) over time, regardless of the time interval that Kibana chooses.

Basically we need to do something like this: `(params.bytes * 8) / ((params._interval * 1000) * (1000 * 1000 * 1000))`, which is `bytes as bits`/(`interval as seconds` \* `bits as gigabits`).

Makes sense?

---

<div class="post-metadata">

**Author:** ![Brandon\_Kobel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brandon_kobel/32/14829_2.png) [@Brandon\_Kobel](https://discuss.elastic.co/u/Brandon_Kobel)\
**Post date:** [September 6, 2017, 5:32pm UTC](https://discuss.elastic.co/t/painless-calculation-based-on-time-interval/99532/4 "2017-09-06T17:32:56Z")

</div>

@shaharmor do you have a document for every 1m? It's possible that you could use a `count` aggregation to determine the total minutes in the bucket to perform the calculation.

---

<div class="post-metadata">

**Author:** ![shaharmor](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@shaharmor](https://discuss.elastic.co/u/shaharmor)\
**Post date:** [September 6, 2017, 5:45pm UTC](https://discuss.elastic.co/t/painless-calculation-based-on-time-interval/99532/5 "2017-09-06T17:45:10Z")

</div>

I have thousands of docs per 1m bucket, each with different dimensions too  
(different values)

---

<div class="post-metadata">

**Author:** ![Brandon\_Kobel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brandon_kobel/32/14829_2.png) [@Brandon\_Kobel](https://discuss.elastic.co/u/Brandon_Kobel)\
**Post date:** [September 6, 2017, 7:03pm UTC](https://discuss.elastic.co/t/painless-calculation-based-on-time-interval/99532/6 "2017-09-06T19:03:52Z")

</div>

@shaharmor to do this, we can follow a very similar approach to what is explained [here](https://www.youtube.com/watch?v=CNR-4kZ6v_E)

The largest difference between what is in the video, and your situation is that the video is dealing with documents that are already calculating the total network traffic over time. That's not the case in your situation, so we're doing a Sum, and then a Cumulative Sum and then what the video describes leaving us with the following configuration:

 ![35 PM](https://us1.discourse-cdn.com/elastic/original/3X/2/2/2249ce4e6cb6e7a207e295735a437b59c20b715f.png)

You'll notice in this solution we're doing kbps as opposed to gbps because my sample data wasn't nearly as extensive as yours, so that will need to be modified.

---

<div class="post-metadata">

**Author:** ![shaharmor](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@shaharmor](https://discuss.elastic.co/u/shaharmor)\
**Post date:** [September 7, 2017, 12:23pm UTC](https://discuss.elastic.co/t/painless-calculation-based-on-time-interval/99532/7 "2017-09-07T12:23:21Z")

</div>

Thanks Brandon!

It really helps, only problem left is showing the series as Gb/s instead of GB/s (currently Kibana only has `Bytes` format and not `Bits`)

---

<div class="post-metadata">

**Author:** ![Brandon\_Kobel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brandon_kobel/32/14829_2.png) [@Brandon\_Kobel](https://discuss.elastic.co/u/Brandon_Kobel)\
**Post date:** [September 7, 2017, 3:25pm UTC](https://discuss.elastic.co/t/painless-calculation-based-on-time-interval/99532/8 "2017-09-07T15:25:10Z")

</div>

@shaharmor good point, we currently have this [issue](https://github.com/elastic/kibana/issues/7892) outstanding to support bits in addition to bytes, if you wouldn't mind +1ing it or adding a comment it'll help us prioritize it appropriately.

---

<div class="post-metadata">

**Author:** ![shaharmor](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@shaharmor](https://discuss.elastic.co/u/shaharmor)\
**Post date:** [September 7, 2017, 3:26pm UTC](https://discuss.elastic.co/t/painless-calculation-based-on-time-interval/99532/9 "2017-09-07T15:26:23Z")

</div>

Done.

---

<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:** [October 5, 2017, 3:26pm UTC](https://discuss.elastic.co/t/painless-calculation-based-on-time-interval/99532/10 "2017-10-05T15:26:32Z")

</div>

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