Skip to content

Commit

Permalink
[CrateDB] Improve standard SQL statement for Grafana instant dashboards
Browse files Browse the repository at this point in the history
Apply time bucketing over interval obtained from Grafana's date range
picker.

Emulate `GROUP BY DATE_BIN()` by using Grafana's `$__timeGroupAlias`
macro for casting `$__interval` values, until CrateDB's `DATE_BIN()`
function understands Grafana's native interval values.
  • Loading branch information
amotl committed Jun 17, 2023
1 parent 8abe55d commit a609bbb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"groupByTags": [],
"measurement": "{{ measurement }}",
"rawQuery": true,
"rawSql": "SELECT time, fields['{{ name }}'] AS {{ alias }} FROM {{ table }} WHERE $__timeFilter(time)"
"rawSql": "SELECT $__timeGroupAlias(time, $__interval), MEAN(fields['{{ name }}']) AS {{ alias }} FROM {{ table }} WHERE $__timeFilter(time) GROUP BY time ORDER BY time"
}
4 changes: 3 additions & 1 deletion test/test_daq_grafana.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ def test_mqtt_cratedb_grafana_single(machinery_cratedb, reset_cratedb, reset_gra
logger.info('Grafana: Checking dashboard layout')
target = dashboard['rows'][0]['panels'][0]['targets'][0]
assert target['measurement'] == settings.cratedb_measurement_sensors
assert target['rawSql'] == "SELECT time, fields['humidity'] AS humidity FROM mqttkit_2_itest.foo_bar_sensors WHERE $__timeFilter(time)"
assert target['rawSql'] == \
"SELECT $__timeGroupAlias(time, $__interval, 0), MEAN(fields['humidity']) AS humidity " \
"FROM mqttkit_2_itest.foo_bar_sensors WHERE $__timeFilter(time) GROUP BY time ORDER BY time"


@pytest_twisted.inlineCallbacks
Expand Down

0 comments on commit a609bbb

Please sign in to comment.