Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Commit

Permalink
use bucket rather than database
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantxu committed Dec 18, 2018
1 parent 2924b8c commit 9fd5505
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Read more about InfluxDB here:

Getting started:

1. Install a recent [InfluxDB nightly](https://portal.influxdata.com/downloads), then run `influxd -config` and catch that config as config.toml. Then run `influxd -config config.toml`. The recent nighlies contain the Flux engine.
1. Install [InfluxDB 1.7+](https://portal.influxdata.com/downloads), then edit `influxdb.conf` setting [`[http] flux-enabled = true`](https://docs.influxdata.com/influxdb/v1.7/administration/config#flux-enabled-false) See also: [https://docs.influxdata.com/flux/v0.7/introduction/installation/](https://docs.influxdata.com/flux/v0.7/introduction/installation/)

2. Install telegraph to get some data: brew install telegraf. Then run telegraf.

Expand Down
26 changes: 16 additions & 10 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export default class InfluxDatasource {
username: string;
password: string;
name: string;
orgName: string;
database: any;
bucket: any;
basicAuth: any;
withCredentials: any;
interval: any;
Expand All @@ -34,11 +33,10 @@ export default class InfluxDatasource {
this.username = instanceSettings.username;
this.password = instanceSettings.password;
this.name = instanceSettings.name;
this.orgName = instanceSettings.orgName || 'defaultorgname';
this.basicAuth = instanceSettings.basicAuth;
this.withCredentials = instanceSettings.withCredentials;
this.interval = (instanceSettings.jsonData || {}).timeInterval;
this.database = (instanceSettings.jsonData || {}).database;
this.bucket = (instanceSettings.jsonData || {}).bucket;
this.supportAnnotations = true;
this.supportMetrics = true;
}
Expand Down Expand Up @@ -137,7 +135,15 @@ export default class InfluxDatasource {
}

testDatasource() {
const query = `from(bucket:"${this.database}") |> last()`;
const query = `from(bucket:"${this.bucket}")
|> range(start:-10y)
|> last()`;
if (this.bucket.indexOf('/') < 0) {
return Promise.resolve({
status: 'error',
message: 'The bucket is missing a retention policy',
});
}

return this._influxRequest('POST', '/api/v2/query', {query: query})
.then(res => {
Expand All @@ -150,7 +156,7 @@ export default class InfluxDatasource {
return {
status: 'error',
message:
'Data source connected, but has no data. Verify the "Database" field and make sure the database has data.',
'Data source connected, but has no data. Verify the "bucket" field and make sure the bucket has data.',
};
})
.catch(err => {
Expand All @@ -159,9 +165,7 @@ export default class InfluxDatasource {
}

_influxRequest(method: string, url: string, data: any, options?: any) {
let params: any = {
organization: `my-org`,
};
let params: any = {};

if (this.username) {
params.u = this.username;
Expand All @@ -177,7 +181,9 @@ export default class InfluxDatasource {
inspect: {type: this.type},
};

req.headers = {};
req.headers = {
Accept: 'application/csv',
};

if (this.basicAuth || this.withCredentials) {
req.withCredentials = true;
Expand Down
9 changes: 6 additions & 3 deletions src/partials/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ <h3 class="page-heading">InfluxDB Details</h3>
<div class="gf-form-group">
<div class="gf-form-inline">
<div class="gf-form max-width-30">
<span class="gf-form-label width-7">Default Database</span>
<input type="text" class="gf-form-input" ng-model='ctrl.current.jsonData.database' placeholder="" required></input>
<span class="gf-form-label width-10">Default Bucket</span>
<input type="text" class="gf-form-input" ng-model='ctrl.current.jsonData.bucket' placeholder="database/policy" required></input>
<info-popover mode="right-absolute">
<p>A combination of the default database and retention policy</p>
</info-popover>
</div>
</div>

<div class="gf-form-inline">
<div class="gf-form max-width-15">
<span class="gf-form-label width-7">User</span>
<span class="gf-form-label width-10">Username</span>
<input type="text" class="gf-form-input" ng-model='ctrl.current.user' placeholder=""></input>
</div>
<div class="gf-form max-width-15">
Expand Down
20 changes: 12 additions & 8 deletions src/query_ctrl.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import appEvents from 'grafana/app/core/app_events';
import { QueryCtrl } from 'grafana/app/plugins/sdk';
import {QueryCtrl} from 'grafana/app/plugins/sdk';

import './editor/editor_component';

function makeDefaultQuery(database) {
return `from(db: "${database}")
function makeDefaultQuery(bucket) {
return `from(bucket: "${bucket}")
|> range($range)
|> limit(n:1000)
`;
Expand All @@ -13,7 +13,7 @@ export class InfluxFluxQueryCtrl extends QueryCtrl {
static templateUrl = 'partials/query.editor.html';

dataPreview: string;
defaultDatabase: string;
defaultBucket: string;
resultRecordCount: string;
resultTableCount: string;
resultFormats: any[];
Expand All @@ -26,11 +26,14 @@ export class InfluxFluxQueryCtrl extends QueryCtrl {
this.resultTableCount = '';

if (this.target.query === undefined) {
this.target.query = makeDefaultQuery(this.datasource.database);
this.target.query = makeDefaultQuery(this.datasource.bucket);
}

this.defaultDatabase = this.datasource.database;
this.resultFormats = [{ text: 'Time series', value: 'time_series' }, { text: 'Table', value: 'table' }];
this.defaultBucket = this.datasource.bucket;
this.resultFormats = [
{text: 'Time series', value: 'time_series'},
{text: 'Table', value: 'table'},
];

appEvents.on('ds-request-response', this.onResponseReceived, $scope);
this.panelCtrl.events.on('refresh', this.onRefresh, $scope);
Expand All @@ -39,7 +42,8 @@ export class InfluxFluxQueryCtrl extends QueryCtrl {

onDataReceived = dataList => {
this.resultRecordCount = dataList.reduce((count, model) => {
const records = model.type === 'table' ? model.rows.length : model.datapoints.length;
const records =
model.type === 'table' ? model.rows.length : model.datapoints.length;
return count + records;
}, 0);
this.resultTableCount = dataList.length;
Expand Down

0 comments on commit 9fd5505

Please sign in to comment.