update reduce examples, closes influxdata/DAR#249 (#3446)
parent
f0cd5390f8
commit
86f67a8e50
|
@ -27,8 +27,8 @@ _`reduce()` is an [aggregate function](/flux/v0.x/function-types/#aggregates)._
|
||||||
|
|
||||||
```js
|
```js
|
||||||
reduce(
|
reduce(
|
||||||
fn: (r, accumulator) => ({ sum: r._value + accumulator.sum }),
|
fn: (r, accumulator) => ({ sum: r._value + accumulator.sum }),
|
||||||
identity: {sum: 0.0}
|
identity: {sum: 0.0}
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -62,10 +62,10 @@ If the output record keys and value types do not match the `identity` keys and v
|
||||||
it will return a type error.
|
it will return a type error.
|
||||||
{{% /note %}}
|
{{% /note %}}
|
||||||
|
|
||||||
#### r
|
#### r {data-type=record}
|
||||||
Record representing each row or record.
|
Record representing each row or record.
|
||||||
|
|
||||||
#### accumulator
|
#### accumulator {data-type=record}
|
||||||
Reducer record defined by [`identity`](#identity).
|
Reducer record defined by [`identity`](#identity).
|
||||||
|
|
||||||
### identity {data-type="record"}
|
### identity {data-type="record"}
|
||||||
|
@ -108,9 +108,7 @@ import "sampledata"
|
||||||
|
|
||||||
sampledata.int()
|
sampledata.int()
|
||||||
|> reduce(
|
|> reduce(
|
||||||
fn: (r, accumulator) => ({
|
fn: (r, accumulator) => ({sum: r._value + accumulator.sum}),
|
||||||
sum: r._value + accumulator.sum
|
|
||||||
}),
|
|
||||||
identity: {sum: 0}
|
identity: {sum: 0}
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
@ -147,8 +145,8 @@ import "sampledata"
|
||||||
sampledata.int()
|
sampledata.int()
|
||||||
|> reduce(
|
|> reduce(
|
||||||
fn: (r, accumulator) => ({
|
fn: (r, accumulator) => ({
|
||||||
sum: r._value + accumulator.sum,
|
sum: r._value + accumulator.sum,
|
||||||
count: accumulator.count + 1
|
count: accumulator.count + 1,
|
||||||
}),
|
}),
|
||||||
identity: {sum: 0, count: 0}
|
identity: {sum: 0, count: 0}
|
||||||
)
|
)
|
||||||
|
@ -185,9 +183,7 @@ import "sampledata"
|
||||||
|
|
||||||
sampledata.int()
|
sampledata.int()
|
||||||
|> reduce(
|
|> reduce(
|
||||||
fn: (r, accumulator) => ({
|
fn: (r, accumulator) => ({prod: r._value * accumulator.prod}),
|
||||||
prod: r._value * accumulator.prod
|
|
||||||
}),
|
|
||||||
identity: {prod: 1}
|
identity: {prod: 1}
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
@ -222,13 +218,14 @@ sampledata.int()
|
||||||
import "sampledata"
|
import "sampledata"
|
||||||
|
|
||||||
sampledata.int()
|
sampledata.int()
|
||||||
|> reduce(fn: (r, accumulator) => ({
|
|> reduce(
|
||||||
count: accumulator.count + 1,
|
fn: (r, accumulator) => ({
|
||||||
total: accumulator.total + r._value,
|
count: accumulator.count + 1,
|
||||||
avg: float(v: (accumulator.total + r._value)) / float(v: accumulator.count)
|
total: accumulator.total + r._value,
|
||||||
}),
|
avg: float(v: (accumulator.total + r._value)) / float(v: accumulator.count + 1)
|
||||||
identity: {count: 1, total: 0, avg: 0.0}
|
}),
|
||||||
)
|
identity: {count: 0, total: 0, avg: 0.0}
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
{{< expand-wrapper >}}
|
{{< expand-wrapper >}}
|
||||||
|
@ -245,11 +242,11 @@ sampledata.int()
|
||||||
##### Output data
|
##### Output data
|
||||||
| tag | avg | count | total |
|
| tag | avg | count | total |
|
||||||
| :-- | --: | ----: | ----: |
|
| :-- | --: | ----: | ----: |
|
||||||
| t1 | 8.5 | 7 | 51 |
|
| t1 | 8.5 | 6 | 51 |
|
||||||
|
|
||||||
| tag | avg | count | total |
|
| tag | avg | count | total |
|
||||||
| :-- | ----------------: | ----: | ----: |
|
| :-- | ----: | ----: | ----: |
|
||||||
| t2 | 8.834 | 7 | 53 |
|
| t2 | 8.834 | 6 | 53 |
|
||||||
|
|
||||||
{{% /flex-content %}}
|
{{% /flex-content %}}
|
||||||
{{< /flex >}}
|
{{< /flex >}}
|
||||||
|
|
|
@ -159,23 +159,18 @@ does the same thing and is much more performant._
|
||||||
{{% code-tab-content %}}
|
{{% code-tab-content %}}
|
||||||
|
|
||||||
```js
|
```js
|
||||||
average = (tables=<-, outputField="average") =>
|
average = (tables=<-, outputField="average") => tables
|
||||||
tables
|
|
||||||
|> reduce(
|
|> reduce(
|
||||||
// Define the initial accumulator record
|
// Define the initial accumulator record
|
||||||
identity: {
|
identity: {count: 0.0, sum: 0.0, avg: 0.0},
|
||||||
count: 1.0,
|
fn: (r, accumulator) => ({
|
||||||
sum: 0.0,
|
// Increment the counter on each reduce loop
|
||||||
avg: 0.0
|
count: accumulator.count + 1.0,
|
||||||
},
|
// Add the _value to the existing sum
|
||||||
fn: (r, accumulator) => ({
|
sum: accumulator.sum + r._value,
|
||||||
// Increment the counter on each reduce loop
|
// Divide the existing sum by the existing count for a new average
|
||||||
count: accumulator.count + 1.0,
|
avg: (accumulator.sum + r._value) / (accumulator.count + 1.0),
|
||||||
// Add the _value to the existing sum
|
}),
|
||||||
sum: accumulator.sum + r._value,
|
|
||||||
// Divide the existing sum by the existing count for a new average
|
|
||||||
avg: accumulator.sum / accumulator.count
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
// Drop the sum and the count columns since they are no longer needed
|
// Drop the sum and the count columns since they are no longer needed
|
||||||
|> drop(columns: ["sum", "count"])
|
|> drop(columns: ["sum", "count"])
|
||||||
|
@ -189,21 +184,16 @@ average = (tables=<-, outputField="average") =>
|
||||||
|
|
||||||
{{% code-tab-content %}}
|
{{% code-tab-content %}}
|
||||||
```js
|
```js
|
||||||
average = (tables=<-, outputField="average") =>
|
average = (tables=<-, outputField="average") => tables
|
||||||
tables
|
|
||||||
|> reduce(
|
|> reduce(
|
||||||
identity: {
|
identity: {count: 0.0, sum: 0.0, avg: 0.0},
|
||||||
count: 1.0,
|
fn: (r, accumulator) => ({
|
||||||
sum: 0.0,
|
count: accumulator.count + 1.0,
|
||||||
avg: 0.0
|
sum: accumulator.sum + r._value,
|
||||||
},
|
avg: (accumulator.sum + r._value) / (accumulator.count + 1.0),
|
||||||
fn: (r, accumulator) => ({
|
}),
|
||||||
count: accumulator.count + 1.0,
|
|
||||||
sum: accumulator.sum + r._value,
|
|
||||||
avg: accumulator.sum / accumulator.count
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
|> drop(columns: ["sum", "count"])
|
|> drop(columns: ["sum", "count"])
|
||||||
|> set(key: "_field", value: outputField)
|
|> set(key: "_field", value: outputField)
|
||||||
|> rename(columns: {avg: "_value"})
|
|> rename(columns: {avg: "_value"})
|
||||||
```
|
```
|
||||||
|
@ -218,23 +208,22 @@ The following function expects input tables to have `c1_value` and `c2_value`
|
||||||
columns and generates an average for each.
|
columns and generates an average for each.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
multiAvg = (tables=<-) =>
|
multiAvg = (tables=<-) => tables
|
||||||
tables
|
|
||||||
|> reduce(
|
|> reduce(
|
||||||
identity: {
|
identity: {
|
||||||
count: 1.0,
|
count: 1.0,
|
||||||
c1_sum: 0.0,
|
c1_sum: 0.0,
|
||||||
c1_avg: 0.0,
|
c1_avg: 0.0,
|
||||||
c2_sum: 0.0,
|
c2_sum: 0.0,
|
||||||
c2_avg: 0.0
|
c2_avg: 0.0,
|
||||||
},
|
},
|
||||||
fn: (r, accumulator) => ({
|
fn: (r, accumulator) => ({
|
||||||
count: accumulator.count + 1.0,
|
count: accumulator.count + 1.0,
|
||||||
c1_sum: accumulator.c1_sum + r.c1_value,
|
c1_sum: accumulator.c1_sum + r.c1_value,
|
||||||
c1_avg: accumulator.c1_sum / accumulator.count,
|
c1_avg: accumulator.c1_sum / accumulator.count,
|
||||||
c2_sum: accumulator.c2_sum + r.c2_value,
|
c2_sum: accumulator.c2_sum + r.c2_value,
|
||||||
c2_avg: accumulator.c2_sum / accumulator.count
|
c2_avg: accumulator.c2_sum / accumulator.count,
|
||||||
})
|
}),
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -243,16 +232,13 @@ Use `reduce()` to create a function that aggregates gross and net profit.
|
||||||
This example expects `profit` and `expenses` columns in the input tables.
|
This example expects `profit` and `expenses` columns in the input tables.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
profitSummary = (tables=<-) =>
|
profitSummary = (tables=<-) => tables
|
||||||
tables
|
|
||||||
|> reduce(
|
|> reduce(
|
||||||
identity: {
|
identity: {gross: 0.0, net: 0.0},
|
||||||
gross: 0.0,
|
fn: (r, accumulator) => ({
|
||||||
net: 0.0
|
gross: accumulator.gross + r.profit,
|
||||||
},
|
net: accumulator.net + r.profit - r.expenses
|
||||||
fn: (r, accumulator) => ({
|
}
|
||||||
gross: accumulator.gross + r.profit,
|
)
|
||||||
net: accumulator.net + r.profit - r.expenses
|
|
||||||
})
|
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in New Issue