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

Commit

Permalink
more db to bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantxu committed Dec 19, 2018
1 parent 1ca0267 commit 075c831
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 25 deletions.
19 changes: 12 additions & 7 deletions specs/datasource.jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import Datasource from '../src/datasource';

describe('InfluxDB (Flux)', () => {
const templateSrv = new TemplateSrv();
const ds = new Datasource({ url: '' }, {}, templateSrv);
const ds = new Datasource({url: ''}, {}, templateSrv);
const DEFAULT_OPTIONS = {
rangeRaw: { to: 'now', from: 'now - 3h' },
rangeRaw: {to: 'now', from: 'now - 3h'},
scopedVars: {},
targets: [],
};
Expand All @@ -18,23 +18,28 @@ describe('InfluxDB (Flux)', () => {
let target: any;

it.skip('replaces $range variable', () => {
target = ds.prepareQueryTarget({ query: 'from(db: "test") |> range($range)' }, DEFAULT_OPTIONS);
expect(target.query).toBe('from(db: "test") |> range(start: -3h)');
target = ds.prepareQueryTarget(
{query: 'from(bucket: "test") |> range($range)'},
DEFAULT_OPTIONS
);
expect(target.query).toBe('from(bucket: "test") |> range(start: -3h)');
});

it.skip('replaces $range variable with custom dates', () => {
const to = moment();
const from = moment().subtract(1, 'hours');
target = ds.prepareQueryTarget(
{ query: 'from(db: "test") |> range($range)' },
{query: 'from(bucket: "test") |> range($range)'},
{
...DEFAULT_OPTIONS,
rangeRaw: { to, from },
rangeRaw: {to, from},
}
);
const start = from.toISOString();
const stop = to.toISOString();
expect(target.query).toBe(`from(db: "test") |> range(start: ${start}, stop: ${stop})`);
expect(target.query).toBe(
`from(bucket: "test") |> range(start: ${start}, stop: ${stop})`
);
});
});
});
16 changes: 9 additions & 7 deletions specs/metric_find_query.jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import expandMacros from '../src/metric_find_query';
describe('metric find query', () => {
describe('expandMacros()', () => {
it('returns a non-macro query unadulterated', () => {
const query = 'from(db:"telegraf") |> last()';
const query = 'from(bucket:"telegraf") |> last()';
const result = expandMacros(query);
expect(result).toBe(query);
});
Expand All @@ -12,31 +12,33 @@ describe('metric find query', () => {
const query = ' measurements(mydb) ';
const result = expandMacros(query).replace(/\s/g, '');
expect(result).toBe(
'from(db:"mydb")|>range($range)|>group(by:["_measurement"])|>distinct(column:"_measurement")|>group(none:true)'
'from(bucket:"mydb")|>range($range)|>group(by:["_measurement"])|>distinct(column:"_measurement")|>group(none:true)'
);
});

it('returns a tags query for tags()', () => {
const query = ' tags(mydb , mymetric) ';
const result = expandMacros(query).replace(/\s/g, '');
expect(result).toBe('from(db:"mydb")|>range($range)|>filter(fn:(r)=>r._measurement=="mymetric")|>keys()');
expect(result).toBe(
'from(bucket:"mydb")|>range($range)|>filter(fn:(r)=>r._measurement=="mymetric")|>keys()'
);
});

it('returns a tag values query for tag_values()', () => {
const query = ' tag_values(mydb , mymetric, mytag) ';
const result = expandMacros(query).replace(/\s/g, '');
expect(result).toBe(
'from(db:"mydb")|>range($range)|>filter(fn:(r)=>r._measurement=="mymetric")' +
'|>group(by:["mytag"])|>distinct(column:"mytag")|>group(none:true)'
'from(bucket:"mydb")|>range($range)|>filter(fn:(r)=>r._measurement=="mymetric")' +
'|>group(by:["mytag"])|>distinct(column:"mytag")|>group(none:true)'
);
});

it('returns a field keys query for field_keys()', () => {
const query = ' field_keys(mydb , mymetric) ';
const result = expandMacros(query).replace(/\s/g, '');
expect(result).toBe(
'from(db:"mydb")|>range($range)|>filter(fn:(r)=>r._measurement=="mymetric")' +
'|>group(by:["_field"])|>distinct(column:"_field")|>group(none:true)'
'from(bucket:"mydb")|>range($range)|>filter(fn:(r)=>r._measurement=="mymetric")' +
'|>group(by:["_field"])|>distinct(column:"_field")|>group(none:true)'
);
});
});
Expand Down
8 changes: 4 additions & 4 deletions src/editor/FluxQueryField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ const wrapText = text => ({ text });
const RATE_RANGES = ['1m', '5m', '10m', '30m', '1h'];
const DEFAULT_DATABASE = 'telegraf';

function expandQuery(database, measurement, field) {
function expandQuery(bucket, measurement, field) {
if (field) {
return (
`from(db: "${database}")\n` +
`from(bucket: "${bucket}")\n` +
` |> filter(fn: (r) => r["_measurement"] == "${measurement}" AND r["_field"] == "${field}")\n |> range($range)\n |> limit(n: 1000)`
);
}
return `from(db: "${database}")\n |> filter(fn: (r) => r["_measurement"] == "${measurement}")\n |> range($range)\n |> limit(n: 1000)`;
return `from(bucket: "${bucket}")\n |> filter(fn: (r) => r["_measurement"] == "${measurement}")\n |> range($range)\n |> limit(n: 1000)`;
}

export default class FluxQueryField extends QueryField {
Expand Down Expand Up @@ -154,7 +154,7 @@ export default class FluxQueryField extends QueryField {
suggestionGroups.push({
prefixMatch: true,
label: 'Templates',
items: [`from(db: "${database}") |> range($range) `].map(wrapText),
items: [`from(bucket: "${database}") |> range($range) `].map(wrapText),
});
suggestionGroups.push({
prefixMatch: true,
Expand Down
4 changes: 2 additions & 2 deletions src/editor/flux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export const FUNCTIONS = [
},
{
text: 'from',
display: 'from(db: "database)',
hint: 'Starting point of a query, produces a table from the given "db".',
display: 'from(bucket: "database/policy")',
hint: 'Starting point of a query, produces a table from the given bucket.',
},
{
text: 'group',
Expand Down
8 changes: 4 additions & 4 deletions src/metric_find_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function expandMacros(query) {
const measurementsQuery = query.match(MEASUREMENTS_REGEXP);
if (measurementsQuery) {
const database = measurementsQuery[1];
return `from(db:"${database}")
return `from(bucket:"${database}")
|> range($range)
|> group(by:["_measurement"])
|> distinct(column:"_measurement")
Expand All @@ -27,7 +27,7 @@ export default function expandMacros(query) {
if (tagsQuery) {
const database = tagsQuery[1];
const measurement = tagsQuery[2];
return `from(db:"${database}")
return `from(bucket:"${database}")
|> range($range)
|> filter(fn:(r) => r._measurement == "${measurement}")
|> keys()`;
Expand All @@ -38,7 +38,7 @@ export default function expandMacros(query) {
const database = tagValuesQuery[1];
const measurement = tagValuesQuery[2];
const tag = tagValuesQuery[3];
return `from(db:"${database}")
return `from(bucket:"${database}")
|> range($range)
|> filter(fn:(r) => r._measurement == "${measurement}")
|> group(by:["${tag}"])
Expand All @@ -50,7 +50,7 @@ export default function expandMacros(query) {
if (fieldKeysQuery) {
const database = fieldKeysQuery[1];
const measurement = fieldKeysQuery[2];
return `from(db:"${database}")
return `from(bucket:"${database}")
|> range($range)
|> filter(fn:(r) => r._measurement == "${measurement}")
|> group(by:["_field"])
Expand Down
2 changes: 1 addition & 1 deletion src/partials/annotations.editor.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="gf-form-group">
<div class="gf-form">
<input type="text" class="gf-form-input" ng-model='ctrl.annotation.query' placeholder='from(db:"telegraf") |> range($range)'></input>
<input type="text" class="gf-form-input" ng-model='ctrl.annotation.query' placeholder='from(bucket:"telegraf/autogen") |> range($range)'></input>
</div>
</div>

Expand Down

0 comments on commit 075c831

Please sign in to comment.