Skip to content

Commit

Permalink
Merge pull request #186 from fgbogdan/feature/SQLLogic
Browse files Browse the repository at this point in the history
Feature/sql logic
  • Loading branch information
Slach authored Jul 24, 2020
2 parents 5242cac + 8ed9438 commit 0c6efab
Show file tree
Hide file tree
Showing 15 changed files with 899 additions and 30 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,51 @@ ORDER BY t
```
// see [issue 80](https://github.com/Vertamedia/clickhouse-grafana/issues/80) for the background

### SQL Logic/Scripting

Plugin allows using simple scripting conditional syntax, which will be evaluated before the SQL calls.
The conditional should be included between <% %> signs.
Possible keywords are:

* var - define a variable used in the code.
```
<%var X=1%>
```
* if/else - conditional
```
<%if($to - $from) > 3600){%>
select 'Please zoom in to view file actions...' as Info
<%}else{%>
select <column> as Info from <Table> where <Timestamp Column> between $from and $to
<%}%>
```
* for - cycle
```
<%var X=1;
for (i = 1; i < 5; i++) {
X=X*i;
}%>
SELECT
$timeSeries as t,
count()*<%X%>
FROM $table
WHERE $timeFilter
GROUP BY t
ORDER BY t
```
* switch/case/break
```
<%var col="WIN_NAME";
switch("$graph1"){
case "Extensions":col="EXTENSION";break;
case "Shares":col="SHARE_NAME";break;
case "Volumes":col="VOLUME_NAME";break;
case "SVM":col="SVM_NAME";break;
}%>
```

Important - the scripting is evaluated only once and before the CH calls !

---

## Templating
Expand Down
45 changes: 45 additions & 0 deletions dist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,51 @@ ORDER BY t
```
// see [issue 80](https://github.com/Vertamedia/clickhouse-grafana/issues/80) for the background

### SQL Logic/Scripting

Plugin allows using simple scripting conditional syntax, which will be evaluated before the SQL calls.
The conditional should be included between <% %> signs.
Possible keywords are:

* var - define a variable used in the code.
```
<%var X=1%>
```
* if/else - conditional
```
<%if($to - $from) > 3600){%>
select 'Please zoom in to view file actions...' as Info
<%}else{%>
select <column> as Info from <Table> where <Timestamp Column> between $from and $to
<%}%>
```
* for - cycle
```
<%var X=1;
for (i = 1; i < 5; i++) {
X=X*i;
}%>
SELECT
$timeSeries as t,
count()*<%X%>
FROM $table
WHERE $timeFilter
GROUP BY t
ORDER BY t
```
* switch/case/break
```
<%var col="WIN_NAME";
switch("$graph1"){
case "Extensions":col="EXTENSION";break;
case "Shares":col="SHARE_NAME";break;
case "Volumes":col="VOLUME_NAME";break;
case "SVM":col="SVM_NAME";break;
}%>
```

Important - the scripting is evaluated only once and before the CH calls !

---

## Templating
Expand Down
294 changes: 282 additions & 12 deletions dist/module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/module.js.map

Large diffs are not rendered by default.

Binary file modified dist/vertamedia-clickhouse-plugin_darwin_amd64
Binary file not shown.
Binary file modified dist/vertamedia-clickhouse-plugin_linux_amd64
Binary file not shown.
Binary file modified dist/vertamedia-clickhouse-plugin_windows_amd64.exe
Binary file not shown.
270 changes: 270 additions & 0 deletions docker/grafana/dashboards/sql_logic_dashboard.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": "-- Grafana --",
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"type": "dashboard"
}
]
},
"editable": true,
"gnetId": null,
"graphTooltip": 0,
"id": 10,
"iteration": 1595574658327,
"links": [],
"panels": [
{
"alert": {
"alertRuleTags": {},
"conditions": [
{
"evaluator": {
"params": [
2000
],
"type": "gt"
},
"operator": {
"type": "and"
},
"query": {
"params": [
"A",
"5m",
"now"
]
},
"reducer": {
"params": [],
"type": "avg"
},
"type": "query"
}
],
"executionErrorState": "alerting",
"for": "5m",
"frequency": "1m",
"handler": 1,
"message": "Alert 2K threshold",
"name": "sql logic histogram alert",
"noDataState": "alerting",
"notifications": []
},
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "clickhouse",
"description": "example how to Sql logic in clickhouse scripting",
"fieldConfig": {
"defaults": {
"custom": {}
},
"overrides": []
},
"fill": 1,
"fillGradient": 0,
"gridPos": {
"h": 8,
"w": 12,
"x": 0,
"y": 0
},
"hiddenSeries": false,
"id": 2,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"nullPointMode": "null",
"percentage": false,
"pluginVersion": "7.1.0",
"pointradius": 2,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"database": "default",
"dateColDataType": "",
"dateLoading": false,
"dateTimeColDataType": "d",
"dateTimeType": "DATETIME",
"datetimeLoading": false,
"extrapolate": true,
"format": "time_series",
"formattedQuery": "SELECT $timeSeries as t, count() FROM $table WHERE $timeFilter GROUP BY t ORDER BY t",
"hide": false,
"intervalFactor": 1,
"query": "<%var m=1;switch(\"$multiplier\"){case \"unchanged\":m=1;break;case \"doubled\":m=2;break;case \"tripled\":m=3;break;case \"alert!\":m=10;break;}%>\n<%if(\"$type\"==\"constant\"){%>\n SELECT\n $timeSeries as t,\n count()*100 * <%m%>\n FROM $table\n WHERE $timeFilter\n GROUP BY t\n ORDER BY t\n<%}else{%>\n SELECT\n $timeSeries as t,\n rand()/10000000 * <%m%> as x\n FROM $table\n WHERE $timeFilter\n GROUP BY t\n ORDER BY t\n<%}%>",
"rawQuery": " SELECT (intDiv(toUInt32(d), 15) * 15) * 1000 as t, count()*100 * 10 FROM default.test_interval WHERE d >= toDateTime(1595553123) GROUP BY t ORDER BY t ",
"refId": "A",
"round": "0s",
"table": "test_interval",
"tableLoading": false
}
],
"thresholds": [
{
"colorMode": "critical",
"fill": true,
"line": true,
"op": "gt",
"value": 2000
}
],
"timeFrom": null,
"timeRegions": [],
"timeShift": null,
"title": "sql logic histogram",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
}
],
"refresh": "5s",
"schemaVersion": 26,
"style": "dark",
"tags": [],
"templating": {
"list": [
{
"allValue": null,
"current": {
"selected": true,
"text": "tripled",
"value": "tripled"
},
"hide": 0,
"includeAll": false,
"label": "",
"multi": false,
"name": "multiplier",
"options": [
{
"selected": false,
"text": "unchanged",
"value": "unchanged"
},
{
"selected": false,
"text": "doubled",
"value": "doubled"
},
{
"selected": true,
"text": "tripled",
"value": "tripled"
},
{
"selected": false,
"text": "alert!",
"value": "alert!"
}
],
"query": "unchanged,doubled,tripled,alert!",
"queryValue": "",
"skipUrlSync": false,
"type": "custom"
},
{
"allValue": null,
"current": {
"selected": true,
"text": "random",
"value": "random"
},
"hide": 0,
"includeAll": false,
"label": null,
"multi": false,
"name": "type",
"options": [
{
"selected": false,
"text": "constant",
"value": "constant"
},
{
"selected": true,
"text": "random",
"value": "random"
}
],
"query": "constant,random",
"queryValue": "",
"skipUrlSync": false,
"type": "custom"
}
]
},
"time": {
"from": "now-6h",
"to": "now"
},
"timepicker": {
"refresh_intervals": [
"10s",
"30s",
"1m",
"5m",
"15m",
"30m",
"1h",
"2h",
"1d"
]
},
"timezone": "",
"title": "sql logic",
"uid": "fhm5om4Gz",
"version": 4
}
Loading

0 comments on commit 0c6efab

Please sign in to comment.