Skip to content

Commit

Permalink
configurable default database, improved ad-hoc filters, parse key col…
Browse files Browse the repository at this point in the history
…umns from query (#60)

* configurable default database, less restrictive ad-hoc filters
* parse dimensions from GROUP BY expression
  • Loading branch information
vavrusa authored and hagen1778 committed May 30, 2018
1 parent ac53fb5 commit 9c0774e
Show file tree
Hide file tree
Showing 29 changed files with 308 additions and 104 deletions.
63 changes: 29 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,43 +220,32 @@ Top5:
```
SELECT
1, /* fake timestamp value */
groupArray((UserName, Reqs))
FROM
(
SELECT
UserName,
sum(Reqs) AS Reqs
FROM requests
GROUP BY UserName
ORDER BY Reqs desc
LIMIT 5
)
UserName,
sum(Reqs) AS Reqs
FROM requests
GROUP BY UserName
ORDER BY Reqs desc
LIMIT 5
```

Other:
```
SELECT
1, /* fake timestamp value */
tuple(tuple('Other', sum(Reqs)))
FROM
(
SELECT
UserName,
sum(Reqs) AS Reqs
FROM requests
GROUP BY UserName
ORDER BY Reqs desc
LIMIT 5,10000000000000 /* select some ridiculous number after first 5 */
)
UserName,
sum(Reqs) AS Reqs
FROM requests
GROUP BY UserName
ORDER BY Reqs
LIMIT 5,10000000000000 /* select some ridiculous number after first 5 */
```

#### Table (https://grafana.com/plugins/table)

There are no any tricks in displaying time-series data. But to display some summary we will need to fake timestamp data:
There are no any tricks in displaying time-series data. To print summary data, omit time column, and format the result as "Table".

```
SELECT
rand() Time, /* fake timestamp value */
UserName,
sum(Reqs) as Reqs
FROM requests
Expand All @@ -266,9 +255,6 @@ ORDER BY
Reqs
```

Better to hide `Time` column at `Options` tab while editing panel


#### Vertical histogram (https://grafana.com/plugins/graph)

![vertical histogram](https://cloud.githubusercontent.com/assets/2902918/25392561/9f3777e0-29e1-11e7-8b23-2ea9ae46a029.png)
Expand Down Expand Up @@ -296,10 +282,6 @@ If you have a table with country/city codes:
```
SELECT
1,
groupArray((c, Reqs)) AS groupArr
FROM
(
SELECT
CountryCode AS c,
sum(requests) AS Reqs
FROM requests
Expand All @@ -312,7 +294,6 @@ WHERE $timeFilter
GROUP BY
c
ORDER BY Reqs DESC
)
```

If you are using [geohash](https://github.com/grafana/worldmap-panel#geohashes-as-the-data-source) set following options:
Expand All @@ -327,11 +308,12 @@ And make following query with `Table` formatting:
### Ad-hoc filters

If there is an Ad-hoc variable, plugin will fetch all columns of all tables of all databases (except system database) as tags.
So in dropdown menu will be options like `database.table.column`. If there are ENUM columns,
So in dropdown menu will be options like `database.table.column`. If the default database is specified, it will only fetch tables and columns from that database, and the dropdown menu will have option like `table.column`. If there are ENUM columns,
plugin will fetch their options and use them as tag values.

Plugin will apply Ad-hoc filters to all queries on the dashboard if their settings `$database` and `$table` are the same
as Ad-hoc's `database.table`
as Ad-hoc's `database.table`. If the ad-hoc filter doesn't specify table, it will apply to all queries regardless of the table.
This is useful if the dashboard contains queries to multiple different tables.

![ad-hoc](https://user-images.githubusercontent.com/2902918/37139531-ed67f222-22b6-11e8-8815-9268850f16fb.png)

Expand All @@ -353,6 +335,19 @@ That's why plugin checks prev datapoints and tries to predict last datapoint val
Alerts feature requires changes in `Grafana`'s backend, which can't be extended for now. `Grafana`'s maintainers are working on this feature.

### Build

The build works with either NPM or Yarn:

```
yarn run build
```

Tests can be run with Karma:

```
yarn run test
```

### Contribute

Expand Down
63 changes: 29 additions & 34 deletions dist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,43 +220,32 @@ Top5:
```
SELECT
1, /* fake timestamp value */
groupArray((UserName, Reqs))
FROM
(
SELECT
UserName,
sum(Reqs) AS Reqs
FROM requests
GROUP BY UserName
ORDER BY Reqs desc
LIMIT 5
)
UserName,
sum(Reqs) AS Reqs
FROM requests
GROUP BY UserName
ORDER BY Reqs desc
LIMIT 5
```

Other:
```
SELECT
1, /* fake timestamp value */
tuple(tuple('Other', sum(Reqs)))
FROM
(
SELECT
UserName,
sum(Reqs) AS Reqs
FROM requests
GROUP BY UserName
ORDER BY Reqs desc
LIMIT 5,10000000000000 /* select some ridiculous number after first 5 */
)
UserName,
sum(Reqs) AS Reqs
FROM requests
GROUP BY UserName
ORDER BY Reqs
LIMIT 5,10000000000000 /* select some ridiculous number after first 5 */
```

#### Table (https://grafana.com/plugins/table)

There are no any tricks in displaying time-series data. But to display some summary we will need to fake timestamp data:
There are no any tricks in displaying time-series data. To print summary data, omit time column, and format the result as "Table".

```
SELECT
rand() Time, /* fake timestamp value */
UserName,
sum(Reqs) as Reqs
FROM requests
Expand All @@ -266,9 +255,6 @@ ORDER BY
Reqs
```

Better to hide `Time` column at `Options` tab while editing panel


#### Vertical histogram (https://grafana.com/plugins/graph)

![vertical histogram](https://cloud.githubusercontent.com/assets/2902918/25392561/9f3777e0-29e1-11e7-8b23-2ea9ae46a029.png)
Expand Down Expand Up @@ -296,10 +282,6 @@ If you have a table with country/city codes:
```
SELECT
1,
groupArray((c, Reqs)) AS groupArr
FROM
(
SELECT
CountryCode AS c,
sum(requests) AS Reqs
FROM requests
Expand All @@ -312,7 +294,6 @@ WHERE $timeFilter
GROUP BY
c
ORDER BY Reqs DESC
)
```

If you are using [geohash](https://github.com/grafana/worldmap-panel#geohashes-as-the-data-source) set following options:
Expand All @@ -327,11 +308,12 @@ And make following query with `Table` formatting:
### Ad-hoc filters

If there is an Ad-hoc variable, plugin will fetch all columns of all tables of all databases (except system database) as tags.
So in dropdown menu will be options like `database.table.column`. If there are ENUM columns,
So in dropdown menu will be options like `database.table.column`. If the default database is specified, it will only fetch tables and columns from that database, and the dropdown menu will have option like `table.column`. If there are ENUM columns,
plugin will fetch their options and use them as tag values.

Plugin will apply Ad-hoc filters to all queries on the dashboard if their settings `$database` and `$table` are the same
as Ad-hoc's `database.table`
as Ad-hoc's `database.table`. If the ad-hoc filter doesn't specify table, it will apply to all queries regardless of the table.
This is useful if the dashboard contains queries to multiple different tables.

![ad-hoc](https://user-images.githubusercontent.com/2902918/37139531-ed67f222-22b6-11e8-8815-9268850f16fb.png)

Expand All @@ -353,6 +335,19 @@ That's why plugin checks prev datapoints and tries to predict last datapoint val
Alerts feature requires changes in `Grafana`'s backend, which can't be extended for now. `Grafana`'s maintainers are working on this feature.

### Build

The build works with either NPM or Yarn:

```
yarn run build
```

Tests can be run with Karma:

```
yarn run test
```

### Contribute

Expand Down
24 changes: 20 additions & 4 deletions dist/adhoc.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 19 additions & 3 deletions dist/adhoc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const columnsQuery = "SELECT database, table, name, type FROM system.columns where database != 'system' ORDER BY database, table";
const queryFilter = "database != 'system'"
const columnsQuery = "SELECT database, table, name, type FROM system.columns WHERE {filter} ORDER BY database, table";
const regexEnum = /'(?:[^']+|'')+'/gmi;

export default class AdhocCtrl {
Expand All @@ -16,10 +17,19 @@ export default class AdhocCtrl {
if (this.tagKeys.length > 0) {
return Promise.resolve(this.tagKeys);
}
return datasource.metricFindQuery(columnsQuery)
let filter = queryFilter;
if (datasource.defaultDatabase.length > 0) {
filter = "database = '" + datasource.defaultDatabase + "' AND " + queryFilter;
}
let query = columnsQuery.replace('{filter}', queryFilter);
return datasource.metricFindQuery(query)
.then(function(response){
let columnNames = {};
response.forEach(function(item){
let text = item.database + '.' + item.table + '.' + item.name;
let text = item.table + '.' + item.name;
if (datasource.defaultDatabase.length == 0) {
text = item.database + '.' + text;
}
let value = item.name;
self.tagKeys.push({text: text, value: value});
if (item.type.slice(0, 4) === 'Enum') {
Expand All @@ -29,8 +39,14 @@ export default class AdhocCtrl {
options.forEach(function(o) {
self.tagValues[text].push({text: o, value: o})
})
self.tagValues[item.name] = self.tagValues[text];
}
}
columnNames[item.name] = true;
});
/* Store unique column names with wildcard table */
Object.keys(columnNames).forEach(columnName => {
self.tagKeys.push({text: columnName, value: columnName});
});
return Promise.resolve(self.tagKeys);
});
Expand Down
2 changes: 1 addition & 1 deletion dist/clickhouse-info.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/datasource.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export declare class ClickHouseDatasource {
basicAuth: any;
withCredentials: any;
usePOST: boolean;
defaultDatabase: string;
addCorsHeader: boolean;
responseParser: any;
adhocCtrl: AdhocCtrl;
Expand Down
Loading

0 comments on commit 9c0774e

Please sign in to comment.