Template variablesとは
公式ドキュメントより。
A variable is a placeholder for a value. You can use variables in metric queries and in panel titles. So when you change the value, using the drop-down list at the top of the dashboard, your panel’s metric queries will change to reflect the new value.
Variables allow you to create more interactive and dynamic dashboards. Instead of hard-coding things like server, application, and sensor names in your metric queries, you can use variables in their place. Variables are displayed as drop-down lists at the top of the dashboard. These drop-downs make it easy to change the data being displayed in your dashboard.
ダッシュボード読み込み時にvariableを動的生成する場合、Query variableを使えばよい。
InfluxDBの場合は通常Query
欄に以下のようにクエリを指定する。
SHOW TAG VALUES FROM "<measurement>" WITH KEY = "<tag key>"
Template variableに時間による絞り込みを加える
上記のSHOW TAG VALUES
クエリの場合、過去すべての<tag key>
の値が表示される。過去の値すべてが不要なケースにおいて、いつまでもTemplate variablesに値が残ってしまうのは都合が悪い。
SELECT
クエリと同様にSHOW TAG VALUES
クエリが以下のようなWHERE句を解釈できればよいか、あいにくサポートされていない。
SHOW TAG VALUES FROM "<measurement>" WITH KEY = "<tag key>" WHERE $timeFilter
or
SHOW TAG VALUES FROM "<measurement>" WITH KEY = "<tag key>" WHERE time > now() - 1h
ワークアラウンドとして、SELECT
クエリを使っう以下の方法ならうまく機能するようだ。
SELECT "<tag value>" FROM ( SELECT "<field>", "<tag value>" FROM "<measurment>" WHERE $timeFilter )
or
SELECT "<tag value>" FROM ( SELECT "<field>", "<tag value>" FROM "<measurment>" WHERE time > now() -1h )