Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
b09a7c5d
Unverified
Commit
b09a7c5d
authored
Jul 23, 2018
by
David
Committed by
GitHub
Jul 23, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #12597 from dehrax/7664-query-var
Interval and range in query template variable
parents
0fa98a81
6b6a23ff
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
3 deletions
+60
-3
public/app/plugins/datasource/prometheus/datasource.ts
+18
-2
public/app/plugins/datasource/prometheus/specs/datasource.jest.ts
+42
-1
No files found.
public/app/plugins/datasource/prometheus/datasource.ts
View file @
b09a7c5d
...
...
@@ -196,13 +196,14 @@ export class PrometheusDatasource {
var intervalFactor = target.intervalFactor || 1;
// Adjust the interval to take into account any specified minimum and interval factor plus Prometheus limits
var adjustedInterval = this.adjustInterval(interval, minInterval, range, intervalFactor);
var scopedVars =
options.scopedVars
;
var scopedVars =
{ ...options.scopedVars, ...this.getRangeScopedVars() }
;
// If the interval was adjusted, make a shallow copy of scopedVars with updated interval vars
if (interval !== adjustedInterval) {
interval = adjustedInterval;
scopedVars = Object.assign({}, options.scopedVars, {
__interval: { text: interval + '
s
', value: interval + '
s
' },
__interval_ms: { text: interval * 1000, value: interval * 1000 },
...this.getRangeScopedVars(),
});
}
query.step = interval;
...
...
@@ -285,11 +286,26 @@ export class PrometheusDatasource {
return this.$q.when([]);
}
let interpolated = this.templateSrv.replace(query, {}, this.interpolateQueryExpr);
let scopedVars = {
__interval: { text: this.interval, value: this.interval },
__interval_ms: { text: kbn.interval_to_ms(this.interval), value: kbn.interval_to_ms(this.interval) },
...this.getRangeScopedVars(),
};
let interpolated = this.templateSrv.replace(query, scopedVars, this.interpolateQueryExpr);
var metricFindQuery = new PrometheusMetricFindQuery(this, interpolated, this.timeSrv);
return metricFindQuery.process();
}
getRangeScopedVars() {
let range = this.timeSrv.timeRange();
let msRange = range.to.diff(range.from);
let regularRange = kbn.secondsToHms(msRange / 1000);
return {
__range_ms: { text: msRange, value: msRange },
__range: { text: regularRange, value: regularRange },
};
}
annotationQuery(options) {
var annotation = options.annotation;
var expr = annotation.expr || '';
...
...
public/app/plugins/datasource/prometheus/specs/datasource.jest.ts
View file @
b09a7c5d
...
...
@@ -2,6 +2,7 @@ import _ from 'lodash';
import
moment
from
'moment'
;
import
q
from
'q'
;
import
{
alignRange
,
PrometheusDatasource
,
prometheusSpecialRegexEscape
,
prometheusRegularEscape
}
from
'../datasource'
;
jest
.
mock
(
'../metric_find_query'
);
describe
(
'PrometheusDatasource'
,
()
=>
{
let
ctx
:
any
=
{};
...
...
@@ -18,7 +19,14 @@ describe('PrometheusDatasource', () => {
ctx
.
templateSrvMock
=
{
replace
:
a
=>
a
,
};
ctx
.
timeSrvMock
=
{};
ctx
.
timeSrvMock
=
{
timeRange
:
()
=>
{
return
{
from
:
moment
(
1531468681
),
to
:
moment
(
1531489712
),
};
},
};
beforeEach
(()
=>
{
ctx
.
ds
=
new
PrometheusDatasource
(
instanceSettings
,
q
,
ctx
.
backendSrvMock
,
ctx
.
templateSrvMock
,
ctx
.
timeSrvMock
);
...
...
@@ -204,4 +212,37 @@ describe('PrometheusDatasource', () => {
expect(prometheusSpecialRegexEscape('
+
looking$glass
?
')).toEqual('
\\\\
+
looking
\\\\
$glass
\\\\
?
');
});
});
describe('
metricFindQuery
', () => {
beforeEach(() => {
let query = '
query_result
(
topk
(
5
,
rate
(
http_request_duration_microseconds_count
[
$__interval
])))
';
ctx.templateSrvMock.replace = jest.fn();
ctx.timeSrvMock.timeRange = () => {
return {
from: moment(1531468681),
to: moment(1531489712),
};
};
ctx.ds = new PrometheusDatasource(instanceSettings, q, ctx.backendSrvMock, ctx.templateSrvMock, ctx.timeSrvMock);
ctx.ds.metricFindQuery(query);
});
it('
should
call
templateSrv
.
replace
with
scopedVars
', () => {
expect(ctx.templateSrvMock.replace.mock.calls[0][1]).toBeDefined();
});
it('
should
have
the
correct
range
and
range_ms
', () => {
let range = ctx.templateSrvMock.replace.mock.calls[0][1].__range;
let rangeMs = ctx.templateSrvMock.replace.mock.calls[0][1].__range_ms;
expect(range).toEqual({ text: '
21
s
', value: '
21
s
' });
expect(rangeMs).toEqual({ text: 21031, value: 21031 });
});
it('
should
pass
the
default
interval
value
', () => {
let interval = ctx.templateSrvMock.replace.mock.calls[0][1].__interval;
let intervalMs = ctx.templateSrvMock.replace.mock.calls[0][1].__interval_ms;
expect(interval).toEqual({ text: '
15
s
', value: '
15
s
' });
expect(intervalMs).toEqual({ text: 15000, value: 15000 });
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment