Commit d018d2a2 by Mitsuhiro Tanda

(prometheus) table support

parent 51f80aff
......@@ -6,6 +6,7 @@ import moment from 'moment';
import * as dateMath from 'app/core/utils/datemath';
import PrometheusMetricFindQuery from './metric_find_query';
import TableModel from 'app/core/table_model';
var durationSplitRegexp = /(\d+)(ms|s|m|h|d|w|M|y)/;
......@@ -117,9 +118,18 @@ export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateS
throw response.error;
}
delete self.lastErrors.query;
_.each(response.data.data.result, function(metricData) {
result.push(self.transformMetricData(metricData, activeTargets[index], start, end));
});
switch (activeTargets[index].resultFormat) {
case 'table': {
result.push(self.transformMetricDataToTable(response.data.data.result));
break;
}
default: {
_.each(response.data.data.result, function(metricData) {
result.push(self.transformMetricData(metricData, activeTargets[index], start, end));
});
break;
}
}
});
return { data: result };
......@@ -260,6 +270,44 @@ export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateS
return { target: metricLabel, datapoints: dps };
};
this.transformMetricDataToTable = function(series) {
var table = new TableModel();
var self = this;
var i, j;
if (series.length === 0) {
return table;
}
_.each(series, function(series, seriesIndex) {
if (seriesIndex === 0) {
table.columns.push({text: 'Time', type: 'time'});
_.each(_.keys(series.metric), function(key) {
table.columns.push({text: key});
});
table.columns.push({text: 'Value'});
}
if (series.values) {
for (i = 0; i < series.values.length; i++) {
var values = series.values[i];
var reordered = [values[0] * 1000];
if (series.metric) {
for (var key in series.metric) {
if (series.metric.hasOwnProperty(key)) {
reordered.push(series.metric[key]);
}
}
}
reordered.push(parseFloat(values[1]));
table.rows.push(reordered);
}
}
});
return table;
};
this.createMetricLabel = function(labelData, options) {
if (_.isUndefined(options) || _.isEmpty(options.legendFormat)) {
return this.getOriginalMetricName(labelData);
......
......@@ -39,6 +39,10 @@
ng-change="ctrl.refreshMetricData()">
</select>
</div>
<label class="gf-form-label">Format as</label>
<div class="gf-form-select-wrapper">
<select class="gf-form-input gf-size-auto" ng-model="ctrl.target.resultFormat" ng-options="f.value as f.text for f in ctrl.resultFormats" ng-change="ctrl.refresh()"></select>
</div>
<label class="gf-form-label">
<a href="{{ctrl.linkToPrometheus}}" target="_blank" bs-tooltip="'Link to Graph in Prometheus'">
<i class="fa fa-share-square-o"></i>
......
......@@ -12,6 +12,7 @@ class PrometheusQueryCtrl extends QueryCtrl {
metric: any;
resolutions: any;
resultFormats: any;
oldTarget: any;
suggestMetrics: any;
linkToPrometheus: any;
......@@ -23,11 +24,16 @@ class PrometheusQueryCtrl extends QueryCtrl {
var target = this.target;
target.expr = target.expr || '';
target.intervalFactor = target.intervalFactor || 2;
target.resultFormat = target.resultFormat || 'time_series';
this.metric = '';
this.resolutions = _.map([1,2,3,4,5,10], function(f) {
return {factor: f, label: '1/' + f};
});
this.resultFormats = [
{text: 'Time series', value: 'time_series'},
{text: 'Table', value: 'table'},
];
$scope.$on('typeahead-updated', () => {
this.$scope.$apply(() => {
......
......@@ -158,4 +158,28 @@ describe('PrometheusDatasource', function() {
expect(results[0].time).to.be(1443454528 * 1000);
});
});
describe('When resultFormat is table', function() {
var response = {
status: "success",
data: {
resultType: "matrix",
result: [{
metric: {"__name__": "test", job: "testjob"},
values: [[1443454528, "3846"]]
}]
}
};
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