• 沒有找到結果。

Text editor mode (raw)

在文檔中 Amazon Managed Grafana (頁 139-144)

You can switch to the raw query editor mode by choosing the hamburger icon and selecting Switch editor mode or by choosing Edit SQL below the query.

NoteIf you use the raw query editor, be sure that your query at minimum has ORDER BY time and a filter on the returned time range.

Macros

To simplify syntax and to allow for dynamic parts, such as date range filters, the query can contain macros.

Macro example Description

$__time(dateColumn) Will be replaced

by an expression to convert to a UNIX timestamp and rename the column to time_sec;

for example,

UNIX_TIMESTAMP(dateColumn) as time_sec.

$__timeEpoch(dateColumn) Will be replaced

by an expression to convert to a UNIX timestamp and rename the column to time_sec;

for example,

UNIX_TIMESTAMP(dateColumn) as time_sec.

Macro example Description

$__timeFilter(dateColumn) Will be replaced

by a time range filter using the specified column name.

For example, dateColumn BETWEEN

FROM_UNIXTIME(1494410783) ANDFROM_UNIXTIME(1494410983).

$__timeFrom() Will be replaced

by the start of the currently active time selection.

For example,

FROM_UNIXTIME(1494410783).

$__timeTo() Will be replaced

by the end of the currently active time selection.

For example,

FROM_UNIXTIME(1494410983).

$__timeGroup(dateColumn,'5m') Will be replaced

by an expression usable in GROUP BY clause.

For example,

cast(cast(UNIX_TIMESTAMP(dateColumn)/

(300) as signed)300 as signed),*

$__timeGroup(dateColumn,'5m', 0) Same as the

previous row, but with a fill parameter so missing points in that series will be added by grafana and 0 will be used as value.

$__timeGroup(dateColumn,'5m', NULL) Same as above

but NULL will be used as value for missing points.

Macro example Description

$__timeGroup(dateColumn,'5m', previous) Same as above

but the previous value in that series will be used as fill value if no value has been seen yet NULL will be used (only available in Grafana 5.3+).

$__timeGroupAlias(dateColumn,'5m') Will be replaced

identical to

$__timeGroup but with an added column alias (available only in Grafana 5.3+).

$__unixEpochFilter(dateColumn) Will be replaced

by a time range filter using the specified column name with times represented as Unix timestamp.

For example, dateColumn

> 1494410783 ANDdateColumn <

1494497183.

$__unixEpochFrom() Will be replaced

by the start of the currently active time selection as Unix timestamp.

For example, 1494410783.

$__unixEpochTo() Will be replaced

by the end of the currently active time selection as Unix timestamp.

For example, 1494497183.

Macro example Description

$__unixEpochGroupAlias(dateColumn,"5m", [fillmode])` Same as above but also adds a column alias (available only in Grafana 5.3+).

The query editor has a Generated SQL link that shows up after a query has run, while in panel edit mode. Choose it, and it will expand and show the raw interpolated SQL string that was run.

Table queries

If the Format as query option is set to Table, you can basically do any type of SQL query. The table panel will automatically show the results of whatever columns and rows your query returns.

The following code shows an example query.

SELECT

title as 'Title',

user.login as 'Created By' , dashboard.created as 'Created On' FROM dashboard

INNER JOIN user on user.id = dashboard.created_by WHERE $__timeFilter(dashboard.created)

You can control the name of the Table panel columns by using regular as SQL column selection syntax.

Time series queries

If you set Format as to Time series, for use in a graph panel for example, the query must return a column named time that returns either a SQL datetime or any numeric data type representing Unix epoch. Any column except time and metric is treated as a value column. You may return a column named metric that is used as metric name for the value column. If you return multiple value columns and a column named metric, this column is used as prefix for the series name (available only in Grafana 5.3+).

Result sets of time series queries must be sorted by time.

The following code example shows the metric column.

SELECT

$__timeGroup(time_date_time,'5m'), min(value_double),

'min' as metric FROM test_data

WHERE $__timeFilter(time_date_time) GROUP BY time

ORDER BY time

The following code example shows using the fill parameter in the $__timeGroup macro to convert null values to be zero instead.

SELECT

$__timeGroup(createdAt,'5m',0), sum(value_double) as value, measurement

FROM test_data WHERE

$__timeFilter(createdAt) GROUP BY time, measurement ORDER BY time

The following code example shows multiple columns.

SELECT

$__timeGroup(time_date_time,'5m'), min(value_double) as min_value, max(value_double) as max_value FROM test_data

WHERE $__timeFilter(time_date_time) GROUP BY time

ORDER BY time

Currently, there is no support for a dynamic group by time based on time range and panel width. This may be supported in the future.

Templating

Instead of hardcoding things such as server, application and sensor name in your metric queries you can use variables in their place. Variables are shown as dropdown select boxes at the top of the dashboard.

You can use these dropdown boxes to change the data being displayed in your dashboard.

For more information about templating and template variables, see Templates (p. 292).

Query variable

If you add a template variable of the type Query, you can write a MySQL query that can return things such as measurement names, key names, or key values that are shown as a dropdown select box.

For example, you can have a variable that contains all values for the hostname column in a table if you specify a query such as this in the templating variable Query setting.

SELECT hostname FROM my_host

A query can return multiple columns and Grafana will automatically create a list from them. For example, the following query will return a list with values from hostname and hostname2.

SELECT my_host.hostname, my_other_host.hostname2 FROM my_host JOIN my_other_host ON my_host.city = my_other_host.city

To use time range dependent macros such as $__timeFilter(column) in your query, the refresh mode of the template variable must be set to On Time Range Change.

SELECT event_name FROM event_log WHERE $__timeFilter(time_column)

Another option is a query that can create a key/value variable. The query should return two columns that are named __text and __value. The __text column value should be unique (if it is not unique, the first value is used). The options in the dropdown list will have a text and value so that you can have a friendly name as text and an ID as the value.

The following code example shows a query with hostname as the text and id as the value.

SELECT hostname AS __text, id AS __value FROM my_host

You can also create nested variables. For example, if you had another variable named region. Then you could have the hosts variable show only hosts from the current selected region with a query such as this (if region is a multi-value variable then use the IN comparison operator rather than = to match against multiple values).

SELECT hostname FROM my_host WHERE region IN($region)

在文檔中 Amazon Managed Grafana (頁 139-144)