mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
(prometheus) table support
This commit is contained in:
@@ -6,6 +6,7 @@ import moment from 'moment';
|
|||||||
|
|
||||||
import * as dateMath from 'app/core/utils/datemath';
|
import * as dateMath from 'app/core/utils/datemath';
|
||||||
import PrometheusMetricFindQuery from './metric_find_query';
|
import PrometheusMetricFindQuery from './metric_find_query';
|
||||||
|
import TableModel from 'app/core/table_model';
|
||||||
|
|
||||||
var durationSplitRegexp = /(\d+)(ms|s|m|h|d|w|M|y)/;
|
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;
|
throw response.error;
|
||||||
}
|
}
|
||||||
delete self.lastErrors.query;
|
delete self.lastErrors.query;
|
||||||
_.each(response.data.data.result, function(metricData) {
|
switch (activeTargets[index].resultFormat) {
|
||||||
result.push(self.transformMetricData(metricData, activeTargets[index], start, end));
|
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 };
|
return { data: result };
|
||||||
@@ -260,6 +270,44 @@ export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateS
|
|||||||
return { target: metricLabel, datapoints: dps };
|
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) {
|
this.createMetricLabel = function(labelData, options) {
|
||||||
if (_.isUndefined(options) || _.isEmpty(options.legendFormat)) {
|
if (_.isUndefined(options) || _.isEmpty(options.legendFormat)) {
|
||||||
return this.getOriginalMetricName(labelData);
|
return this.getOriginalMetricName(labelData);
|
||||||
|
|||||||
@@ -39,6 +39,10 @@
|
|||||||
ng-change="ctrl.refreshMetricData()">
|
ng-change="ctrl.refreshMetricData()">
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</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">
|
<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>
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ class PrometheusQueryCtrl extends QueryCtrl {
|
|||||||
|
|
||||||
metric: any;
|
metric: any;
|
||||||
resolutions: any;
|
resolutions: any;
|
||||||
|
resultFormats: any;
|
||||||
oldTarget: any;
|
oldTarget: any;
|
||||||
suggestMetrics: any;
|
suggestMetrics: any;
|
||||||
linkToPrometheus: any;
|
linkToPrometheus: any;
|
||||||
@@ -23,11 +24,16 @@ class PrometheusQueryCtrl extends QueryCtrl {
|
|||||||
var target = this.target;
|
var target = this.target;
|
||||||
target.expr = target.expr || '';
|
target.expr = target.expr || '';
|
||||||
target.intervalFactor = target.intervalFactor || 2;
|
target.intervalFactor = target.intervalFactor || 2;
|
||||||
|
target.resultFormat = target.resultFormat || 'time_series';
|
||||||
|
|
||||||
this.metric = '';
|
this.metric = '';
|
||||||
this.resolutions = _.map([1,2,3,4,5,10], function(f) {
|
this.resolutions = _.map([1,2,3,4,5,10], function(f) {
|
||||||
return {factor: f, label: '1/' + f};
|
return {factor: f, label: '1/' + f};
|
||||||
});
|
});
|
||||||
|
this.resultFormats = [
|
||||||
|
{text: 'Time series', value: 'time_series'},
|
||||||
|
{text: 'Table', value: 'table'},
|
||||||
|
];
|
||||||
|
|
||||||
$scope.$on('typeahead-updated', () => {
|
$scope.$on('typeahead-updated', () => {
|
||||||
this.$scope.$apply(() => {
|
this.$scope.$apply(() => {
|
||||||
|
|||||||
@@ -158,4 +158,28 @@ describe('PrometheusDatasource', function() {
|
|||||||
expect(results[0].time).to.be(1443454528 * 1000);
|
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' }
|
||||||
|
]
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user