add timescaledb option to postgres datasource

This adds an option to the postgres datasource config for
timescaledb support. When set to auto it will check for
timescaledb when testing the datasource.

When this option is enabled the $__timeGroup macro will
use the time_bucket function from timescaledb to group
times by an interval.

This also passes the datasource edit control to testDatasource
to allow for setting additional settings, this might be useful
for other datasources aswell which have optional or version
dependant features which can be queried.
This commit is contained in:
Sven Klemm
2018-07-21 20:00:26 +02:00
parent d48f1f57f0
commit b987aee7cf
6 changed files with 85 additions and 24 deletions

View File

@ -123,27 +123,27 @@ export class PostgresDatasource {
.then(data => this.responseParser.parseMetricFindQueryResult(refId, data));
}
testDatasource() {
return this.backendSrv
.datasourceRequest({
url: '/api/tsdb/query',
method: 'POST',
data: {
from: '5m',
to: 'now',
queries: [
{
refId: 'A',
intervalMs: 1,
maxDataPoints: 1,
datasourceId: this.id,
rawSql: 'SELECT 1',
format: 'table',
},
],
},
})
testDatasource(control) {
return this.metricFindQuery('SELECT 1', {})
.then(res => {
if (control.current.jsonData.timescaledb === 'auto') {
return this.metricFindQuery("SELECT 1 FROM pg_extension WHERE extname='timescaledb'", {})
.then(res => {
if (res.length === 1) {
control.current.jsonData.timescaledb = 'enabled';
return this.backendSrv.put('/api/datasources/' + this.id, control.current).then(settings => {
control.current = settings.datasource;
control.updateFrontendSettings();
return { status: 'success', message: 'Database Connection OK, TimescaleDB found' };
});
}
throw new Error('timescaledb not found');
})
.catch(err => {
// query errored out or empty so timescaledb is not available
return { status: 'success', message: 'Database Connection OK' };
});
}
return { status: 'success', message: 'Database Connection OK' };
})
.catch(err => {

View File

@ -38,6 +38,20 @@
</div>
</div>
<h3 class="page-heading">PostgreSQL details</h3>
<div class="gf-form-group">
<div class="gf-form">
<label class="gf-form-label width-7">TimescaleDB</label>
<div class="gf-form-select-wrapper max-width-8 gf-form-select-wrapper--has-help-icon">
<select class="gf-form-input" ng-model="ctrl.current.jsonData.timescaledb" ng-options="mode for mode in ['auto', 'enabled', 'disabled']" ng-init="ctrl.current.jsonData.timescaledb=ctrl.current.jsonData.timescaledb || 'auto'"></select>
<info-popover mode="right-absolute">
This option determines whether TimescaleDB features will be used.
</info-popover>
</div>
</div>
</div>
<div class="gf-form-group">
<div class="grafana-info-box">
<h5>User Permission</h5>