Commit 6f811095 by Mitsuhiro Tanda

(prometheus) support instant query for table format, use checkbox to switch query type

parent 56cb16ff
...@@ -100,7 +100,7 @@ export class PrometheusDatasource { ...@@ -100,7 +100,7 @@ export class PrometheusDatasource {
var query: any = {}; var query: any = {};
query.expr = this.templateSrv.replace(target.expr, options.scopedVars, self.interpolateQueryExpr); query.expr = this.templateSrv.replace(target.expr, options.scopedVars, self.interpolateQueryExpr);
query.requestId = options.panelId + target.refId; query.requestId = options.panelId + target.refId;
query.format = target.format; query.instant = target.instant;
var interval = this.templateSrv.replace(target.interval, options.scopedVars) || options.interval; var interval = this.templateSrv.replace(target.interval, options.scopedVars) || options.interval;
var intervalFactor = target.intervalFactor || 1; var intervalFactor = target.intervalFactor || 1;
...@@ -116,7 +116,7 @@ export class PrometheusDatasource { ...@@ -116,7 +116,7 @@ export class PrometheusDatasource {
} }
var allQueryPromise = _.map(queries, query => { var allQueryPromise = _.map(queries, query => {
if (query.format === 'time_series') { if (!query.instant) {
return this.performTimeSeriesQuery(query, start, end); return this.performTimeSeriesQuery(query, start, end);
} else { } else {
return this.performInstantQuery(query, end); return this.performInstantQuery(query, end);
...@@ -333,6 +333,9 @@ export class PrometheusDatasource { ...@@ -333,6 +333,9 @@ export class PrometheusDatasource {
// Populate rows, set value to empty string when label not present. // Populate rows, set value to empty string when label not present.
_.each(md, function(series) { _.each(md, function(series) {
if (series.value) {
series.values = [series.value];
}
if (series.values) { if (series.values) {
for (i = 0; i < series.values.length; i++) { for (i = 0; i < series.values.length; i++) {
var values = series.values[i]; var values = series.values[i];
......
...@@ -45,6 +45,8 @@ ...@@ -45,6 +45,8 @@
<div class="gf-form-select-wrapper width-8"> <div class="gf-form-select-wrapper width-8">
<select class="gf-form-input gf-size-auto" ng-model="ctrl.target.format" ng-options="f.value as f.text for f in ctrl.formats" ng-change="ctrl.refresh()"></select> <select class="gf-form-input gf-size-auto" ng-model="ctrl.target.format" ng-options="f.value as f.text for f in ctrl.formats" ng-change="ctrl.refresh()"></select>
</div> </div>
<gf-form-switch class="gf-form" label="Instant" label-class="width-5" checked="ctrl.target.instant" on-change="ctrl.refresh()">
</gf-form-switch>
<label class="gf-form-label"> <label class="gf-form-label">
<a href="{{ctrl.linkToPrometheus}}" target="_blank" bs-tooltip="'Link to Graph in Prometheus'"> <a href="{{ctrl.linkToPrometheus}}" target="_blank" bs-tooltip="'Link to Graph in Prometheus'">
<i class="fa fa-share-square-o"></i> <i class="fa fa-share-square-o"></i>
......
...@@ -14,6 +14,7 @@ class PrometheusQueryCtrl extends QueryCtrl { ...@@ -14,6 +14,7 @@ class PrometheusQueryCtrl extends QueryCtrl {
metric: any; metric: any;
resolutions: any; resolutions: any;
formats: any; formats: any;
instant: any;
oldTarget: any; oldTarget: any;
suggestMetrics: any; suggestMetrics: any;
getMetricsAutocomplete: any; getMetricsAutocomplete: any;
...@@ -36,9 +37,10 @@ class PrometheusQueryCtrl extends QueryCtrl { ...@@ -36,9 +37,10 @@ class PrometheusQueryCtrl extends QueryCtrl {
this.formats = [ this.formats = [
{text: 'Time series', value: 'time_series'}, {text: 'Time series', value: 'time_series'},
{text: 'Table', value: 'table'}, {text: 'Table', value: 'table'},
{text: 'Instant', value: 'instant'},
]; ];
this.instant = false;
this.updateLink(); this.updateLink();
} }
......
...@@ -119,14 +119,14 @@ describe('PrometheusDatasource', function() { ...@@ -119,14 +119,14 @@ describe('PrometheusDatasource', function() {
expect(results.data[1].datapoints[3][0]).to.be(null); expect(results.data[1].datapoints[3][0]).to.be(null);
}); });
}); });
describe('When querying prometheus with one target and format = instant', function () { describe('When querying prometheus with one target and instant = true', function () {
var results; var results;
var urlExpected = 'proxied/api/v1/query?query=' + var urlExpected = 'proxied/api/v1/query?query=' +
encodeURIComponent('test{job="testjob"}') + encodeURIComponent('test{job="testjob"}') +
'&time=1443460275'; '&time=1443460275';
var query = { var query = {
range: { from: moment(1443438674760), to: moment(1443460274760) }, range: { from: moment(1443438674760), to: moment(1443460274760) },
targets: [{ expr: 'test{job="testjob"}', format: 'instant' }], targets: [{ expr: 'test{job="testjob"}', format: 'time_series', instant: true }],
interval: '60s' interval: '60s'
}; };
var response = { var response = {
...@@ -228,4 +228,45 @@ describe('PrometheusDatasource', function() { ...@@ -228,4 +228,45 @@ describe('PrometheusDatasource', function() {
); );
}); });
}); });
describe('When resultFormat is table and instant = true', function() {
var results;
var urlExpected = 'proxied/api/v1/query?query=' +
encodeURIComponent('test{job="testjob"}') +
'&time=1443460275';
var query = {
range: { from: moment(1443438674760), to: moment(1443460274760) },
targets: [{ expr: 'test{job="testjob"}', format: 'time_series', instant: true }],
interval: '60s'
};
var response = {
status: "success",
data: {
resultType: "vector",
result: [{
metric: { "__name__": "test", job: "testjob" },
value: [1443454528, "3846"]
}]
}
};
beforeEach(function () {
ctx.$httpBackend.expect('GET', urlExpected).respond(response);
ctx.ds.query(query).then(function (data) { results = data; });
ctx.$httpBackend.flush();
});
it('should return table model', function() {
var table = ctx.ds.transformMetricDataToTable(response.data.result);
expect(table.type).to.be('table');
expect(table.rows).to.eql(
[
[ 1443454528000, 'test', 'testjob', 3846]
]);
expect(table.columns).to.eql(
[ { text: 'Time', type: 'time' },
{ text: '__name__' },
{ text: 'job' },
{ text: 'Value' }
]
);
});
});
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment