From d018d2a23f50921ba39e84082da7500f0e55d959 Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Wed, 28 Sep 2016 21:31:26 +0900 Subject: [PATCH 001/365] (prometheus) table support --- .../datasource/prometheus/datasource.ts | 54 +++++++++++++++++-- .../prometheus/partials/query.editor.html | 4 ++ .../datasource/prometheus/query_ctrl.ts | 6 +++ .../prometheus/specs/datasource_specs.ts | 24 +++++++++ 4 files changed, 85 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index 972fa2c7c55..4ba24a99fb1 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/public/app/plugins/datasource/prometheus/datasource.ts @@ -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); diff --git a/public/app/plugins/datasource/prometheus/partials/query.editor.html b/public/app/plugins/datasource/prometheus/partials/query.editor.html index 37c27ace58a..c4db774565f 100644 --- a/public/app/plugins/datasource/prometheus/partials/query.editor.html +++ b/public/app/plugins/datasource/prometheus/partials/query.editor.html @@ -39,6 +39,10 @@ ng-change="ctrl.refreshMetricData()"> + +
+ +