docs-v2/data/query_examples.yml

581 lines
17 KiB
YAML

aggregate_window:
-
code: |
```js
data
|> aggregateWindow(every: 20m, fn: mean)
```
input: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:00:00Z | 250 |
| 2020-01-01T00:04:00Z | 160 |
| 2020-01-01T00:12:00Z | 150 |
| 2020-01-01T00:19:00Z | 220 |
| 2020-01-01T00:32:00Z | 200 |
| 2020-01-01T00:51:00Z | 290 |
| 2020-01-01T01:00:00Z | 340 |
output: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:20:00Z | 195 |
| 2020-01-01T00:40:00Z | 200 |
| 2020-01-01T01:00:00Z | 290 |
| 2020-01-01T01:20:00Z | 340 |
cumulative_sum:
-
code: |
```js
data
|> cumulativeSum()
```
input: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | 1 |
| 2020-01-01T00:02:00Z | 2 |
| 2020-01-01T00:03:00Z | 1 |
| 2020-01-01T00:04:00Z | 3 |
output: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | 1 |
| 2020-01-01T00:02:00Z | 3 |
| 2020-01-01T00:03:00Z | 4 |
| 2020-01-01T00:04:00Z | 7 |
fill_null:
-
code: |
```js
data
|> fill(usePrevious: true)
```
input: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | null |
| 2020-01-01T00:02:00Z | 0.8 |
| 2020-01-01T00:03:00Z | null |
| 2020-01-01T00:04:00Z | null |
| 2020-01-01T00:05:00Z | 1.4 |
output: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | null |
| 2020-01-01T00:02:00Z | 0.8 |
| 2020-01-01T00:03:00Z | 0.8 |
| 2020-01-01T00:04:00Z | 0.8 |
| 2020-01-01T00:05:00Z | 1.4 |
-
code: |
```js
data
|> fill(value: 0.0)
```
input: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | null |
| 2020-01-01T00:02:00Z | 0.8 |
| 2020-01-01T00:03:00Z | null |
| 2020-01-01T00:04:00Z | null |
| 2020-01-01T00:05:00Z | 1.4 |
output: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | 0.0 |
| 2020-01-01T00:02:00Z | 0.8 |
| 2020-01-01T00:03:00Z | 0.0 |
| 2020-01-01T00:04:00Z | 0.0 |
| 2020-01-01T00:05:00Z | 1.4 |
first_last:
-
code: |
```js
data
|> first()
```
input: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | 1.0 |
| 2020-01-01T00:02:00Z | 1.0 |
| 2020-01-01T00:03:00Z | 2.0 |
| 2020-01-01T00:04:00Z | 3.0 |
output: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | 1.0 |
-
code: |
```js
data
|> last()
```
input: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | 1.0 |
| 2020-01-01T00:02:00Z | 1.0 |
| 2020-01-01T00:03:00Z | 2.0 |
| 2020-01-01T00:04:00Z | 3.0 |
output: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:04:00Z | 3.0 |
group:
-
code: |
```js
data
|> group(columns: ["host"], mode: "by")
```
input: |
| _time | host | _value |
|:----- |:----: | ------:|
| 2020-01-01T00:01:00Z | host1 | 1.0 |
| 2020-01-01T00:01:00Z | host2 | 2.0 |
| 2020-01-01T00:02:00Z | host1 | 1.0 |
| 2020-01-01T00:02:00Z | host2 | 3.0 |
output: |
| _time | host | _value |
|:----- |:----: | ------:|
| 2020-01-01T00:01:00Z | host1 | 1.0 |
| 2020-01-01T00:02:00Z | host1 | 1.0 |
| _time | host | _value |
|:----- |:----: | ------:|
| 2020-01-01T00:01:00Z | host2 | 2.0 |
| 2020-01-01T00:02:00Z | host2 | 3.0 |
histogram:
-
code: |
```js
data
|> histogram(
column: "_value",
upperBoundColumn: "le",
countColumn: "_value",
bins: [100.0, 200.0, 300.0, 400.0],
)
```
input: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:00:00Z | 250.0 |
| 2020-01-01T00:01:00Z | 160.0 |
| 2020-01-01T00:02:00Z | 150.0 |
| 2020-01-01T00:03:00Z | 220.0 |
| 2020-01-01T00:04:00Z | 200.0 |
| 2020-01-01T00:05:00Z | 290.0 |
| 2020-01-01T01:00:00Z | 340.0 |
output: |
| le | _value |
|:-- | ------:|
| 100.0 | 0.0 |
| 200.0 | 3.0 |
| 300.0 | 6.0 |
| 400.0 | 7.0 |
increase:
-
code: |
```js
data
|> increase()
```
input: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | 1 |
| 2020-01-01T00:02:00Z | 2 |
| 2020-01-01T00:03:00Z | 8 |
| 2020-01-01T00:04:00Z | 10 |
| 2020-01-01T00:05:00Z | 0 |
| 2020-01-01T00:06:00Z | 4 |
output: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:02:00Z | 1 |
| 2020-01-01T00:03:00Z | 7 |
| 2020-01-01T00:04:00Z | 9 |
| 2020-01-01T00:05:00Z | 9 |
| 2020-01-01T00:06:00Z | 13 |
interpolate_linear:
-
code: |
```js
import "interpolate"
data
|> interpolate.linear(every: 1d)
```
input: |
| _time | _value |
|:----- | ------:|
| 2021-01-01T00:00:00Z | 10.0 |
| 2021-01-02T00:00:00Z | 20.0 |
| 2021-01-04T00:00:00Z | 40.0 |
| 2021-01-05T00:00:00Z | 50.0 |
| 2021-01-08T00:00:00Z | 80.0 |
| 2021-01-09T00:00:00Z | 90.0 |
output: |
| _time | _value |
|:----- | ------:|
| 2021-01-01T00:00:00Z | 10.0 |
| 2021-01-02T00:00:00Z | 20.0 |
| 2021-01-04T00:00:00Z | 40.0 |
| 2021-01-05T00:00:00Z | 50.0 |
| 2021-01-06T00:00:00Z | 60.0 |
| 2021-01-07T00:00:00Z | 70.0 |
| 2021-01-08T00:00:00Z | 80.0 |
| 2021-01-09T00:00:00Z | 90.0 |
join-new:
-
code: |
```js
import "join"
import "sql"
left =
from(bucket: "example-bucket-1")
|> range(start: "-1h")
|> filter(fn: (r) => r._measurement == "example-m")
|> filter(fn: (r) => r._field == "example-f")
|> drop(columns: ["_measurement", "_field"])
right =
sql.from(
driverName: "postgres",
dataSourceName: "postgresql://username:password@localhost:5432",
query: "SELECT * FROM example_table",
)
join.inner(
left: left |> group(),
right: right,
on: (l, r) => l.sensorID == r.ID,
as: (l, r) => ({l with expired: r.expired}),
)
|> group(columns: ["_time", "_value"], mode: "except")
```
input: |
###### left
| _time | sensorID | _value |
|:----- | :------- | ------:|
| 2020-01-01T00:01:00Z | 1234 | 1 |
| 2020-01-01T00:02:00Z | 1234 | 2 |
| 2020-01-01T00:03:00Z | 1234 | 1 |
| 2020-01-01T00:04:00Z | 1234 | 3 |
| _time | sensorID | _value |
|:----- | :------- | ------:|
| 2020-01-01T00:01:00Z | 5678 | 2 |
| 2020-01-01T00:02:00Z | 5678 | 5 |
| 2020-01-01T00:03:00Z | 5678 | 1 |
| 2020-01-01T00:04:00Z | 5678 | 8 |
###### right
| ID | expired | serviced |
|:---- | :------ | ---------: |
| 1234 | false | 2022-01-01 |
| 5678 | true | 2022-01-01 |
output: |
| _time | sensorID | _value | expired |
| :------------------- | :------- | -----: | :------ |
| 2020-01-01T00:01:00Z | 1234 | 1 | false |
| 2020-01-01T00:02:00Z | 1234 | 2 | false |
| 2020-01-01T00:03:00Z | 1234 | 1 | false |
| 2020-01-01T00:04:00Z | 1234 | 3 | false |
| _time | sensorID | _value | expired |
| :------------------- | :------- | -----: | :------ |
| 2020-01-01T00:01:00Z | 5678 | 2 | true |
| 2020-01-01T00:02:00Z | 5678 | 5 | true |
| 2020-01-01T00:03:00Z | 5678 | 1 | true |
| 2020-01-01T00:04:00Z | 5678 | 8 | true |
join:
-
code: |
```js
t1 = from(bucket: "example-bucket")
|> range(start: 2020-01-01T00:00:00Z)
|> filter(fn: (r) => r.m == "foo")
t2 = from(bucket: "example-bucket")
|> range(start: 2020-01-01T00:00:00Z)
|> filter(fn: (r) => r.m == "bar")
join(tables: {t1: t1, t2: t2}, on: ["_time"])
```
input: |
###### t1
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | 1 |
| 2020-01-01T00:02:00Z | 2 |
| 2020-01-01T00:03:00Z | 1 |
| 2020-01-01T00:04:00Z | 3 |
###### t2
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | 5 |
| 2020-01-01T00:02:00Z | 2 |
| 2020-01-01T00:03:00Z | 3 |
| 2020-01-01T00:04:00Z | 4 |
output: |
| _time | _value_t1 | _value_t2 |
|:----- | ---------:| ---------:|
| 2020-01-01T00:01:00Z | 1 | 5 |
| 2020-01-01T00:02:00Z | 2 | 2 |
| 2020-01-01T00:03:00Z | 1 | 3 |
| 2020-01-01T00:04:00Z | 3 | 4 |
map_math:
-
code: |
```js
data
|> map(fn: (r) => ({ r with _value: r._value * r._value }))
```
input: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | 2 |
| 2020-01-01T00:02:00Z | 4 |
| 2020-01-01T00:03:00Z | 3 |
| 2020-01-01T00:04:00Z | 5 |
output: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | 4 |
| 2020-01-01T00:02:00Z | 16 |
| 2020-01-01T00:03:00Z | 9 |
| 2020-01-01T00:04:00Z | 25 |
median:
-
code: |
```js
data
|> median()
```
input: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | 1.0 |
| 2020-01-01T00:02:00Z | 1.0 |
| 2020-01-01T00:03:00Z | 2.0 |
| 2020-01-01T00:04:00Z | 3.0 |
output: |
| _value |
|:------:|
| 1.5 |
moving_average:
-
code: |
```js
data
|> movingAverage(n: 3)
```
input: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | 1.0 |
| 2020-01-01T00:02:00Z | 1.2 |
| 2020-01-01T00:03:00Z | 1.8 |
| 2020-01-01T00:04:00Z | 0.9 |
| 2020-01-01T00:05:00Z | 1.4 |
| 2020-01-01T00:06:00Z | 2.0 |
output: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:03:00Z | 1.33 |
| 2020-01-01T00:04:00Z | 1.30 |
| 2020-01-01T00:05:00Z | 1.36 |
| 2020-01-01T00:06:00Z | 1.43 |
-
code: |
```js
data
|> timedMovingAverage(every: 2m, period: 4m)
```
input: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | 1.0 |
| 2020-01-01T00:02:00Z | 1.2 |
| 2020-01-01T00:03:00Z | 1.8 |
| 2020-01-01T00:04:00Z | 0.9 |
| 2020-01-01T00:05:00Z | 1.4 |
| 2020-01-01T00:06:00Z | 2.0 |
output: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:02:00Z | 1.000 |
| 2020-01-01T00:04:00Z | 1.333 |
| 2020-01-01T00:06:00Z | 1.325 |
| 2020-01-01T00:06:00Z | 1.150 |
percentages:
-
code: |
```js
data
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> map(
fn: (r) => ({
_time: r._time,
_field: "used_percent",
_value: float(v: r.used) / float(v: r.total) * 100.0,
}),
)
```
input: |
| _time | _field | _value |
|:----- |:------: | ------:|
| 2020-01-01T00:00:00Z | used | 2.5 |
| 2020-01-01T00:00:10Z | used | 3.1 |
| 2020-01-01T00:00:20Z | used | 4.2 |
| _time | _field | _value |
|:----- |:------: | ------:|
| 2020-01-01T00:00:00Z | total | 8.0 |
| 2020-01-01T00:00:10Z | total | 8.0 |
| 2020-01-01T00:00:20Z | total | 8.0 |
output: |
| _time | _field | _value |
|:----- |:------: | ------:|
| 2020-01-01T00:00:00Z | used_percent | 31.25 |
| 2020-01-01T00:00:10Z | used_percent | 38.75 |
| 2020-01-01T00:00:20Z | used_percent | 52.50 |
quantile:
-
code: |
```js
data
|> quantile(q: 0.99, method: "estimate_tdigest")
```
input: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:01:00Z | 1.0 |
| 2020-01-01T00:02:00Z | 1.0 |
| 2020-01-01T00:03:00Z | 2.0 |
| 2020-01-01T00:04:00Z | 3.0 |
output: |
| _value |
|:------:|
| 3.0 |
rate_of_change:
-
code: |
```js
data
|> derivative(unit: 1m, nonNegative: true)
```
input: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:00:00Z | 250 |
| 2020-01-01T00:04:00Z | 160 |
| 2020-01-01T00:12:00Z | 150 |
| 2020-01-01T00:19:00Z | 220 |
| 2020-01-01T00:32:00Z | 200 |
| 2020-01-01T00:51:00Z | 290 |
| 2020-01-01T01:00:00Z | 340 |
output: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:04:00Z | |
| 2020-01-01T00:12:00Z | |
| 2020-01-01T00:19:00Z | 10.0 |
| 2020-01-01T00:32:00Z | |
| 2020-01-01T00:51:00Z | 4.74 |
| 2020-01-01T01:00:00Z | 5.56 |
-
code: |
```js
import "experimental/aggregate"
data
|> aggregate.rate(every: 20m, unit: 1m)
```
input: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:00:00Z | 250 |
| 2020-01-01T00:04:00Z | 160 |
| 2020-01-01T00:12:00Z | 150 |
| 2020-01-01T00:19:00Z | 220 |
| 2020-01-01T00:32:00Z | 200 |
| 2020-01-01T00:51:00Z | 290 |
| 2020-01-01T01:00:00Z | 340 |
output: |
| _time | _value |
|:----- | ------:|
| 2020-01-01T00:20:00Z | |
| 2020-01-01T00:40:00Z | 10.0 |
| 2020-01-01T01:00:00Z | 4.74 |
| 2020-01-01T01:20:00Z | 5.56 |
regular_expressions:
-
code: |
```js
data
|> filter(fn: (r) => r.tag =~ /^foo[1-3]/)
```
input: |
| _time | tag | _value |
|:----- |:----:| ------:|
| 2020-01-01T00:01:00Z | foo1 | 1.0 |
| 2020-01-01T00:02:00Z | foo5 | 1.2 |
| 2020-01-01T00:03:00Z | bar3 | 1.8 |
| 2020-01-01T00:04:00Z | foo3 | 0.9 |
| 2020-01-01T00:05:00Z | foo2 | 1.4 |
| 2020-01-01T00:06:00Z | bar1 | 2.0 |
output: |
| _time | tag | _value |
|:----- |:----:| ------:|
| 2020-01-01T00:01:00Z | foo1 | 1.0 |
| 2020-01-01T00:04:00Z | foo3 | 0.9 |
| 2020-01-01T00:05:00Z | foo2 | 1.4 |
sort_limit:
-
code: |
```js
data
|> sort(columns: ["host", "_value"])
|> limit(n: 4)
```
input: |
| _time | host | _value |
|:----- |:----:| ------:|
| 2020-01-01T00:01:00Z | A | 1.0 |
| 2020-01-01T00:02:00Z | B | 1.2 |
| 2020-01-01T00:03:00Z | A | 1.8 |
| 2020-01-01T00:04:00Z | B | 0.9 |
| 2020-01-01T00:05:00Z | B | 1.4 |
| 2020-01-01T00:06:00Z | B | 2.0 |
output: |
| _time | host | _value |
|:----- |:----:| ------:|
| 2020-01-01T00:03:00Z | A | 1.8 |
| 2020-01-01T00:01:00Z | A | 1.0 |
| 2020-01-01T00:06:00Z | B | 2.0 |
| 2020-01-01T00:05:00Z | B | 1.4 |