updated the custom functions and execute queries guides

pull/22/head
Scott Anderson 2019-01-22 08:54:31 -07:00
parent b07b6544f1
commit 6024e26dcc
3 changed files with 34 additions and 26 deletions

View File

@ -141,12 +141,14 @@
font-family: 'Inconsolata', monospace; font-family: 'Inconsolata', monospace;
} }
p code { p,li,table,h2,h3,h4,h5,h6 {
padding: .15rem .45rem .25rem; code {
border-radius: $border-radius; padding: .15rem .45rem .25rem;
color: $article-code; border-radius: $border-radius;
white-space: nowrap; color: $article-code;
font-style: normal; white-space: nowrap;
font-style: normal;
}
} }
a { a {

View File

@ -20,14 +20,14 @@ The basic structure for defining functions in Flux is as follows:
functionName = (functionParameters) => functionOperations functionName = (functionParameters) => functionOperations
``` ```
##### `functionName` ##### functionName
The name used to call the function in your Flux script. The name used to call the function in your Flux script.
##### `functionParameters` ##### functionParameters
A comma-separated list of parameters passed into the function and used in its operations. A comma-separated list of parameters passed into the function and used in its operations.
[Parameter defaults](#define-parameter-defaults) can be defined for each. [Parameter defaults](#define-parameter-defaults) can be defined for each.
##### `functionOperations` ##### functionOperations
Operations and functions that manipulate the input into the desired output. Operations and functions that manipulate the input into the desired output.
#### Basic function examples #### Basic function examples
@ -52,14 +52,14 @@ multiply = (x, y) => x * y
30 30
``` ```
## Functions that manipulate pipe-forwarded data ## Functions that manipulate piped-forward data
Most Flux functions manipulate data pipe-forwarded into the function. Most Flux functions manipulate data piped-forward into the function.
In order for a custom function to process pipe-forwarded data, one of the function In order for a custom function to process piped-forward data, one of the function
parameters must capture the input tables using the `<-` pipe-receive expression. parameters must capture the input tables using the `<-` pipe-receive expression.
In the example below, the `tables` parameter is assigned to the `<-` expression, In the example below, the `tables` parameter is assigned to the `<-` expression,
which represents all data pipe-forwarded into the function. which represents all data piped-forward into the function.
`tables` is then pipe-forwarded into other operations in the function definition. `tables` is then piped-forward into other operations in the function definition.
```js ```js
functionName = (tables=<-) => tables |> functionOperations functionName = (tables=<-) => tables |> functionOperations
@ -70,7 +70,8 @@ functionName = (tables=<-) => tables |> functionOperations
###### Multiply row values by x ###### Multiply row values by x
The example below defines a `multByX` function that multiplies the `_value` column The example below defines a `multByX` function that multiplies the `_value` column
of each row in the input table by the `x` parameter. of each row in the input table by the `x` parameter.
It uses the [`map()` function](/v2.0/reference/flux/functions/transformations/map) to modify each `_value`. It uses the [`map()` function](/v2.0/reference/flux/functions/transformations/map)
to modify each `_value`.
```js ```js
// Function definition // Function definition
@ -89,8 +90,8 @@ from(bucket: "telegraf/autogen")
``` ```
## Define parameter defaults ## Define parameter defaults
To define parameters with default values, use the `=` assignment operator to assign Use the `=` assignment operator to assign a default value to function parameters
a default in your function definition: in your function definition:
```js ```js
functionName = (param1=defaultValue1, param2=defaultValue2) => functionOperation functionName = (param1=defaultValue1, param2=defaultValue2) => functionOperation
@ -103,8 +104,10 @@ Defaults are overridden by explicitly defining the parameter in the function cal
###### Get the winner or the "winner" ###### Get the winner or the "winner"
The example below defines a `getWinner` function that returns the record with the highest The example below defines a `getWinner` function that returns the record with the highest
or lowest `_value` (winner versus "winner") depending on the `noSarcasm` parameter which defaults to `true`. or lowest `_value` (winner versus "winner") depending on the `noSarcasm` parameter which defaults to `true`.
It uses the [`sort()` function](/v2.0/reference/flux/functions/transformations/sort) to sort records in either descending or ascending order. It uses the [`sort()` function](/v2.0/reference/flux/functions/transformations/sort)
It then uses the [`limit()` function](/v2.0/reference/flux/functions/transformations/limit) to return the first record from the sorted table. to sort records in either descending or ascending order.
It then uses the [`limit()` function](/v2.0/reference/flux/functions/transformations/limit)
to return the first record from the sorted table.
```js ```js
// Function definition // Function definition

View File

@ -48,17 +48,18 @@ data = from(bucket: "example-bucket") |> range(start: -10m) # ...
``` ```
## InfluxDB API ## InfluxDB API
Flux can be used to query InfluxDB through InfluxDB's `/api/v2/query` endpoint. Query InfluxDB through the `/api/v2/query` endpoint.
Queried data is returned in annotated CSV format. Queried data is returned in annotated CSV format.
In your request, set the following: In your request, set the following:
- `Authorization` header to `Token ` + your authentication token.
- `accept` header to `application/csv` - `accept` header to `application/csv`
- `content-type` header to `application/vnd.flux` - `content-type` header to `application/vnd.flux`
This allows you to POST the Flux query in plain text and receive the annotated CSV response. This allows you to POST the Flux query in plain text and receive the annotated CSV response.
Below is an example `curl` command that queries InfluxDB using Flux: Below is an example `curl` command that queries InfluxDB:
{{< code-tabs-wrapper >}} {{< code-tabs-wrapper >}}
{{% code-tabs %}} {{% code-tabs %}}
@ -68,18 +69,20 @@ Below is an example `curl` command that queries InfluxDB using Flux:
{{% code-tab-content %}} {{% code-tab-content %}}
```bash ```bash
curl localhost:8086/api/v2/query -XPOST -sS \ curl http://localhost:9999/api/v2/query -XPOST -sS \
-H 'Authorization: Token YOURAUTHTOKEN' \
-H 'accept:application/csv' \ -H 'accept:application/csv' \
-H 'content-type:application/vnd.flux' \ -H 'content-type:application/vnd.flux' \
-d 'from(bucket:"telegraf") -d 'from(bucket:“test”)
|> range(start:-5m) |> range(start:-1000h)
|> filter(fn:(r) => r._measurement == "cpu")' |> group(columns:[“_measurement”], mode:“by”)
|> sum()'
``` ```
{{% /code-tab-content %}} {{% /code-tab-content %}}
{{% code-tab-content %}} {{% code-tab-content %}}
```bash ```bash
curl localhost:8086/api/v2/query -XPOST -sS -H 'accept:application/csv' -H 'content-type:application/vnd.flux' -d 'from(bucket:"telegraf") |> range(start:-5m) |> filter(fn:(r) => r._measurement == "cpu")' curl http://localhost:9999/api/v2/query -XPOST -sS -H 'Authorization: Token TOKENSTRINGHERE' -H 'accept:application/csv' -H 'content-type:application/vnd.flux' -d 'from(bucket:“test”) |> range(start:-1000h) |> group(columns:[“_measurement”], mode:“by”) |> sum()'
``` ```
{{% /code-tab-content %}} {{% /code-tab-content %}}
{{< /code-tabs-wrapper >}} {{< /code-tabs-wrapper >}}