content/flux/v0/stdlib/contrib/tomhollingworth/events/duration.md
events.duration() calculates the duration of events.
The function determines the time between a record and the subsequent record
and associates the duration with the first record (start of the event).
To calculate the duration of the last event,
the function compares the timestamp of the final record
to the timestamp in the stopColumn or the specified stop time.
events.duration() is similar to elapsed() and stateDuration(), but differs in important ways:
elapsed() drops the first record. events.duration() does not.stateDuration() calculates the total time spent in a state (determined by a predicate function).
events.duration() returns the duration between all records and their subsequent records.See the example below.
(
<-tables: stream[A],
?columnName: string,
?stop: time,
?stopColumn: string,
?timeColumn: string,
?unit: duration,
) => stream[B] where A: Record, B: Record
{{% caption %}} For more information, see Function type signatures. {{% /caption %}}
Duration unit of the calculated state duration.
Default is 1ns.
Name of the result column.
Default is "duration".
Name of the time column.
Default is "_time".
Name of the stop column.
Default is "_stop".
The latest time to use when calculating results.
If provided, stop overrides the time value in the stopColumn.
Input data. Default is piped-forward data (<-).
import "array"
import "contrib/tomhollingworth/events"
data
|> events.duration(unit: 1m, stop: 2020-01-02T00:00:00Z)
{{< expand-wrapper >}} {{% expand "View example input and output" %}}
| _time | state |
|---|---|
| 2020-01-01T00:00:00Z | ok |
| 2020-01-01T00:12:34Z | warn |
| 2020-01-01T00:25:01Z | ok |
| 2020-01-01T16:07:55Z | crit |
| 2020-01-01T16:54:21Z | warn |
| 2020-01-01T18:20:45Z | ok |
| _time | state | duration |
|---|---|---|
| 2020-01-01T00:00:00Z | ok | 12 |
| 2020-01-01T00:12:34Z | warn | 12 |
| 2020-01-01T00:25:01Z | ok | 942 |
| 2020-01-01T16:07:55Z | crit | 46 |
| 2020-01-01T16:54:21Z | warn | 86 |
| 2020-01-01T18:20:45Z | ok | 339 |
{{% /expand %}} {{< /expand-wrapper >}}
The example below includes output values of
events.duration(), elapsed(), and stateDuration()
related to the _time and state values of input data.
import "array"
import "contrib/tomhollingworth/events"
union(
tables: [
data
|> events.duration(unit: 1m, stop: 2020-01-02T00:00:00Z)
|> map(
fn: (r) =>
({
_time: r._time,
state: r.state,
function: "events.Duration()",
value: r.duration,
}),
),
data
|> elapsed(unit: 1m)
|> map(
fn: (r) =>
({_time: r._time, state: r.state, function: "elapsed()", value: r.elapsed}),
),
data
|> stateDuration(unit: 1m, fn: (r) => true)
|> map(
fn: (r) =>
({
_time: r._time,
state: r.state,
function: "stateDuration()",
value: r.stateDuration,
}),
),
],
)
|> pivot(rowKey: ["_time", "state"], columnKey: ["function"], valueColumn: "value")
{{< expand-wrapper >}} {{% expand "View example output" %}}
| _time | state | events.Duration() | elapsed() | stateDuration() |
|---|---|---|---|---|
| 2020-01-01T00:00:00Z | ok | 12 | 0 | |
| 2020-01-01T00:12:34Z | warn | 12 | 12 | 12 |
| 2020-01-01T00:25:01Z | ok | 942 | 12 | 25 |
| 2020-01-01T16:07:55Z | crit | 46 | 942 | 967 |
| 2020-01-01T16:54:21Z | warn | 86 | 46 | 1014 |
| 2020-01-01T18:20:45Z | ok | 339 | 86 | 1100 |
{{% /expand %}} {{< /expand-wrapper >}}