Documentation

events.duration() function

events.duration() is a user-contributed function maintained by the package author.

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.

Similar functions

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.

Function type signature
(
    <-tables: stream[A],
    ?columnName: string,
    ?stop: time,
    ?stopColumn: string,
    ?timeColumn: string,
    ?unit: duration,
) => stream[B] where A: Record, B: Record

For more information, see Function type signatures.

Parameters

unit

Duration unit of the calculated state duration. Default is 1ns.

columnName

Name of the result column. Default is "duration".

timeColumn

Name of the time column. Default is "_time".

stopColumn

Name of the stop column. Default is "_stop".

stop

The latest time to use when calculating results.

If provided, stop overrides the time value in the stopColumn.

tables

Input data. Default is piped-forward data (<-).

Examples

Calculate the duration of states

import "array"
import "contrib/tomhollingworth/events"

data
    |> events.duration(unit: 1m, stop: 2020-01-02T00:00:00Z)

View example input and output

Compared to similar functions

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")

View example output


Was this page helpful?

Thank you for your feedback!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Read more

InfluxDB v3 enhancements and InfluxDB Clustered is now generally available

New capabilities, including faster query performance and management tooling advance the InfluxDB v3 product line. InfluxDB Clustered is now generally available.

InfluxDB v3 performance and features

The InfluxDB v3 product line has seen significant enhancements in query performance and has made new management tooling available. These enhancements include an operational dashboard to monitor the health of your InfluxDB cluster, single sign-on (SSO) support in InfluxDB Cloud Dedicated, and new management APIs for tokens and databases.

Learn about the new v3 enhancements


InfluxDB Clustered general availability

InfluxDB Clustered is now generally available and gives you the power of InfluxDB v3 in your self-managed stack.

Talk to us about InfluxDB Clustered