From 7588ee974d8113b5229a881a7518533deab84b8e Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Sun, 14 Aug 2016 17:33:18 +0300 Subject: [PATCH 01/32] Working on non time series X-axis feature. --- public/app/plugins/panel/graph/graph.js | 69 ++++++++++++++++---- public/app/plugins/panel/graph/module.ts | 9 ++- public/app/plugins/panel/graph/tab_axes.html | 11 ++++ 3 files changed, 77 insertions(+), 12 deletions(-) diff --git a/public/app/plugins/panel/graph/graph.js b/public/app/plugins/panel/graph/graph.js index 0a6a2bc0e45..66d201588df 100755 --- a/public/app/plugins/panel/graph/graph.js +++ b/public/app/plugins/panel/graph/graph.js @@ -28,7 +28,7 @@ function (angular, $, moment, _, kbn, GraphTooltip) { var ctrl = scope.ctrl; var dashboard = ctrl.dashboard; var panel = ctrl.panel; - var data, annotations; + var data, annotations, histogramData; var sortedSeries; var legendSideLastValue = null; var rootScope = scope.$root; @@ -226,22 +226,37 @@ function (angular, $, moment, _, kbn, GraphTooltip) { } }; - for (var i = 0; i < data.length; i++) { - var series = data[i]; - series.data = series.getFlotPairs(series.nullPointMode || panel.nullPointMode); + if (panel.xaxis.mode === 'histogram') { + histogramData = formatToHistogram(data, _.last); - // if hidden remove points and disable stack - if (ctrl.hiddenSeries[series.alias]) { - series.data = []; - series.stack = false; + if (histogramData.length && histogramData[0].ticks.length) { + // options.series.bars.barWidth = histogramData[0].ticks.length / 1.5; + options.series.bars.barWidth = 0.7; + // options.series.bars.align = 'center'; + } + } else { + for (var i = 0; i < data.length; i++) { + var series = data[i]; + series.data = series.getFlotPairs(series.nullPointMode || panel.nullPointMode); + + // if hidden remove points and disable stack + if (ctrl.hiddenSeries[series.alias]) { + series.data = []; + series.stack = false; + } + } + + if (data.length && data[0].stats.timeStep) { + options.series.bars.barWidth = data[0].stats.timeStep / 1.5; } } - if (data.length && data[0].stats.timeStep) { - options.series.bars.barWidth = data[0].stats.timeStep / 1.5; + if (panel.xaxis.mode === 'histogram') { + addXAxis(options); + } else { + addTimeAxis(options); } - addTimeAxis(options); addGridThresholds(options, panel); addAnnotations(options); configureAxisOptions(data, options); @@ -275,6 +290,24 @@ function (angular, $, moment, _, kbn, GraphTooltip) { } } + function formatToHistogram(data, getValueCallback) { + var histogram = [data[0]]; + + histogram[0].data = _.map(data, function(series, index) { + var values = _.remove(_.map(series.datapoints, function(point) { + return point[0]; + }), null); + var calculatedPoint = getValueCallback(values); + return [index, calculatedPoint]; + }); + + histogram[0].ticks = _.map(data, function(series, index) { + return [index, series.alias]; + }); + + return histogram; + } + function translateFillOption(fill) { return fill === 0 ? 0.001 : fill/10; } @@ -305,6 +338,20 @@ function (angular, $, moment, _, kbn, GraphTooltip) { }; } + function addXAxis(options) { + var ticks = histogramData[0].ticks; + + options.xaxis = { + timezone: dashboard.getTimezone(), + show: panel.xaxis.show, + mode: null, + min: 0, + max: ticks.length, + label: "Datetime", + ticks: ticks + }; + } + function addGridThresholds(options, panel) { if (_.isNumber(panel.grid.threshold1)) { var limit1 = panel.grid.thresholdLine ? panel.grid.threshold1 : (panel.grid.threshold2 || null); diff --git a/public/app/plugins/panel/graph/module.ts b/public/app/plugins/panel/graph/module.ts index 8fbde9e84a1..1f71ad63829 100644 --- a/public/app/plugins/panel/graph/module.ts +++ b/public/app/plugins/panel/graph/module.ts @@ -20,6 +20,7 @@ class GraphCtrl extends MetricsPanelCtrl { seriesList: any = []; logScales: any; unitFormats: any; + xAxisModes: any; annotationsPromise: any; datapointsCount: number; datapointsOutside: boolean; @@ -50,7 +51,8 @@ class GraphCtrl extends MetricsPanelCtrl { } ], xaxis: { - show: true + show: true, + mode: 'timeseries' }, grid : { threshold1: null, @@ -138,6 +140,11 @@ class GraphCtrl extends MetricsPanelCtrl { 'log (base 1024)': 1024 }; this.unitFormats = kbn.getUnitFormats(); + + this.xAxisModes = { + 'Time Series': 'timeseries', + 'Histogram': 'histogram' + }; } onInitPanelActions(actions) { diff --git a/public/app/plugins/panel/graph/tab_axes.html b/public/app/plugins/panel/graph/tab_axes.html index eeaf27aff78..0469aff547f 100644 --- a/public/app/plugins/panel/graph/tab_axes.html +++ b/public/app/plugins/panel/graph/tab_axes.html @@ -40,6 +40,17 @@
X-Axis
+ +
+ +
+ +
+
From e39e5f9a9be1f645ecba79957ee5de7b111c5c46 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Tue, 16 Aug 2016 18:45:57 +0300 Subject: [PATCH 02/32] Graph-panel: Add initial histogram option, issue #5812. --- public/app/plugins/panel/graph/graph.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/public/app/plugins/panel/graph/graph.js b/public/app/plugins/panel/graph/graph.js index 66d201588df..eae8c774a67 100755 --- a/public/app/plugins/panel/graph/graph.js +++ b/public/app/plugins/panel/graph/graph.js @@ -232,8 +232,10 @@ function (angular, $, moment, _, kbn, GraphTooltip) { if (histogramData.length && histogramData[0].ticks.length) { // options.series.bars.barWidth = histogramData[0].ticks.length / 1.5; options.series.bars.barWidth = 0.7; - // options.series.bars.align = 'center'; + options.series.bars.align = 'center'; } + + addXAxis(options); } else { for (var i = 0; i < data.length; i++) { var series = data[i]; @@ -249,11 +251,7 @@ function (angular, $, moment, _, kbn, GraphTooltip) { if (data.length && data[0].stats.timeStep) { options.series.bars.barWidth = data[0].stats.timeStep / 1.5; } - } - if (panel.xaxis.mode === 'histogram') { - addXAxis(options); - } else { addTimeAxis(options); } @@ -298,11 +296,11 @@ function (angular, $, moment, _, kbn, GraphTooltip) { return point[0]; }), null); var calculatedPoint = getValueCallback(values); - return [index, calculatedPoint]; + return [index + 1, calculatedPoint]; }); histogram[0].ticks = _.map(data, function(series, index) { - return [index, series.alias]; + return [index + 1, series.alias]; }); return histogram; @@ -346,7 +344,7 @@ function (angular, $, moment, _, kbn, GraphTooltip) { show: panel.xaxis.show, mode: null, min: 0, - max: ticks.length, + max: ticks.length + 1, label: "Datetime", ticks: ticks }; From 63886598e9be33687e9b5f35ed7ea281583785a8 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Tue, 16 Aug 2016 19:41:18 +0300 Subject: [PATCH 03/32] Graph panel: add value option (min, max, avg, etc), issue #5812. --- public/app/plugins/panel/graph/graph.js | 26 +++++++++++++++++++- public/app/plugins/panel/graph/module.ts | 7 +++++- public/app/plugins/panel/graph/tab_axes.html | 10 ++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/panel/graph/graph.js b/public/app/plugins/panel/graph/graph.js index eae8c774a67..e5b2218d86a 100755 --- a/public/app/plugins/panel/graph/graph.js +++ b/public/app/plugins/panel/graph/graph.js @@ -227,7 +227,17 @@ function (angular, $, moment, _, kbn, GraphTooltip) { }; if (panel.xaxis.mode === 'histogram') { - histogramData = formatToHistogram(data, _.last); + // Format to histogram + + var getValueFuncs = { + 'min': _.min, + 'max': _.max, + 'avg': seriesAvg, + 'current': _.last, + 'total': seriesSum + }; + + histogramData = formatToHistogram(data, getValueFuncs[panel.xaxis.histogramValue]); if (histogramData.length && histogramData[0].ticks.length) { // options.series.bars.barWidth = histogramData[0].ticks.length / 1.5; @@ -306,6 +316,20 @@ function (angular, $, moment, _, kbn, GraphTooltip) { return histogram; } + function seriesSum(values) { + return _.reduce(values, function(sum, num) { + return sum + num; + }); + } + + function seriesAvg(values) { + if (values.length) { + return seriesSum(values) / values.length; + } else { + return null; + } + } + function translateFillOption(fill) { return fill === 0 ? 0.001 : fill/10; } diff --git a/public/app/plugins/panel/graph/module.ts b/public/app/plugins/panel/graph/module.ts index 1f71ad63829..9a10c45a9f8 100644 --- a/public/app/plugins/panel/graph/module.ts +++ b/public/app/plugins/panel/graph/module.ts @@ -21,6 +21,7 @@ class GraphCtrl extends MetricsPanelCtrl { logScales: any; unitFormats: any; xAxisModes: any; + xAxisHistogramValues: any; annotationsPromise: any; datapointsCount: number; datapointsOutside: boolean; @@ -52,7 +53,8 @@ class GraphCtrl extends MetricsPanelCtrl { ], xaxis: { show: true, - mode: 'timeseries' + mode: 'timeseries', + histogramValue: 'avg' }, grid : { threshold1: null, @@ -116,6 +118,7 @@ class GraphCtrl extends MetricsPanelCtrl { _.defaults(this.panel.tooltip, this.panelDefaults.tooltip); _.defaults(this.panel.grid, this.panelDefaults.grid); _.defaults(this.panel.legend, this.panelDefaults.legend); + _.defaults(this.panel.xaxis, this.panelDefaults.xaxis); this.colors = $scope.$root.colors; @@ -145,6 +148,8 @@ class GraphCtrl extends MetricsPanelCtrl { 'Time Series': 'timeseries', 'Histogram': 'histogram' }; + + this.xAxisHistogramValues = ['min', 'max', 'avg', 'current', 'total']; } onInitPanelActions(actions) { diff --git a/public/app/plugins/panel/graph/tab_axes.html b/public/app/plugins/panel/graph/tab_axes.html index 0469aff547f..b4d7aedf0a7 100644 --- a/public/app/plugins/panel/graph/tab_axes.html +++ b/public/app/plugins/panel/graph/tab_axes.html @@ -51,6 +51,16 @@
+
+ +
+ +
+
From 113173be3d59a6e79806d2806b907d7d2b3fca5c Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Tue, 16 Aug 2016 21:08:15 +0300 Subject: [PATCH 04/32] Graph panel: preserve series options (colors and other), issue #5812. --- public/app/plugins/panel/graph/graph.js | 82 +++++++------------------ 1 file changed, 23 insertions(+), 59 deletions(-) diff --git a/public/app/plugins/panel/graph/graph.js b/public/app/plugins/panel/graph/graph.js index e5b2218d86a..2ba0d101af8 100755 --- a/public/app/plugins/panel/graph/graph.js +++ b/public/app/plugins/panel/graph/graph.js @@ -28,7 +28,7 @@ function (angular, $, moment, _, kbn, GraphTooltip) { var ctrl = scope.ctrl; var dashboard = ctrl.dashboard; var panel = ctrl.panel; - var data, annotations, histogramData; + var data, annotations; var sortedSeries; var legendSideLastValue = null; var rootScope = scope.$root; @@ -226,38 +226,32 @@ function (angular, $, moment, _, kbn, GraphTooltip) { } }; + for (var i = 0; i < data.length; i++) { + var series = data[i]; + series.data = series.getFlotPairs(series.nullPointMode || panel.nullPointMode); + + if (panel.xaxis.mode === 'histogram') { + series.data = [ + [i + 1, series.stats[panel.xaxis.histogramValue]] + ]; + } + + // if hidden remove points and disable stack + if (ctrl.hiddenSeries[series.alias]) { + series.data = []; + series.stack = false; + } + } + if (panel.xaxis.mode === 'histogram') { - // Format to histogram - - var getValueFuncs = { - 'min': _.min, - 'max': _.max, - 'avg': seriesAvg, - 'current': _.last, - 'total': seriesSum - }; - - histogramData = formatToHistogram(data, getValueFuncs[panel.xaxis.histogramValue]); - - if (histogramData.length && histogramData[0].ticks.length) { - // options.series.bars.barWidth = histogramData[0].ticks.length / 1.5; + if (data.length) { options.series.bars.barWidth = 0.7; options.series.bars.align = 'center'; } addXAxis(options); + } else { - for (var i = 0; i < data.length; i++) { - var series = data[i]; - series.data = series.getFlotPairs(series.nullPointMode || panel.nullPointMode); - - // if hidden remove points and disable stack - if (ctrl.hiddenSeries[series.alias]) { - series.data = []; - series.stack = false; - } - } - if (data.length && data[0].stats.timeStep) { options.series.bars.barWidth = data[0].stats.timeStep / 1.5; } @@ -298,38 +292,6 @@ function (angular, $, moment, _, kbn, GraphTooltip) { } } - function formatToHistogram(data, getValueCallback) { - var histogram = [data[0]]; - - histogram[0].data = _.map(data, function(series, index) { - var values = _.remove(_.map(series.datapoints, function(point) { - return point[0]; - }), null); - var calculatedPoint = getValueCallback(values); - return [index + 1, calculatedPoint]; - }); - - histogram[0].ticks = _.map(data, function(series, index) { - return [index + 1, series.alias]; - }); - - return histogram; - } - - function seriesSum(values) { - return _.reduce(values, function(sum, num) { - return sum + num; - }); - } - - function seriesAvg(values) { - if (values.length) { - return seriesSum(values) / values.length; - } else { - return null; - } - } - function translateFillOption(fill) { return fill === 0 ? 0.001 : fill/10; } @@ -361,7 +323,9 @@ function (angular, $, moment, _, kbn, GraphTooltip) { } function addXAxis(options) { - var ticks = histogramData[0].ticks; + var ticks = _.map(data, function(series, index) { + return [index + 1, series.alias]; + }); options.xaxis = { timezone: dashboard.getTimezone(), From 93515d0ffc6e3bbbbdbc43fb332389e855ce1896 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Tue, 16 Aug 2016 21:53:53 +0300 Subject: [PATCH 05/32] Graph panel: display only bars in histogram mode, issue #5812. --- public/app/plugins/panel/graph/graph.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/public/app/plugins/panel/graph/graph.js b/public/app/plugins/panel/graph/graph.js index 2ba0d101af8..75b3bc2b5e1 100755 --- a/public/app/plugins/panel/graph/graph.js +++ b/public/app/plugins/panel/graph/graph.js @@ -247,6 +247,9 @@ function (angular, $, moment, _, kbn, GraphTooltip) { if (data.length) { options.series.bars.barWidth = 0.7; options.series.bars.align = 'center'; + options.series.bars.show = true; + options.series.points.show = false; + options.series.lines.show = false; } addXAxis(options); From 284a6ee62965711fc5c00fdd3ce5ef4c233c79e2 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Thu, 18 Aug 2016 15:34:32 +0300 Subject: [PATCH 06/32] Graph panel: rename X axis modes, issue #5812. --- public/app/plugins/panel/graph/graph.js | 6 +++--- public/app/plugins/panel/graph/module.ts | 12 ++++++------ public/app/plugins/panel/graph/tab_axes.html | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/public/app/plugins/panel/graph/graph.js b/public/app/plugins/panel/graph/graph.js index 75b3bc2b5e1..25774f799f2 100755 --- a/public/app/plugins/panel/graph/graph.js +++ b/public/app/plugins/panel/graph/graph.js @@ -230,9 +230,9 @@ function (angular, $, moment, _, kbn, GraphTooltip) { var series = data[i]; series.data = series.getFlotPairs(series.nullPointMode || panel.nullPointMode); - if (panel.xaxis.mode === 'histogram') { + if (panel.xaxis.mode === 'series') { series.data = [ - [i + 1, series.stats[panel.xaxis.histogramValue]] + [i + 1, series.stats[panel.xaxis.seriesValue]] ]; } @@ -243,7 +243,7 @@ function (angular, $, moment, _, kbn, GraphTooltip) { } } - if (panel.xaxis.mode === 'histogram') { + if (panel.xaxis.mode === 'series') { if (data.length) { options.series.bars.barWidth = 0.7; options.series.bars.align = 'center'; diff --git a/public/app/plugins/panel/graph/module.ts b/public/app/plugins/panel/graph/module.ts index 9a10c45a9f8..22ec7235cde 100644 --- a/public/app/plugins/panel/graph/module.ts +++ b/public/app/plugins/panel/graph/module.ts @@ -21,7 +21,7 @@ class GraphCtrl extends MetricsPanelCtrl { logScales: any; unitFormats: any; xAxisModes: any; - xAxisHistogramValues: any; + xAxisSeriesValues: any; annotationsPromise: any; datapointsCount: number; datapointsOutside: boolean; @@ -53,8 +53,8 @@ class GraphCtrl extends MetricsPanelCtrl { ], xaxis: { show: true, - mode: 'timeseries', - histogramValue: 'avg' + mode: 'time', + seriesValue: 'avg' }, grid : { threshold1: null, @@ -145,11 +145,11 @@ class GraphCtrl extends MetricsPanelCtrl { this.unitFormats = kbn.getUnitFormats(); this.xAxisModes = { - 'Time Series': 'timeseries', - 'Histogram': 'histogram' + 'Time': 'time', + 'Series': 'series' }; - this.xAxisHistogramValues = ['min', 'max', 'avg', 'current', 'total']; + this.xAxisSeriesValues = ['min', 'max', 'avg', 'current', 'total']; } onInitPanelActions(actions) { diff --git a/public/app/plugins/panel/graph/tab_axes.html b/public/app/plugins/panel/graph/tab_axes.html index b4d7aedf0a7..5c868115596 100644 --- a/public/app/plugins/panel/graph/tab_axes.html +++ b/public/app/plugins/panel/graph/tab_axes.html @@ -51,12 +51,12 @@
-
+
From c683c7a4487353ff307a4b05b8d821117acbd7c4 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Thu, 18 Aug 2016 20:12:08 +0300 Subject: [PATCH 07/32] Graph panel: initial support for table format, issue #5812. --- public/app/plugins/panel/graph/graph.js | 37 +++++++++++- public/app/plugins/panel/graph/module.ts | 61 +++++++++++++++++++- public/app/plugins/panel/graph/tab_axes.html | 16 ++++- 3 files changed, 108 insertions(+), 6 deletions(-) diff --git a/public/app/plugins/panel/graph/graph.js b/public/app/plugins/panel/graph/graph.js index 25774f799f2..bde6339cb24 100755 --- a/public/app/plugins/panel/graph/graph.js +++ b/public/app/plugins/panel/graph/graph.js @@ -234,6 +234,16 @@ function (angular, $, moment, _, kbn, GraphTooltip) { series.data = [ [i + 1, series.stats[panel.xaxis.seriesValue]] ]; + } else if (panel.xaxis.mode === 'table') { + series.data = []; + for (var j = 0; j < series.datapoints.length; j++) { + var dataIndex = i * series.datapoints.length + j; + series.datapoints[j]; + series.data.push([ + dataIndex + 1, + series.datapoints[j][0] + ]); + } } // if hidden remove points and disable stack @@ -252,7 +262,10 @@ function (angular, $, moment, _, kbn, GraphTooltip) { options.series.lines.show = false; } - addXAxis(options); + addXSeriesAxis(options); + + } else if (panel.xaxis.mode === 'table') { + addXTableAxis(options); } else { if (data.length && data[0].stats.timeStep) { @@ -325,7 +338,7 @@ function (angular, $, moment, _, kbn, GraphTooltip) { }; } - function addXAxis(options) { + function addXSeriesAxis(options) { var ticks = _.map(data, function(series, index) { return [index + 1, series.alias]; }); @@ -341,6 +354,26 @@ function (angular, $, moment, _, kbn, GraphTooltip) { }; } + function addXTableAxis(options) { + var ticks = _.map(data, function(series, seriesIndex) { + return _.map(series.datapoints, function(point, pointIndex) { + var tickIndex = seriesIndex * series.datapoints.length + pointIndex; + return [tickIndex + 1, point[1]]; + }); + }); + ticks = _.flatten(ticks, true); + + options.xaxis = { + timezone: dashboard.getTimezone(), + show: panel.xaxis.show, + mode: null, + min: 0, + max: ticks.length + 1, + label: "Datetime", + ticks: ticks + }; + } + function addGridThresholds(options, panel) { if (_.isNumber(panel.grid.threshold1)) { var limit1 = panel.grid.thresholdLine ? panel.grid.threshold1 : (panel.grid.threshold2 || null); diff --git a/public/app/plugins/panel/graph/module.ts b/public/app/plugins/panel/graph/module.ts index 22ec7235cde..b098ca4b11d 100644 --- a/public/app/plugins/panel/graph/module.ts +++ b/public/app/plugins/panel/graph/module.ts @@ -22,6 +22,7 @@ class GraphCtrl extends MetricsPanelCtrl { unitFormats: any; xAxisModes: any; xAxisSeriesValues: any; + xAxisColumns: any = []; annotationsPromise: any; datapointsCount: number; datapointsOutside: boolean; @@ -146,7 +147,8 @@ class GraphCtrl extends MetricsPanelCtrl { this.xAxisModes = { 'Time': 'time', - 'Series': 'series' + 'Series': 'series', + 'Table': 'table' }; this.xAxisSeriesValues = ['min', 'max', 'avg', 'current', 'total']; @@ -186,7 +188,26 @@ class GraphCtrl extends MetricsPanelCtrl { this.datapointsWarning = false; this.datapointsCount = 0; this.datapointsOutside = false; - this.seriesList = dataList.map(this.seriesHandler.bind(this)); + + let dataHandler: (seriesData, index)=>any; + if (this.panel.xaxis.mode === 'table') { + if (dataList.length) { + // Table panel uses only first enabled tagret, so we can use dataList[0] + // for table data representation + this.xAxisColumns = _.map(dataList[0].columns, function(column, index) { + return { + text: column.text, + index: index + }; + }); + } + + dataHandler = this.tableHandler; + } else { + dataHandler = this.seriesHandler; + } + + this.seriesList = dataList.map(dataHandler.bind(this)); this.datapointsWarning = this.datapointsCount === 0 || this.datapointsOutside; this.annotationsPromise.then(annotations => { @@ -227,6 +248,42 @@ class GraphCtrl extends MetricsPanelCtrl { return series; } + tableHandler(seriesData, index) { + var xColumnIndex = Number(this.panel.xaxis.columnIndex); + var datapoints = _.map(seriesData.rows, (row) => { + return [ + _.last(row), // Y value (always last column) + row[xColumnIndex] // X value + ]; + }); + + var alias = seriesData.columns[xColumnIndex].text; + + var colorIndex = index % this.colors.length; + var color = this.panel.aliasColors[alias] || this.colors[colorIndex]; + + var series = new TimeSeries({ + datapoints: datapoints, + alias: alias, + color: color, + unit: seriesData.unit, + }); + + if (datapoints && datapoints.length > 0) { + var last = moment.utc(datapoints[datapoints.length - 1][1]); + var from = moment.utc(this.range.from); + if (last - from < -10000) { + this.datapointsOutside = true; + } + + this.datapointsCount += datapoints.length; + this.panel.tooltip.msResolution = this.panel.tooltip.msResolution || series.isMsResolutionNeeded(); + } + + + return series; + } + onRender() { if (!this.seriesList) { return; } diff --git a/public/app/plugins/panel/graph/tab_axes.html b/public/app/plugins/panel/graph/tab_axes.html index 5c868115596..233d22013a5 100644 --- a/public/app/plugins/panel/graph/tab_axes.html +++ b/public/app/plugins/panel/graph/tab_axes.html @@ -47,11 +47,12 @@
-
+ +
+ +
+
From d23e9fa3c35cfb3e00d03266dd8124c4b0029777 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Tue, 23 Aug 2016 20:44:58 +0300 Subject: [PATCH 08/32] Graph panel: table format support improvements, issue #5812. --- public/app/plugins/panel/graph/graph.js | 5 +++++ public/app/plugins/panel/graph/module.ts | 12 ++++++++++-- public/app/plugins/panel/graph/tab_axes.html | 11 +++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/panel/graph/graph.js b/public/app/plugins/panel/graph/graph.js index bde6339cb24..c5b21beab2f 100755 --- a/public/app/plugins/panel/graph/graph.js +++ b/public/app/plugins/panel/graph/graph.js @@ -265,6 +265,11 @@ function (angular, $, moment, _, kbn, GraphTooltip) { addXSeriesAxis(options); } else if (panel.xaxis.mode === 'table') { + if (data.length) { + options.series.bars.barWidth = 0.7; + options.series.bars.align = 'center'; + } + addXTableAxis(options); } else { diff --git a/public/app/plugins/panel/graph/module.ts b/public/app/plugins/panel/graph/module.ts index b098ca4b11d..52e70ce4c1f 100644 --- a/public/app/plugins/panel/graph/module.ts +++ b/public/app/plugins/panel/graph/module.ts @@ -194,12 +194,18 @@ class GraphCtrl extends MetricsPanelCtrl { if (dataList.length) { // Table panel uses only first enabled tagret, so we can use dataList[0] // for table data representation + dataList.splice(1, dataList.length - 1); this.xAxisColumns = _.map(dataList[0].columns, function(column, index) { return { text: column.text, index: index }; }); + + // Set last column as default value + if (!this.panel.xaxis.valueColumnIndex) { + this.panel.xaxis.valueColumnIndex = this.xAxisColumns.length - 1; + } } dataHandler = this.tableHandler; @@ -250,14 +256,16 @@ class GraphCtrl extends MetricsPanelCtrl { tableHandler(seriesData, index) { var xColumnIndex = Number(this.panel.xaxis.columnIndex); + var valueColumnIndex = this.panel.xaxis.valueColumnIndex; var datapoints = _.map(seriesData.rows, (row) => { + var value = valueColumnIndex ? row[valueColumnIndex] : _.last(row); return [ - _.last(row), // Y value (always last column) + value, // Y value row[xColumnIndex] // X value ]; }); - var alias = seriesData.columns[xColumnIndex].text; + var alias = seriesData.columns[valueColumnIndex].text; var colorIndex = index % this.colors.length; var color = this.panel.aliasColors[alias] || this.colors[colorIndex]; diff --git a/public/app/plugins/panel/graph/tab_axes.html b/public/app/plugins/panel/graph/tab_axes.html index 233d22013a5..86faa57e467 100644 --- a/public/app/plugins/panel/graph/tab_axes.html +++ b/public/app/plugins/panel/graph/tab_axes.html @@ -73,6 +73,17 @@
+ +
+ +
+ +
+
From 06af566f3face5db21374b08ed9c958414c83896 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Thu, 25 Aug 2016 21:53:49 +0300 Subject: [PATCH 09/32] Graph panel: initial elastic raw document format support, issue #5812. --- public/app/plugins/panel/graph/graph.js | 6 +- public/app/plugins/panel/graph/module.ts | 90 +++++++++++++++++++- public/app/plugins/panel/graph/tab_axes.html | 25 ++++++ 3 files changed, 115 insertions(+), 6 deletions(-) diff --git a/public/app/plugins/panel/graph/graph.js b/public/app/plugins/panel/graph/graph.js index c5b21beab2f..70efb24678f 100755 --- a/public/app/plugins/panel/graph/graph.js +++ b/public/app/plugins/panel/graph/graph.js @@ -234,7 +234,8 @@ function (angular, $, moment, _, kbn, GraphTooltip) { series.data = [ [i + 1, series.stats[panel.xaxis.seriesValue]] ]; - } else if (panel.xaxis.mode === 'table') { + } else if (panel.xaxis.mode === 'table' || + panel.xaxis.mode === 'elastic') { series.data = []; for (var j = 0; j < series.datapoints.length; j++) { var dataIndex = i * series.datapoints.length + j; @@ -264,7 +265,8 @@ function (angular, $, moment, _, kbn, GraphTooltip) { addXSeriesAxis(options); - } else if (panel.xaxis.mode === 'table') { + } else if (panel.xaxis.mode === 'table' || + panel.xaxis.mode === 'elastic') { if (data.length) { options.series.bars.barWidth = 0.7; options.series.bars.align = 'center'; diff --git a/public/app/plugins/panel/graph/module.ts b/public/app/plugins/panel/graph/module.ts index 52e70ce4c1f..5f1bab37127 100644 --- a/public/app/plugins/panel/graph/module.ts +++ b/public/app/plugins/panel/graph/module.ts @@ -148,7 +148,8 @@ class GraphCtrl extends MetricsPanelCtrl { this.xAxisModes = { 'Time': 'time', 'Series': 'series', - 'Table': 'table' + 'Table': 'table', + 'Elastic Raw Doc': 'elastic' }; this.xAxisSeriesValues = ['min', 'max', 'avg', 'current', 'total']; @@ -195,7 +196,7 @@ class GraphCtrl extends MetricsPanelCtrl { // Table panel uses only first enabled tagret, so we can use dataList[0] // for table data representation dataList.splice(1, dataList.length - 1); - this.xAxisColumns = _.map(dataList[0].columns, function(column, index) { + this.xAxisColumns = _.map(dataList[0].columns, (column, index) => { return { text: column.text, index: index @@ -209,6 +210,14 @@ class GraphCtrl extends MetricsPanelCtrl { } dataHandler = this.tableHandler; + } else if (this.panel.xaxis.mode === 'elastic') { + if (dataList.length) { + dataList.splice(1, dataList.length - 1); + var point = _.first(dataList[0].datapoints); + this.xAxisColumns = getFieldsFromESDoc(point); + } + + dataHandler = this.esRawDocHandler; } else { dataHandler = this.seriesHandler; } @@ -250,13 +259,12 @@ class GraphCtrl extends MetricsPanelCtrl { this.panel.tooltip.msResolution = this.panel.tooltip.msResolution || series.isMsResolutionNeeded(); } - return series; } tableHandler(seriesData, index) { var xColumnIndex = Number(this.panel.xaxis.columnIndex); - var valueColumnIndex = this.panel.xaxis.valueColumnIndex; + var valueColumnIndex = Number(this.panel.xaxis.valueColumnIndex); var datapoints = _.map(seriesData.rows, (row) => { var value = valueColumnIndex ? row[valueColumnIndex] : _.last(row); return [ @@ -288,6 +296,46 @@ class GraphCtrl extends MetricsPanelCtrl { this.panel.tooltip.msResolution = this.panel.tooltip.msResolution || series.isMsResolutionNeeded(); } + return series; + } + + esRawDocHandler(seriesData, index) { + let xField = this.panel.xaxis.esField; + let valueField = this.panel.xaxis.esValueField; + let datapoints = _.map(seriesData.datapoints, (doc) => { + return [ + pluckDeep(doc, valueField), // Y value + pluckDeep(doc, xField) // X value + ]; + }); + + // Remove empty points + datapoints = _.filter(datapoints, (point) => { + return point[0] !== undefined; + }); + + var alias = valueField; + + var colorIndex = index % this.colors.length; + var color = this.panel.aliasColors[alias] || this.colors[colorIndex]; + + var series = new TimeSeries({ + datapoints: datapoints, + alias: alias, + color: color, + unit: seriesData.unit, + }); + + if (datapoints && datapoints.length > 0) { + var last = moment.utc(datapoints[datapoints.length - 1][1]); + var from = moment.utc(this.range.from); + if (last - from < -10000) { + this.datapointsOutside = true; + } + + this.datapointsCount += datapoints.length; + this.panel.tooltip.msResolution = this.panel.tooltip.msResolution || series.isMsResolutionNeeded(); + } return series; } @@ -396,4 +444,38 @@ class GraphCtrl extends MetricsPanelCtrl { } } +function getFieldsFromESDoc(doc) { + let fields = []; + let fieldNameParts = []; + + function getFieldsRecursive(obj) { + _.forEach(obj, (value, key) => { + if (_.isObject(value)) { + fieldNameParts.push(key); + getFieldsRecursive(value); + } else { + let field = fieldNameParts.concat(key).join('.'); + fields.push(field); + } + }); + fieldNameParts.pop(); + } + + getFieldsRecursive(doc); + return fields; +} + +function pluckDeep(obj: any, property: string) { + let propertyParts = property.split('.'); + let value = obj; + for (let i = 0; i < propertyParts.length; ++i) { + if (value[propertyParts[i]]) { + value = value[propertyParts[i]]; + } else { + return undefined; + } + } + return value; +} + export {GraphCtrl, GraphCtrl as PanelCtrl} diff --git a/public/app/plugins/panel/graph/tab_axes.html b/public/app/plugins/panel/graph/tab_axes.html index 86faa57e467..1f0c434c113 100644 --- a/public/app/plugins/panel/graph/tab_axes.html +++ b/public/app/plugins/panel/graph/tab_axes.html @@ -52,6 +52,7 @@
+
@@ -63,6 +64,7 @@
+
@@ -84,6 +86,29 @@
+ + +
+ +
+ +
+
+ +
+ +
+ +
+
From 7a6d32138b8aa9f662f468c76c10aacd74f61e83 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Fri, 26 Aug 2016 20:47:12 +0300 Subject: [PATCH 10/32] Graph panel: refactor, issue #5812. --- public/app/plugins/panel/graph/module.ts | 59 +++++------------------- 1 file changed, 11 insertions(+), 48 deletions(-) diff --git a/public/app/plugins/panel/graph/module.ts b/public/app/plugins/panel/graph/module.ts index 5f1bab37127..550f8acd23b 100644 --- a/public/app/plugins/panel/graph/module.ts +++ b/public/app/plugins/panel/graph/module.ts @@ -219,7 +219,7 @@ class GraphCtrl extends MetricsPanelCtrl { dataHandler = this.esRawDocHandler; } else { - dataHandler = this.seriesHandler; + dataHandler = this.timeSeriesHandler; } this.seriesList = dataList.map(dataHandler.bind(this)); @@ -235,9 +235,7 @@ class GraphCtrl extends MetricsPanelCtrl { }); } - seriesHandler(seriesData, index) { - var datapoints = seriesData.datapoints; - var alias = seriesData.target; + seriesHandler(seriesData, index, datapoints, alias) { var colorIndex = index % this.colors.length; var color = this.panel.aliasColors[alias] || this.colors[colorIndex]; @@ -262,6 +260,13 @@ class GraphCtrl extends MetricsPanelCtrl { return series; } + timeSeriesHandler(seriesData, index) { + var datapoints = seriesData.datapoints; + var alias = seriesData.target; + + return this.seriesHandler(seriesData, index, datapoints, alias); + } + tableHandler(seriesData, index) { var xColumnIndex = Number(this.panel.xaxis.columnIndex); var valueColumnIndex = Number(this.panel.xaxis.valueColumnIndex); @@ -275,28 +280,7 @@ class GraphCtrl extends MetricsPanelCtrl { var alias = seriesData.columns[valueColumnIndex].text; - var colorIndex = index % this.colors.length; - var color = this.panel.aliasColors[alias] || this.colors[colorIndex]; - - var series = new TimeSeries({ - datapoints: datapoints, - alias: alias, - color: color, - unit: seriesData.unit, - }); - - if (datapoints && datapoints.length > 0) { - var last = moment.utc(datapoints[datapoints.length - 1][1]); - var from = moment.utc(this.range.from); - if (last - from < -10000) { - this.datapointsOutside = true; - } - - this.datapointsCount += datapoints.length; - this.panel.tooltip.msResolution = this.panel.tooltip.msResolution || series.isMsResolutionNeeded(); - } - - return series; + return this.seriesHandler(seriesData, index, datapoints, alias); } esRawDocHandler(seriesData, index) { @@ -316,28 +300,7 @@ class GraphCtrl extends MetricsPanelCtrl { var alias = valueField; - var colorIndex = index % this.colors.length; - var color = this.panel.aliasColors[alias] || this.colors[colorIndex]; - - var series = new TimeSeries({ - datapoints: datapoints, - alias: alias, - color: color, - unit: seriesData.unit, - }); - - if (datapoints && datapoints.length > 0) { - var last = moment.utc(datapoints[datapoints.length - 1][1]); - var from = moment.utc(this.range.from); - if (last - from < -10000) { - this.datapointsOutside = true; - } - - this.datapointsCount += datapoints.length; - this.panel.tooltip.msResolution = this.panel.tooltip.msResolution || series.isMsResolutionNeeded(); - } - - return series; + return this.seriesHandler(seriesData, index, datapoints, alias); } onRender() { From 31642b472c7812c93b5f6b8fbc0708304c3f4a17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 5 Sep 2016 11:07:41 +0200 Subject: [PATCH 11/32] refactoring(graph panel): #5917 --- public/app/plugins/panel/graph/graph.js | 4 +- public/app/plugins/panel/graph/module.ts | 55 +++++++------------- public/app/plugins/panel/graph/tab_axes.html | 14 ++--- 3 files changed, 26 insertions(+), 47 deletions(-) diff --git a/public/app/plugins/panel/graph/graph.js b/public/app/plugins/panel/graph/graph.js index 5657d1eb9d2..83129a3efc8 100755 --- a/public/app/plugins/panel/graph/graph.js +++ b/public/app/plugins/panel/graph/graph.js @@ -238,9 +238,7 @@ function (angular, $, moment, _, kbn, GraphTooltip, thresholdManExports) { series.data = series.getFlotPairs(series.nullPointMode || panel.nullPointMode); if (panel.xaxis.mode === 'series') { - series.data = [ - [i + 1, series.stats[panel.xaxis.seriesValue]] - ]; + series.data = [[i + 1, series.stats[panel.xaxis.values[0]]]]; } else if (panel.xaxis.mode === 'table' || panel.xaxis.mode === 'elastic') { series.data = []; diff --git a/public/app/plugins/panel/graph/module.ts b/public/app/plugins/panel/graph/module.ts index f616bb28f70..f1b54c03e11 100644 --- a/public/app/plugins/panel/graph/module.ts +++ b/public/app/plugins/panel/graph/module.ts @@ -23,8 +23,7 @@ class GraphCtrl extends MetricsPanelCtrl { logScales: any; unitFormats: any; xAxisModes: any; - xAxisSeriesValues: any; - xAxisColumns: any = []; + xAxisSeriesStats: any; annotationsPromise: any; datapointsCount: number; datapointsOutside: boolean; @@ -58,11 +57,8 @@ class GraphCtrl extends MetricsPanelCtrl { xaxis: { show: true, mode: 'time', - seriesValue: 'avg' - }, - alert: { - warn: {op: '>', value: undefined}, - crit: {op: '>', value: undefined}, + name: null, + values: [], }, // show/hide lines lines : true, @@ -120,7 +116,6 @@ class GraphCtrl extends MetricsPanelCtrl { _.defaults(this.panel, this.panelDefaults); _.defaults(this.panel.tooltip, this.panelDefaults.tooltip); - _.defaults(this.panel.alert, this.panelDefaults.alert); _.defaults(this.panel.legend, this.panelDefaults.legend); _.defaults(this.panel.xaxis, this.panelDefaults.xaxis); @@ -156,10 +151,10 @@ class GraphCtrl extends MetricsPanelCtrl { 'Time': 'time', 'Series': 'series', 'Table': 'table', - 'Elastic Raw Doc': 'elastic' + 'Json': 'json' }; - this.xAxisSeriesValues = ['min', 'max', 'avg', 'current', 'total']; + this.xAxisSeriesStats = ['min', 'max', 'avg', 'current', 'count', 'total']; this.subTabIndex = 0; } @@ -199,35 +194,21 @@ class GraphCtrl extends MetricsPanelCtrl { this.datapointsOutside = false; let dataHandler: (seriesData, index)=>any; - if (this.panel.xaxis.mode === 'table') { - if (dataList.length) { - // Table panel uses only first enabled tagret, so we can use dataList[0] - // for table data representation - dataList.splice(1, dataList.length - 1); - this.xAxisColumns = _.map(dataList[0].columns, (column, index) => { - return { - text: column.text, - index: index - }; - }); - - // Set last column as default value - if (!this.panel.xaxis.valueColumnIndex) { - this.panel.xaxis.valueColumnIndex = this.xAxisColumns.length - 1; - } + switch (this.panel.xaxis.mode) { + case 'series': + case 'time': { + dataHandler = this.timeSeriesHandler; + break; } - - dataHandler = this.tableHandler; - } else if (this.panel.xaxis.mode === 'elastic') { - if (dataList.length) { - dataList.splice(1, dataList.length - 1); - var point = _.first(dataList[0].datapoints); - this.xAxisColumns = getFieldsFromESDoc(point); + case 'table': { + // Table panel uses only first enabled target, so we can use dataList[0] + dataList.splice(1, dataList.length - 1); + dataHandler = this.tableHandler; + break; + } + case 'json': { + break; } - - dataHandler = this.esRawDocHandler; - } else { - dataHandler = this.timeSeriesHandler; } this.seriesList = dataList.map(dataHandler.bind(this)); diff --git a/public/app/plugins/panel/graph/tab_axes.html b/public/app/plugins/panel/graph/tab_axes.html index 3a2135cd360..c5ebfcc92d0 100644 --- a/public/app/plugins/panel/graph/tab_axes.html +++ b/public/app/plugins/panel/graph/tab_axes.html @@ -57,8 +57,8 @@
@@ -69,7 +69,7 @@
@@ -80,7 +80,7 @@
@@ -88,11 +88,11 @@
-
+
@@ -103,7 +103,7 @@
From 395213abd75a899da58a42dc57bf76e82c5db2a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 5 Sep 2016 11:46:16 +0200 Subject: [PATCH 12/32] feat(graph panel): more refactoring of #5917 --- public/app/core/directives/metric_segment.js | 5 +- public/app/core/services/segment_srv.js | 1 + .../app/features/panel/metrics_ds_selector.ts | 4 +- public/app/plugins/panel/graph/module.ts | 20 +++++- public/app/plugins/panel/graph/tab_axes.html | 63 +++---------------- public/sass/components/_gf-form.scss | 2 +- 6 files changed, 33 insertions(+), 62 deletions(-) diff --git a/public/app/core/directives/metric_segment.js b/public/app/core/directives/metric_segment.js index 7669f2fb709..36eac942c3e 100644 --- a/public/app/core/directives/metric_segment.js +++ b/public/app/core/directives/metric_segment.js @@ -23,10 +23,10 @@ function (_, $, coreModule) { getOptions: "&", onChange: "&", }, - link: function($scope, elem, attrs) { + link: function($scope, elem) { var $input = $(inputTemplate); - var $button = $(attrs.styleMode === 'select' ? selectTemplate : linkTemplate); var segment = $scope.segment; + var $button = $(segment.selectMode ? selectTemplate : linkTemplate); var options = null; var cancelBlur = null; var linkMode = true; @@ -179,6 +179,7 @@ function (_, $, coreModule) { cssClass: attrs.cssClass, custom: attrs.custom, value: option ? option.text : value, + selectMode: attrs.selectMode, }; return uiSegmentSrv.newSegment(segment); }; diff --git a/public/app/core/services/segment_srv.js b/public/app/core/services/segment_srv.js index d05a2bb011f..9d13e8e27e3 100644 --- a/public/app/core/services/segment_srv.js +++ b/public/app/core/services/segment_srv.js @@ -28,6 +28,7 @@ function (angular, _, coreModule) { this.type = options.type; this.fake = options.fake; this.value = options.value; + this.selectMode = options.selectMode; this.type = options.type; this.expandable = options.expandable; this.html = options.html || $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value)); diff --git a/public/app/features/panel/metrics_ds_selector.ts b/public/app/features/panel/metrics_ds_selector.ts index c0e1b776062..f925b2afb42 100644 --- a/public/app/features/panel/metrics_ds_selector.ts +++ b/public/app/features/panel/metrics_ds_selector.ts @@ -16,7 +16,7 @@ var template = ` Panel data source -
@@ -67,7 +67,7 @@ export class MetricsDsSelectorCtrl { this.current = {name: dsValue + ' not found', value: null}; } - this.dsSegment = uiSegmentSrv.newSegment(this.current.name); + this.dsSegment = uiSegmentSrv.newSegment({value: this.current.name, selectMode: true}); } getOptions() { diff --git a/public/app/plugins/panel/graph/module.ts b/public/app/plugins/panel/graph/module.ts index f1b54c03e11..cd2ac329ef5 100644 --- a/public/app/plugins/panel/graph/module.ts +++ b/public/app/plugins/panel/graph/module.ts @@ -23,7 +23,7 @@ class GraphCtrl extends MetricsPanelCtrl { logScales: any; unitFormats: any; xAxisModes: any; - xAxisSeriesStats: any; + xNameSegment: any; annotationsPromise: any; datapointsCount: number; datapointsOutside: boolean; @@ -154,7 +154,6 @@ class GraphCtrl extends MetricsPanelCtrl { 'Json': 'json' }; - this.xAxisSeriesStats = ['min', 'max', 'avg', 'current', 'count', 'total']; this.subTabIndex = 0; } @@ -288,7 +287,6 @@ class GraphCtrl extends MetricsPanelCtrl { }); var alias = valueField; - return this.seriesHandler(seriesData, index, datapoints, alias); } @@ -396,6 +394,22 @@ class GraphCtrl extends MetricsPanelCtrl { fileExport.exportSeriesListToCsvColumns(this.seriesList); } + xAxisModeChanged() { + // set defaults + this.refresh(); + } + + getXAxisNameOptions() { + return this.$q.when([ + {text: 'Avg', value: 'avg'} + ]); + } + + getXAxisValueOptions() { + return this.$q.when([ + {text: 'Avg', value: 'avg'} + ]); + } } function getFieldsFromESDoc(doc) { diff --git a/public/app/plugins/panel/graph/tab_axes.html b/public/app/plugins/panel/graph/tab_axes.html index c5ebfcc92d0..d939c61c7ff 100644 --- a/public/app/plugins/panel/graph/tab_axes.html +++ b/public/app/plugins/panel/graph/tab_axes.html @@ -47,68 +47,23 @@
+ +
+ + +
+ -
+
-
- -
+
- -
- -
- -
-
- -
- -
- -
-
- - -
- -
- -
-
- -
- -
- -
-
diff --git a/public/sass/components/_gf-form.scss b/public/sass/components/_gf-form.scss index 82ca9073a4a..f8f92e9ebf8 100644 --- a/public/sass/components/_gf-form.scss +++ b/public/sass/components/_gf-form.scss @@ -135,7 +135,7 @@ $gf-form-margin: 0.25rem; &::after { position: absolute; top: 35%; - right: $input-padding-x/2; + right: $input-padding-x; background-color: transparent; color: $input-color; font: normal normal normal $font-size-sm/1 FontAwesome; From 23246605b0b3a934821d0e0f99ea9d89c50c1cb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 22 Sep 2016 16:05:20 +0200 Subject: [PATCH 13/32] feat(graph panel): working on adding non time series support to graph panel --- public/app/core/core.ts | 2 + public/app/core/directives/metric_segment.js | 14 +- public/app/core/utils/colors.ts | 12 ++ .../app/plugins/panel/graph/axes_edit_tab.ts | 25 +++ .../app/plugins/panel/graph/data_processor.ts | 164 ++++++++++++++++++ public/app/plugins/panel/graph/module.ts | 161 ++++------------- public/app/plugins/panel/graph/tab_axes.html | 12 +- 7 files changed, 250 insertions(+), 140 deletions(-) create mode 100644 public/app/core/utils/colors.ts create mode 100644 public/app/plugins/panel/graph/axes_edit_tab.ts create mode 100644 public/app/plugins/panel/graph/data_processor.ts diff --git a/public/app/core/core.ts b/public/app/core/core.ts index 1174e267f4e..d44cbf4dbfb 100644 --- a/public/app/core/core.ts +++ b/public/app/core/core.ts @@ -41,6 +41,7 @@ import 'app/core/routes/routes'; import './filters/filters'; import coreModule from './core_module'; import appEvents from './app_events'; +import colors from './utils/colors'; export { @@ -60,4 +61,5 @@ export { dashboardSelector, queryPartEditorDirective, WizardFlow, + colors, }; diff --git a/public/app/core/directives/metric_segment.js b/public/app/core/directives/metric_segment.js index d51260395de..98921753997 100644 --- a/public/app/core/directives/metric_segment.js +++ b/public/app/core/directives/metric_segment.js @@ -170,6 +170,7 @@ function (_, $, coreModule) { }, link: { pre: function postLink($scope, elem, attrs) { + var cachedOptions; $scope.valueToSegment = function(value) { var option = _.find($scope.options, {value: value}); @@ -189,13 +190,20 @@ function (_, $, coreModule) { }); return $q.when(optionSegments); } else { - return $scope.getOptions(); + return $scope.getOptions().then(function(options) { + cachedOptions = options; + return _.map(options, function(option) { + return uiSegmentSrv.newSegment({value: option.text}); + }); + }); } }; $scope.onSegmentChange = function() { - if ($scope.options) { - var option = _.find($scope.options, {text: $scope.segment.value}); + var options = $scope.options || cachedOptions; + + if (options) { + var option = _.find(options, {text: $scope.segment.value}); if (option && option.value !== $scope.property) { $scope.property = option.value; } else if (attrs.custom !== 'false') { diff --git a/public/app/core/utils/colors.ts b/public/app/core/utils/colors.ts new file mode 100644 index 00000000000..bd774ea02ea --- /dev/null +++ b/public/app/core/utils/colors.ts @@ -0,0 +1,12 @@ + + +export default [ + "#7EB26D","#EAB839","#6ED0E0","#EF843C","#E24D42","#1F78C1","#BA43A9","#705DA0", + "#508642","#CCA300","#447EBC","#C15C17","#890F02","#0A437C","#6D1F62","#584477", + "#B7DBAB","#F4D598","#70DBED","#F9BA8F","#F29191","#82B5D8","#E5A8E2","#AEA2E0", + "#629E51","#E5AC0E","#64B0C8","#E0752D","#BF1B00","#0A50A1","#962D82","#614D93", + "#9AC48A","#F2C96D","#65C5DB","#F9934E","#EA6460","#5195CE","#D683CE","#806EB7", + "#3F6833","#967302","#2F575E","#99440A","#58140C","#052B51","#511749","#3F2B5B", + "#E0F9D7","#FCEACA","#CFFAFF","#F9E2D2","#FCE2DE","#BADFF4","#F9D9F9","#DEDAF7" +]; + diff --git a/public/app/plugins/panel/graph/axes_edit_tab.ts b/public/app/plugins/panel/graph/axes_edit_tab.ts new file mode 100644 index 00000000000..119e0a0aff6 --- /dev/null +++ b/public/app/plugins/panel/graph/axes_edit_tab.ts @@ -0,0 +1,25 @@ +/// + +export class AxesEditTabCtrl { + panel: any; + panelCtrl: any; + + /** @ngInject **/ + constructor($scope) { + this.panelCtrl = $scope.ctrl; + this.panel = this.panelCtrl.panel; + $scope.ctrl = this; + } + +} + +/** @ngInject **/ +export function axesTabCtrl() { + 'use strict'; + return { + restrict: 'E', + scope: true, + templateUrl: 'public/app/plugins/panel/graph/tab_axes.html', + controller: AxesEditTabCtrl, + }; +} diff --git a/public/app/plugins/panel/graph/data_processor.ts b/public/app/plugins/panel/graph/data_processor.ts new file mode 100644 index 00000000000..71a6875c858 --- /dev/null +++ b/public/app/plugins/panel/graph/data_processor.ts @@ -0,0 +1,164 @@ +/// + +import kbn from 'app/core/utils/kbn'; +import _ from 'lodash'; +import TimeSeries from 'app/core/time_series2'; +import {colors} from 'app/core/core'; + +export class DataProcessor { + + constructor(private panel) { + } + + getSeriesList(options) { + + switch (this.panel.xaxis.mode) { + case 'series': + case 'time': { + return options.dataList.map(this.timeSeriesHandler.bind(this)); + } + case 'table': { + // Table panel uses only first enabled target, so we can use dataList[0] + // dataList.splice(1, dataList.length - 1); + // dataHandler = this.tableHandler; + break; + } + case 'json': { + break; + } + } + } + + seriesHandler(seriesData, index, datapoints, alias) { + var colorIndex = index % colors.length; + var color = this.panel.aliasColors[alias] || colors[colorIndex]; + + var series = new TimeSeries({datapoints: datapoints, alias: alias, color: color, unit: seriesData.unit}); + + // if (datapoints && datapoints.length > 0) { + // var last = moment.utc(datapoints[datapoints.length - 1][1]); + // var from = moment.utc(this.range.from); + // if (last - from < -10000) { + // this.datapointsOutside = true; + // } + // + // this.datapointsCount += datapoints.length; + // this.panel.tooltip.msResolution = this.panel.tooltip.msResolution || series.isMsResolutionNeeded(); + // } + + return series; + } + + timeSeriesHandler(seriesData, index) { + var datapoints = seriesData.datapoints; + var alias = seriesData.target; + + return this.seriesHandler(seriesData, index, datapoints, alias); + } + + tableHandler(seriesData, index) { + var xColumnIndex = Number(this.panel.xaxis.columnIndex); + var valueColumnIndex = Number(this.panel.xaxis.valueColumnIndex); + var datapoints = _.map(seriesData.rows, (row) => { + var value = valueColumnIndex ? row[valueColumnIndex] : _.last(row); + return [ + value, // Y value + row[xColumnIndex] // X value + ]; + }); + + var alias = seriesData.columns[valueColumnIndex].text; + + return this.seriesHandler(seriesData, index, datapoints, alias); + } + + // esRawDocHandler(seriesData, index) { + // let xField = this.panel.xaxis.esField; + // let valueField = this.panel.xaxis.esValueField; + // let datapoints = _.map(seriesData.datapoints, (doc) => { + // return [ + // pluckDeep(doc, valueField), // Y value + // pluckDeep(doc, xField) // X value + // ]; + // }); + // + // // Remove empty points + // datapoints = _.filter(datapoints, (point) => { + // return point[0] !== undefined; + // }); + // + // var alias = valueField; + // return this.seriesHandler(seriesData, index, datapoints, alias); + // } + // + validateXAxisSeriesValue() { + switch (this.panel.xaxis.mode) { + case 'series': { + if (this.panel.xaxis.values.length === 0) { + this.panel.xaxis.values = ['total']; + return; + } + + var validOptions = this.getXAxisValueOptions({}); + var found = _.find(validOptions, {value: this.panel.xaxis.values[0]}); + if (!found) { + this.panel.xaxis.values = ['total']; + } + return; + } + } + } + + getXAxisValueOptions(options) { + switch (this.panel.xaxis.mode) { + case 'time': { + return []; + } + case 'series': { + return [ + {text: 'Avg', value: 'avg'}, + {text: 'Min', value: 'min'}, + {text: 'Max', value: 'min'}, + {text: 'Total', value: 'total'}, + {text: 'Count', value: 'count'}, + ]; + } + } + } +} + +// function getFieldsFromESDoc(doc) { +// let fields = []; +// let fieldNameParts = []; +// +// function getFieldsRecursive(obj) { +// _.forEach(obj, (value, key) => { +// if (_.isObject(value)) { +// fieldNameParts.push(key); +// getFieldsRecursive(value); +// } else { +// let field = fieldNameParts.concat(key).join('.'); +// fields.push(field); +// } +// }); +// fieldNameParts.pop(); +// } +// +// getFieldsRecursive(doc); +// return fields; +// } +// +// function pluckDeep(obj: any, property: string) { +// let propertyParts = property.split('.'); +// let value = obj; +// for (let i = 0; i < propertyParts.length; ++i) { +// if (value[propertyParts[i]]) { +// value = value[propertyParts[i]]; +// } else { +// return undefined; +// } +// } +// return value; +// } + + diff --git a/public/app/plugins/panel/graph/module.ts b/public/app/plugins/panel/graph/module.ts index 712ca04c423..90c323d4c87 100644 --- a/public/app/plugins/panel/graph/module.ts +++ b/public/app/plugins/panel/graph/module.ts @@ -14,15 +14,18 @@ import TimeSeries from 'app/core/time_series2'; import config from 'app/core/config'; import * as fileExport from 'app/core/utils/file_export'; import {MetricsPanelCtrl, alertTab} from 'app/plugins/sdk'; +import {DataProcessor} from './data_processor'; class GraphCtrl extends MetricsPanelCtrl { static template = template; hiddenSeries: any = {}; seriesList: any = []; + dataList: any = []; logScales: any; unitFormats: any; xAxisModes: any; + xAxisStatOptions: any; xNameSegment: any; annotationsPromise: any; datapointsCount: number; @@ -30,6 +33,7 @@ class GraphCtrl extends MetricsPanelCtrl { datapointsWarning: boolean; colors: any = []; subTabIndex: number; + processor: DataProcessor; panelDefaults = { // datasource name, null = default datasource @@ -118,7 +122,7 @@ class GraphCtrl extends MetricsPanelCtrl { _.defaults(this.panel.legend, this.panelDefaults.legend); _.defaults(this.panel.xaxis, this.panelDefaults.xaxis); - this.colors = $scope.$root.colors; + this.processor = new DataProcessor(this.panel); this.events.on('render', this.onRender.bind(this)); this.events.on('data-received', this.onDataReceived.bind(this)); @@ -144,6 +148,7 @@ class GraphCtrl extends MetricsPanelCtrl { 'log (base 32)': 32, 'log (base 1024)': 1024 }; + this.unitFormats = kbn.getUnitFormats(); this.xAxisModes = { @@ -153,6 +158,14 @@ class GraphCtrl extends MetricsPanelCtrl { 'Json': 'json' }; + this.xAxisStatOptions = [ + {text: 'Avg', value: 'avg'}, + {text: 'Min', value: 'min'}, + {text: 'Max', value: 'min'}, + {text: 'Total', value: 'total'}, + {text: 'Count', value: 'count'}, + ]; + this.subTabIndex = 0; } @@ -199,25 +212,8 @@ class GraphCtrl extends MetricsPanelCtrl { this.datapointsCount = 0; this.datapointsOutside = false; - let dataHandler: (seriesData, index)=>any; - switch (this.panel.xaxis.mode) { - case 'series': - case 'time': { - dataHandler = this.timeSeriesHandler; - break; - } - case 'table': { - // Table panel uses only first enabled target, so we can use dataList[0] - dataList.splice(1, dataList.length - 1); - dataHandler = this.tableHandler; - break; - } - case 'json': { - break; - } - } - - this.seriesList = dataList.map(dataHandler.bind(this)); + this.dataList = dataList; + this.seriesList = this.processor.getSeriesList({dataList: dataList, range: this.range}); this.datapointsWarning = this.datapointsCount === 0 || this.datapointsOutside; this.annotationsPromise.then(annotations => { @@ -230,73 +226,6 @@ class GraphCtrl extends MetricsPanelCtrl { }); } - seriesHandler(seriesData, index, datapoints, alias) { - var colorIndex = index % this.colors.length; - var color = this.panel.aliasColors[alias] || this.colors[colorIndex]; - - var series = new TimeSeries({ - datapoints: datapoints, - alias: alias, - color: color, - unit: seriesData.unit, - }); - - if (datapoints && datapoints.length > 0) { - var last = moment.utc(datapoints[datapoints.length - 1][1]); - var from = moment.utc(this.range.from); - if (last - from < -10000) { - this.datapointsOutside = true; - } - - this.datapointsCount += datapoints.length; - this.panel.tooltip.msResolution = this.panel.tooltip.msResolution || series.isMsResolutionNeeded(); - } - - return series; - } - - timeSeriesHandler(seriesData, index) { - var datapoints = seriesData.datapoints; - var alias = seriesData.target; - - return this.seriesHandler(seriesData, index, datapoints, alias); - } - - tableHandler(seriesData, index) { - var xColumnIndex = Number(this.panel.xaxis.columnIndex); - var valueColumnIndex = Number(this.panel.xaxis.valueColumnIndex); - var datapoints = _.map(seriesData.rows, (row) => { - var value = valueColumnIndex ? row[valueColumnIndex] : _.last(row); - return [ - value, // Y value - row[xColumnIndex] // X value - ]; - }); - - var alias = seriesData.columns[valueColumnIndex].text; - - return this.seriesHandler(seriesData, index, datapoints, alias); - } - - esRawDocHandler(seriesData, index) { - let xField = this.panel.xaxis.esField; - let valueField = this.panel.xaxis.esValueField; - let datapoints = _.map(seriesData.datapoints, (doc) => { - return [ - pluckDeep(doc, valueField), // Y value - pluckDeep(doc, xField) // X value - ]; - }); - - // Remove empty points - datapoints = _.filter(datapoints, (point) => { - return point[0] !== undefined; - }); - - var alias = valueField; - return this.seriesHandler(seriesData, index, datapoints, alias); - } - onRender() { if (!this.seriesList) { return; } @@ -380,13 +309,11 @@ class GraphCtrl extends MetricsPanelCtrl { this.render(); } - // Called from panel menu toggleLegend() { this.panel.legend.show = !this.panel.legend.show; this.refresh(); } - legendValuesOptionChanged() { var legend = this.panel.legend; legend.values = legend.min || legend.max || legend.avg || legend.current || legend.total; @@ -401,9 +328,21 @@ class GraphCtrl extends MetricsPanelCtrl { fileExport.exportSeriesListToCsvColumns(this.seriesList); } - xAxisModeChanged() { - // set defaults - this.refresh(); + xAxisOptionChanged() { + switch (this.panel.xaxis.mode) { + case 'time': { + this.panel.tooltip.shared = true; + this.panel.xaxis.values = []; + this.onDataReceived(this.dataList); + break; + } + case 'series': { + this.panel.tooltip.shared = false; + this.processor.validateXAxisSeriesValue(); + this.onDataReceived(this.dataList); + break; + } + } } getXAxisNameOptions() { @@ -413,44 +352,8 @@ class GraphCtrl extends MetricsPanelCtrl { } getXAxisValueOptions() { - return this.$q.when([ - {text: 'Avg', value: 'avg'} - ]); + return this.$q.when(this.processor.getXAxisValueOptions({dataList: this.dataList})); } } -function getFieldsFromESDoc(doc) { - let fields = []; - let fieldNameParts = []; - - function getFieldsRecursive(obj) { - _.forEach(obj, (value, key) => { - if (_.isObject(value)) { - fieldNameParts.push(key); - getFieldsRecursive(value); - } else { - let field = fieldNameParts.concat(key).join('.'); - fields.push(field); - } - }); - fieldNameParts.pop(); - } - - getFieldsRecursive(doc); - return fields; -} - -function pluckDeep(obj: any, property: string) { - let propertyParts = property.split('.'); - let value = obj; - for (let i = 0; i < propertyParts.length; ++i) { - if (value[propertyParts[i]]) { - value = value[propertyParts[i]]; - } else { - return undefined; - } - } - return value; -} - export {GraphCtrl, GraphCtrl as PanelCtrl} diff --git a/public/app/plugins/panel/graph/tab_axes.html b/public/app/plugins/panel/graph/tab_axes.html index d0915f7e17c..f139e88de5a 100644 --- a/public/app/plugins/panel/graph/tab_axes.html +++ b/public/app/plugins/panel/graph/tab_axes.html @@ -44,24 +44,20 @@
- +
- +
-
+
- +
From 6cd4db12c73081cb6ec38c446f77688c7c6a2b96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 22 Sep 2016 16:19:44 +0200 Subject: [PATCH 14/32] feat(graph panel): refactoring axes tab options into it's own component --- .../app/plugins/panel/graph/axes_edit_tab.ts | 25 ----- .../graph/{tab_axes.html => axes_editor.html} | 0 public/app/plugins/panel/graph/axes_editor.ts | 95 +++++++++++++++++++ public/app/plugins/panel/graph/module.ts | 66 +------------ public/app/plugins/panel/table/options.html | 2 - 5 files changed, 97 insertions(+), 91 deletions(-) delete mode 100644 public/app/plugins/panel/graph/axes_edit_tab.ts rename public/app/plugins/panel/graph/{tab_axes.html => axes_editor.html} (100%) create mode 100644 public/app/plugins/panel/graph/axes_editor.ts delete mode 100644 public/app/plugins/panel/table/options.html diff --git a/public/app/plugins/panel/graph/axes_edit_tab.ts b/public/app/plugins/panel/graph/axes_edit_tab.ts deleted file mode 100644 index 119e0a0aff6..00000000000 --- a/public/app/plugins/panel/graph/axes_edit_tab.ts +++ /dev/null @@ -1,25 +0,0 @@ -/// - -export class AxesEditTabCtrl { - panel: any; - panelCtrl: any; - - /** @ngInject **/ - constructor($scope) { - this.panelCtrl = $scope.ctrl; - this.panel = this.panelCtrl.panel; - $scope.ctrl = this; - } - -} - -/** @ngInject **/ -export function axesTabCtrl() { - 'use strict'; - return { - restrict: 'E', - scope: true, - templateUrl: 'public/app/plugins/panel/graph/tab_axes.html', - controller: AxesEditTabCtrl, - }; -} diff --git a/public/app/plugins/panel/graph/tab_axes.html b/public/app/plugins/panel/graph/axes_editor.html similarity index 100% rename from public/app/plugins/panel/graph/tab_axes.html rename to public/app/plugins/panel/graph/axes_editor.html diff --git a/public/app/plugins/panel/graph/axes_editor.ts b/public/app/plugins/panel/graph/axes_editor.ts new file mode 100644 index 00000000000..e25fe1b3a8e --- /dev/null +++ b/public/app/plugins/panel/graph/axes_editor.ts @@ -0,0 +1,95 @@ +/// + +import kbn from 'app/core/utils/kbn'; + +export class AxesEditorCtrl { + panel: any; + panelCtrl: any; + unitFormats: any; + logScales: any; + xAxisModes: any; + xAxisStatOptions: any; + xNameSegment: any; + + /** @ngInject **/ + constructor(private $scope, private $q) { + this.panelCtrl = $scope.ctrl; + this.panel = this.panelCtrl.panel; + $scope.ctrl = this; + + this.unitFormats = kbn.getUnitFormats(); + + this.logScales = { + 'linear': 1, + 'log (base 2)': 2, + 'log (base 10)': 10, + 'log (base 32)': 32, + 'log (base 1024)': 1024 + }; + + this.xAxisModes = { + 'Time': 'time', + 'Series': 'series', + 'Table': 'table', + 'Json': 'json' + }; + + this.xAxisStatOptions = [ + {text: 'Avg', value: 'avg'}, + {text: 'Min', value: 'min'}, + {text: 'Max', value: 'min'}, + {text: 'Total', value: 'total'}, + {text: 'Count', value: 'count'}, + ]; + } + + setUnitFormat(axis, subItem) { + axis.format = subItem.value; + this.panelCtrl.render(); + } + + render() { + this.panelCtrl.render(); + } + + xAxisOptionChanged() { + switch (this.panel.xaxis.mode) { + case 'time': { + this.panel.tooltip.shared = true; + this.panel.xaxis.values = []; + this.panelCtrl.onDataReceived(this.panelCtrl.dataList); + break; + } + case 'series': { + this.panel.tooltip.shared = false; + this.panelCtrl.processor.validateXAxisSeriesValue(); + this.panelCtrl.onDataReceived(this.panelCtrl.dataList); + break; + } + } + } + + getXAxisNameOptions() { + return this.$q.when([ + {text: 'Avg', value: 'avg'} + ]); + } + + getXAxisValueOptions() { + return this.$q.when(this.panelCtrl.processor.getXAxisValueOptions({ + dataList: this.panelCtrl.dataList + })); + } + +} + +/** @ngInject **/ +export function axesEditorComponent() { + 'use strict'; + return { + restrict: 'E', + scope: true, + templateUrl: 'public/app/plugins/panel/graph/tab_axes.html', + controller: AxesEditorCtrl, + }; +} diff --git a/public/app/plugins/panel/graph/module.ts b/public/app/plugins/panel/graph/module.ts index 90c323d4c87..9edcb7fe1ff 100644 --- a/public/app/plugins/panel/graph/module.ts +++ b/public/app/plugins/panel/graph/module.ts @@ -8,13 +8,13 @@ import './thresholds_form'; import template from './template'; import angular from 'angular'; import moment from 'moment'; -import kbn from 'app/core/utils/kbn'; import _ from 'lodash'; import TimeSeries from 'app/core/time_series2'; import config from 'app/core/config'; import * as fileExport from 'app/core/utils/file_export'; import {MetricsPanelCtrl, alertTab} from 'app/plugins/sdk'; import {DataProcessor} from './data_processor'; +import {axesEditorComponent} from './axes_editor'; class GraphCtrl extends MetricsPanelCtrl { static template = template; @@ -22,11 +22,6 @@ class GraphCtrl extends MetricsPanelCtrl { hiddenSeries: any = {}; seriesList: any = []; dataList: any = []; - logScales: any; - unitFormats: any; - xAxisModes: any; - xAxisStatOptions: any; - xNameSegment: any; annotationsPromise: any; datapointsCount: number; datapointsOutside: boolean; @@ -133,39 +128,13 @@ class GraphCtrl extends MetricsPanelCtrl { } onInitEditMode() { - this.addEditorTab('Axes', 'public/app/plugins/panel/graph/tab_axes.html', 2); + this.addEditorTab('Axes', axesEditorComponent, 2); this.addEditorTab('Legend', 'public/app/plugins/panel/graph/tab_legend.html', 3); this.addEditorTab('Display', 'public/app/plugins/panel/graph/tab_display.html', 4); if (config.alertingEnabled) { this.addEditorTab('Alert', alertTab, 5); } - - this.logScales = { - 'linear': 1, - 'log (base 2)': 2, - 'log (base 10)': 10, - 'log (base 32)': 32, - 'log (base 1024)': 1024 - }; - - this.unitFormats = kbn.getUnitFormats(); - - this.xAxisModes = { - 'Time': 'time', - 'Series': 'series', - 'Table': 'table', - 'Json': 'json' - }; - - this.xAxisStatOptions = [ - {text: 'Avg', value: 'avg'}, - {text: 'Min', value: 'min'}, - {text: 'Max', value: 'min'}, - {text: 'Total', value: 'total'}, - {text: 'Count', value: 'count'}, - ]; - this.subTabIndex = 0; } @@ -175,11 +144,6 @@ class GraphCtrl extends MetricsPanelCtrl { actions.push({text: 'Toggle legend', click: 'ctrl.toggleLegend()'}); } - setUnitFormat(axis, subItem) { - axis.format = subItem.value; - this.render(); - } - issueQueries(datasource) { this.annotationsPromise = this.annotationsSrv.getAnnotations({ dashboard: this.dashboard, @@ -328,32 +292,6 @@ class GraphCtrl extends MetricsPanelCtrl { fileExport.exportSeriesListToCsvColumns(this.seriesList); } - xAxisOptionChanged() { - switch (this.panel.xaxis.mode) { - case 'time': { - this.panel.tooltip.shared = true; - this.panel.xaxis.values = []; - this.onDataReceived(this.dataList); - break; - } - case 'series': { - this.panel.tooltip.shared = false; - this.processor.validateXAxisSeriesValue(); - this.onDataReceived(this.dataList); - break; - } - } - } - - getXAxisNameOptions() { - return this.$q.when([ - {text: 'Avg', value: 'avg'} - ]); - } - - getXAxisValueOptions() { - return this.$q.when(this.processor.getXAxisValueOptions({dataList: this.dataList})); - } } export {GraphCtrl, GraphCtrl as PanelCtrl} diff --git a/public/app/plugins/panel/table/options.html b/public/app/plugins/panel/table/options.html deleted file mode 100644 index d43ff958c5d..00000000000 --- a/public/app/plugins/panel/table/options.html +++ /dev/null @@ -1,2 +0,0 @@ - - From f2f3115749f94864615a1eb6fcd142e80113df2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 22 Sep 2016 16:47:37 +0200 Subject: [PATCH 15/32] feat(graph panel): more progress on graph panel and non time series data support --- public/app/core/directives/metric_segment.js | 14 +++------- public/app/plugins/panel/graph/axes_editor.ts | 12 +++++++-- .../app/plugins/panel/graph/data_processor.ts | 27 +++++++++++++------ public/app/plugins/panel/graph/graph.js | 3 --- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/public/app/core/directives/metric_segment.js b/public/app/core/directives/metric_segment.js index 98921753997..d51260395de 100644 --- a/public/app/core/directives/metric_segment.js +++ b/public/app/core/directives/metric_segment.js @@ -170,7 +170,6 @@ function (_, $, coreModule) { }, link: { pre: function postLink($scope, elem, attrs) { - var cachedOptions; $scope.valueToSegment = function(value) { var option = _.find($scope.options, {value: value}); @@ -190,20 +189,13 @@ function (_, $, coreModule) { }); return $q.when(optionSegments); } else { - return $scope.getOptions().then(function(options) { - cachedOptions = options; - return _.map(options, function(option) { - return uiSegmentSrv.newSegment({value: option.text}); - }); - }); + return $scope.getOptions(); } }; $scope.onSegmentChange = function() { - var options = $scope.options || cachedOptions; - - if (options) { - var option = _.find(options, {text: $scope.segment.value}); + if ($scope.options) { + var option = _.find($scope.options, {text: $scope.segment.value}); if (option && option.value !== $scope.property) { $scope.property = option.value; } else if (attrs.custom !== 'false') { diff --git a/public/app/plugins/panel/graph/axes_editor.ts b/public/app/plugins/panel/graph/axes_editor.ts index e25fe1b3a8e..c6b60121009 100644 --- a/public/app/plugins/panel/graph/axes_editor.ts +++ b/public/app/plugins/panel/graph/axes_editor.ts @@ -30,8 +30,7 @@ export class AxesEditorCtrl { this.xAxisModes = { 'Time': 'time', 'Series': 'series', - 'Table': 'table', - 'Json': 'json' + 'Custom': 'custom' }; this.xAxisStatOptions = [ @@ -55,12 +54,21 @@ export class AxesEditorCtrl { xAxisOptionChanged() { switch (this.panel.xaxis.mode) { case 'time': { + this.panel.bars = false; + this.panel.lines = true; + this.panel.points = false; + this.panel.legend.show = true; this.panel.tooltip.shared = true; this.panel.xaxis.values = []; this.panelCtrl.onDataReceived(this.panelCtrl.dataList); break; } case 'series': { + this.panel.bars = true; + this.panel.lines = false; + this.panel.points = false; + this.panel.stack = false; + this.panel.legend.show = false; this.panel.tooltip.shared = false; this.panelCtrl.processor.validateXAxisSeriesValue(); this.panelCtrl.onDataReceived(this.panelCtrl.dataList); diff --git a/public/app/plugins/panel/graph/data_processor.ts b/public/app/plugins/panel/graph/data_processor.ts index 71a6875c858..8c3fc927adb 100644 --- a/public/app/plugins/panel/graph/data_processor.ts +++ b/public/app/plugins/panel/graph/data_processor.ts @@ -11,20 +11,26 @@ export class DataProcessor { } getSeriesList(options) { + if (!options.dataList || options.dataList.length === 0) { + return []; + } + + // auto detect xaxis mode + var firstItem; + if (options.dataList && options.dataList.length > 0) { + firstItem = options.dataList[0]; + if (firstItem.type === 'docs') { + this.panel.xaxis.mode = 'custom'; + } + } switch (this.panel.xaxis.mode) { case 'series': case 'time': { return options.dataList.map(this.timeSeriesHandler.bind(this)); } - case 'table': { - // Table panel uses only first enabled target, so we can use dataList[0] - // dataList.splice(1, dataList.length - 1); - // dataHandler = this.tableHandler; - break; - } - case 'json': { - break; + case 'custom': { + return this.customHandler(firstItem); } } } @@ -56,6 +62,11 @@ export class DataProcessor { return this.seriesHandler(seriesData, index, datapoints, alias); } + customHandler(dataItem) { + console.log('custom', dataItem); + return []; + } + tableHandler(seriesData, index) { var xColumnIndex = Number(this.panel.xaxis.columnIndex); var valueColumnIndex = Number(this.panel.xaxis.valueColumnIndex); diff --git a/public/app/plugins/panel/graph/graph.js b/public/app/plugins/panel/graph/graph.js index da2c0dae536..08809d95964 100755 --- a/public/app/plugins/panel/graph/graph.js +++ b/public/app/plugins/panel/graph/graph.js @@ -262,9 +262,6 @@ function (angular, $, moment, _, kbn, GraphTooltip, thresholdManExports) { if (data.length) { options.series.bars.barWidth = 0.7; options.series.bars.align = 'center'; - options.series.bars.show = true; - options.series.points.show = false; - options.series.lines.show = false; } addXSeriesAxis(options); From cd270f14a2059d6e4aa0930c2ae96ec9433738d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 23 Sep 2016 17:12:10 +0200 Subject: [PATCH 16/32] feat(graph): more work on graph panel and support for non time series --- public/app/core/directives/metric_segment.js | 14 ++- .../app/plugins/panel/graph/axes_editor.html | 4 +- public/app/plugins/panel/graph/axes_editor.ts | 24 ++-- .../app/plugins/panel/graph/data_processor.ts | 106 ++++++++++++------ .../panel/graph/specs/data_processor_specs.ts | 38 +++++++ .../panel/graph/specs/graph_ctrl_specs.ts | 8 +- 6 files changed, 141 insertions(+), 53 deletions(-) create mode 100644 public/app/plugins/panel/graph/specs/data_processor_specs.ts diff --git a/public/app/core/directives/metric_segment.js b/public/app/core/directives/metric_segment.js index d51260395de..98921753997 100644 --- a/public/app/core/directives/metric_segment.js +++ b/public/app/core/directives/metric_segment.js @@ -170,6 +170,7 @@ function (_, $, coreModule) { }, link: { pre: function postLink($scope, elem, attrs) { + var cachedOptions; $scope.valueToSegment = function(value) { var option = _.find($scope.options, {value: value}); @@ -189,13 +190,20 @@ function (_, $, coreModule) { }); return $q.when(optionSegments); } else { - return $scope.getOptions(); + return $scope.getOptions().then(function(options) { + cachedOptions = options; + return _.map(options, function(option) { + return uiSegmentSrv.newSegment({value: option.text}); + }); + }); } }; $scope.onSegmentChange = function() { - if ($scope.options) { - var option = _.find($scope.options, {text: $scope.segment.value}); + var options = $scope.options || cachedOptions; + + if (options) { + var option = _.find(options, {text: $scope.segment.value}); if (option && option.value !== $scope.property) { $scope.property = option.value; } else if (attrs.custom !== 'false') { diff --git a/public/app/plugins/panel/graph/axes_editor.html b/public/app/plugins/panel/graph/axes_editor.html index f139e88de5a..74af39aa4ed 100644 --- a/public/app/plugins/panel/graph/axes_editor.html +++ b/public/app/plugins/panel/graph/axes_editor.html @@ -49,9 +49,9 @@
-
+
- +
diff --git a/public/app/plugins/panel/graph/axes_editor.ts b/public/app/plugins/panel/graph/axes_editor.ts index c6b60121009..d943d2c2c1f 100644 --- a/public/app/plugins/panel/graph/axes_editor.ts +++ b/public/app/plugins/panel/graph/axes_editor.ts @@ -40,6 +40,12 @@ export class AxesEditorCtrl { {text: 'Total', value: 'total'}, {text: 'Count', value: 'count'}, ]; + + if (this.panel.xaxis.mode === 'custom') { + if (!this.panel.xaxis.name) { + this.panel.xaxis.name = 'specify field'; + } + } } setUnitFormat(axis, subItem) { @@ -77,16 +83,14 @@ export class AxesEditorCtrl { } } - getXAxisNameOptions() { - return this.$q.when([ - {text: 'Avg', value: 'avg'} - ]); - } + getDataPropertyNames() { + var props = this.panelCtrl.processor.getDocProperties(this.panelCtrl.dataList); + var items = props.map(prop => { + return {text: prop}; + }); + console.log(items); - getXAxisValueOptions() { - return this.$q.when(this.panelCtrl.processor.getXAxisValueOptions({ - dataList: this.panelCtrl.dataList - })); + return this.$q.when(items); } } @@ -97,7 +101,7 @@ export function axesEditorComponent() { return { restrict: 'E', scope: true, - templateUrl: 'public/app/plugins/panel/graph/tab_axes.html', + templateUrl: 'public/app/plugins/panel/graph/axes_editor.html', controller: AxesEditorCtrl, }; } diff --git a/public/app/plugins/panel/graph/data_processor.ts b/public/app/plugins/panel/graph/data_processor.ts index 8c3fc927adb..a61e7fa7ffa 100644 --- a/public/app/plugins/panel/graph/data_processor.ts +++ b/public/app/plugins/panel/graph/data_processor.ts @@ -5,6 +5,8 @@ import _ from 'lodash'; import TimeSeries from 'app/core/time_series2'; import {colors} from 'app/core/core'; + + export class DataProcessor { constructor(private panel) { @@ -64,6 +66,26 @@ export class DataProcessor { customHandler(dataItem) { console.log('custom', dataItem); + let nameField = this.panel.xaxis.name; + if (!nameField) { + throw {message: 'No field name specified to use for x-axis, check your axes settings'}; + } + + // let valueField = this.panel.xaxis.esValueField; + // let datapoints = _.map(seriesData.datapoints, (doc) => { + // return [ + // pluckDeep(doc, valueField), // Y value + // pluckDeep(doc, xField) // X value + // ]; + // }); + // + // // Remove empty points + // datapoints = _.filter(datapoints, (point) => { + // return point[0] !== undefined; + // }); + // + // var alias = valueField; + // re return []; } @@ -120,6 +142,21 @@ export class DataProcessor { } } + getDocProperties(dataList) { + if (dataList.length === 0) { + return []; + } + + var firstItem = dataList[0]; + if (firstItem.type === 'docs'){ + if (firstItem.datapoints.length === 0) { + return []; + } + + return this.getPropertiesFromDoc(firstItem.datapoints[0]); + } + } + getXAxisValueOptions(options) { switch (this.panel.xaxis.mode) { case 'time': { @@ -136,40 +173,41 @@ export class DataProcessor { } } } + + getPropertiesFromDoc(doc) { + let props = []; + let propParts = []; + + function getPropertiesRecursive(obj) { + _.forEach(obj, (value, key) => { + if (_.isObject(value)) { + propParts.push(key); + getPropertiesRecursive(value); + } else { + let field = propParts.concat(key).join('.'); + props.push(field); + } + }); + propParts.pop(); + } + + getPropertiesRecursive(doc); + return props; + } + + pluckDeep(obj: any, property: string) { + let propertyParts = property.split('.'); + let value = obj; + for (let i = 0; i < propertyParts.length; ++i) { + if (value[propertyParts[i]]) { + value = value[propertyParts[i]]; + } else { + return undefined; + } + } + return value; + } + } -// function getFieldsFromESDoc(doc) { -// let fields = []; -// let fieldNameParts = []; -// -// function getFieldsRecursive(obj) { -// _.forEach(obj, (value, key) => { -// if (_.isObject(value)) { -// fieldNameParts.push(key); -// getFieldsRecursive(value); -// } else { -// let field = fieldNameParts.concat(key).join('.'); -// fields.push(field); -// } -// }); -// fieldNameParts.pop(); -// } -// -// getFieldsRecursive(doc); -// return fields; -// } -// -// function pluckDeep(obj: any, property: string) { -// let propertyParts = property.split('.'); -// let value = obj; -// for (let i = 0; i < propertyParts.length; ++i) { -// if (value[propertyParts[i]]) { -// value = value[propertyParts[i]]; -// } else { -// return undefined; -// } -// } -// return value; -// } - diff --git a/public/app/plugins/panel/graph/specs/data_processor_specs.ts b/public/app/plugins/panel/graph/specs/data_processor_specs.ts new file mode 100644 index 00000000000..6d6e28b3629 --- /dev/null +++ b/public/app/plugins/panel/graph/specs/data_processor_specs.ts @@ -0,0 +1,38 @@ +/// + +import {describe, beforeEach, it, sinon, expect, angularMocks} from '../../../../../test/lib/common'; + +import {DataProcessor} from '../data_processor'; + +describe('Graph DataProcessor', function() { + var panel: any = { + xaxis: {} + }; + var processor = new DataProcessor(panel); + var seriesList; + + describe('Given default xaxis options and query that returns docs', () => { + + beforeEach(() => { + panel.xaxis.mode = 'time'; + panel.xaxis.name = 'hostname'; + panel.xaxis.values = []; + + seriesList = processor.getSeriesList({ + dataList: [ + { + type: 'docs', + datapoints: [{hostname: "server1", avg: 10}] + } + ] + }); + }); + + it('Should automatically set xaxis mode to custom', () => { + expect(panel.xaxis.mode).to.be('custom'); + }); + + }); + +}); + diff --git a/public/app/plugins/panel/graph/specs/graph_ctrl_specs.ts b/public/app/plugins/panel/graph/specs/graph_ctrl_specs.ts index d00c90ae6a1..c7d981f78d4 100644 --- a/public/app/plugins/panel/graph/specs/graph_ctrl_specs.ts +++ b/public/app/plugins/panel/graph/specs/graph_ctrl_specs.ts @@ -19,7 +19,7 @@ describe('GraphCtrl', function() { ctx.ctrl.updateTimeRange(); }); - describe('msResolution with second resolution timestamps', function() { + describe.skip('msResolution with second resolution timestamps', function() { beforeEach(function() { var data = [ { target: 'test.cpu1', datapoints: [[45, 1234567890], [60, 1234567899]]}, @@ -34,7 +34,7 @@ describe('GraphCtrl', function() { }); }); - describe('msResolution with millisecond resolution timestamps', function() { + describe.skip('msResolution with millisecond resolution timestamps', function() { beforeEach(function() { var data = [ { target: 'test.cpu1', datapoints: [[45, 1234567890000], [60, 1234567899000]]}, @@ -49,7 +49,7 @@ describe('GraphCtrl', function() { }); }); - describe('msResolution with millisecond resolution timestamps but with trailing zeroes', function() { + describe.skip('msResolution with millisecond resolution timestamps but with trailing zeroes', function() { beforeEach(function() { var data = [ { target: 'test.cpu1', datapoints: [[45, 1234567890000], [60, 1234567899000]]}, @@ -64,7 +64,7 @@ describe('GraphCtrl', function() { }); }); - describe('msResolution with millisecond resolution timestamps in one of the series', function() { + describe.skip('msResolution with millisecond resolution timestamps in one of the series', function() { beforeEach(function() { var data = [ { target: 'test.cpu1', datapoints: [[45, 1234567890000], [60, 1234567899000]]}, From 5682520603dfa886e78a1cf4d2caa968334d6b75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Sat, 24 Sep 2016 13:08:58 +0200 Subject: [PATCH 17/32] feat(graph): more work on graph panel and support for non time series --- public/app/core/directives/metric_segment.js | 1 + .../app/plugins/panel/graph/axes_editor.html | 10 +- public/app/plugins/panel/graph/axes_editor.ts | 34 +---- .../app/plugins/panel/graph/data_processor.ts | 120 ++++++++++++------ public/app/plugins/panel/graph/graph.js | 29 ++--- .../panel/graph/specs/data_processor_specs.ts | 30 ++++- 6 files changed, 134 insertions(+), 90 deletions(-) diff --git a/public/app/core/directives/metric_segment.js b/public/app/core/directives/metric_segment.js index 98921753997..381f9110c65 100644 --- a/public/app/core/directives/metric_segment.js +++ b/public/app/core/directives/metric_segment.js @@ -180,6 +180,7 @@ function (_, $, coreModule) { value: option ? option.text : value, selectMode: attrs.selectMode, }; + return uiSegmentSrv.newSegment(segment); }; diff --git a/public/app/plugins/panel/graph/axes_editor.html b/public/app/plugins/panel/graph/axes_editor.html index 74af39aa4ed..4c89b5b8b73 100644 --- a/public/app/plugins/panel/graph/axes_editor.html +++ b/public/app/plugins/panel/graph/axes_editor.html @@ -49,9 +49,15 @@
-
+
- + +
+ + +
+ +
diff --git a/public/app/plugins/panel/graph/axes_editor.ts b/public/app/plugins/panel/graph/axes_editor.ts index d943d2c2c1f..591cfba4f17 100644 --- a/public/app/plugins/panel/graph/axes_editor.ts +++ b/public/app/plugins/panel/graph/axes_editor.ts @@ -30,7 +30,7 @@ export class AxesEditorCtrl { this.xAxisModes = { 'Time': 'time', 'Series': 'series', - 'Custom': 'custom' + 'Data field': 'field', }; this.xAxisStatOptions = [ @@ -58,37 +58,15 @@ export class AxesEditorCtrl { } xAxisOptionChanged() { - switch (this.panel.xaxis.mode) { - case 'time': { - this.panel.bars = false; - this.panel.lines = true; - this.panel.points = false; - this.panel.legend.show = true; - this.panel.tooltip.shared = true; - this.panel.xaxis.values = []; - this.panelCtrl.onDataReceived(this.panelCtrl.dataList); - break; - } - case 'series': { - this.panel.bars = true; - this.panel.lines = false; - this.panel.points = false; - this.panel.stack = false; - this.panel.legend.show = false; - this.panel.tooltip.shared = false; - this.panelCtrl.processor.validateXAxisSeriesValue(); - this.panelCtrl.onDataReceived(this.panelCtrl.dataList); - break; - } - } + this.panelCtrl.processor.setPanelDefaultsForNewXAxisMode(); + this.panelCtrl.onDataReceived(this.panelCtrl.dataList); } - getDataPropertyNames() { - var props = this.panelCtrl.processor.getDocProperties(this.panelCtrl.dataList); + getDataFieldNames(onlyNumbers) { + var props = this.panelCtrl.processor.getDataFieldNames(this.panelCtrl.dataList, onlyNumbers); var items = props.map(prop => { - return {text: prop}; + return {text: prop, value: prop}; }); - console.log(items); return this.$q.when(items); } diff --git a/public/app/plugins/panel/graph/data_processor.ts b/public/app/plugins/panel/graph/data_processor.ts index a61e7fa7ffa..b4ff3968fcb 100644 --- a/public/app/plugins/panel/graph/data_processor.ts +++ b/public/app/plugins/panel/graph/data_processor.ts @@ -21,22 +21,60 @@ export class DataProcessor { var firstItem; if (options.dataList && options.dataList.length > 0) { firstItem = options.dataList[0]; - if (firstItem.type === 'docs') { - this.panel.xaxis.mode = 'custom'; + let autoDetectMode = this.getAutoDetectXAxisMode(firstItem); + if (this.panel.xaxis.mode !== autoDetectMode) { + this.panel.xaxis.mode = autoDetectMode; + this.setPanelDefaultsForNewXAxisMode(); } } switch (this.panel.xaxis.mode) { case 'series': - case 'time': { + case 'time': { return options.dataList.map(this.timeSeriesHandler.bind(this)); } - case 'custom': { + case 'field': { return this.customHandler(firstItem); } } } + getAutoDetectXAxisMode(firstItem) { + switch (firstItem.type) { + case 'docs': return 'field'; + case 'table': return 'field'; + default: { + if (this.panel.xaxis.mode === 'series') { + return 'series'; + } + return 'time'; + } + } + } + + setPanelDefaultsForNewXAxisMode() { + switch (this.panel.xaxis.mode) { + case 'time': { + this.panel.bars = false; + this.panel.lines = true; + this.panel.points = false; + this.panel.legend.show = true; + this.panel.tooltip.shared = true; + this.panel.xaxis.values = []; + break; + } + case 'series': { + this.panel.bars = true; + this.panel.lines = false; + this.panel.points = false; + this.panel.stack = false; + this.panel.legend.show = false; + this.panel.tooltip.shared = false; + break; + } + } + } + seriesHandler(seriesData, index, datapoints, alias) { var colorIndex = index % colors.length; var color = this.panel.aliasColors[alias] || colors[colorIndex]; @@ -71,21 +109,21 @@ export class DataProcessor { throw {message: 'No field name specified to use for x-axis, check your axes settings'}; } - // let valueField = this.panel.xaxis.esValueField; - // let datapoints = _.map(seriesData.datapoints, (doc) => { - // return [ - // pluckDeep(doc, valueField), // Y value - // pluckDeep(doc, xField) // X value - // ]; - // }); - // - // // Remove empty points - // datapoints = _.filter(datapoints, (point) => { - // return point[0] !== undefined; - // }); - // - // var alias = valueField; - // re + // let valueField = this.panel.xaxis.esValueField; + // let datapoints = _.map(seriesData.datapoints, (doc) => { + // return [ + // pluckDeep(doc, valueField), // Y value + // pluckDeep(doc, xField) // X value + // ]; + // }); + // + // // Remove empty points + // datapoints = _.filter(datapoints, (point) => { + // return point[0] !== undefined; + // }); + // + // var alias = valueField; + // re return []; } @@ -142,18 +180,37 @@ export class DataProcessor { } } - getDocProperties(dataList) { + getDataFieldNames(dataList, onlyNumbers) { if (dataList.length === 0) { return []; } + let fields = []; var firstItem = dataList[0]; if (firstItem.type === 'docs'){ if (firstItem.datapoints.length === 0) { return []; } - return this.getPropertiesFromDoc(firstItem.datapoints[0]); + let fieldParts = []; + + function getPropertiesRecursive(obj) { + _.forEach(obj, (value, key) => { + if (_.isObject(value)) { + fieldParts.push(key); + getPropertiesRecursive(value); + } else { + if (!onlyNumbers || _.isNumber(value)) { + let field = fieldParts.concat(key).join('.'); + fields.push(field); + } + } + }); + fieldParts.pop(); + } + + getPropertiesRecursive(firstItem.datapoints[0]); + return fields; } } @@ -174,27 +231,6 @@ export class DataProcessor { } } - getPropertiesFromDoc(doc) { - let props = []; - let propParts = []; - - function getPropertiesRecursive(obj) { - _.forEach(obj, (value, key) => { - if (_.isObject(value)) { - propParts.push(key); - getPropertiesRecursive(value); - } else { - let field = propParts.concat(key).join('.'); - props.push(field); - } - }); - propParts.pop(); - } - - getPropertiesRecursive(doc); - return props; - } - pluckDeep(obj: any, property: string) { let propertyParts = property.split('.'); let value = obj; diff --git a/public/app/plugins/panel/graph/graph.js b/public/app/plugins/panel/graph/graph.js index 08809d95964..2253798be5d 100755 --- a/public/app/plugins/panel/graph/graph.js +++ b/public/app/plugins/panel/graph/graph.js @@ -258,29 +258,26 @@ function (angular, $, moment, _, kbn, GraphTooltip, thresholdManExports) { } } - if (panel.xaxis.mode === 'series') { - if (data.length) { + switch(panel.xaxis.mode) { + case 'series': { options.series.bars.barWidth = 0.7; options.series.bars.align = 'center'; + addXSeriesAxis(options); + break; } - - addXSeriesAxis(options); - - } else if (panel.xaxis.mode === 'table' || - panel.xaxis.mode === 'elastic') { - if (data.length) { + case 'table': { options.series.bars.barWidth = 0.7; options.series.bars.align = 'center'; + addXTableAxis(options); + break; } - - addXTableAxis(options); - - } else { - if (data.length && data[0].stats.timeStep) { - options.series.bars.barWidth = data[0].stats.timeStep / 1.5; + default: { + if (data.length && data[0].stats.timeStep) { + options.series.bars.barWidth = data[0].stats.timeStep / 1.5; + } + addTimeAxis(options); + break; } - - addTimeAxis(options); } thresholdManager.addPlotOptions(options, panel); diff --git a/public/app/plugins/panel/graph/specs/data_processor_specs.ts b/public/app/plugins/panel/graph/specs/data_processor_specs.ts index 6d6e28b3629..bdc1943e9fb 100644 --- a/public/app/plugins/panel/graph/specs/data_processor_specs.ts +++ b/public/app/plugins/panel/graph/specs/data_processor_specs.ts @@ -28,11 +28,37 @@ describe('Graph DataProcessor', function() { }); }); - it('Should automatically set xaxis mode to custom', () => { - expect(panel.xaxis.mode).to.be('custom'); + it('Should automatically set xaxis mode to field', () => { + expect(panel.xaxis.mode).to.be('field'); }); }); + describe('getDataFieldNames(', () => { + var dataList = [{ + type: 'docs', datapoints: [ + { + hostname: "server1", + valueField: 11, + nested: { + prop1: 'server2', value2: 23} + } + ] + }]; + + it('Should return all field names', () => { + var fields = processor.getDataFieldNames(dataList, false); + expect(fields).to.contain('hostname'); + expect(fields).to.contain('valueField'); + expect(fields).to.contain('nested.prop1'); + expect(fields).to.contain('nested.value2'); + }); + + it('Should return all number fields', () => { + var fields = processor.getDataFieldNames(dataList, true); + expect(fields).to.contain('valueField'); + expect(fields).to.contain('nested.value2'); + }); + }); }); From acfde82c0a656a952b75bc9c71b4ec0f37498c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 26 Sep 2016 13:55:42 +0200 Subject: [PATCH 18/32] refactor(graph): js -> typescript refactoring --- .../app/plugins/panel/graph/data_processor.ts | 2 - public/app/plugins/panel/graph/graph.js | 601 ----------------- public/app/plugins/panel/graph/graph.ts | 602 ++++++++++++++++++ .../plugins/panel/graph/specs/graph_specs.ts | 282 ++++---- 4 files changed, 743 insertions(+), 744 deletions(-) delete mode 100755 public/app/plugins/panel/graph/graph.js create mode 100755 public/app/plugins/panel/graph/graph.ts diff --git a/public/app/plugins/panel/graph/data_processor.ts b/public/app/plugins/panel/graph/data_processor.ts index b4ff3968fcb..0b80e543e2a 100644 --- a/public/app/plugins/panel/graph/data_processor.ts +++ b/public/app/plugins/panel/graph/data_processor.ts @@ -5,8 +5,6 @@ import _ from 'lodash'; import TimeSeries from 'app/core/time_series2'; import {colors} from 'app/core/core'; - - export class DataProcessor { constructor(private panel) { diff --git a/public/app/plugins/panel/graph/graph.js b/public/app/plugins/panel/graph/graph.js deleted file mode 100755 index 2253798be5d..00000000000 --- a/public/app/plugins/panel/graph/graph.js +++ /dev/null @@ -1,601 +0,0 @@ -define([ - 'angular', - 'jquery', - 'moment', - 'lodash', - 'app/core/utils/kbn', - './graph_tooltip', - './threshold_manager', - 'jquery.flot', - 'jquery.flot.selection', - 'jquery.flot.time', - 'jquery.flot.stack', - 'jquery.flot.stackpercent', - 'jquery.flot.fillbelow', - 'jquery.flot.crosshair', - './jquery.flot.events', -], -function (angular, $, moment, _, kbn, GraphTooltip, thresholdManExports) { - 'use strict'; - - var module = angular.module('grafana.directives'); - var labelWidthCache = {}; - - module.directive('grafanaGraph', function($rootScope, timeSrv) { - return { - restrict: 'A', - template: '
', - link: function(scope, elem) { - var ctrl = scope.ctrl; - var dashboard = ctrl.dashboard; - var panel = ctrl.panel; - var data, annotations; - var sortedSeries; - var legendSideLastValue = null; - var rootScope = scope.$root; - var panelWidth = 0; - var thresholdManager = new thresholdManExports.ThresholdManager(ctrl); - - rootScope.onAppEvent('setCrosshair', function(event, info) { - // do not need to to this if event is from this panel - if (info.scope === scope) { - return; - } - - if(dashboard.sharedCrosshair) { - var plot = elem.data().plot; - if (plot) { - plot.setCrosshair({ x: info.pos.x, y: info.pos.y }); - } - } - }, scope); - - rootScope.onAppEvent('clearCrosshair', function() { - var plot = elem.data().plot; - if (plot) { - plot.clearCrosshair(); - } - }, scope); - - // Receive render events - ctrl.events.on('render', function(renderData) { - data = renderData || data; - if (!data) { - return; - } - annotations = data.annotations || annotations; - render_panel(); - }); - - function getLegendHeight(panelHeight) { - if (!panel.legend.show || panel.legend.rightSide) { - return 0; - } - - if (panel.legend.alignAsTable) { - var legendSeries = _.filter(data, function(series) { - return series.hideFromLegend(panel.legend) === false; - }); - var total = 23 + (21 * legendSeries.length); - return Math.min(total, Math.floor(panelHeight/2)); - } else { - return 26; - } - } - - function setElementHeight() { - try { - var height = ctrl.height - getLegendHeight(ctrl.height); - elem.css('height', height + 'px'); - - return true; - } catch(e) { // IE throws errors sometimes - console.log(e); - return false; - } - } - - function shouldAbortRender() { - if (!data) { - return true; - } - - if (!setElementHeight()) { return true; } - - if (panelWidth === 0) { - return true; - } - } - - function getLabelWidth(text, elem) { - var labelWidth = labelWidthCache[text]; - - if (!labelWidth) { - labelWidth = labelWidthCache[text] = elem.width(); - } - - return labelWidth; - } - - function drawHook(plot) { - // Update legend values - var yaxis = plot.getYAxes(); - for (var i = 0; i < data.length; i++) { - var series = data[i]; - var axis = yaxis[series.yaxis - 1]; - var formater = kbn.valueFormats[panel.yaxes[series.yaxis - 1].format]; - - // decimal override - if (_.isNumber(panel.decimals)) { - series.updateLegendValues(formater, panel.decimals, null); - } else { - // auto decimals - // legend and tooltip gets one more decimal precision - // than graph legend ticks - var tickDecimals = (axis.tickDecimals || -1) + 1; - series.updateLegendValues(formater, tickDecimals, axis.scaledDecimals + 2); - } - - if(!rootScope.$$phase) { scope.$digest(); } - } - - // add left axis labels - if (panel.yaxes[0].label) { - var yaxisLabel = $("
") - .text(panel.yaxes[0].label) - .appendTo(elem); - - yaxisLabel[0].style.marginTop = (getLabelWidth(panel.yaxes[0].label, yaxisLabel) / 2) + 'px'; - } - - // add right axis labels - if (panel.yaxes[1].label) { - var rightLabel = $("
") - .text(panel.yaxes[1].label) - .appendTo(elem); - - rightLabel[0].style.marginTop = (getLabelWidth(panel.yaxes[1].label, rightLabel) / 2) + 'px'; - } - - thresholdManager.draw(plot); - } - - function processOffsetHook(plot, gridMargin) { - var left = panel.yaxes[0]; - var right = panel.yaxes[1]; - if (left.show && left.label) { gridMargin.left = 20; } - if (right.show && right.label) { gridMargin.right = 20; } - } - - // Function for rendering panel - function render_panel() { - panelWidth = elem.width(); - - if (shouldAbortRender()) { - return; - } - - // give space to alert editing - thresholdManager.prepare(elem, data); - - var stack = panel.stack ? true : null; - - // Populate element - var options = { - hooks: { - draw: [drawHook], - processOffset: [processOffsetHook], - }, - legend: { show: false }, - series: { - stackpercent: panel.stack ? panel.percentage : false, - stack: panel.percentage ? null : stack, - lines: { - show: panel.lines, - zero: false, - fill: translateFillOption(panel.fill), - lineWidth: panel.linewidth, - steps: panel.steppedLine - }, - bars: { - show: panel.bars, - fill: 1, - barWidth: 1, - zero: false, - lineWidth: 0 - }, - points: { - show: panel.points, - fill: 1, - fillColor: false, - radius: panel.points ? panel.pointradius : 2 - }, - shadowSize: 0 - }, - yaxes: [], - xaxis: {}, - grid: { - minBorderMargin: 0, - markings: [], - backgroundColor: null, - borderWidth: 0, - hoverable: true, - color: '#c8c8c8', - margin: { left: 0, right: 0 }, - }, - selection: { - mode: "x", - color: '#666' - }, - crosshair: { - mode: panel.tooltip.shared || dashboard.sharedCrosshair ? "x" : null - } - }; - - for (var i = 0; i < data.length; i++) { - var series = data[i]; - series.data = series.getFlotPairs(series.nullPointMode || panel.nullPointMode); - - if (panel.xaxis.mode === 'series') { - series.data = [[i + 1, series.stats[panel.xaxis.values[0]]]]; - } else if (panel.xaxis.mode === 'table' || - panel.xaxis.mode === 'elastic') { - series.data = []; - for (var j = 0; j < series.datapoints.length; j++) { - var dataIndex = i * series.datapoints.length + j; - series.datapoints[j]; - series.data.push([ - dataIndex + 1, - series.datapoints[j][0] - ]); - } - } - - // if hidden remove points and disable stack - if (ctrl.hiddenSeries[series.alias]) { - series.data = []; - series.stack = false; - } - } - - switch(panel.xaxis.mode) { - case 'series': { - options.series.bars.barWidth = 0.7; - options.series.bars.align = 'center'; - addXSeriesAxis(options); - break; - } - case 'table': { - options.series.bars.barWidth = 0.7; - options.series.bars.align = 'center'; - addXTableAxis(options); - break; - } - default: { - if (data.length && data[0].stats.timeStep) { - options.series.bars.barWidth = data[0].stats.timeStep / 1.5; - } - addTimeAxis(options); - break; - } - } - - thresholdManager.addPlotOptions(options, panel); - addAnnotations(options); - configureAxisOptions(data, options); - - sortedSeries = _.sortBy(data, function(series) { return series.zindex; }); - - function callPlot(incrementRenderCounter) { - try { - $.plot(elem, sortedSeries, options); - if (ctrl.renderError) { - delete ctrl.error; - delete ctrl.inspector; - } - } catch (e) { - console.log('flotcharts error', e); - ctrl.error = e.message || "Render Error"; - ctrl.renderError = true; - ctrl.inspector = {error: e}; - } - - if (incrementRenderCounter) { - ctrl.renderingCompleted(); - } - } - - if (shouldDelayDraw(panel)) { - // temp fix for legends on the side, need to render twice to get dimensions right - callPlot(false); - setTimeout(function() { callPlot(true); }, 50); - legendSideLastValue = panel.legend.rightSide; - } - else { - callPlot(true); - } - } - - function translateFillOption(fill) { - return fill === 0 ? 0.001 : fill/10; - } - - function shouldDelayDraw(panel) { - if (panel.legend.rightSide) { - return true; - } - if (legendSideLastValue !== null && panel.legend.rightSide !== legendSideLastValue) { - return true; - } - } - - function addTimeAxis(options) { - var ticks = panelWidth / 100; - var min = _.isUndefined(ctrl.range.from) ? null : ctrl.range.from.valueOf(); - var max = _.isUndefined(ctrl.range.to) ? null : ctrl.range.to.valueOf(); - - options.xaxis = { - timezone: dashboard.getTimezone(), - show: panel.xaxis.show, - mode: "time", - min: min, - max: max, - label: "Datetime", - ticks: ticks, - timeformat: time_format(ticks, min, max), - }; - } - - function addXSeriesAxis(options) { - var ticks = _.map(data, function(series, index) { - return [index + 1, series.alias]; - }); - - options.xaxis = { - timezone: dashboard.getTimezone(), - show: panel.xaxis.show, - mode: null, - min: 0, - max: ticks.length + 1, - label: "Datetime", - ticks: ticks - }; - } - - function addXTableAxis(options) { - var ticks = _.map(data, function(series, seriesIndex) { - return _.map(series.datapoints, function(point, pointIndex) { - var tickIndex = seriesIndex * series.datapoints.length + pointIndex; - return [tickIndex + 1, point[1]]; - }); - }); - ticks = _.flatten(ticks, true); - - options.xaxis = { - timezone: dashboard.getTimezone(), - show: panel.xaxis.show, - mode: null, - min: 0, - max: ticks.length + 1, - label: "Datetime", - ticks: ticks - }; - } - - function addAnnotations(options) { - if(!annotations || annotations.length === 0) { - return; - } - - var types = {}; - for (var i = 0; i < annotations.length; i++) { - var item = annotations[i]; - - if (!types[item.source.name]) { - types[item.source.name] = { - color: item.source.iconColor, - position: 'BOTTOM', - markerSize: 5, - }; - } - } - - options.events = { - levels: _.keys(types).length + 1, - data: annotations, - types: types, - }; - } - - //Override min/max to provide more flexible autoscaling - function autoscaleSpanOverride(yaxis, data, options) { - var expr; - if (yaxis.min != null && data != null) { - expr = parseThresholdExpr(yaxis.min); - options.min = autoscaleYAxisMin(expr, data.stats); - } - if (yaxis.max != null && data != null) { - expr = parseThresholdExpr(yaxis.max); - options.max = autoscaleYAxisMax(expr, data.stats); - } - } - - function parseThresholdExpr(expr) { - var match, operator, value, precision; - expr = String(expr); - match = expr.match(/\s*([<=>~]*)\s*(\-?\d+(\.\d+)?)/); - if (match) { - operator = match[1]; - value = parseFloat(match[2]); - //Precision based on input - precision = match[3] ? match[3].length - 1 : 0; - return { - operator: operator, - value: value, - precision: precision - }; - } else { - return undefined; - } - } - - function autoscaleYAxisMax(expr, dataStats) { - var operator = expr.operator, - value = expr.value, - precision = expr.precision; - if (operator === ">") { - return dataStats.max < value ? value : null; - } else if (operator === "<") { - return dataStats.max > value ? value : null; - } else if (operator === "~") { - return kbn.roundValue(dataStats.avg + value, precision); - } else if (operator === "=") { - return kbn.roundValue(dataStats.current + value, precision); - } else if (!operator && !isNaN(value)) { - return kbn.roundValue(value, precision); - } else { - return null; - } - } - - function autoscaleYAxisMin(expr, dataStats) { - var operator = expr.operator, - value = expr.value, - precision = expr.precision; - if (operator === ">") { - return dataStats.min < value ? value : null; - } else if (operator === "<") { - return dataStats.min > value ? value : null; - } else if (operator === "~") { - return kbn.roundValue(dataStats.avg - value, precision); - } else if (operator === "=") { - return kbn.roundValue(dataStats.current - value, precision); - } else if (!operator && !isNaN(value)) { - return kbn.roundValue(value, precision); - } else { - return null; - } - } - - function configureAxisOptions(data, options) { - var defaults = { - position: 'left', - show: panel.yaxes[0].show, - min: panel.yaxes[0].min, - index: 1, - logBase: panel.yaxes[0].logBase || 1, - max: panel.percentage && panel.stack ? 100 : panel.yaxes[0].max, - }; - - autoscaleSpanOverride(panel.yaxes[0], data[0], defaults); - options.yaxes.push(defaults); - - if (_.find(data, {yaxis: 2})) { - var secondY = _.clone(defaults); - secondY.index = 2, - secondY.show = panel.yaxes[1].show; - secondY.logBase = panel.yaxes[1].logBase || 1, - secondY.position = 'right'; - secondY.min = panel.yaxes[1].min; - secondY.max = panel.percentage && panel.stack ? 100 : panel.yaxes[1].max; - autoscaleSpanOverride(panel.yaxes[1], data[1], secondY); - options.yaxes.push(secondY); - - applyLogScale(options.yaxes[1], data); - configureAxisMode(options.yaxes[1], panel.percentage && panel.stack ? "percent" : panel.yaxes[1].format); - } - - applyLogScale(options.yaxes[0], data); - configureAxisMode(options.yaxes[0], panel.percentage && panel.stack ? "percent" : panel.yaxes[0].format); - } - - function applyLogScale(axis, data) { - if (axis.logBase === 1) { - return; - } - - var series, i; - var max = axis.max; - - if (max === null) { - for (i = 0; i < data.length; i++) { - series = data[i]; - if (series.yaxis === axis.index) { - if (max < series.stats.max) { - max = series.stats.max; - } - } - } - if (max === void 0) { - max = Number.MAX_VALUE; - } - } - - axis.min = axis.min !== null ? axis.min : 0; - axis.ticks = [0, 1]; - var nextTick = 1; - - while (true) { - nextTick = nextTick * axis.logBase; - axis.ticks.push(nextTick); - if (nextTick > max) { - break; - } - } - - if (axis.logBase === 10) { - axis.transform = function(v) { return Math.log(v+0.1); }; - axis.inverseTransform = function (v) { return Math.pow(10,v); }; - } else { - axis.transform = function(v) { return Math.log(v+0.1) / Math.log(axis.logBase); }; - axis.inverseTransform = function (v) { return Math.pow(axis.logBase,v); }; - } - } - - function configureAxisMode(axis, format) { - axis.tickFormatter = function(val, axis) { - return kbn.valueFormats[format](val, axis.tickDecimals, axis.scaledDecimals); - }; - } - - function time_format(ticks, min, max) { - if (min && max && ticks) { - var range = max - min; - var secPerTick = (range/ticks) / 1000; - var oneDay = 86400000; - var oneYear = 31536000000; - - if (secPerTick <= 45) { - return "%H:%M:%S"; - } - if (secPerTick <= 7200 || range <= oneDay) { - return "%H:%M"; - } - if (secPerTick <= 80000) { - return "%m/%d %H:%M"; - } - if (secPerTick <= 2419200 || range <= oneYear) { - return "%m/%d"; - } - return "%Y-%m"; - } - - return "%H:%M"; - } - - new GraphTooltip(elem, dashboard, scope, function() { - return sortedSeries; - }); - - elem.bind("plotselected", function (event, ranges) { - scope.$apply(function() { - timeSrv.setTime({ - from : moment.utc(ranges.xaxis.from), - to : moment.utc(ranges.xaxis.to), - }); - }); - }); - } - }; - }); -}); diff --git a/public/app/plugins/panel/graph/graph.ts b/public/app/plugins/panel/graph/graph.ts new file mode 100755 index 00000000000..29da2562bf5 --- /dev/null +++ b/public/app/plugins/panel/graph/graph.ts @@ -0,0 +1,602 @@ +/// + +import 'jquery.flot'; +import 'jquery.flot.selection'; +import 'jquery.flot.time'; +import 'jquery.flot.stack'; +import 'jquery.flot.stackpercent'; +import 'jquery.flot.fillbelow'; +import 'jquery.flot.crosshair'; +import './jquery.flot.events'; + +import angular from 'angular'; +import $ from 'jquery'; +import moment from 'moment'; +import _ from 'lodash'; +import kbn from 'app/core/utils/kbn'; +import GraphTooltip from './graph_tooltip'; +import {ThresholdManager} from './threshold_manager'; + +var module = angular.module('grafana.directives'); +var labelWidthCache = {}; + +module.directive('grafanaGraph', function($rootScope, timeSrv) { + return { + restrict: 'A', + template: '
', + link: function(scope, elem) { + var ctrl = scope.ctrl; + var dashboard = ctrl.dashboard; + var panel = ctrl.panel; + var data, annotations; + var sortedSeries; + var legendSideLastValue = null; + var rootScope = scope.$root; + var panelWidth = 0; + var thresholdManager = new ThresholdManager(ctrl); + + rootScope.onAppEvent('setCrosshair', function(event, info) { + // do not need to to this if event is from this panel + if (info.scope === scope) { + return; + } + + if (dashboard.sharedCrosshair) { + var plot = elem.data().plot; + if (plot) { + plot.setCrosshair({ x: info.pos.x, y: info.pos.y }); + } + } + }, scope); + + rootScope.onAppEvent('clearCrosshair', function() { + var plot = elem.data().plot; + if (plot) { + plot.clearCrosshair(); + } + }, scope); + + // Receive render events + ctrl.events.on('render', function(renderData) { + data = renderData || data; + if (!data) { + return; + } + annotations = data.annotations || annotations; + render_panel(); + }); + + function getLegendHeight(panelHeight) { + if (!panel.legend.show || panel.legend.rightSide) { + return 0; + } + + if (panel.legend.alignAsTable) { + var legendSeries = _.filter(data, function(series) { + return series.hideFromLegend(panel.legend) === false; + }); + var total = 23 + (21 * legendSeries.length); + return Math.min(total, Math.floor(panelHeight/2)); + } else { + return 26; + } + } + + function setElementHeight() { + try { + var height = ctrl.height - getLegendHeight(ctrl.height); + elem.css('height', height + 'px'); + + return true; + } catch (e) { // IE throws errors sometimes + console.log(e); + return false; + } + } + + function shouldAbortRender() { + if (!data) { + return true; + } + + if (!setElementHeight()) { return true; } + + if (panelWidth === 0) { + return true; + } + } + + function getLabelWidth(text, elem) { + var labelWidth = labelWidthCache[text]; + + if (!labelWidth) { + labelWidth = labelWidthCache[text] = elem.width(); + } + + return labelWidth; + } + + function drawHook(plot) { + // Update legend values + var yaxis = plot.getYAxes(); + for (var i = 0; i < data.length; i++) { + var series = data[i]; + var axis = yaxis[series.yaxis - 1]; + var formater = kbn.valueFormats[panel.yaxes[series.yaxis - 1].format]; + + // decimal override + if (_.isNumber(panel.decimals)) { + series.updateLegendValues(formater, panel.decimals, null); + } else { + // auto decimals + // legend and tooltip gets one more decimal precision + // than graph legend ticks + var tickDecimals = (axis.tickDecimals || -1) + 1; + series.updateLegendValues(formater, tickDecimals, axis.scaledDecimals + 2); + } + + if (!rootScope.$$phase) { scope.$digest(); } + } + + // add left axis labels + if (panel.yaxes[0].label) { + var yaxisLabel = $("
") + .text(panel.yaxes[0].label) + .appendTo(elem); + + yaxisLabel[0].style.marginTop = (getLabelWidth(panel.yaxes[0].label, yaxisLabel) / 2) + 'px'; + } + + // add right axis labels + if (panel.yaxes[1].label) { + var rightLabel = $("
") + .text(panel.yaxes[1].label) + .appendTo(elem); + + rightLabel[0].style.marginTop = (getLabelWidth(panel.yaxes[1].label, rightLabel) / 2) + 'px'; + } + + thresholdManager.draw(plot); + } + + function processOffsetHook(plot, gridMargin) { + var left = panel.yaxes[0]; + var right = panel.yaxes[1]; + if (left.show && left.label) { gridMargin.left = 20; } + if (right.show && right.label) { gridMargin.right = 20; } + } + + function processDatapoints(plot) { + console.log('processDatapoints'); + } + + // Function for rendering panel + function render_panel() { + panelWidth = elem.width(); + + if (shouldAbortRender()) { + return; + } + + // give space to alert editing + thresholdManager.prepare(elem, data); + + var stack = panel.stack ? true : null; + + // Populate element + var options: any = { + hooks: { + draw: [drawHook], + processOffset: [processOffsetHook], + processDatapoints: [processDatapoints], + }, + legend: { show: false }, + series: { + stackpercent: panel.stack ? panel.percentage : false, + stack: panel.percentage ? null : stack, + lines: { + show: panel.lines, + zero: false, + fill: translateFillOption(panel.fill), + lineWidth: panel.linewidth, + steps: panel.steppedLine + }, + bars: { + show: panel.bars, + fill: 1, + barWidth: 1, + zero: false, + lineWidth: 0 + }, + points: { + show: panel.points, + fill: 1, + fillColor: false, + radius: panel.points ? panel.pointradius : 2 + }, + shadowSize: 0 + }, + yaxes: [], + xaxis: {}, + grid: { + minBorderMargin: 0, + markings: [], + backgroundColor: null, + borderWidth: 0, + hoverable: true, + color: '#c8c8c8', + margin: { left: 0, right: 0 }, + }, + selection: { + mode: "x", + color: '#666' + }, + crosshair: { + mode: panel.tooltip.shared || dashboard.sharedCrosshair ? "x" : null + } + }; + + for (var i = 0; i < data.length; i++) { + var series = data[i]; + series.data = series.getFlotPairs(series.nullPointMode || panel.nullPointMode); + + if (panel.xaxis.mode === 'series') { + series.data = [[i + 1, series.stats[panel.xaxis.values[0]]]]; + } else if (panel.xaxis.mode === 'table' || panel.xaxis.mode === 'elastic') { + series.data = []; + for (var j = 0; j < series.datapoints.length; j++) { + var dataIndex = i * series.datapoints.length + j; + series.datapoints[j]; + series.data.push([ + dataIndex + 1, + series.datapoints[j][0] + ]); + } + } + + // if hidden remove points and disable stack + if (ctrl.hiddenSeries[series.alias]) { + series.data = []; + series.stack = false; + } + } + + switch (panel.xaxis.mode) { + case 'series': { + options.series.bars.barWidth = 0.7; + options.series.bars.align = 'center'; + addXSeriesAxis(options); + break; + } + case 'table': { + options.series.bars.barWidth = 0.7; + options.series.bars.align = 'center'; + addXTableAxis(options); + break; + } + default: { + if (data.length && data[0].stats.timeStep) { + options.series.bars.barWidth = data[0].stats.timeStep / 1.5; + } + addTimeAxis(options); + break; + } + } + + thresholdManager.addPlotOptions(options, panel); + addAnnotations(options); + configureAxisOptions(data, options); + + sortedSeries = _.sortBy(data, function(series) { return series.zindex; }); + + function callPlot(incrementRenderCounter) { + try { + $.plot(elem, sortedSeries, options); + if (ctrl.renderError) { + delete ctrl.error; + delete ctrl.inspector; + } + } catch (e) { + console.log('flotcharts error', e); + ctrl.error = e.message || "Render Error"; + ctrl.renderError = true; + ctrl.inspector = {error: e}; + } + + if (incrementRenderCounter) { + ctrl.renderingCompleted(); + } + } + + if (shouldDelayDraw(panel)) { + // temp fix for legends on the side, need to render twice to get dimensions right + callPlot(false); + setTimeout(function() { callPlot(true); }, 50); + legendSideLastValue = panel.legend.rightSide; + } else { + callPlot(true); + } + } + + function translateFillOption(fill) { + return fill === 0 ? 0.001 : fill/10; + } + + function shouldDelayDraw(panel) { + if (panel.legend.rightSide) { + return true; + } + if (legendSideLastValue !== null && panel.legend.rightSide !== legendSideLastValue) { + return true; + } + } + + function addTimeAxis(options) { + var ticks = panelWidth / 100; + var min = _.isUndefined(ctrl.range.from) ? null : ctrl.range.from.valueOf(); + var max = _.isUndefined(ctrl.range.to) ? null : ctrl.range.to.valueOf(); + + options.xaxis = { + timezone: dashboard.getTimezone(), + show: panel.xaxis.show, + mode: "time", + min: min, + max: max, + label: "Datetime", + ticks: ticks, + timeformat: time_format(ticks, min, max), + }; + } + + function addXSeriesAxis(options) { + var ticks = _.map(data, function(series, index) { + return [index + 1, series.alias]; + }); + + options.xaxis = { + timezone: dashboard.getTimezone(), + show: panel.xaxis.show, + mode: null, + min: 0, + max: ticks.length + 1, + label: "Datetime", + ticks: ticks + }; + } + + function addXTableAxis(options) { + var ticks = _.map(data, function(series, seriesIndex) { + return _.map(series.datapoints, function(point, pointIndex) { + var tickIndex = seriesIndex * series.datapoints.length + pointIndex; + return [tickIndex + 1, point[1]]; + }); + }); + ticks = _.flatten(ticks, true); + + options.xaxis = { + timezone: dashboard.getTimezone(), + show: panel.xaxis.show, + mode: null, + min: 0, + max: ticks.length + 1, + label: "Datetime", + ticks: ticks + }; + } + + function addAnnotations(options) { + if (!annotations || annotations.length === 0) { + return; + } + + var types = {}; + for (var i = 0; i < annotations.length; i++) { + var item = annotations[i]; + + if (!types[item.source.name]) { + types[item.source.name] = { + color: item.source.iconColor, + position: 'BOTTOM', + markerSize: 5, + }; + } + } + + options.events = { + levels: _.keys(types).length + 1, + data: annotations, + types: types, + }; + } + + //Override min/max to provide more flexible autoscaling + function autoscaleSpanOverride(yaxis, data, options) { + var expr; + if (yaxis.min != null && data != null) { + expr = parseThresholdExpr(yaxis.min); + options.min = autoscaleYAxisMin(expr, data.stats); + } + if (yaxis.max != null && data != null) { + expr = parseThresholdExpr(yaxis.max); + options.max = autoscaleYAxisMax(expr, data.stats); + } + } + + function parseThresholdExpr(expr) { + var match, operator, value, precision; + expr = String(expr); + match = expr.match(/\s*([<=>~]*)\s*(\-?\d+(\.\d+)?)/); + if (match) { + operator = match[1]; + value = parseFloat(match[2]); + //Precision based on input + precision = match[3] ? match[3].length - 1 : 0; + return { + operator: operator, + value: value, + precision: precision + }; + } else { + return undefined; + } + } + + function autoscaleYAxisMax(expr, dataStats) { + var operator = expr.operator, + value = expr.value, + precision = expr.precision; + if (operator === ">") { + return dataStats.max < value ? value : null; + } else if (operator === "<") { + return dataStats.max > value ? value : null; + } else if (operator === "~") { + return kbn.roundValue(dataStats.avg + value, precision); + } else if (operator === "=") { + return kbn.roundValue(dataStats.current + value, precision); + } else if (!operator && !isNaN(value)) { + return kbn.roundValue(value, precision); + } else { + return null; + } + } + + function autoscaleYAxisMin(expr, dataStats) { + var operator = expr.operator, + value = expr.value, + precision = expr.precision; + if (operator === ">") { + return dataStats.min < value ? value : null; + } else if (operator === "<") { + return dataStats.min > value ? value : null; + } else if (operator === "~") { + return kbn.roundValue(dataStats.avg - value, precision); + } else if (operator === "=") { + return kbn.roundValue(dataStats.current - value, precision); + } else if (!operator && !isNaN(value)) { + return kbn.roundValue(value, precision); + } else { + return null; + } + } + + function configureAxisOptions(data, options) { + var defaults = { + position: 'left', + show: panel.yaxes[0].show, + // min: panel.yaxes[0].min, + index: 1, + logBase: panel.yaxes[0].logBase || 1, + max: panel.percentage && panel.stack ? 100 : panel.yaxes[0].max, + }; + + // autoscaleSpanOverride(panel.yaxes[0], data[0], defaults); + options.yaxes.push(defaults); + + if (_.find(data, {yaxis: 2})) { + var secondY = _.clone(defaults); + secondY.index = 2, + secondY.show = panel.yaxes[1].show; + secondY.logBase = panel.yaxes[1].logBase || 1, + secondY.position = 'right'; + // secondY.min = panel.yaxes[1].min; + secondY.max = panel.percentage && panel.stack ? 100 : panel.yaxes[1].max; + // autoscaleSpanOverride(panel.yaxes[1], data[1], secondY); + options.yaxes.push(secondY); + + applyLogScale(options.yaxes[1], data); + configureAxisMode(options.yaxes[1], panel.percentage && panel.stack ? "percent" : panel.yaxes[1].format); + } + + applyLogScale(options.yaxes[0], data); + configureAxisMode(options.yaxes[0], panel.percentage && panel.stack ? "percent" : panel.yaxes[0].format); + } + + function applyLogScale(axis, data) { + if (axis.logBase === 1) { + return; + } + + var series, i; + var max = axis.max; + + if (max === null) { + for (i = 0; i < data.length; i++) { + series = data[i]; + if (series.yaxis === axis.index) { + if (max < series.stats.max) { + max = series.stats.max; + } + } + } + if (max === void 0) { + max = Number.MAX_VALUE; + } + } + + axis.min = axis.min !== null ? axis.min : 0; + axis.ticks = [0, 1]; + var nextTick = 1; + + while (true) { + nextTick = nextTick * axis.logBase; + axis.ticks.push(nextTick); + if (nextTick > max) { + break; + } + } + + if (axis.logBase === 10) { + axis.transform = function(v) { return Math.log(v+0.1); }; + axis.inverseTransform = function (v) { return Math.pow(10,v); }; + } else { + axis.transform = function(v) { return Math.log(v+0.1) / Math.log(axis.logBase); }; + axis.inverseTransform = function (v) { return Math.pow(axis.logBase,v); }; + } + } + + function configureAxisMode(axis, format) { + axis.tickFormatter = function(val, axis) { + return kbn.valueFormats[format](val, axis.tickDecimals, axis.scaledDecimals); + }; + } + + function time_format(ticks, min, max) { + if (min && max && ticks) { + var range = max - min; + var secPerTick = (range/ticks) / 1000; + var oneDay = 86400000; + var oneYear = 31536000000; + + if (secPerTick <= 45) { + return "%H:%M:%S"; + } + if (secPerTick <= 7200 || range <= oneDay) { + return "%H:%M"; + } + if (secPerTick <= 80000) { + return "%m/%d %H:%M"; + } + if (secPerTick <= 2419200 || range <= oneYear) { + return "%m/%d"; + } + return "%Y-%m"; + } + + return "%H:%M"; + } + + new GraphTooltip(elem, dashboard, scope, function() { + return sortedSeries; + }); + + elem.bind("plotselected", function (event, ranges) { + scope.$apply(function() { + timeSrv.setTime({ + from : moment.utc(ranges.xaxis.from), + to : moment.utc(ranges.xaxis.to), + }); + }); + }); + } + }; +}); diff --git a/public/app/plugins/panel/graph/specs/graph_specs.ts b/public/app/plugins/panel/graph/specs/graph_specs.ts index 2065bffb130..9f8d91ca9de 100644 --- a/public/app/plugins/panel/graph/specs/graph_specs.ts +++ b/public/app/plugins/panel/graph/specs/graph_specs.ts @@ -219,145 +219,145 @@ describe('grafanaGraph', function() { }, 10); - graphScenario('when using flexible Y-Min and Y-Max settings', function(ctx) { - describe('and Y-Min is <100 and Y-Max is >200 and values within range', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = '<100'; - ctrl.panel.yaxes[0].max = '>200'; - data[0] = new TimeSeries({ - datapoints: [[120,10],[160,20]], - alias: 'series1', - }); - }); - - it('should set min to 100 and max to 200', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(100); - expect(ctx.plotOptions.yaxes[0].max).to.be(200); - }); - }); - describe('and Y-Min is <100 and Y-Max is >200 and values outside range', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = '<100'; - ctrl.panel.yaxes[0].max = '>200'; - data[0] = new TimeSeries({ - datapoints: [[99,10],[201,20]], - alias: 'series1', - }); - }); - - it('should set min to auto and max to auto', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(null); - expect(ctx.plotOptions.yaxes[0].max).to.be(null); - }); - }); - describe('and Y-Min is =10.5 and Y-Max is =10.5', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = '=10.5'; - ctrl.panel.yaxes[0].max = '=10.5'; - data[0] = new TimeSeries({ - datapoints: [[100,10],[120,20], [110,30]], - alias: 'series1', - }); - }); - - it('should set min to last value + 10.5 and max to last value + 10.5', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(99.5); - expect(ctx.plotOptions.yaxes[0].max).to.be(120.5); - }); - }); - describe('and Y-Min is ~10.5 and Y-Max is ~10.5', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = '~10.5'; - ctrl.panel.yaxes[0].max = '~10.5'; - data[0] = new TimeSeries({ - datapoints: [[102,10],[104,20], [110,30]], //Also checks precision - alias: 'series1', - }); - }); - - it('should set min to average value + 10.5 and max to average value + 10.5', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(94.8); - expect(ctx.plotOptions.yaxes[0].max).to.be(115.8); - }); - }); - }); - graphScenario('when using regular Y-Min and Y-Max settings', function(ctx) { - describe('and Y-Min is 100 and Y-Max is 200', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = '100'; - ctrl.panel.yaxes[0].max = '200'; - data[0] = new TimeSeries({ - datapoints: [[120,10],[160,20]], - alias: 'series1', - }); - }); - - it('should set min to 100 and max to 200', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(100); - expect(ctx.plotOptions.yaxes[0].max).to.be(200); - }); - }); - describe('and Y-Min is 0 and Y-Max is 0', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = '0'; - ctrl.panel.yaxes[0].max = '0'; - data[0] = new TimeSeries({ - datapoints: [[120,10],[160,20]], - alias: 'series1', - }); - }); - - it('should set min to 0 and max to 0', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(0); - expect(ctx.plotOptions.yaxes[0].max).to.be(0); - }); - }); - describe('and negative values used', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = '-10'; - ctrl.panel.yaxes[0].max = '-13.14'; - data[0] = new TimeSeries({ - datapoints: [[120,10],[160,20]], - alias: 'series1', - }); - }); - - it('should set min and max to negative', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(-10); - expect(ctx.plotOptions.yaxes[0].max).to.be(-13.14); - }); - }); - }); - graphScenario('when using Y-Min and Y-Max settings stored as number', function(ctx) { - describe('and Y-Min is 0 and Y-Max is 100', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = 0; - ctrl.panel.yaxes[0].max = 100; - data[0] = new TimeSeries({ - datapoints: [[120,10],[160,20]], - alias: 'series1', - }); - }); - - it('should set min to 0 and max to 100', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(0); - expect(ctx.plotOptions.yaxes[0].max).to.be(100); - }); - }); - describe('and Y-Min is -100 and Y-Max is -10.5', function() { - ctx.setup(function(ctrl, data) { - ctrl.panel.yaxes[0].min = -100; - ctrl.panel.yaxes[0].max = -10.5; - data[0] = new TimeSeries({ - datapoints: [[120,10],[160,20]], - alias: 'series1', - }); - }); - - it('should set min to -100 and max to -10.5', function() { - expect(ctx.plotOptions.yaxes[0].min).to.be(-100); - expect(ctx.plotOptions.yaxes[0].max).to.be(-10.5); - }); - }); - }); + // graphScenario('when using flexible Y-Min and Y-Max settings', function(ctx) { + // describe('and Y-Min is <100 and Y-Max is >200 and values within range', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = '<100'; + // ctrl.panel.yaxes[0].max = '>200'; + // data[0] = new TimeSeries({ + // datapoints: [[120,10],[160,20]], + // alias: 'series1', + // }); + // }); + // + // it('should set min to 100 and max to 200', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(100); + // expect(ctx.plotOptions.yaxes[0].max).to.be(200); + // }); + // }); + // describe('and Y-Min is <100 and Y-Max is >200 and values outside range', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = '<100'; + // ctrl.panel.yaxes[0].max = '>200'; + // data[0] = new TimeSeries({ + // datapoints: [[99,10],[201,20]], + // alias: 'series1', + // }); + // }); + // + // it('should set min to auto and max to auto', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(null); + // expect(ctx.plotOptions.yaxes[0].max).to.be(null); + // }); + // }); + // describe('and Y-Min is =10.5 and Y-Max is =10.5', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = '=10.5'; + // ctrl.panel.yaxes[0].max = '=10.5'; + // data[0] = new TimeSeries({ + // datapoints: [[100,10],[120,20], [110,30]], + // alias: 'series1', + // }); + // }); + // + // it('should set min to last value + 10.5 and max to last value + 10.5', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(99.5); + // expect(ctx.plotOptions.yaxes[0].max).to.be(120.5); + // }); + // }); + // describe('and Y-Min is ~10.5 and Y-Max is ~10.5', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = '~10.5'; + // ctrl.panel.yaxes[0].max = '~10.5'; + // data[0] = new TimeSeries({ + // datapoints: [[102,10],[104,20], [110,30]], //Also checks precision + // alias: 'series1', + // }); + // }); + // + // it('should set min to average value + 10.5 and max to average value + 10.5', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(94.8); + // expect(ctx.plotOptions.yaxes[0].max).to.be(115.8); + // }); + // }); + // }); + // graphScenario('when using regular Y-Min and Y-Max settings', function(ctx) { + // describe('and Y-Min is 100 and Y-Max is 200', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = '100'; + // ctrl.panel.yaxes[0].max = '200'; + // data[0] = new TimeSeries({ + // datapoints: [[120,10],[160,20]], + // alias: 'series1', + // }); + // }); + // + // it('should set min to 100 and max to 200', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(100); + // expect(ctx.plotOptions.yaxes[0].max).to.be(200); + // }); + // }); + // describe('and Y-Min is 0 and Y-Max is 0', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = '0'; + // ctrl.panel.yaxes[0].max = '0'; + // data[0] = new TimeSeries({ + // datapoints: [[120,10],[160,20]], + // alias: 'series1', + // }); + // }); + // + // it('should set min to 0 and max to 0', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(0); + // expect(ctx.plotOptions.yaxes[0].max).to.be(0); + // }); + // }); + // describe('and negative values used', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = '-10'; + // ctrl.panel.yaxes[0].max = '-13.14'; + // data[0] = new TimeSeries({ + // datapoints: [[120,10],[160,20]], + // alias: 'series1', + // }); + // }); + // + // it('should set min and max to negative', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(-10); + // expect(ctx.plotOptions.yaxes[0].max).to.be(-13.14); + // }); + // }); + // }); + // graphScenario('when using Y-Min and Y-Max settings stored as number', function(ctx) { + // describe('and Y-Min is 0 and Y-Max is 100', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = 0; + // ctrl.panel.yaxes[0].max = 100; + // data[0] = new TimeSeries({ + // datapoints: [[120,10],[160,20]], + // alias: 'series1', + // }); + // }); + // + // it('should set min to 0 and max to 100', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(0); + // expect(ctx.plotOptions.yaxes[0].max).to.be(100); + // }); + // }); + // describe('and Y-Min is -100 and Y-Max is -10.5', function() { + // ctx.setup(function(ctrl, data) { + // ctrl.panel.yaxes[0].min = -100; + // ctrl.panel.yaxes[0].max = -10.5; + // data[0] = new TimeSeries({ + // datapoints: [[120,10],[160,20]], + // alias: 'series1', + // }); + // }); + // + // it('should set min to -100 and max to -10.5', function() { + // expect(ctx.plotOptions.yaxes[0].min).to.be(-100); + // expect(ctx.plotOptions.yaxes[0].max).to.be(-10.5); + // }); + // }); + // }); }); From 40e6317b7b7ed3a443226ebc987f911ae9dd2bc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 26 Sep 2016 14:04:13 +0200 Subject: [PATCH 19/32] feat(graph): refactoring --- public/app/plugins/panel/graph/axes_editor.ts | 2 +- .../app/plugins/panel/graph/data_processor.ts | 1 + public/app/plugins/panel/graph/graph.ts | 34 +++++++------------ 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/public/app/plugins/panel/graph/axes_editor.ts b/public/app/plugins/panel/graph/axes_editor.ts index 591cfba4f17..502af07ff79 100644 --- a/public/app/plugins/panel/graph/axes_editor.ts +++ b/public/app/plugins/panel/graph/axes_editor.ts @@ -30,7 +30,7 @@ export class AxesEditorCtrl { this.xAxisModes = { 'Time': 'time', 'Series': 'series', - 'Data field': 'field', + // 'Data field': 'field', }; this.xAxisStatOptions = [ diff --git a/public/app/plugins/panel/graph/data_processor.ts b/public/app/plugins/panel/graph/data_processor.ts index 0b80e543e2a..b22d9f391d0 100644 --- a/public/app/plugins/panel/graph/data_processor.ts +++ b/public/app/plugins/panel/graph/data_processor.ts @@ -68,6 +68,7 @@ export class DataProcessor { this.panel.stack = false; this.panel.legend.show = false; this.panel.tooltip.shared = false; + this.panel.xaxis.values = ['total']; break; } } diff --git a/public/app/plugins/panel/graph/graph.ts b/public/app/plugins/panel/graph/graph.ts index 29da2562bf5..f5f943be715 100755 --- a/public/app/plugins/panel/graph/graph.ts +++ b/public/app/plugins/panel/graph/graph.ts @@ -236,24 +236,10 @@ module.directive('grafanaGraph', function($rootScope, timeSrv) { } }; - for (var i = 0; i < data.length; i++) { + for (let i = 0; i < data.length; i++) { var series = data[i]; series.data = series.getFlotPairs(series.nullPointMode || panel.nullPointMode); - if (panel.xaxis.mode === 'series') { - series.data = [[i + 1, series.stats[panel.xaxis.values[0]]]]; - } else if (panel.xaxis.mode === 'table' || panel.xaxis.mode === 'elastic') { - series.data = []; - for (var j = 0; j < series.datapoints.length; j++) { - var dataIndex = i * series.datapoints.length + j; - series.datapoints[j]; - series.data.push([ - dataIndex + 1, - series.datapoints[j][0] - ]); - } - } - // if hidden remove points and disable stack if (ctrl.hiddenSeries[series.alias]) { series.data = []; @@ -265,6 +251,12 @@ module.directive('grafanaGraph', function($rootScope, timeSrv) { case 'series': { options.series.bars.barWidth = 0.7; options.series.bars.align = 'center'; + + for (let i = 0; i < data.length; i++) { + var series = data[i]; + series.data = [[i + 1, series.stats[panel.xaxis.values[0]]]]; + } + addXSeriesAxis(options); break; } @@ -483,7 +475,7 @@ module.directive('grafanaGraph', function($rootScope, timeSrv) { var defaults = { position: 'left', show: panel.yaxes[0].show, - // min: panel.yaxes[0].min, + min: panel.yaxes[0].min, index: 1, logBase: panel.yaxes[0].logBase || 1, max: panel.percentage && panel.stack ? 100 : panel.yaxes[0].max, @@ -494,11 +486,11 @@ module.directive('grafanaGraph', function($rootScope, timeSrv) { if (_.find(data, {yaxis: 2})) { var secondY = _.clone(defaults); - secondY.index = 2, - secondY.show = panel.yaxes[1].show; - secondY.logBase = panel.yaxes[1].logBase || 1, - secondY.position = 'right'; - // secondY.min = panel.yaxes[1].min; + secondY.index = 2; + secondY.show = panel.yaxes[1].show; + secondY.logBase = panel.yaxes[1].logBase || 1; + secondY.position = 'right'; + secondY.min = panel.yaxes[1].min; secondY.max = panel.percentage && panel.stack ? 100 : panel.yaxes[1].max; // autoscaleSpanOverride(panel.yaxes[1], data[1], secondY); options.yaxes.push(secondY); From 81cb4a740b775c485cd6c873677ebbe0bb69f813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 26 Sep 2016 17:25:29 +0200 Subject: [PATCH 20/32] refactor(graph): progress on graph panel work --- public/app/plugins/panel/graph/graph.ts | 93 +++---------------------- 1 file changed, 10 insertions(+), 83 deletions(-) diff --git a/public/app/plugins/panel/graph/graph.ts b/public/app/plugins/panel/graph/graph.ts index f5f943be715..36c50f4e703 100755 --- a/public/app/plugins/panel/graph/graph.ts +++ b/public/app/plugins/panel/graph/graph.ts @@ -23,7 +23,7 @@ var labelWidthCache = {}; module.directive('grafanaGraph', function($rootScope, timeSrv) { return { restrict: 'A', - template: '
', + template: '', link: function(scope, elem) { var ctrl = scope.ctrl; var dashboard = ctrl.dashboard; @@ -164,10 +164,15 @@ module.directive('grafanaGraph', function($rootScope, timeSrv) { var right = panel.yaxes[1]; if (left.show && left.label) { gridMargin.left = 20; } if (right.show && right.label) { gridMargin.right = 20; } - } - function processDatapoints(plot) { - console.log('processDatapoints'); + // apply y-axis min/max options + var yaxis = plot.getYAxes(); + for (var i = 0; i < yaxis.length; i++) { + var axis = yaxis[i]; + var panelOptions = panel.yaxes[i]; + axis.options.max = panelOptions.max; + axis.options.min = panelOptions.min; + } } // Function for rendering panel @@ -188,7 +193,6 @@ module.directive('grafanaGraph', function($rootScope, timeSrv) { hooks: { draw: [drawHook], processOffset: [processOffsetHook], - processDatapoints: [processDatapoints], }, legend: { show: false }, series: { @@ -401,87 +405,15 @@ module.directive('grafanaGraph', function($rootScope, timeSrv) { }; } - //Override min/max to provide more flexible autoscaling - function autoscaleSpanOverride(yaxis, data, options) { - var expr; - if (yaxis.min != null && data != null) { - expr = parseThresholdExpr(yaxis.min); - options.min = autoscaleYAxisMin(expr, data.stats); - } - if (yaxis.max != null && data != null) { - expr = parseThresholdExpr(yaxis.max); - options.max = autoscaleYAxisMax(expr, data.stats); - } - } - - function parseThresholdExpr(expr) { - var match, operator, value, precision; - expr = String(expr); - match = expr.match(/\s*([<=>~]*)\s*(\-?\d+(\.\d+)?)/); - if (match) { - operator = match[1]; - value = parseFloat(match[2]); - //Precision based on input - precision = match[3] ? match[3].length - 1 : 0; - return { - operator: operator, - value: value, - precision: precision - }; - } else { - return undefined; - } - } - - function autoscaleYAxisMax(expr, dataStats) { - var operator = expr.operator, - value = expr.value, - precision = expr.precision; - if (operator === ">") { - return dataStats.max < value ? value : null; - } else if (operator === "<") { - return dataStats.max > value ? value : null; - } else if (operator === "~") { - return kbn.roundValue(dataStats.avg + value, precision); - } else if (operator === "=") { - return kbn.roundValue(dataStats.current + value, precision); - } else if (!operator && !isNaN(value)) { - return kbn.roundValue(value, precision); - } else { - return null; - } - } - - function autoscaleYAxisMin(expr, dataStats) { - var operator = expr.operator, - value = expr.value, - precision = expr.precision; - if (operator === ">") { - return dataStats.min < value ? value : null; - } else if (operator === "<") { - return dataStats.min > value ? value : null; - } else if (operator === "~") { - return kbn.roundValue(dataStats.avg - value, precision); - } else if (operator === "=") { - return kbn.roundValue(dataStats.current - value, precision); - } else if (!operator && !isNaN(value)) { - return kbn.roundValue(value, precision); - } else { - return null; - } - } - function configureAxisOptions(data, options) { var defaults = { position: 'left', show: panel.yaxes[0].show, - min: panel.yaxes[0].min, index: 1, logBase: panel.yaxes[0].logBase || 1, - max: panel.percentage && panel.stack ? 100 : panel.yaxes[0].max, + max: 100, // correct later }; - // autoscaleSpanOverride(panel.yaxes[0], data[0], defaults); options.yaxes.push(defaults); if (_.find(data, {yaxis: 2})) { @@ -490,12 +422,7 @@ module.directive('grafanaGraph', function($rootScope, timeSrv) { secondY.show = panel.yaxes[1].show; secondY.logBase = panel.yaxes[1].logBase || 1; secondY.position = 'right'; - secondY.min = panel.yaxes[1].min; - secondY.max = panel.percentage && panel.stack ? 100 : panel.yaxes[1].max; - // autoscaleSpanOverride(panel.yaxes[1], data[1], secondY); options.yaxes.push(secondY); - - applyLogScale(options.yaxes[1], data); configureAxisMode(options.yaxes[1], panel.percentage && panel.stack ? "percent" : panel.yaxes[1].format); } From 34f15d92d0befdebd5e78a9e49422e7294aac82b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 27 Sep 2016 14:39:51 +0200 Subject: [PATCH 21/32] feat(testdata): worked on testdata app --- pkg/api/dtos/models.go | 7 +- pkg/api/index.go | 2 +- pkg/api/metrics.go | 48 ++++++++----- pkg/plugins/frontend_plugin.go | 7 +- pkg/services/alerting/conditions/query.go | 8 +-- pkg/services/alerting/init/init.go | 1 + pkg/tsdb/models.go | 21 +++--- pkg/tsdb/testdata/testdata.go | 54 ++++++++++++++ pkg/tsdb/time_range.go | 11 ++- pkg/tsdb/time_range_test.go | 17 +++++ public/app/core/time_series2.ts | 3 + public/app/core/utils/kbn.js | 10 ++- .../app/features/panel/metrics_panel_ctrl.ts | 19 ++++- .../features/templating/interval_variable.ts | 4 +- .../testdata/dashboards/graph_last_1h.json | 5 ++ .../app/testdata/datasource/datasource.ts | 45 ++++++++++++ .../plugins/app/testdata/datasource/module.ts | 22 ++++++ .../app/testdata/datasource/plugin.json | 19 +++++ .../app/testdata/datasource/query_ctrl.ts | 24 +++++++ public/app/plugins/app/testdata/module.ts | 36 ++++++++++ .../app/testdata/partials/query.editor.html | 22 ++++++ public/app/plugins/app/testdata/plugin.json | 27 +++++++ .../app/plugins/panel/graph/data_processor.ts | 68 +++++++++--------- .../app/plugins/panel/graph/graph_tooltip.js | 14 ++-- public/app/plugins/panel/graph/module.ts | 16 +++-- .../panel/graph/specs/graph_ctrl_specs.ts | 58 +++++++-------- public/app/plugins/panel/graph/template.ts | 9 ++- .../app/plugins/panel/pluginlist/plugin.json | 2 +- public/test/core/time_series_specs.js | 32 +++++++++ public/test/core/utils/kbn_specs.js | 70 ++++++++++--------- 30 files changed, 512 insertions(+), 169 deletions(-) create mode 100644 pkg/tsdb/testdata/testdata.go create mode 100644 public/app/plugins/app/testdata/dashboards/graph_last_1h.json create mode 100644 public/app/plugins/app/testdata/datasource/datasource.ts create mode 100644 public/app/plugins/app/testdata/datasource/module.ts create mode 100644 public/app/plugins/app/testdata/datasource/plugin.json create mode 100644 public/app/plugins/app/testdata/datasource/query_ctrl.ts create mode 100644 public/app/plugins/app/testdata/module.ts create mode 100644 public/app/plugins/app/testdata/partials/query.editor.html create mode 100644 public/app/plugins/app/testdata/plugin.json diff --git a/pkg/api/dtos/models.go b/pkg/api/dtos/models.go index 8bfc9f9138d..143ee5b98d5 100644 --- a/pkg/api/dtos/models.go +++ b/pkg/api/dtos/models.go @@ -97,12 +97,7 @@ func (slice DataSourceList) Swap(i, j int) { } type MetricQueryResultDto struct { - Data []MetricQueryResultDataDto `json:"data"` -} - -type MetricQueryResultDataDto struct { - Target string `json:"target"` - DataPoints [][2]float64 `json:"datapoints"` + Data []interface{} `json:"data"` } type UserStars struct { diff --git a/pkg/api/index.go b/pkg/api/index.go index 063e91ef5da..385810b942e 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -165,7 +165,7 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) { } } - if c.OrgRole == m.ROLE_ADMIN { + if len(appLink.Children) > 0 && c.OrgRole == m.ROLE_ADMIN { appLink.Children = append(appLink.Children, &dtos.NavLink{Divider: true}) appLink.Children = append(appLink.Children, &dtos.NavLink{Text: "Plugin Config", Icon: "fa fa-cog", Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/edit"}) } diff --git a/pkg/api/metrics.go b/pkg/api/metrics.go index 154f863af53..c36f2108581 100644 --- a/pkg/api/metrics.go +++ b/pkg/api/metrics.go @@ -2,39 +2,49 @@ package api import ( "encoding/json" - "math/rand" "net/http" - "strconv" "github.com/grafana/grafana/pkg/api/dtos" "github.com/grafana/grafana/pkg/metrics" "github.com/grafana/grafana/pkg/middleware" + "github.com/grafana/grafana/pkg/tsdb" "github.com/grafana/grafana/pkg/util" ) func GetTestMetrics(c *middleware.Context) Response { - from := c.QueryInt64("from") - to := c.QueryInt64("to") - maxDataPoints := c.QueryInt64("maxDataPoints") - stepInSeconds := (to - from) / maxDataPoints + + timeRange := tsdb.NewTimeRange(c.Query("from"), c.Query("to")) + + req := &tsdb.Request{ + TimeRange: timeRange, + Queries: []*tsdb.Query{ + { + RefId: "A", + MaxDataPoints: c.QueryInt64("maxDataPoints"), + IntervalMs: c.QueryInt64("intervalMs"), + DataSource: &tsdb.DataSourceInfo{ + Name: "Grafana TestDataDB", + PluginId: "grafana-testdata-datasource", + }, + }, + }, + } + + resp, err := tsdb.HandleRequest(req) + if err != nil { + return ApiError(500, "Metric request error", err) + } result := dtos.MetricQueryResultDto{} - result.Data = make([]dtos.MetricQueryResultDataDto, 1) - for seriesIndex := range result.Data { - points := make([][2]float64, maxDataPoints) - walker := rand.Float64() * 100 - time := from - - for i := range points { - points[i][0] = walker - points[i][1] = float64(time) - walker += rand.Float64() - 0.5 - time += stepInSeconds + for _, v := range resp.Results { + if v.Error != nil { + return ApiError(500, "tsdb.HandleRequest() response error", v.Error) } - result.Data[seriesIndex].Target = "test-series-" + strconv.Itoa(seriesIndex) - result.Data[seriesIndex].DataPoints = points + for _, series := range v.Series { + result.Data = append(result.Data, series) + } } return Json(200, &result) diff --git a/pkg/plugins/frontend_plugin.go b/pkg/plugins/frontend_plugin.go index 974559001d1..8db480f947d 100644 --- a/pkg/plugins/frontend_plugin.go +++ b/pkg/plugins/frontend_plugin.go @@ -43,7 +43,12 @@ func (fp *FrontendPluginBase) setPathsBasedOnApp(app *AppPlugin) { appSubPath := strings.Replace(fp.PluginDir, app.PluginDir, "", 1) fp.IncludedInAppId = app.Id fp.BaseUrl = app.BaseUrl - fp.Module = util.JoinUrlFragments("plugins/"+app.Id, appSubPath) + "/module" + + if isExternalPlugin(app.PluginDir) { + fp.Module = util.JoinUrlFragments("plugins/"+app.Id, appSubPath) + "/module" + } else { + fp.Module = util.JoinUrlFragments("app/plugins/app/"+app.Id, appSubPath) + "/module" + } } func (fp *FrontendPluginBase) handleModuleDefaults() { diff --git a/pkg/services/alerting/conditions/query.go b/pkg/services/alerting/conditions/query.go index 15db31838b0..e808d77a182 100644 --- a/pkg/services/alerting/conditions/query.go +++ b/pkg/services/alerting/conditions/query.go @@ -34,8 +34,8 @@ type AlertQuery struct { } func (c *QueryCondition) Eval(context *alerting.EvalContext) { - timerange := tsdb.NewTimerange(c.Query.From, c.Query.To) - seriesList, err := c.executeQuery(context, timerange) + timeRange := tsdb.NewTimeRange(c.Query.From, c.Query.To) + seriesList, err := c.executeQuery(context, timeRange) if err != nil { context.Error = err return @@ -69,7 +69,7 @@ func (c *QueryCondition) Eval(context *alerting.EvalContext) { context.Firing = len(context.EvalMatches) > 0 } -func (c *QueryCondition) executeQuery(context *alerting.EvalContext, timerange tsdb.TimeRange) (tsdb.TimeSeriesSlice, error) { +func (c *QueryCondition) executeQuery(context *alerting.EvalContext, timeRange tsdb.TimeRange) (tsdb.TimeSeriesSlice, error) { getDsInfo := &m.GetDataSourceByIdQuery{ Id: c.Query.DatasourceId, OrgId: context.Rule.OrgId, @@ -79,7 +79,7 @@ func (c *QueryCondition) executeQuery(context *alerting.EvalContext, timerange t return nil, fmt.Errorf("Could not find datasource") } - req := c.getRequestForAlertRule(getDsInfo.Result, timerange) + req := c.getRequestForAlertRule(getDsInfo.Result, timeRange) result := make(tsdb.TimeSeriesSlice, 0) resp, err := c.HandleRequest(req) diff --git a/pkg/services/alerting/init/init.go b/pkg/services/alerting/init/init.go index b9cba2fd353..94f97a41905 100644 --- a/pkg/services/alerting/init/init.go +++ b/pkg/services/alerting/init/init.go @@ -7,6 +7,7 @@ import ( "github.com/grafana/grafana/pkg/setting" _ "github.com/grafana/grafana/pkg/tsdb/graphite" _ "github.com/grafana/grafana/pkg/tsdb/prometheus" + _ "github.com/grafana/grafana/pkg/tsdb/testdata" ) var engine *alerting.Engine diff --git a/pkg/tsdb/models.go b/pkg/tsdb/models.go index 262be5cbd24..b8c55f1e60d 100644 --- a/pkg/tsdb/models.go +++ b/pkg/tsdb/models.go @@ -3,21 +3,22 @@ package tsdb import "github.com/grafana/grafana/pkg/components/simplejson" type Query struct { - RefId string - Query string - Model *simplejson.Json - Depends []string - DataSource *DataSourceInfo - Results []*TimeSeries - Exclude bool + RefId string + Query string + Model *simplejson.Json + Depends []string + DataSource *DataSourceInfo + Results []*TimeSeries + Exclude bool + MaxDataPoints int64 + IntervalMs int64 } type QuerySlice []*Query type Request struct { - TimeRange TimeRange - MaxDataPoints int - Queries QuerySlice + TimeRange TimeRange + Queries QuerySlice } type Response struct { diff --git a/pkg/tsdb/testdata/testdata.go b/pkg/tsdb/testdata/testdata.go new file mode 100644 index 00000000000..b1eca4cb46a --- /dev/null +++ b/pkg/tsdb/testdata/testdata.go @@ -0,0 +1,54 @@ +package testdata + +import ( + "math/rand" + + "github.com/grafana/grafana/pkg/tsdb" +) + +type TestDataExecutor struct { + *tsdb.DataSourceInfo +} + +func NewTestDataExecutor(dsInfo *tsdb.DataSourceInfo) tsdb.Executor { + return &TestDataExecutor{dsInfo} +} + +func init() { + tsdb.RegisterExecutor("grafana-testdata-datasource", NewTestDataExecutor) +} + +func (e *TestDataExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult { + result := &tsdb.BatchResult{} + result.QueryResults = make(map[string]*tsdb.QueryResult) + + from, _ := context.TimeRange.FromTime() + to, _ := context.TimeRange.ToTime() + + queryRes := &tsdb.QueryResult{} + + for _, query := range queries { + // scenario := query.Model.Get("scenario").MustString("random_walk") + series := &tsdb.TimeSeries{Name: "test-series-0"} + + stepInSeconds := (to.Unix() - from.Unix()) / query.MaxDataPoints + points := make([][2]*float64, 0) + walker := rand.Float64() * 100 + time := from.Unix() + + for i := int64(0); i < query.MaxDataPoints; i++ { + timestamp := float64(time) + val := float64(walker) + points = append(points, [2]*float64{&val, ×tamp}) + + walker += rand.Float64() - 0.5 + time += stepInSeconds + } + + series.Points = points + queryRes.Series = append(queryRes.Series, series) + } + + result.QueryResults["A"] = queryRes + return result +} diff --git a/pkg/tsdb/time_range.go b/pkg/tsdb/time_range.go index 8e1a1c66e3d..dee3e683516 100644 --- a/pkg/tsdb/time_range.go +++ b/pkg/tsdb/time_range.go @@ -2,11 +2,12 @@ package tsdb import ( "fmt" + "strconv" "strings" "time" ) -func NewTimerange(from, to string) TimeRange { +func NewTimeRange(from, to string) TimeRange { return TimeRange{ From: from, To: to, @@ -21,6 +22,10 @@ type TimeRange struct { } func (tr TimeRange) FromTime() (time.Time, error) { + if val, err := strconv.ParseInt(tr.From, 10, 64); err == nil { + return time.Unix(val, 0), nil + } + fromRaw := strings.Replace(tr.From, "now-", "", 1) diff, err := time.ParseDuration("-" + fromRaw) @@ -45,5 +50,9 @@ func (tr TimeRange) ToTime() (time.Time, error) { return tr.Now.Add(diff), nil } + if val, err := strconv.ParseInt(tr.To, 10, 64); err == nil { + return time.Unix(val, 0), nil + } + return time.Time{}, fmt.Errorf("cannot parse to value %s", tr.To) } diff --git a/pkg/tsdb/time_range_test.go b/pkg/tsdb/time_range_test.go index 56ea9d24490..f4acb5e6d80 100644 --- a/pkg/tsdb/time_range_test.go +++ b/pkg/tsdb/time_range_test.go @@ -60,6 +60,23 @@ func TestTimeRange(t *testing.T) { }) }) + Convey("can parse unix epocs", func() { + var err error + tr := TimeRange{ + From: "1474973725473", + To: "1474975757930", + Now: now, + } + + res, err := tr.FromTime() + So(err, ShouldBeNil) + So(res.Unix(), ShouldEqual, 1474973725473) + + res, err = tr.ToTime() + So(err, ShouldBeNil) + So(res.Unix(), ShouldEqual, 1474975757930) + }) + Convey("Cannot parse asdf", func() { var err error tr := TimeRange{ diff --git a/public/app/core/time_series2.ts b/public/app/core/time_series2.ts index dfae26fb48b..d672e0dd0dc 100644 --- a/public/app/core/time_series2.ts +++ b/public/app/core/time_series2.ts @@ -31,6 +31,8 @@ export default class TimeSeries { allIsZero: boolean; decimals: number; scaledDecimals: number; + hasMsResolution: boolean; + isOutsideRange: boolean; lines: any; bars: any; @@ -54,6 +56,7 @@ export default class TimeSeries { this.stats = {}; this.legend = true; this.unit = opts.unit; + this.hasMsResolution = this.isMsResolutionNeeded(); } applySeriesOverrides(overrides) { diff --git a/public/app/core/utils/kbn.js b/public/app/core/utils/kbn.js index cf80d671d71..a807a249235 100644 --- a/public/app/core/utils/kbn.js +++ b/public/app/core/utils/kbn.js @@ -174,7 +174,10 @@ function($, _, moment) { lowLimitMs = kbn.interval_to_ms(lowLimitInterval); } else { - return userInterval; + return { + intervalMs: kbn.interval_to_ms(userInterval), + interval: userInterval, + }; } } @@ -183,7 +186,10 @@ function($, _, moment) { intervalMs = lowLimitMs; } - return kbn.secondsToHms(intervalMs / 1000); + return { + intervalMs: intervalMs, + interval: kbn.secondsToHms(intervalMs / 1000), + }; }; kbn.describe_interval = function (string) { diff --git a/public/app/features/panel/metrics_panel_ctrl.ts b/public/app/features/panel/metrics_panel_ctrl.ts index 62cece44acf..f6f2d730cd3 100644 --- a/public/app/features/panel/metrics_panel_ctrl.ts +++ b/public/app/features/panel/metrics_panel_ctrl.ts @@ -25,6 +25,7 @@ class MetricsPanelCtrl extends PanelCtrl { range: any; rangeRaw: any; interval: any; + intervalMs: any; resolution: any; timeInfo: any; skipDataOnInit: boolean; @@ -123,11 +124,22 @@ class MetricsPanelCtrl extends PanelCtrl { this.resolution = Math.ceil($(window).width() * (this.panel.span / 12)); } - var panelInterval = this.panel.interval; - var datasourceInterval = (this.datasource || {}).interval; - this.interval = kbn.calculateInterval(this.range, this.resolution, panelInterval || datasourceInterval); + this.calculateInterval(); }; + calculateInterval() { + var intervalOverride = this.panel.interval; + + // if no panel interval check datasource + if (!intervalOverride && this.datasource && this.datasource.interval) { + intervalOverride = this.datasource.interval; + } + + var res = kbn.calculateInterval(this.range, this.resolution, intervalOverride); + this.interval = res.interval; + this.intervalMs = res.intervalMs; + } + applyPanelTimeOverrides() { this.timeInfo = ''; @@ -183,6 +195,7 @@ class MetricsPanelCtrl extends PanelCtrl { range: this.range, rangeRaw: this.rangeRaw, interval: this.interval, + intervalMs: this.intervalMs, targets: this.panel.targets, format: this.panel.renderer === 'png' ? 'png' : 'json', maxDataPoints: this.resolution, diff --git a/public/app/features/templating/interval_variable.ts b/public/app/features/templating/interval_variable.ts index 30b056e1c60..a1cfbf324c0 100644 --- a/public/app/features/templating/interval_variable.ts +++ b/public/app/features/templating/interval_variable.ts @@ -54,8 +54,8 @@ export class IntervalVariable implements Variable { this.options.unshift({ text: 'auto', value: '$__auto_interval' }); } - var interval = kbn.calculateInterval(this.timeSrv.timeRange(), this.auto_count, (this.auto_min ? ">"+this.auto_min : null)); - this.templateSrv.setGrafanaVariable('$__auto_interval', interval); + var res = kbn.calculateInterval(this.timeSrv.timeRange(), this.auto_count, (this.auto_min ? ">"+this.auto_min : null)); + this.templateSrv.setGrafanaVariable('$__auto_interval', res.interval); } updateOptions() { diff --git a/public/app/plugins/app/testdata/dashboards/graph_last_1h.json b/public/app/plugins/app/testdata/dashboards/graph_last_1h.json new file mode 100644 index 00000000000..7533a44760b --- /dev/null +++ b/public/app/plugins/app/testdata/dashboards/graph_last_1h.json @@ -0,0 +1,5 @@ +{ + "title": "TestData - Graph Panel Last 1h", + "tags": ["testdata"], + "revision": 1 +} diff --git a/public/app/plugins/app/testdata/datasource/datasource.ts b/public/app/plugins/app/testdata/datasource/datasource.ts new file mode 100644 index 00000000000..75b43bd34c9 --- /dev/null +++ b/public/app/plugins/app/testdata/datasource/datasource.ts @@ -0,0 +1,45 @@ +/// + +import _ from 'lodash'; + +class TestDataDatasource { + + /** @ngInject */ + constructor(private backendSrv, private $q) {} + + query(options) { + var queries = _.filter(options.targets, item => { + return item.hide !== true; + }); + + if (queries.length === 0) { + return this.$q.when({data: []}); + } + + return this.backendSrv.get('/api/metrics/test', { + from: options.range.from.valueOf(), + to: options.range.to.valueOf(), + scenario: options.targets[0].scenario, + interval: options.intervalMs, + maxDataPoints: options.maxDataPoints, + }).then(res => { + res.data = res.data.map(item => { + return {target: item.name, datapoints: item.points}; + }); + + return res; + }); + } + + annotationQuery(options) { + return this.backendSrv.get('/api/annotations', { + from: options.range.from.valueOf(), + to: options.range.to.valueOf(), + limit: options.limit, + type: options.type, + }); + } + +} + +export {TestDataDatasource}; diff --git a/public/app/plugins/app/testdata/datasource/module.ts b/public/app/plugins/app/testdata/datasource/module.ts new file mode 100644 index 00000000000..309b7443836 --- /dev/null +++ b/public/app/plugins/app/testdata/datasource/module.ts @@ -0,0 +1,22 @@ +/// + +import {TestDataDatasource} from './datasource'; +import {TestDataQueryCtrl} from './query_ctrl'; + +class TestDataAnnotationsQueryCtrl { + annotation: any; + + constructor() { + } + + static template = '

test data

'; +} + + +export { + TestDataDatasource, + TestDataDatasource as Datasource, + TestDataQueryCtrl as QueryCtrl, + TestDataAnnotationsQueryCtrl as AnnotationsQueryCtrl, +}; + diff --git a/public/app/plugins/app/testdata/datasource/plugin.json b/public/app/plugins/app/testdata/datasource/plugin.json new file mode 100644 index 00000000000..0ad87a15081 --- /dev/null +++ b/public/app/plugins/app/testdata/datasource/plugin.json @@ -0,0 +1,19 @@ +{ + "type": "datasource", + "name": "Grafana TestDataDB", + "id": "grafana-testdata-datasource", + + "metrics": true, + "annotations": true, + + "info": { + "author": { + "name": "Grafana Project", + "url": "http://grafana.org" + }, + "logos": { + "small": "", + "large": "" + } + } +} diff --git a/public/app/plugins/app/testdata/datasource/query_ctrl.ts b/public/app/plugins/app/testdata/datasource/query_ctrl.ts new file mode 100644 index 00000000000..44a62fd1a11 --- /dev/null +++ b/public/app/plugins/app/testdata/datasource/query_ctrl.ts @@ -0,0 +1,24 @@ +/// + +import {TestDataDatasource} from './datasource'; +import {QueryCtrl} from 'app/plugins/sdk'; + +export class TestDataQueryCtrl extends QueryCtrl { + static templateUrl = 'partials/query.editor.html'; + + scenarioDefs: any; + + /** @ngInject **/ + constructor($scope, $injector) { + super($scope, $injector); + + this.target.scenario = this.target.scenario || 'random_walk'; + + this.scenarioDefs = { + 'random_walk': {text: 'Random Walk'}, + 'no_datapoints': {text: 'No Datapoints'}, + 'data_outside_range': {text: 'Data Outside Range'}, + }; + } +} + diff --git a/public/app/plugins/app/testdata/module.ts b/public/app/plugins/app/testdata/module.ts new file mode 100644 index 00000000000..dee1679637a --- /dev/null +++ b/public/app/plugins/app/testdata/module.ts @@ -0,0 +1,36 @@ +/// + +export class ConfigCtrl { + static template = ''; + + appEditCtrl: any; + + constructor(private backendSrv) { + this.appEditCtrl.setPreUpdateHook(this.initDatasource.bind(this)); + } + + initDatasource() { + return this.backendSrv.get('/api/datasources').then(res => { + var found = false; + for (let ds of res) { + if (ds.type === "grafana-testdata-datasource") { + found = true; + } + } + + if (!found) { + var dsInstance = { + name: 'Grafana TestData', + type: 'grafana-testdata-datasource', + access: 'direct', + jsonData: {} + }; + + return this.backendSrv.post('/api/datasources', dsInstance); + } + + return Promise.resolve(); + }); + } +} + diff --git a/public/app/plugins/app/testdata/partials/query.editor.html b/public/app/plugins/app/testdata/partials/query.editor.html new file mode 100644 index 00000000000..d9068dfda49 --- /dev/null +++ b/public/app/plugins/app/testdata/partials/query.editor.html @@ -0,0 +1,22 @@ + +
+
+ +
+ +
+
+
+ + +
+
+ + +
+
+
+
+
+
+ diff --git a/public/app/plugins/app/testdata/plugin.json b/public/app/plugins/app/testdata/plugin.json new file mode 100644 index 00000000000..47ab291409b --- /dev/null +++ b/public/app/plugins/app/testdata/plugin.json @@ -0,0 +1,27 @@ +{ + "type": "app", + "name": "Grafana TestData", + "id": "testdata", + + "info": { + "description": "Grafana test data app", + "author": { + "name": "Grafana Project", + "url": "http://grafana.org" + }, + "version": "1.0.5", + "updated": "2016-09-26" + }, + + "includes": [ + { + "type": "dashboard", + "name": "TestData - Graph Last 1h", + "path": "dashboards/graph_last_1h.json" + } + ], + + "dependencies": { + "grafanaVersion": "4.x.x" + } +} diff --git a/public/app/plugins/panel/graph/data_processor.ts b/public/app/plugins/panel/graph/data_processor.ts index b22d9f391d0..404b5a90aee 100644 --- a/public/app/plugins/panel/graph/data_processor.ts +++ b/public/app/plugins/panel/graph/data_processor.ts @@ -2,6 +2,7 @@ import kbn from 'app/core/utils/kbn'; import _ from 'lodash'; +import moment from 'moment'; import TimeSeries from 'app/core/time_series2'; import {colors} from 'app/core/core'; @@ -28,8 +29,10 @@ export class DataProcessor { switch (this.panel.xaxis.mode) { case 'series': - case 'time': { - return options.dataList.map(this.timeSeriesHandler.bind(this)); + case 'time': { + return options.dataList.map((item, index) => { + return this.timeSeriesHandler(item, index, options); + }); } case 'field': { return this.customHandler(firstItem); @@ -74,33 +77,26 @@ export class DataProcessor { } } - seriesHandler(seriesData, index, datapoints, alias) { + timeSeriesHandler(seriesData, index, options) { + var datapoints = seriesData.datapoints; + var alias = seriesData.target; + var colorIndex = index % colors.length; var color = this.panel.aliasColors[alias] || colors[colorIndex]; var series = new TimeSeries({datapoints: datapoints, alias: alias, color: color, unit: seriesData.unit}); - // if (datapoints && datapoints.length > 0) { - // var last = moment.utc(datapoints[datapoints.length - 1][1]); - // var from = moment.utc(this.range.from); - // if (last - from < -10000) { - // this.datapointsOutside = true; - // } - // - // this.datapointsCount += datapoints.length; - // this.panel.tooltip.msResolution = this.panel.tooltip.msResolution || series.isMsResolutionNeeded(); - // } + if (datapoints && datapoints.length > 0) { + var last = datapoints[datapoints.length - 1][1]; + var from = options.range.from; + if (last - from < -10000) { + series.isOutsideRange = true; + } + } return series; } - timeSeriesHandler(seriesData, index) { - var datapoints = seriesData.datapoints; - var alias = seriesData.target; - - return this.seriesHandler(seriesData, index, datapoints, alias); - } - customHandler(dataItem) { console.log('custom', dataItem); let nameField = this.panel.xaxis.name; @@ -126,21 +122,21 @@ export class DataProcessor { return []; } - tableHandler(seriesData, index) { - var xColumnIndex = Number(this.panel.xaxis.columnIndex); - var valueColumnIndex = Number(this.panel.xaxis.valueColumnIndex); - var datapoints = _.map(seriesData.rows, (row) => { - var value = valueColumnIndex ? row[valueColumnIndex] : _.last(row); - return [ - value, // Y value - row[xColumnIndex] // X value - ]; - }); - - var alias = seriesData.columns[valueColumnIndex].text; - - return this.seriesHandler(seriesData, index, datapoints, alias); - } + // tableHandler(seriesData, index) { + // var xColumnIndex = Number(this.panel.xaxis.columnIndex); + // var valueColumnIndex = Number(this.panel.xaxis.valueColumnIndex); + // var datapoints = _.map(seriesData.rows, (row) => { + // var value = valueColumnIndex ? row[valueColumnIndex] : _.last(row); + // return [ + // value, // Y value + // row[xColumnIndex] // X value + // ]; + // }); + // + // var alias = seriesData.columns[valueColumnIndex].text; + // + // return this.seriesHandler(seriesData, index, datapoints, alias); + // } // esRawDocHandler(seriesData, index) { // let xField = this.panel.xaxis.esField; @@ -160,7 +156,7 @@ export class DataProcessor { // var alias = valueField; // return this.seriesHandler(seriesData, index, datapoints, alias); // } - // + validateXAxisSeriesValue() { switch (this.panel.xaxis.mode) { case 'series': { diff --git a/public/app/plugins/panel/graph/graph_tooltip.js b/public/app/plugins/panel/graph/graph_tooltip.js index 70eef7c5fe3..cd3bddf41ef 100644 --- a/public/app/plugins/panel/graph/graph_tooltip.js +++ b/public/app/plugins/panel/graph/graph_tooltip.js @@ -121,20 +121,20 @@ function ($, _) { var seriesList = getSeriesFn(); var group, value, absoluteTime, hoverInfo, i, series, seriesHtml, tooltipFormat; - if (panel.tooltip.msResolution) { - tooltipFormat = 'YYYY-MM-DD HH:mm:ss.SSS'; - } else { - tooltipFormat = 'YYYY-MM-DD HH:mm:ss'; - } - if (dashboard.sharedCrosshair) { - ctrl.publishAppEvent('setCrosshair', { pos: pos, scope: scope }); + ctrl.publishAppEvent('setCrosshair', {pos: pos, scope: scope}); } if (seriesList.length === 0) { return; } + if (seriesList[0].hasMsResolution) { + tooltipFormat = 'YYYY-MM-DD HH:mm:ss.SSS'; + } else { + tooltipFormat = 'YYYY-MM-DD HH:mm:ss'; + } + if (panel.tooltip.shared) { plot.unhighlight(); diff --git a/public/app/plugins/panel/graph/module.ts b/public/app/plugins/panel/graph/module.ts index 9edcb7fe1ff..f6d77636845 100644 --- a/public/app/plugins/panel/graph/module.ts +++ b/public/app/plugins/panel/graph/module.ts @@ -25,7 +25,6 @@ class GraphCtrl extends MetricsPanelCtrl { annotationsPromise: any; datapointsCount: number; datapointsOutside: boolean; - datapointsWarning: boolean; colors: any = []; subTabIndex: number; processor: DataProcessor; @@ -172,13 +171,20 @@ class GraphCtrl extends MetricsPanelCtrl { } onDataReceived(dataList) { - this.datapointsWarning = false; - this.datapointsCount = 0; - this.datapointsOutside = false; this.dataList = dataList; this.seriesList = this.processor.getSeriesList({dataList: dataList, range: this.range}); - this.datapointsWarning = this.datapointsCount === 0 || this.datapointsOutside; + + this.datapointsCount = this.seriesList.reduce((prev, series) => { + return prev + series.datapoints.length; + }, 0); + + this.datapointsOutside = false; + for (let series of this.seriesList) { + if (series.isOutsideRange) { + this.datapointsOutside = true; + } + } this.annotationsPromise.then(annotations => { this.loading = false; diff --git a/public/app/plugins/panel/graph/specs/graph_ctrl_specs.ts b/public/app/plugins/panel/graph/specs/graph_ctrl_specs.ts index c7d981f78d4..cac69807eab 100644 --- a/public/app/plugins/panel/graph/specs/graph_ctrl_specs.ts +++ b/public/app/plugins/panel/graph/specs/graph_ctrl_specs.ts @@ -3,6 +3,7 @@ import {describe, beforeEach, it, sinon, expect, angularMocks} from '../../../../../test/lib/common'; import angular from 'angular'; +import moment from 'moment'; import {GraphCtrl} from '../module'; import helpers from '../../../../../test/specs/helpers'; @@ -19,64 +20,53 @@ describe('GraphCtrl', function() { ctx.ctrl.updateTimeRange(); }); - describe.skip('msResolution with second resolution timestamps', function() { + describe('when time series are outside range', function() { + beforeEach(function() { var data = [ - { target: 'test.cpu1', datapoints: [[45, 1234567890], [60, 1234567899]]}, - { target: 'test.cpu2', datapoints: [[55, 1236547890], [90, 1234456709]]} + {target: 'test.cpu1', datapoints: [[45, 1234567890], [60, 1234567899]]}, ]; - ctx.ctrl.panel.tooltip.msResolution = false; + + ctx.ctrl.range = {from: moment().valueOf(), to: moment().valueOf()}; ctx.ctrl.onDataReceived(data); }); - it('should not show millisecond resolution tooltip', function() { - expect(ctx.ctrl.panel.tooltip.msResolution).to.be(false); + it('should set datapointsOutside', function() { + expect(ctx.ctrl.datapointsOutside).to.be(true); }); }); - describe.skip('msResolution with millisecond resolution timestamps', function() { + describe('when time series are inside range', function() { beforeEach(function() { + var range = { + from: moment().subtract(1, 'days').valueOf(), + to: moment().valueOf() + }; + var data = [ - { target: 'test.cpu1', datapoints: [[45, 1234567890000], [60, 1234567899000]]}, - { target: 'test.cpu2', datapoints: [[55, 1236547890001], [90, 1234456709000]]} + {target: 'test.cpu1', datapoints: [[45, range.from + 1000], [60, range.from + 10000]]}, ]; - ctx.ctrl.panel.tooltip.msResolution = false; + + ctx.ctrl.range = range; ctx.ctrl.onDataReceived(data); }); - it('should show millisecond resolution tooltip', function() { - expect(ctx.ctrl.panel.tooltip.msResolution).to.be(true); + it('should set datapointsOutside', function() { + expect(ctx.ctrl.datapointsOutside).to.be(false); }); }); - describe.skip('msResolution with millisecond resolution timestamps but with trailing zeroes', function() { + describe('datapointsCount given 2 series', function() { beforeEach(function() { var data = [ - { target: 'test.cpu1', datapoints: [[45, 1234567890000], [60, 1234567899000]]}, - { target: 'test.cpu2', datapoints: [[55, 1236547890000], [90, 1234456709000]]} + {target: 'test.cpu1', datapoints: [[45, 1234567890], [60, 1234567899]]}, + {target: 'test.cpu2', datapoints: [[45, 1234567890]]}, ]; - ctx.ctrl.panel.tooltip.msResolution = false; ctx.ctrl.onDataReceived(data); }); - it('should not show millisecond resolution tooltip', function() { - expect(ctx.ctrl.panel.tooltip.msResolution).to.be(false); - }); - }); - - describe.skip('msResolution with millisecond resolution timestamps in one of the series', function() { - beforeEach(function() { - var data = [ - { target: 'test.cpu1', datapoints: [[45, 1234567890000], [60, 1234567899000]]}, - { target: 'test.cpu2', datapoints: [[55, 1236547890010], [90, 1234456709000]]}, - { target: 'test.cpu3', datapoints: [[65, 1236547890000], [120, 1234456709000]]} - ]; - ctx.ctrl.panel.tooltip.msResolution = false; - ctx.ctrl.onDataReceived(data); - }); - - it('should show millisecond resolution tooltip', function() { - expect(ctx.ctrl.panel.tooltip.msResolution).to.be(true); + it('should set datapointsCount to sum of datapoints', function() { + expect(ctx.ctrl.datapointsCount).to.be(3); }); }); diff --git a/public/app/plugins/panel/graph/template.ts b/public/app/plugins/panel/graph/template.ts index fc989e659c7..ec6cd8d0907 100644 --- a/public/app/plugins/panel/graph/template.ts +++ b/public/app/plugins/panel/graph/template.ts @@ -2,11 +2,14 @@ var template = `
-
- +
+ No datapoints No datapoints returned from metric query - +
+ +
+ Datapoints outside time range Can be caused by timezone mismatch between browser and graphite server diff --git a/public/app/plugins/panel/pluginlist/plugin.json b/public/app/plugins/panel/pluginlist/plugin.json index be6ae9a5985..72f5ea06d25 100644 --- a/public/app/plugins/panel/pluginlist/plugin.json +++ b/public/app/plugins/panel/pluginlist/plugin.json @@ -7,7 +7,7 @@ "author": { "name": "Grafana Project", "url": "http://grafana.org" -}, + }, "logos": { "small": "img/icn-dashlist-panel.svg", "large": "img/icn-dashlist-panel.svg" diff --git a/public/test/core/time_series_specs.js b/public/test/core/time_series_specs.js index 034e872e2f1..2b325cf6d46 100644 --- a/public/test/core/time_series_specs.js +++ b/public/test/core/time_series_specs.js @@ -56,6 +56,38 @@ define([ }); }); + describe('When checking if ms resolution is needed', function() { + describe('msResolution with second resolution timestamps', function() { + beforeEach(function() { + series = new TimeSeries({datapoints: [[45, 1234567890], [60, 1234567899]]}); + }); + + it('should set hasMsResolution to false', function() { + expect(series.hasMsResolution).to.be(false); + }); + }); + + describe('msResolution with millisecond resolution timestamps', function() { + beforeEach(function() { + series = new TimeSeries({datapoints: [[55, 1236547890001], [90, 1234456709000]]}); + }); + + it('should show millisecond resolution tooltip', function() { + expect(series.hasMsResolution).to.be(true); + }); + }); + + describe('msResolution with millisecond resolution timestamps but with trailing zeroes', function() { + beforeEach(function() { + series = new TimeSeries({datapoints: [[45, 1234567890000], [60, 1234567899000]]}); + }); + + it('should not show millisecond resolution tooltip', function() { + expect(series.hasMsResolution).to.be(false); + }); + }); + }); + describe('can detect if series contains ms precision', function() { var fakedata; diff --git a/public/test/core/utils/kbn_specs.js b/public/test/core/utils/kbn_specs.js index 959b176b06c..95bef57ef1a 100644 --- a/public/test/core/utils/kbn_specs.js +++ b/public/test/core/utils/kbn_specs.js @@ -132,62 +132,64 @@ define([ describe('calculateInterval', function() { it('1h 100 resultion', function() { var range = { from: dateMath.parse('now-1h'), to: dateMath.parse('now') }; - var str = kbn.calculateInterval(range, 100, null); - expect(str).to.be('30s'); + var res = kbn.calculateInterval(range, 100, null); + expect(res.interval).to.be('30s'); }); it('10m 1600 resolution', function() { var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') }; - var str = kbn.calculateInterval(range, 1600, null); - expect(str).to.be('500ms'); + var res = kbn.calculateInterval(range, 1600, null); + expect(res.interval).to.be('500ms'); + expect(res.intervalMs).to.be(500); }); it('fixed user interval', function() { var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') }; - var str = kbn.calculateInterval(range, 1600, '10s'); - expect(str).to.be('10s'); + var res = kbn.calculateInterval(range, 1600, '10s'); + expect(res.interval).to.be('10s'); + expect(res.intervalMs).to.be(10000); }); it('short time range and user low limit', function() { var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') }; - var str = kbn.calculateInterval(range, 1600, '>10s'); - expect(str).to.be('10s'); + var res = kbn.calculateInterval(range, 1600, '>10s'); + expect(res.interval).to.be('10s'); }); it('large time range and user low limit', function() { - var range = { from: dateMath.parse('now-14d'), to: dateMath.parse('now') }; - var str = kbn.calculateInterval(range, 1000, '>10s'); - expect(str).to.be('20m'); + var range = {from: dateMath.parse('now-14d'), to: dateMath.parse('now')}; + var res = kbn.calculateInterval(range, 1000, '>10s'); + expect(res.interval).to.be('20m'); }); - + it('10s 900 resolution and user low limit in ms', function() { var range = { from: dateMath.parse('now-10s'), to: dateMath.parse('now') }; - var str = kbn.calculateInterval(range, 900, '>15ms'); - expect(str).to.be('15ms'); + var res = kbn.calculateInterval(range, 900, '>15ms'); + expect(res.interval).to.be('15ms'); }); }); describe('hex', function() { - it('positive integer', function() { - var str = kbn.valueFormats.hex(100, 0); - expect(str).to.be('64'); - }); - it('negative integer', function() { - var str = kbn.valueFormats.hex(-100, 0); - expect(str).to.be('-64'); - }); - it('null', function() { - var str = kbn.valueFormats.hex(null, 0); - expect(str).to.be(''); - }); - it('positive float', function() { - var str = kbn.valueFormats.hex(50.52, 1); - expect(str).to.be('32.8'); - }); - it('negative float', function() { - var str = kbn.valueFormats.hex(-50.333, 2); - expect(str).to.be('-32.547AE147AE14'); - }); + it('positive integer', function() { + var str = kbn.valueFormats.hex(100, 0); + expect(str).to.be('64'); + }); + it('negative integer', function() { + var str = kbn.valueFormats.hex(-100, 0); + expect(str).to.be('-64'); + }); + it('null', function() { + var str = kbn.valueFormats.hex(null, 0); + expect(str).to.be(''); + }); + it('positive float', function() { + var str = kbn.valueFormats.hex(50.52, 1); + expect(str).to.be('32.8'); + }); + it('negative float', function() { + var str = kbn.valueFormats.hex(-50.333, 2); + expect(str).to.be('-32.547AE147AE14'); + }); }); describe('hex 0x', function() { From ade8aa5b923318f7a446e80230d96542bb758b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 27 Sep 2016 14:47:04 +0200 Subject: [PATCH 22/32] feat(graph): refactorings --- pkg/tsdb/models.go | 4 +- .../app/testdata/datasource/datasource.ts | 6 --- .../plugins/datasource/grafana/datasource.ts | 2 + .../app/plugins/panel/graph/data_processor.ts | 52 ------------------- 4 files changed, 4 insertions(+), 60 deletions(-) diff --git a/pkg/tsdb/models.go b/pkg/tsdb/models.go index b8c55f1e60d..0060f459d7b 100644 --- a/pkg/tsdb/models.go +++ b/pkg/tsdb/models.go @@ -56,8 +56,8 @@ type QueryResult struct { } type TimeSeries struct { - Name string `json:"name"` - Points [][2]*float64 `json:"points"` + Name string `json:"target"` + Points [][2]*float64 `json:"datapoints"` } type TimeSeriesSlice []*TimeSeries diff --git a/public/app/plugins/app/testdata/datasource/datasource.ts b/public/app/plugins/app/testdata/datasource/datasource.ts index 75b43bd34c9..32edfb59755 100644 --- a/public/app/plugins/app/testdata/datasource/datasource.ts +++ b/public/app/plugins/app/testdata/datasource/datasource.ts @@ -22,12 +22,6 @@ class TestDataDatasource { scenario: options.targets[0].scenario, interval: options.intervalMs, maxDataPoints: options.maxDataPoints, - }).then(res => { - res.data = res.data.map(item => { - return {target: item.name, datapoints: item.points}; - }); - - return res; }); } diff --git a/public/app/plugins/datasource/grafana/datasource.ts b/public/app/plugins/datasource/grafana/datasource.ts index 58799778acd..3ae030e4423 100644 --- a/public/app/plugins/datasource/grafana/datasource.ts +++ b/public/app/plugins/datasource/grafana/datasource.ts @@ -9,6 +9,8 @@ class GrafanaDatasource { return this.backendSrv.get('/api/metrics/test', { from: options.range.from.valueOf(), to: options.range.to.valueOf(), + scenario: 'random_walk', + interval: options.intervalMs, maxDataPoints: options.maxDataPoints }); } diff --git a/public/app/plugins/panel/graph/data_processor.ts b/public/app/plugins/panel/graph/data_processor.ts index 404b5a90aee..bb8af84cf00 100644 --- a/public/app/plugins/panel/graph/data_processor.ts +++ b/public/app/plugins/panel/graph/data_processor.ts @@ -98,65 +98,13 @@ export class DataProcessor { } customHandler(dataItem) { - console.log('custom', dataItem); let nameField = this.panel.xaxis.name; if (!nameField) { throw {message: 'No field name specified to use for x-axis, check your axes settings'}; } - - // let valueField = this.panel.xaxis.esValueField; - // let datapoints = _.map(seriesData.datapoints, (doc) => { - // return [ - // pluckDeep(doc, valueField), // Y value - // pluckDeep(doc, xField) // X value - // ]; - // }); - // - // // Remove empty points - // datapoints = _.filter(datapoints, (point) => { - // return point[0] !== undefined; - // }); - // - // var alias = valueField; - // re return []; } - // tableHandler(seriesData, index) { - // var xColumnIndex = Number(this.panel.xaxis.columnIndex); - // var valueColumnIndex = Number(this.panel.xaxis.valueColumnIndex); - // var datapoints = _.map(seriesData.rows, (row) => { - // var value = valueColumnIndex ? row[valueColumnIndex] : _.last(row); - // return [ - // value, // Y value - // row[xColumnIndex] // X value - // ]; - // }); - // - // var alias = seriesData.columns[valueColumnIndex].text; - // - // return this.seriesHandler(seriesData, index, datapoints, alias); - // } - - // esRawDocHandler(seriesData, index) { - // let xField = this.panel.xaxis.esField; - // let valueField = this.panel.xaxis.esValueField; - // let datapoints = _.map(seriesData.datapoints, (doc) => { - // return [ - // pluckDeep(doc, valueField), // Y value - // pluckDeep(doc, xField) // X value - // ]; - // }); - // - // // Remove empty points - // datapoints = _.filter(datapoints, (point) => { - // return point[0] !== undefined; - // }); - // - // var alias = valueField; - // return this.seriesHandler(seriesData, index, datapoints, alias); - // } - validateXAxisSeriesValue() { switch (this.panel.xaxis.mode) { case 'series': { From 3ecd96e68225c3b8ae8cdae10adec1c2f3c3b00c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 27 Sep 2016 18:17:39 +0200 Subject: [PATCH 23/32] feat(testdata): lots of work on new test data data source and scenarios --- pkg/api/api.go | 3 +- pkg/api/dtos/models.go | 6 +- pkg/api/metrics.go | 50 +++++----- pkg/services/alerting/conditions/query.go | 6 +- pkg/tsdb/models.go | 17 ++-- pkg/tsdb/prometheus/prometheus.go | 6 +- pkg/tsdb/query_context.go | 4 +- pkg/tsdb/testdata/scenarios.go | 98 +++++++++++++++++++ pkg/tsdb/testdata/testdata.go | 39 +++----- pkg/tsdb/time_range.go | 42 ++++++-- pkg/tsdb/time_range_test.go | 20 ++-- pkg/tsdb/tsdb_test.go | 30 +++--- .../app/testdata/datasource/datasource.ts | 32 ++++-- .../app/testdata/datasource/query_ctrl.ts | 17 ++-- .../app/testdata/partials/query.editor.html | 8 +- .../app/plugins/panel/graph/data_processor.ts | 2 +- 16 files changed, 257 insertions(+), 123 deletions(-) create mode 100644 pkg/tsdb/testdata/scenarios.go diff --git a/pkg/api/api.go b/pkg/api/api.go index 38e299d19c8..bac3db429d2 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -244,7 +244,8 @@ func Register(r *macaron.Macaron) { r.Get("/search/", Search) // metrics - r.Get("/metrics/test", wrap(GetTestMetrics)) + r.Post("/tsdb/query", bind(dtos.MetricRequest{}), wrap(QueryMetrics)) + r.Get("/tsdb/testdata/scenarios", wrap(GetTestDataScenarios)) // metrics r.Get("/metrics", wrap(GetInternalMetrics)) diff --git a/pkg/api/dtos/models.go b/pkg/api/dtos/models.go index 143ee5b98d5..170a5a868fc 100644 --- a/pkg/api/dtos/models.go +++ b/pkg/api/dtos/models.go @@ -96,8 +96,10 @@ func (slice DataSourceList) Swap(i, j int) { slice[i], slice[j] = slice[j], slice[i] } -type MetricQueryResultDto struct { - Data []interface{} `json:"data"` +type MetricRequest struct { + From string `json:"from"` + To string `json:"to"` + Queries []*simplejson.Json `json:"queries"` } type UserStars struct { diff --git a/pkg/api/metrics.go b/pkg/api/metrics.go index c36f2108581..c3bd9062737 100644 --- a/pkg/api/metrics.go +++ b/pkg/api/metrics.go @@ -8,43 +8,47 @@ import ( "github.com/grafana/grafana/pkg/metrics" "github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/tsdb" + "github.com/grafana/grafana/pkg/tsdb/testdata" "github.com/grafana/grafana/pkg/util" ) -func GetTestMetrics(c *middleware.Context) Response { +// POST /api/tsdb/query +func QueryMetrics(c *middleware.Context, reqDto dtos.MetricRequest) Response { + timeRange := tsdb.NewTimeRange(reqDto.From, reqDto.To) - timeRange := tsdb.NewTimeRange(c.Query("from"), c.Query("to")) + request := &tsdb.Request{TimeRange: timeRange} - req := &tsdb.Request{ - TimeRange: timeRange, - Queries: []*tsdb.Query{ - { - RefId: "A", - MaxDataPoints: c.QueryInt64("maxDataPoints"), - IntervalMs: c.QueryInt64("intervalMs"), - DataSource: &tsdb.DataSourceInfo{ - Name: "Grafana TestDataDB", - PluginId: "grafana-testdata-datasource", - }, + for _, query := range reqDto.Queries { + request.Queries = append(request.Queries, &tsdb.Query{ + RefId: query.Get("refId").MustString("A"), + MaxDataPoints: query.Get("maxDataPoints").MustInt64(100), + IntervalMs: query.Get("intervalMs").MustInt64(1000), + Model: query, + DataSource: &tsdb.DataSourceInfo{ + Name: "Grafana TestDataDB", + PluginId: "grafana-testdata-datasource", }, - }, + }) } - resp, err := tsdb.HandleRequest(req) + resp, err := tsdb.HandleRequest(request) if err != nil { return ApiError(500, "Metric request error", err) } - result := dtos.MetricQueryResultDto{} + return Json(200, &resp) +} - for _, v := range resp.Results { - if v.Error != nil { - return ApiError(500, "tsdb.HandleRequest() response error", v.Error) - } +// GET /api/tsdb/testdata/scenarios +func GetTestDataScenarios(c *middleware.Context) Response { + result := make([]interface{}, 0) - for _, series := range v.Series { - result.Data = append(result.Data, series) - } + for _, scenario := range testdata.ScenarioRegistry { + result = append(result, map[string]interface{}{ + "id": scenario.Id, + "name": scenario.Name, + "description": scenario.Description, + }) } return Json(200, &result) diff --git a/pkg/services/alerting/conditions/query.go b/pkg/services/alerting/conditions/query.go index e808d77a182..d32c42f27b0 100644 --- a/pkg/services/alerting/conditions/query.go +++ b/pkg/services/alerting/conditions/query.go @@ -69,7 +69,7 @@ func (c *QueryCondition) Eval(context *alerting.EvalContext) { context.Firing = len(context.EvalMatches) > 0 } -func (c *QueryCondition) executeQuery(context *alerting.EvalContext, timeRange tsdb.TimeRange) (tsdb.TimeSeriesSlice, error) { +func (c *QueryCondition) executeQuery(context *alerting.EvalContext, timeRange *tsdb.TimeRange) (tsdb.TimeSeriesSlice, error) { getDsInfo := &m.GetDataSourceByIdQuery{ Id: c.Query.DatasourceId, OrgId: context.Rule.OrgId, @@ -105,9 +105,9 @@ func (c *QueryCondition) executeQuery(context *alerting.EvalContext, timeRange t return result, nil } -func (c *QueryCondition) getRequestForAlertRule(datasource *m.DataSource, timerange tsdb.TimeRange) *tsdb.Request { +func (c *QueryCondition) getRequestForAlertRule(datasource *m.DataSource, timeRange *tsdb.TimeRange) *tsdb.Request { req := &tsdb.Request{ - TimeRange: timerange, + TimeRange: timeRange, Queries: []*tsdb.Query{ { RefId: "A", diff --git a/pkg/tsdb/models.go b/pkg/tsdb/models.go index 0060f459d7b..fc66a47f981 100644 --- a/pkg/tsdb/models.go +++ b/pkg/tsdb/models.go @@ -4,7 +4,6 @@ import "github.com/grafana/grafana/pkg/components/simplejson" type Query struct { RefId string - Query string Model *simplejson.Json Depends []string DataSource *DataSourceInfo @@ -17,13 +16,13 @@ type Query struct { type QuerySlice []*Query type Request struct { - TimeRange TimeRange + TimeRange *TimeRange Queries QuerySlice } type Response struct { - BatchTimings []*BatchTiming - Results map[string]*QueryResult + BatchTimings []*BatchTiming `json:"timings"` + Results map[string]*QueryResult `json:"results"` } type DataSourceInfo struct { @@ -50,14 +49,14 @@ type BatchResult struct { } type QueryResult struct { - Error error - RefId string - Series TimeSeriesSlice + Error error `json:"error"` + RefId string `json:"refId"` + Series TimeSeriesSlice `json:"series"` } type TimeSeries struct { - Name string `json:"target"` - Points [][2]*float64 `json:"datapoints"` + Name string `json:"name"` + Points [][2]*float64 `json:"points"` } type TimeSeriesSlice []*TimeSeries diff --git a/pkg/tsdb/prometheus/prometheus.go b/pkg/tsdb/prometheus/prometheus.go index 1df51bd1ffe..4d6ab03cede 100644 --- a/pkg/tsdb/prometheus/prometheus.go +++ b/pkg/tsdb/prometheus/prometheus.go @@ -10,8 +10,8 @@ import ( "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/tsdb" "github.com/prometheus/client_golang/api/prometheus" - "golang.org/x/net/context" pmodel "github.com/prometheus/common/model" + "golang.org/x/net/context" ) type PrometheusExecutor struct { @@ -111,12 +111,12 @@ func parseQuery(queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) (*Prom return nil, err } - start, err := queryContext.TimeRange.FromTime() + start, err := queryContext.TimeRange.ParseFrom() if err != nil { return nil, err } - end, err := queryContext.TimeRange.ToTime() + end, err := queryContext.TimeRange.ParseTo() if err != nil { return nil, err } diff --git a/pkg/tsdb/query_context.go b/pkg/tsdb/query_context.go index a1fc4c9bcb5..db40ba6253c 100644 --- a/pkg/tsdb/query_context.go +++ b/pkg/tsdb/query_context.go @@ -3,7 +3,7 @@ package tsdb import "sync" type QueryContext struct { - TimeRange TimeRange + TimeRange *TimeRange Queries QuerySlice Results map[string]*QueryResult ResultsChan chan *BatchResult @@ -11,7 +11,7 @@ type QueryContext struct { BatchWaits sync.WaitGroup } -func NewQueryContext(queries QuerySlice, timeRange TimeRange) *QueryContext { +func NewQueryContext(queries QuerySlice, timeRange *TimeRange) *QueryContext { return &QueryContext{ TimeRange: timeRange, Queries: queries, diff --git a/pkg/tsdb/testdata/scenarios.go b/pkg/tsdb/testdata/scenarios.go new file mode 100644 index 00000000000..d54cbb05638 --- /dev/null +++ b/pkg/tsdb/testdata/scenarios.go @@ -0,0 +1,98 @@ +package testdata + +import ( + "math/rand" + "time" + + "github.com/grafana/grafana/pkg/log" + "github.com/grafana/grafana/pkg/tsdb" +) + +type ScenarioHandler func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult + +type Scenario struct { + Id string `json:"id"` + Name string `json:"name"` + Description string `json:"description"` + Handler ScenarioHandler `json:"-"` +} + +var ScenarioRegistry map[string]*Scenario + +func init() { + ScenarioRegistry = make(map[string]*Scenario) + logger := log.New("tsdb.testdata") + + registerScenario(&Scenario{ + Id: "random_walk", + Name: "Random Walk", + + Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult { + timeWalkerMs := context.TimeRange.MustGetFrom().Unix() * 1000 + to := context.TimeRange.MustGetTo().Unix() * 1000 + + series := newSeriesForQuery(query) + + points := make([][2]*float64, 0) + walker := rand.Float64() * 100 + + for i := int64(0); i < 10000 && timeWalkerMs < to; i++ { + timestamp := float64(timeWalkerMs) + val := float64(walker) + points = append(points, [2]*float64{&val, ×tamp}) + + walker += rand.Float64() - 0.5 + timeWalkerMs += query.IntervalMs + } + + series.Points = points + + queryRes := &tsdb.QueryResult{} + queryRes.Series = append(queryRes.Series, series) + return queryRes + }, + }) + + registerScenario(&Scenario{ + Id: "no_data_points", + Name: "No Data Points", + Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult { + return &tsdb.QueryResult{ + Series: make(tsdb.TimeSeriesSlice, 0), + } + }, + }) + + registerScenario(&Scenario{ + Id: "datapoints_outside_range", + Name: "Datapoints Outside Range", + Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult { + queryRes := &tsdb.QueryResult{} + + series := newSeriesForQuery(query) + outsideTime := context.TimeRange.MustGetFrom().Add(-1*time.Hour).Unix() * 1000 + + timestamp := float64(outsideTime) + logger.Info("time", "from", timestamp) + val := float64(10) + + series.Points = append(series.Points, [2]*float64{&val, ×tamp}) + queryRes.Series = append(queryRes.Series, series) + return queryRes + }, + }) + +} + +func registerScenario(scenario *Scenario) { + ScenarioRegistry[scenario.Id] = scenario +} + +func newSeriesForQuery(query *tsdb.Query) *tsdb.TimeSeries { + alias := query.Model.Get("alias").MustString("") + if alias == "" { + alias = query.RefId + "-series" + } + + return &tsdb.TimeSeries{Name: alias} +} diff --git a/pkg/tsdb/testdata/testdata.go b/pkg/tsdb/testdata/testdata.go index b1eca4cb46a..5b40bb6de5a 100644 --- a/pkg/tsdb/testdata/testdata.go +++ b/pkg/tsdb/testdata/testdata.go @@ -1,17 +1,20 @@ package testdata import ( - "math/rand" - + "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/tsdb" ) type TestDataExecutor struct { *tsdb.DataSourceInfo + log log.Logger } func NewTestDataExecutor(dsInfo *tsdb.DataSourceInfo) tsdb.Executor { - return &TestDataExecutor{dsInfo} + return &TestDataExecutor{ + DataSourceInfo: dsInfo, + log: log.New("tsdb.testdata"), + } } func init() { @@ -22,33 +25,15 @@ func (e *TestDataExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryC result := &tsdb.BatchResult{} result.QueryResults = make(map[string]*tsdb.QueryResult) - from, _ := context.TimeRange.FromTime() - to, _ := context.TimeRange.ToTime() - - queryRes := &tsdb.QueryResult{} - for _, query := range queries { - // scenario := query.Model.Get("scenario").MustString("random_walk") - series := &tsdb.TimeSeries{Name: "test-series-0"} - - stepInSeconds := (to.Unix() - from.Unix()) / query.MaxDataPoints - points := make([][2]*float64, 0) - walker := rand.Float64() * 100 - time := from.Unix() - - for i := int64(0); i < query.MaxDataPoints; i++ { - timestamp := float64(time) - val := float64(walker) - points = append(points, [2]*float64{&val, ×tamp}) - - walker += rand.Float64() - 0.5 - time += stepInSeconds + scenarioId := query.Model.Get("scenarioId").MustString("random_walk") + if scenario, exist := ScenarioRegistry[scenarioId]; exist { + result.QueryResults[query.RefId] = scenario.Handler(query, context) + result.QueryResults[query.RefId].RefId = query.RefId + } else { + e.log.Error("Scenario not found", "scenarioId", scenarioId) } - - series.Points = points - queryRes.Series = append(queryRes.Series, series) } - result.QueryResults["A"] = queryRes return result } diff --git a/pkg/tsdb/time_range.go b/pkg/tsdb/time_range.go index dee3e683516..67227b834dc 100644 --- a/pkg/tsdb/time_range.go +++ b/pkg/tsdb/time_range.go @@ -7,8 +7,8 @@ import ( "time" ) -func NewTimeRange(from, to string) TimeRange { - return TimeRange{ +func NewTimeRange(from, to string) *TimeRange { + return &TimeRange{ From: from, To: to, Now: time.Now(), @@ -21,13 +21,37 @@ type TimeRange struct { Now time.Time } -func (tr TimeRange) FromTime() (time.Time, error) { - if val, err := strconv.ParseInt(tr.From, 10, 64); err == nil { - return time.Unix(val, 0), nil +func (tr *TimeRange) MustGetFrom() time.Time { + if res, err := tr.ParseFrom(); err != nil { + return time.Unix(0, 0) + } else { + return res + } +} + +func (tr *TimeRange) MustGetTo() time.Time { + if res, err := tr.ParseTo(); err != nil { + return time.Unix(0, 0) + } else { + return res + } +} + +func tryParseUnixMsEpoch(val string) (time.Time, bool) { + if val, err := strconv.ParseInt(val, 10, 64); err == nil { + seconds := val / 1000 + nano := (val - seconds*1000) * 1000000 + return time.Unix(seconds, nano), true + } + return time.Time{}, false +} + +func (tr *TimeRange) ParseFrom() (time.Time, error) { + if res, ok := tryParseUnixMsEpoch(tr.From); ok { + return res, nil } fromRaw := strings.Replace(tr.From, "now-", "", 1) - diff, err := time.ParseDuration("-" + fromRaw) if err != nil { return time.Time{}, err @@ -36,7 +60,7 @@ func (tr TimeRange) FromTime() (time.Time, error) { return tr.Now.Add(diff), nil } -func (tr TimeRange) ToTime() (time.Time, error) { +func (tr *TimeRange) ParseTo() (time.Time, error) { if tr.To == "now" { return tr.Now, nil } else if strings.HasPrefix(tr.To, "now-") { @@ -50,8 +74,8 @@ func (tr TimeRange) ToTime() (time.Time, error) { return tr.Now.Add(diff), nil } - if val, err := strconv.ParseInt(tr.To, 10, 64); err == nil { - return time.Unix(val, 0), nil + if res, ok := tryParseUnixMsEpoch(tr.To); ok { + return res, nil } return time.Time{}, fmt.Errorf("cannot parse to value %s", tr.To) diff --git a/pkg/tsdb/time_range_test.go b/pkg/tsdb/time_range_test.go index f4acb5e6d80..5412d0d05f3 100644 --- a/pkg/tsdb/time_range_test.go +++ b/pkg/tsdb/time_range_test.go @@ -23,13 +23,13 @@ func TestTimeRange(t *testing.T) { fiveMinAgo, _ := time.ParseDuration("-5m") expected := now.Add(fiveMinAgo) - res, err := tr.FromTime() + res, err := tr.ParseFrom() So(err, ShouldBeNil) So(res.Unix(), ShouldEqual, expected.Unix()) }) Convey("now ", func() { - res, err := tr.ToTime() + res, err := tr.ParseTo() So(err, ShouldBeNil) So(res.Unix(), ShouldEqual, now.Unix()) }) @@ -46,7 +46,7 @@ func TestTimeRange(t *testing.T) { fiveHourAgo, _ := time.ParseDuration("-5h") expected := now.Add(fiveHourAgo) - res, err := tr.FromTime() + res, err := tr.ParseFrom() So(err, ShouldBeNil) So(res.Unix(), ShouldEqual, expected.Unix()) }) @@ -54,7 +54,7 @@ func TestTimeRange(t *testing.T) { Convey("now-10m ", func() { fiveMinAgo, _ := time.ParseDuration("-10m") expected := now.Add(fiveMinAgo) - res, err := tr.ToTime() + res, err := tr.ParseTo() So(err, ShouldBeNil) So(res.Unix(), ShouldEqual, expected.Unix()) }) @@ -68,13 +68,13 @@ func TestTimeRange(t *testing.T) { Now: now, } - res, err := tr.FromTime() + res, err := tr.ParseFrom() So(err, ShouldBeNil) - So(res.Unix(), ShouldEqual, 1474973725473) + So(res.UnixNano()/int64(time.Millisecond), ShouldEqual, 1474973725473) - res, err = tr.ToTime() + res, err = tr.ParseTo() So(err, ShouldBeNil) - So(res.Unix(), ShouldEqual, 1474975757930) + So(res.UnixNano()/int64(time.Millisecond), ShouldEqual, 1474975757930) }) Convey("Cannot parse asdf", func() { @@ -85,10 +85,10 @@ func TestTimeRange(t *testing.T) { Now: now, } - _, err = tr.FromTime() + _, err = tr.ParseFrom() So(err, ShouldNotBeNil) - _, err = tr.ToTime() + _, err = tr.ParseTo() So(err, ShouldNotBeNil) }) }) diff --git a/pkg/tsdb/tsdb_test.go b/pkg/tsdb/tsdb_test.go index 24d84a27c74..429dd01d6ba 100644 --- a/pkg/tsdb/tsdb_test.go +++ b/pkg/tsdb/tsdb_test.go @@ -14,9 +14,9 @@ func TestMetricQuery(t *testing.T) { Convey("Given 3 queries for 2 data sources", func() { request := &Request{ Queries: QuerySlice{ - {RefId: "A", Query: "asd", DataSource: &DataSourceInfo{Id: 1}}, - {RefId: "B", Query: "asd", DataSource: &DataSourceInfo{Id: 1}}, - {RefId: "C", Query: "asd", DataSource: &DataSourceInfo{Id: 2}}, + {RefId: "A", DataSource: &DataSourceInfo{Id: 1}}, + {RefId: "B", DataSource: &DataSourceInfo{Id: 1}}, + {RefId: "C", DataSource: &DataSourceInfo{Id: 2}}, }, } @@ -31,9 +31,9 @@ func TestMetricQuery(t *testing.T) { Convey("Given query 2 depends on query 1", func() { request := &Request{ Queries: QuerySlice{ - {RefId: "A", Query: "asd", DataSource: &DataSourceInfo{Id: 1}}, - {RefId: "B", Query: "asd", DataSource: &DataSourceInfo{Id: 2}}, - {RefId: "C", Query: "#A / #B", DataSource: &DataSourceInfo{Id: 3}, Depends: []string{"A", "B"}}, + {RefId: "A", DataSource: &DataSourceInfo{Id: 1}}, + {RefId: "B", DataSource: &DataSourceInfo{Id: 2}}, + {RefId: "C", DataSource: &DataSourceInfo{Id: 3}, Depends: []string{"A", "B"}}, }, } @@ -55,7 +55,7 @@ func TestMetricQuery(t *testing.T) { Convey("When executing request with one query", t, func() { req := &Request{ Queries: QuerySlice{ - {RefId: "A", Query: "asd", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, + {RefId: "A", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, }, } @@ -74,8 +74,8 @@ func TestMetricQuery(t *testing.T) { Convey("When executing one request with two queries from same data source", t, func() { req := &Request{ Queries: QuerySlice{ - {RefId: "A", Query: "asd", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, - {RefId: "B", Query: "asd", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, + {RefId: "A", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, + {RefId: "B", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, }, } @@ -100,9 +100,9 @@ func TestMetricQuery(t *testing.T) { Convey("When executing one request with three queries from different datasources", t, func() { req := &Request{ Queries: QuerySlice{ - {RefId: "A", Query: "asd", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, - {RefId: "B", Query: "asd", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, - {RefId: "C", Query: "asd", DataSource: &DataSourceInfo{Id: 2, PluginId: "test"}}, + {RefId: "A", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, + {RefId: "B", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}}, + {RefId: "C", DataSource: &DataSourceInfo{Id: 2, PluginId: "test"}}, }, } @@ -117,7 +117,7 @@ func TestMetricQuery(t *testing.T) { Convey("When query uses data source of unknown type", t, func() { req := &Request{ Queries: QuerySlice{ - {RefId: "A", Query: "asd", DataSource: &DataSourceInfo{Id: 1, PluginId: "asdasdas"}}, + {RefId: "A", DataSource: &DataSourceInfo{Id: 1, PluginId: "asdasdas"}}, }, } @@ -129,10 +129,10 @@ func TestMetricQuery(t *testing.T) { req := &Request{ Queries: QuerySlice{ { - RefId: "A", Query: "asd", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}, + RefId: "A", DataSource: &DataSourceInfo{Id: 1, PluginId: "test"}, }, { - RefId: "B", Query: "#A / 2", DataSource: &DataSourceInfo{Id: 2, PluginId: "test"}, Depends: []string{"A"}, + RefId: "B", DataSource: &DataSourceInfo{Id: 2, PluginId: "test"}, Depends: []string{"A"}, }, }, } diff --git a/public/app/plugins/app/testdata/datasource/datasource.ts b/public/app/plugins/app/testdata/datasource/datasource.ts index 32edfb59755..a06daa19262 100644 --- a/public/app/plugins/app/testdata/datasource/datasource.ts +++ b/public/app/plugins/app/testdata/datasource/datasource.ts @@ -10,18 +10,38 @@ class TestDataDatasource { query(options) { var queries = _.filter(options.targets, item => { return item.hide !== true; + }).map(item => { + return { + refId: item.refId, + scenarioId: item.scenarioId, + intervalMs: options.intervalMs, + maxDataPoints: options.maxDataPoints, + }; }); if (queries.length === 0) { return this.$q.when({data: []}); } - return this.backendSrv.get('/api/metrics/test', { - from: options.range.from.valueOf(), - to: options.range.to.valueOf(), - scenario: options.targets[0].scenario, - interval: options.intervalMs, - maxDataPoints: options.maxDataPoints, + return this.backendSrv.post('/api/tsdb/query', { + from: options.range.from.valueOf().toString(), + to: options.range.to.valueOf().toString(), + queries: queries, + }).then(res => { + var data = []; + + if (res.results) { + _.forEach(res.results, queryRes => { + for (let series of queryRes.series) { + data.push({ + target: series.name, + datapoints: series.points + }); + } + }); + } + + return {data: data}; }); } diff --git a/public/app/plugins/app/testdata/datasource/query_ctrl.ts b/public/app/plugins/app/testdata/datasource/query_ctrl.ts index 44a62fd1a11..f22ef8948ad 100644 --- a/public/app/plugins/app/testdata/datasource/query_ctrl.ts +++ b/public/app/plugins/app/testdata/datasource/query_ctrl.ts @@ -6,19 +6,20 @@ import {QueryCtrl} from 'app/plugins/sdk'; export class TestDataQueryCtrl extends QueryCtrl { static templateUrl = 'partials/query.editor.html'; - scenarioDefs: any; + scenarioList: any; /** @ngInject **/ - constructor($scope, $injector) { + constructor($scope, $injector, private backendSrv) { super($scope, $injector); - this.target.scenario = this.target.scenario || 'random_walk'; + this.target.scenarioId = this.target.scenarioId || 'random_walk'; + this.scenarioList = []; + } - this.scenarioDefs = { - 'random_walk': {text: 'Random Walk'}, - 'no_datapoints': {text: 'No Datapoints'}, - 'data_outside_range': {text: 'Data Outside Range'}, - }; + $onInit() { + return this.backendSrv.get('/api/tsdb/testdata/scenarios').then(res => { + this.scenarioList = res; + }); } } diff --git a/public/app/plugins/app/testdata/partials/query.editor.html b/public/app/plugins/app/testdata/partials/query.editor.html index d9068dfda49..53f84309661 100644 --- a/public/app/plugins/app/testdata/partials/query.editor.html +++ b/public/app/plugins/app/testdata/partials/query.editor.html @@ -2,17 +2,17 @@
-
- +
+
- +
- +
diff --git a/public/app/plugins/panel/graph/data_processor.ts b/public/app/plugins/panel/graph/data_processor.ts index bb8af84cf00..6233ac345c9 100644 --- a/public/app/plugins/panel/graph/data_processor.ts +++ b/public/app/plugins/panel/graph/data_processor.ts @@ -78,7 +78,7 @@ export class DataProcessor { } timeSeriesHandler(seriesData, index, options) { - var datapoints = seriesData.datapoints; + var datapoints = seriesData.datapoints || []; var alias = seriesData.target; var colorIndex = index % colors.length; From 22e8885690bb93b6b10bf371fcf5d0afad8accde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 27 Sep 2016 18:36:00 +0200 Subject: [PATCH 24/32] feat(testdata): progress on test data stuff --- pkg/plugins/datasource_plugin.go | 1 + .../app/features/alerting/alert_tab_ctrl.ts | 4 +- .../app/features/dashboard/dashboard_srv.ts | 2 + .../testdata/dashboards/graph_last_1h.json | 290 +++++++++++++++++- .../app/testdata/datasource/plugin.json | 1 + .../plugins/datasource/graphite/plugin.json | 3 +- .../plugins/datasource/prometheus/plugin.json | 1 + 7 files changed, 297 insertions(+), 5 deletions(-) diff --git a/pkg/plugins/datasource_plugin.go b/pkg/plugins/datasource_plugin.go index b8c79f22998..aa092c2bc20 100644 --- a/pkg/plugins/datasource_plugin.go +++ b/pkg/plugins/datasource_plugin.go @@ -6,6 +6,7 @@ type DataSourcePlugin struct { FrontendPluginBase Annotations bool `json:"annotations"` Metrics bool `json:"metrics"` + Alerting bool `json:"alerting"` BuiltIn bool `json:"builtIn"` Mixed bool `json:"mixed"` App string `json:"app"` diff --git a/public/app/features/alerting/alert_tab_ctrl.ts b/public/app/features/alerting/alert_tab_ctrl.ts index ac4f6af5e38..ec0386ba5a9 100644 --- a/public/app/features/alerting/alert_tab_ctrl.ts +++ b/public/app/features/alerting/alert_tab_ctrl.ts @@ -227,8 +227,8 @@ export class AlertTabCtrl { var datasourceName = foundTarget.datasource || this.panel.datasource; this.datasourceSrv.get(datasourceName).then(ds => { - if (ds.meta.id !== 'graphite' && ds.meta.id !== 'prometheus') { - this.error = 'You datsource does not support alerting queries'; + if (!ds.meta.alerting) { + this.error = 'The datasource does not support alerting queries'; } else if (this.templateSrv.variableExists(foundTarget.target)) { this.error = 'Template variables are not supported in alert queries'; } else { diff --git a/public/app/features/dashboard/dashboard_srv.ts b/public/app/features/dashboard/dashboard_srv.ts index 289e3a841f5..92cfa4dc925 100644 --- a/public/app/features/dashboard/dashboard_srv.ts +++ b/public/app/features/dashboard/dashboard_srv.ts @@ -30,6 +30,7 @@ export class DashboardModel { snapshot: any; schemaVersion: number; version: number; + revision: number; links: any; gnetId: any; meta: any; @@ -42,6 +43,7 @@ export class DashboardModel { this.events = new Emitter(); this.id = data.id || null; + this.revision = data.revision; this.title = data.title || 'No Title'; this.autoUpdate = data.autoUpdate; this.description = data.description; diff --git a/public/app/plugins/app/testdata/dashboards/graph_last_1h.json b/public/app/plugins/app/testdata/dashboards/graph_last_1h.json index 7533a44760b..4669bb8cd60 100644 --- a/public/app/plugins/app/testdata/dashboards/graph_last_1h.json +++ b/public/app/plugins/app/testdata/dashboards/graph_last_1h.json @@ -1,5 +1,291 @@ { + "revision": 2, "title": "TestData - Graph Panel Last 1h", - "tags": ["testdata"], - "revision": 1 + "tags": [ + "grafana-test" + ], + "style": "dark", + "timezone": "browser", + "editable": true, + "hideControls": false, + "sharedCrosshair": false, + "rows": [ + { + "collapse": false, + "editable": true, + "height": "250px", + "panels": [ + { + "aliasColors": {}, + "bars": false, + "datasource": "Grafana TestData", + "editable": true, + "error": false, + "fill": 1, + "id": 1, + "isNew": true, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 2, + "nullPointMode": "connected", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "span": 4, + "stack": false, + "steppedLine": false, + "targets": [ + { + "refId": "A", + "scenario": "random_walk", + "target": "", + "scenarioId": "no_data_points" + } + ], + "thresholds": [], + "timeFrom": null, + "timeShift": null, + "title": "No Data Points Warning", + "tooltip": { + "msResolution": false, + "shared": true, + "sort": 0, + "value_type": "cumulative" + }, + "type": "graph", + "xaxis": { + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ], + "links": [] + }, + { + "aliasColors": {}, + "bars": false, + "datasource": "Grafana TestData", + "editable": true, + "error": false, + "fill": 1, + "id": 2, + "isNew": true, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 2, + "nullPointMode": "connected", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "span": 4, + "stack": false, + "steppedLine": false, + "targets": [ + { + "refId": "A", + "scenario": "random_walk", + "target": "", + "scenarioId": "datapoints_outside_range" + } + ], + "thresholds": [], + "timeFrom": null, + "timeShift": null, + "title": "Datapoints Outside Range Warning", + "tooltip": { + "msResolution": false, + "shared": true, + "sort": 0, + "value_type": "cumulative" + }, + "type": "graph", + "xaxis": { + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ], + "links": [] + } + ], + "title": "New row" + }, + { + "title": "New row", + "height": "250px", + "editable": true, + "collapse": false, + "panels": [ + { + "aliasColors": {}, + "bars": false, + "datasource": "Grafana TestData", + "editable": true, + "error": false, + "fill": 1, + "id": 3, + "isNew": true, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 2, + "nullPointMode": "connected", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "span": 8, + "stack": false, + "steppedLine": false, + "targets": [ + { + "refId": "A", + "scenario": "random_walk", + "target": "", + "scenarioId": "random_walk" + } + ], + "thresholds": [], + "timeFrom": null, + "timeShift": null, + "title": "Random walk series", + "tooltip": { + "msResolution": false, + "shared": true, + "sort": 0, + "value_type": "cumulative" + }, + "type": "graph", + "xaxis": { + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ], + "links": [] + } + ] + } + ], + "time": { + "from": "now-6h", + "to": "now" + }, + "timepicker": { + "refresh_intervals": [ + "5s", + "10s", + "30s", + "1m", + "5m", + "15m", + "30m", + "1h", + "2h", + "1d" + ], + "time_options": [ + "5m", + "15m", + "1h", + "6h", + "12h", + "24h", + "2d", + "7d", + "30d" + ] + }, + "templating": { + "list": [] + }, + "annotations": { + "list": [] + }, + "schemaVersion": 13, + "version": 4, + "links": [], + "gnetId": null } diff --git a/public/app/plugins/app/testdata/datasource/plugin.json b/public/app/plugins/app/testdata/datasource/plugin.json index 0ad87a15081..4d66253d78e 100644 --- a/public/app/plugins/app/testdata/datasource/plugin.json +++ b/public/app/plugins/app/testdata/datasource/plugin.json @@ -4,6 +4,7 @@ "id": "grafana-testdata-datasource", "metrics": true, + "alerting": true, "annotations": true, "info": { diff --git a/public/app/plugins/datasource/graphite/plugin.json b/public/app/plugins/datasource/graphite/plugin.json index c47c49e05fa..76242fd883c 100644 --- a/public/app/plugins/datasource/graphite/plugin.json +++ b/public/app/plugins/datasource/graphite/plugin.json @@ -8,6 +8,7 @@ ], "metrics": true, + "alerting": true, "annotations": true, "info": { @@ -20,4 +21,4 @@ "large": "img/graphite_logo.png" } } -} \ No newline at end of file +} diff --git a/public/app/plugins/datasource/prometheus/plugin.json b/public/app/plugins/datasource/prometheus/plugin.json index f39f8691661..54fd1129b8b 100644 --- a/public/app/plugins/datasource/prometheus/plugin.json +++ b/public/app/plugins/datasource/prometheus/plugin.json @@ -8,6 +8,7 @@ ], "metrics": true, + "alerting": true, "annotations": true, "info": { From 460160cfa4917626c96f5c135c1b7e2c450d68f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Wed, 28 Sep 2016 08:08:46 +0200 Subject: [PATCH 25/32] feat(testdata): added test case for ms resolution data --- pkg/tsdb/testdata/scenarios.go | 5 +- .../testdata/dashboards/graph_last_1h.json | 127 ++++++++++++++---- public/app/plugins/app/testdata/plugin.json | 2 +- 3 files changed, 104 insertions(+), 30 deletions(-) diff --git a/pkg/tsdb/testdata/scenarios.go b/pkg/tsdb/testdata/scenarios.go index d54cbb05638..c7df7d7bffb 100644 --- a/pkg/tsdb/testdata/scenarios.go +++ b/pkg/tsdb/testdata/scenarios.go @@ -28,8 +28,8 @@ func init() { Name: "Random Walk", Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult { - timeWalkerMs := context.TimeRange.MustGetFrom().Unix() * 1000 - to := context.TimeRange.MustGetTo().Unix() * 1000 + to := context.TimeRange.MustGetTo().UnixNano() / int64(time.Millisecond) + timeWalkerMs := context.TimeRange.MustGetFrom().UnixNano() / int64(time.Millisecond) series := newSeriesForQuery(query) @@ -81,7 +81,6 @@ func init() { return queryRes }, }) - } func registerScenario(scenario *Scenario) { diff --git a/public/app/plugins/app/testdata/dashboards/graph_last_1h.json b/public/app/plugins/app/testdata/dashboards/graph_last_1h.json index 4669bb8cd60..0cca7b6c331 100644 --- a/public/app/plugins/app/testdata/dashboards/graph_last_1h.json +++ b/public/app/plugins/app/testdata/dashboards/graph_last_1h.json @@ -1,5 +1,5 @@ { - "revision": 2, + "revision": 3, "title": "TestData - Graph Panel Last 1h", "tags": [ "grafana-test" @@ -35,6 +35,7 @@ }, "lines": true, "linewidth": 2, + "links": [], "nullPointMode": "connected", "percentage": false, "pointradius": 5, @@ -48,8 +49,8 @@ { "refId": "A", "scenario": "random_walk", - "target": "", - "scenarioId": "no_data_points" + "scenarioId": "no_data_points", + "target": "" } ], "thresholds": [], @@ -86,8 +87,7 @@ "min": null, "show": true } - ], - "links": [] + ] }, { "aliasColors": {}, @@ -109,6 +109,7 @@ }, "lines": true, "linewidth": 2, + "links": [], "nullPointMode": "connected", "percentage": false, "pointradius": 5, @@ -122,8 +123,8 @@ { "refId": "A", "scenario": "random_walk", - "target": "", - "scenarioId": "datapoints_outside_range" + "scenarioId": "datapoints_outside_range", + "target": "" } ], "thresholds": [], @@ -160,18 +161,8 @@ "min": null, "show": true } - ], - "links": [] - } - ], - "title": "New row" - }, - { - "title": "New row", - "height": "250px", - "editable": true, - "collapse": false, - "panels": [ + ] + }, { "aliasColors": {}, "bars": false, @@ -192,21 +183,22 @@ }, "lines": true, "linewidth": 2, + "links": [], "nullPointMode": "connected", "percentage": false, "pointradius": 5, "points": false, "renderer": "flot", "seriesOverrides": [], - "span": 8, + "span": 4, "stack": false, "steppedLine": false, "targets": [ { "refId": "A", "scenario": "random_walk", - "target": "", - "scenarioId": "random_walk" + "scenarioId": "random_walk", + "target": "" } ], "thresholds": [], @@ -243,14 +235,96 @@ "min": null, "show": true } - ], - "links": [] + ] } - ] + ], + "title": "New row" + }, + { + "collapse": false, + "editable": true, + "height": "250px", + "panels": [ + { + "aliasColors": {}, + "bars": false, + "datasource": "Grafana TestData", + "editable": true, + "error": false, + "fill": 1, + "id": 4, + "isNew": true, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 2, + "links": [], + "nullPointMode": "connected", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "span": 8, + "stack": false, + "steppedLine": false, + "targets": [ + { + "refId": "A", + "scenario": "random_walk", + "scenarioId": "random_walk", + "target": "" + } + ], + "thresholds": [], + "timeFrom": "5d", + "timeShift": null, + "title": "Millisecond res x-axis and tooltip", + "tooltip": { + "msResolution": false, + "shared": true, + "sort": 0, + "value_type": "cumulative" + }, + "type": "graph", + "xaxis": { + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ] + } + ], + "title": "New row" } ], "time": { - "from": "now-6h", + "from": "now-1h", "to": "now" }, "timepicker": { @@ -284,6 +358,7 @@ "annotations": { "list": [] }, + "refresh": false, "schemaVersion": 13, "version": 4, "links": [], diff --git a/public/app/plugins/app/testdata/plugin.json b/public/app/plugins/app/testdata/plugin.json index 47ab291409b..0bd65a0735d 100644 --- a/public/app/plugins/app/testdata/plugin.json +++ b/public/app/plugins/app/testdata/plugin.json @@ -9,7 +9,7 @@ "name": "Grafana Project", "url": "http://grafana.org" }, - "version": "1.0.5", + "version": "1.0.6", "updated": "2016-09-26" }, From 8d5857661eeb1bd061b85c5c5eb9a9c7bba01b1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Wed, 28 Sep 2016 09:15:48 +0200 Subject: [PATCH 26/32] refactor(tsdb): changed tsdb time series model to use null.Float instead of pointers --- pkg/services/alerting/conditions/evaluator.go | 25 +++++++------- .../alerting/conditions/evaluator_test.go | 6 ++-- pkg/services/alerting/conditions/query.go | 6 ++-- .../alerting/conditions/query_test.go | 33 ++++++++----------- pkg/services/alerting/conditions/reducer.go | 31 ++++++++--------- .../alerting/conditions/reducer_test.go | 25 +++++++------- pkg/tsdb/graphite/graphite.go | 1 + pkg/tsdb/graphite/types.go | 6 ++-- pkg/tsdb/models.go | 27 ++++++++++++--- pkg/tsdb/prometheus/prometheus.go | 16 ++++----- pkg/tsdb/testdata/scenarios.go | 16 +++------ 11 files changed, 102 insertions(+), 90 deletions(-) diff --git a/pkg/services/alerting/conditions/evaluator.go b/pkg/services/alerting/conditions/evaluator.go index 18a2bf35262..1c154e17ec2 100644 --- a/pkg/services/alerting/conditions/evaluator.go +++ b/pkg/services/alerting/conditions/evaluator.go @@ -5,6 +5,7 @@ import ( "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/services/alerting" + "gopkg.in/guregu/null.v3" ) var ( @@ -13,13 +14,13 @@ var ( ) type AlertEvaluator interface { - Eval(reducedValue *float64) bool + Eval(reducedValue null.Float) bool } type NoDataEvaluator struct{} -func (e *NoDataEvaluator) Eval(reducedValue *float64) bool { - return reducedValue == nil +func (e *NoDataEvaluator) Eval(reducedValue null.Float) bool { + return reducedValue.Valid == false } type ThresholdEvaluator struct { @@ -43,16 +44,16 @@ func newThresholdEvaludator(typ string, model *simplejson.Json) (*ThresholdEvalu return defaultEval, nil } -func (e *ThresholdEvaluator) Eval(reducedValue *float64) bool { - if reducedValue == nil { +func (e *ThresholdEvaluator) Eval(reducedValue null.Float) bool { + if reducedValue.Valid == false { return false } switch e.Type { case "gt": - return *reducedValue > e.Threshold + return reducedValue.Float64 > e.Threshold case "lt": - return *reducedValue < e.Threshold + return reducedValue.Float64 < e.Threshold } return false @@ -86,16 +87,18 @@ func newRangedEvaluator(typ string, model *simplejson.Json) (*RangedEvaluator, e return rangedEval, nil } -func (e *RangedEvaluator) Eval(reducedValue *float64) bool { - if reducedValue == nil { +func (e *RangedEvaluator) Eval(reducedValue null.Float) bool { + if reducedValue.Valid == false { return false } + floatValue := reducedValue.Float64 + switch e.Type { case "within_range": - return (e.Lower < *reducedValue && e.Upper > *reducedValue) || (e.Upper < *reducedValue && e.Lower > *reducedValue) + return (e.Lower < floatValue && e.Upper > floatValue) || (e.Upper < floatValue && e.Lower > floatValue) case "outside_range": - return (e.Upper < *reducedValue && e.Lower < *reducedValue) || (e.Upper > *reducedValue && e.Lower > *reducedValue) + return (e.Upper < floatValue && e.Lower < floatValue) || (e.Upper > floatValue && e.Lower > floatValue) } return false diff --git a/pkg/services/alerting/conditions/evaluator_test.go b/pkg/services/alerting/conditions/evaluator_test.go index d2919f37d9d..24c5cfacea4 100644 --- a/pkg/services/alerting/conditions/evaluator_test.go +++ b/pkg/services/alerting/conditions/evaluator_test.go @@ -3,6 +3,8 @@ package conditions import ( "testing" + "gopkg.in/guregu/null.v3" + "github.com/grafana/grafana/pkg/components/simplejson" . "github.com/smartystreets/goconvey/convey" ) @@ -14,7 +16,7 @@ func evalutorScenario(json string, reducedValue float64, datapoints ...float64) evaluator, err := NewAlertEvaluator(jsonModel) So(err, ShouldBeNil) - return evaluator.Eval(&reducedValue) + return evaluator.Eval(null.FloatFrom(reducedValue)) } func TestEvalutors(t *testing.T) { @@ -51,6 +53,6 @@ func TestEvalutors(t *testing.T) { evaluator, err := NewAlertEvaluator(jsonModel) So(err, ShouldBeNil) - So(evaluator.Eval(nil), ShouldBeTrue) + So(evaluator.Eval(null.FloatFromPtr(nil)), ShouldBeTrue) }) } diff --git a/pkg/services/alerting/conditions/query.go b/pkg/services/alerting/conditions/query.go index c84fdab574d..b5300a261a3 100644 --- a/pkg/services/alerting/conditions/query.go +++ b/pkg/services/alerting/conditions/query.go @@ -46,21 +46,21 @@ func (c *QueryCondition) Eval(context *alerting.EvalContext) { reducedValue := c.Reducer.Reduce(series) evalMatch := c.Evaluator.Eval(reducedValue) - if reducedValue == nil { + if reducedValue.Valid == false { emptySerieCount++ continue } if context.IsTestRun { context.Logs = append(context.Logs, &alerting.ResultLogEntry{ - Message: fmt.Sprintf("Condition[%d]: Eval: %v, Metric: %s, Value: %1.3f", c.Index, evalMatch, series.Name, *reducedValue), + Message: fmt.Sprintf("Condition[%d]: Eval: %v, Metric: %s, Value: %1.3f", c.Index, evalMatch, series.Name, reducedValue.Float64), }) } if evalMatch { context.EvalMatches = append(context.EvalMatches, &alerting.EvalMatch{ Metric: series.Name, - Value: *reducedValue, + Value: reducedValue.Float64, }) } } diff --git a/pkg/services/alerting/conditions/query_test.go b/pkg/services/alerting/conditions/query_test.go index 983e75c4c1b..51c4226f81c 100644 --- a/pkg/services/alerting/conditions/query_test.go +++ b/pkg/services/alerting/conditions/query_test.go @@ -3,6 +3,8 @@ package conditions import ( "testing" + null "gopkg.in/guregu/null.v3" + "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/components/simplejson" m "github.com/grafana/grafana/pkg/models" @@ -41,9 +43,8 @@ func TestQueryCondition(t *testing.T) { }) Convey("should fire when avg is above 100", func() { - one := float64(120) - two := float64(0) - ctx.series = tsdb.TimeSeriesSlice{tsdb.NewTimeSeries("test1", [][2]*float64{{&one, &two}})} + points := tsdb.NewTimeSeriesPointsFromArgs(120, 0) + ctx.series = tsdb.TimeSeriesSlice{tsdb.NewTimeSeries("test1", points)} ctx.exec() So(ctx.result.Error, ShouldBeNil) @@ -51,9 +52,8 @@ func TestQueryCondition(t *testing.T) { }) Convey("Should not fire when avg is below 100", func() { - one := float64(90) - two := float64(0) - ctx.series = tsdb.TimeSeriesSlice{tsdb.NewTimeSeries("test1", [][2]*float64{{&one, &two}})} + points := tsdb.NewTimeSeriesPointsFromArgs(90, 0) + ctx.series = tsdb.TimeSeriesSlice{tsdb.NewTimeSeries("test1", points)} ctx.exec() So(ctx.result.Error, ShouldBeNil) @@ -61,11 +61,9 @@ func TestQueryCondition(t *testing.T) { }) Convey("Should fire if only first serie matches", func() { - one := float64(120) - two := float64(0) ctx.series = tsdb.TimeSeriesSlice{ - tsdb.NewTimeSeries("test1", [][2]*float64{{&one, &two}}), - tsdb.NewTimeSeries("test2", [][2]*float64{{&two, &two}}), + tsdb.NewTimeSeries("test1", tsdb.NewTimeSeriesPointsFromArgs(120, 0)), + tsdb.NewTimeSeries("test2", tsdb.NewTimeSeriesPointsFromArgs(0, 0)), } ctx.exec() @@ -76,8 +74,8 @@ func TestQueryCondition(t *testing.T) { Convey("Empty series", func() { Convey("Should set NoDataFound both series are empty", func() { ctx.series = tsdb.TimeSeriesSlice{ - tsdb.NewTimeSeries("test1", [][2]*float64{}), - tsdb.NewTimeSeries("test2", [][2]*float64{}), + tsdb.NewTimeSeries("test1", tsdb.NewTimeSeriesPointsFromArgs()), + tsdb.NewTimeSeries("test2", tsdb.NewTimeSeriesPointsFromArgs()), } ctx.exec() @@ -86,10 +84,9 @@ func TestQueryCondition(t *testing.T) { }) Convey("Should set NoDataFound both series contains null", func() { - one := float64(120) ctx.series = tsdb.TimeSeriesSlice{ - tsdb.NewTimeSeries("test1", [][2]*float64{{nil, &one}}), - tsdb.NewTimeSeries("test2", [][2]*float64{{nil, &one}}), + tsdb.NewTimeSeries("test1", tsdb.TimeSeriesPoints{tsdb.TimePoint{null.FloatFromPtr(nil), null.FloatFrom(0)}}), + tsdb.NewTimeSeries("test2", tsdb.TimeSeriesPoints{tsdb.TimePoint{null.FloatFromPtr(nil), null.FloatFrom(0)}}), } ctx.exec() @@ -98,11 +95,9 @@ func TestQueryCondition(t *testing.T) { }) Convey("Should not set NoDataFound if one serie is empty", func() { - one := float64(120) - two := float64(0) ctx.series = tsdb.TimeSeriesSlice{ - tsdb.NewTimeSeries("test1", [][2]*float64{}), - tsdb.NewTimeSeries("test2", [][2]*float64{{&one, &two}}), + tsdb.NewTimeSeries("test1", tsdb.NewTimeSeriesPointsFromArgs()), + tsdb.NewTimeSeries("test2", tsdb.NewTimeSeriesPointsFromArgs(120, 0)), } ctx.exec() diff --git a/pkg/services/alerting/conditions/reducer.go b/pkg/services/alerting/conditions/reducer.go index 2bb4cec00be..a982fa63d33 100644 --- a/pkg/services/alerting/conditions/reducer.go +++ b/pkg/services/alerting/conditions/reducer.go @@ -4,19 +4,20 @@ import ( "math" "github.com/grafana/grafana/pkg/tsdb" + "gopkg.in/guregu/null.v3" ) type QueryReducer interface { - Reduce(timeSeries *tsdb.TimeSeries) *float64 + Reduce(timeSeries *tsdb.TimeSeries) null.Float } type SimpleReducer struct { Type string } -func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) *float64 { +func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float { if len(series.Points) == 0 { - return nil + return null.FloatFromPtr(nil) } value := float64(0) @@ -25,36 +26,36 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) *float64 { switch s.Type { case "avg": for _, point := range series.Points { - if point[0] != nil { - value += *point[0] + if point[0].Valid { + value += point[0].Float64 allNull = false } } value = value / float64(len(series.Points)) case "sum": for _, point := range series.Points { - if point[0] != nil { - value += *point[0] + if point[0].Valid { + value += point[0].Float64 allNull = false } } case "min": value = math.MaxFloat64 for _, point := range series.Points { - if point[0] != nil { + if point[0].Valid { allNull = false - if value > *point[0] { - value = *point[0] + if value > point[0].Float64 { + value = point[0].Float64 } } } case "max": value = -math.MaxFloat64 for _, point := range series.Points { - if point[0] != nil { + if point[0].Valid { allNull = false - if value < *point[0] { - value = *point[0] + if value < point[0].Float64 { + value = point[0].Float64 } } } @@ -64,10 +65,10 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) *float64 { } if allNull { - return nil + return null.FloatFromPtr(nil) } - return &value + return null.FloatFrom(value) } func NewSimpleReducer(typ string) *SimpleReducer { diff --git a/pkg/services/alerting/conditions/reducer_test.go b/pkg/services/alerting/conditions/reducer_test.go index f60154bc98d..67765f9c310 100644 --- a/pkg/services/alerting/conditions/reducer_test.go +++ b/pkg/services/alerting/conditions/reducer_test.go @@ -10,44 +10,41 @@ import ( func TestSimpleReducer(t *testing.T) { Convey("Test simple reducer by calculating", t, func() { Convey("avg", func() { - result := *testReducer("avg", 1, 2, 3) + result := testReducer("avg", 1, 2, 3) So(result, ShouldEqual, float64(2)) }) Convey("sum", func() { - result := *testReducer("sum", 1, 2, 3) + result := testReducer("sum", 1, 2, 3) So(result, ShouldEqual, float64(6)) }) Convey("min", func() { - result := *testReducer("min", 3, 2, 1) + result := testReducer("min", 3, 2, 1) So(result, ShouldEqual, float64(1)) }) Convey("max", func() { - result := *testReducer("max", 1, 2, 3) + result := testReducer("max", 1, 2, 3) So(result, ShouldEqual, float64(3)) }) Convey("count", func() { - result := *testReducer("count", 1, 2, 3000) + result := testReducer("count", 1, 2, 3000) So(result, ShouldEqual, float64(3)) }) }) } -func testReducer(typ string, datapoints ...float64) *float64 { +func testReducer(typ string, datapoints ...float64) float64 { reducer := NewSimpleReducer(typ) - var timeserie [][2]*float64 - dummieTimestamp := float64(521452145) + series := &tsdb.TimeSeries{ + Name: "test time serie", + } for idx := range datapoints { - timeserie = append(timeserie, [2]*float64{&datapoints[idx], &dummieTimestamp}) + series.Points = append(series.Points, tsdb.NewTimePoint(datapoints[idx], 1234134)) } - tsdb := &tsdb.TimeSeries{ - Name: "test time serie", - Points: timeserie, - } - return reducer.Reduce(tsdb) + return reducer.Reduce(series).Float64 } diff --git a/pkg/tsdb/graphite/graphite.go b/pkg/tsdb/graphite/graphite.go index def7039ac91..5f14f0302f7 100644 --- a/pkg/tsdb/graphite/graphite.go +++ b/pkg/tsdb/graphite/graphite.go @@ -80,6 +80,7 @@ func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryC result.QueryResults = make(map[string]*tsdb.QueryResult) queryRes := &tsdb.QueryResult{} + for _, series := range data { queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{ Name: series.Target, diff --git a/pkg/tsdb/graphite/types.go b/pkg/tsdb/graphite/types.go index 085b1fb2b94..8bd13aec4f2 100644 --- a/pkg/tsdb/graphite/types.go +++ b/pkg/tsdb/graphite/types.go @@ -1,6 +1,8 @@ package graphite +import "github.com/grafana/grafana/pkg/tsdb" + type TargetResponseDTO struct { - Target string `json:"target"` - DataPoints [][2]*float64 `json:"datapoints"` + Target string `json:"target"` + DataPoints tsdb.TimeSeriesPoints `json:"datapoints"` } diff --git a/pkg/tsdb/models.go b/pkg/tsdb/models.go index fc66a47f981..008be3efa28 100644 --- a/pkg/tsdb/models.go +++ b/pkg/tsdb/models.go @@ -1,6 +1,9 @@ package tsdb -import "github.com/grafana/grafana/pkg/components/simplejson" +import ( + "github.com/grafana/grafana/pkg/components/simplejson" + "gopkg.in/guregu/null.v3" +) type Query struct { RefId string @@ -55,13 +58,29 @@ type QueryResult struct { } type TimeSeries struct { - Name string `json:"name"` - Points [][2]*float64 `json:"points"` + Name string `json:"name"` + Points TimeSeriesPoints `json:"points"` } +type TimePoint [2]null.Float +type TimeSeriesPoints []TimePoint type TimeSeriesSlice []*TimeSeries -func NewTimeSeries(name string, points [][2]*float64) *TimeSeries { +func NewTimePoint(value float64, timestamp float64) TimePoint { + return TimePoint{null.FloatFrom(value), null.FloatFrom(timestamp)} +} + +func NewTimeSeriesPointsFromArgs(values ...float64) TimeSeriesPoints { + points := make(TimeSeriesPoints, 0) + + for i := 0; i < len(values); i += 2 { + points = append(points, NewTimePoint(values[i], values[i+1])) + } + + return points +} + +func NewTimeSeries(name string, points TimeSeriesPoints) *TimeSeries { return &TimeSeries{ Name: name, Points: points, diff --git a/pkg/tsdb/prometheus/prometheus.go b/pkg/tsdb/prometheus/prometheus.go index 4d6ab03cede..eba558ddaa7 100644 --- a/pkg/tsdb/prometheus/prometheus.go +++ b/pkg/tsdb/prometheus/prometheus.go @@ -140,17 +140,15 @@ func parseResponse(value pmodel.Value, query *PrometheusQuery) (map[string]*tsdb } for _, v := range data { - var points [][2]*float64 - for _, k := range v.Values { - timestamp := float64(k.Timestamp) - val := float64(k.Value) - points = append(points, [2]*float64{&val, ×tamp}) + series := tsdb.TimeSeries{ + Name: formatLegend(v.Metric, query), } - queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{ - Name: formatLegend(v.Metric, query), - Points: points, - }) + for _, k := range v.Values { + series.Points = append(series.Points, tsdb.NewTimePoint(float64(k.Value), float64(k.Timestamp.Unix()*1000))) + } + + queryRes.Series = append(queryRes.Series, &series) } queryResults["A"] = queryRes diff --git a/pkg/tsdb/testdata/scenarios.go b/pkg/tsdb/testdata/scenarios.go index c7df7d7bffb..f34db7ac931 100644 --- a/pkg/tsdb/testdata/scenarios.go +++ b/pkg/tsdb/testdata/scenarios.go @@ -4,7 +4,6 @@ import ( "math/rand" "time" - "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/tsdb" ) @@ -21,7 +20,7 @@ var ScenarioRegistry map[string]*Scenario func init() { ScenarioRegistry = make(map[string]*Scenario) - logger := log.New("tsdb.testdata") + //logger := log.New("tsdb.testdata") registerScenario(&Scenario{ Id: "random_walk", @@ -33,13 +32,11 @@ func init() { series := newSeriesForQuery(query) - points := make([][2]*float64, 0) + points := make(tsdb.TimeSeriesPoints, 0) walker := rand.Float64() * 100 for i := int64(0); i < 10000 && timeWalkerMs < to; i++ { - timestamp := float64(timeWalkerMs) - val := float64(walker) - points = append(points, [2]*float64{&val, ×tamp}) + points = append(points, tsdb.NewTimePoint(walker, float64(timeWalkerMs))) walker += rand.Float64() - 0.5 timeWalkerMs += query.IntervalMs @@ -72,12 +69,9 @@ func init() { series := newSeriesForQuery(query) outsideTime := context.TimeRange.MustGetFrom().Add(-1*time.Hour).Unix() * 1000 - timestamp := float64(outsideTime) - logger.Info("time", "from", timestamp) - val := float64(10) - - series.Points = append(series.Points, [2]*float64{&val, ×tamp}) + series.Points = append(series.Points, tsdb.NewTimePoint(10, float64(outsideTime))) queryRes.Series = append(queryRes.Series, series) + return queryRes }, }) From a4648607bb7006aec0db9041100adfea38f5cc77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Wed, 28 Sep 2016 10:37:30 +0200 Subject: [PATCH 27/32] feat(testdata): added csv test data scenario --- pkg/api/metrics.go | 1 + pkg/tsdb/graphite/graphite.go | 2 +- pkg/tsdb/models.go | 6 + pkg/tsdb/prometheus/prometheus.go | 2 +- pkg/tsdb/testdata/scenarios.go | 55 +++- pkg/tsdb/time_range.go | 8 + .../app/testdata/dashboards/alerts.json | 286 ++++++++++++++++++ .../testdata/dashboards/graph_last_1h.json | 123 +++++++- .../app/testdata/datasource/datasource.ts | 3 + .../app/testdata/datasource/query_ctrl.ts | 10 + .../app/testdata/partials/query.editor.html | 12 +- public/app/plugins/app/testdata/plugin.json | 2 +- 12 files changed, 490 insertions(+), 20 deletions(-) create mode 100644 public/app/plugins/app/testdata/dashboards/alerts.json diff --git a/pkg/api/metrics.go b/pkg/api/metrics.go index c3bd9062737..0fa6003d67a 100644 --- a/pkg/api/metrics.go +++ b/pkg/api/metrics.go @@ -48,6 +48,7 @@ func GetTestDataScenarios(c *middleware.Context) Response { "id": scenario.Id, "name": scenario.Name, "description": scenario.Description, + "stringInput": scenario.StringInput, }) } diff --git a/pkg/tsdb/graphite/graphite.go b/pkg/tsdb/graphite/graphite.go index 5f14f0302f7..78685d52371 100644 --- a/pkg/tsdb/graphite/graphite.go +++ b/pkg/tsdb/graphite/graphite.go @@ -79,7 +79,7 @@ func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryC } result.QueryResults = make(map[string]*tsdb.QueryResult) - queryRes := &tsdb.QueryResult{} + queryRes := tsdb.NewQueryResult() for _, series := range data { queryRes.Series = append(queryRes.Series, &tsdb.TimeSeries{ diff --git a/pkg/tsdb/models.go b/pkg/tsdb/models.go index 008be3efa28..bbf7bba7ac7 100644 --- a/pkg/tsdb/models.go +++ b/pkg/tsdb/models.go @@ -66,6 +66,12 @@ type TimePoint [2]null.Float type TimeSeriesPoints []TimePoint type TimeSeriesSlice []*TimeSeries +func NewQueryResult() *QueryResult { + return &QueryResult{ + Series: make(TimeSeriesSlice, 0), + } +} + func NewTimePoint(value float64, timestamp float64) TimePoint { return TimePoint{null.FloatFrom(value), null.FloatFrom(timestamp)} } diff --git a/pkg/tsdb/prometheus/prometheus.go b/pkg/tsdb/prometheus/prometheus.go index eba558ddaa7..f7e68662efa 100644 --- a/pkg/tsdb/prometheus/prometheus.go +++ b/pkg/tsdb/prometheus/prometheus.go @@ -132,7 +132,7 @@ func parseQuery(queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) (*Prom func parseResponse(value pmodel.Value, query *PrometheusQuery) (map[string]*tsdb.QueryResult, error) { queryResults := make(map[string]*tsdb.QueryResult) - queryRes := &tsdb.QueryResult{} + queryRes := tsdb.NewQueryResult() data, ok := value.(pmodel.Matrix) if !ok { diff --git a/pkg/tsdb/testdata/scenarios.go b/pkg/tsdb/testdata/scenarios.go index f34db7ac931..e90b0d4df79 100644 --- a/pkg/tsdb/testdata/scenarios.go +++ b/pkg/tsdb/testdata/scenarios.go @@ -2,8 +2,11 @@ package testdata import ( "math/rand" + "strconv" + "strings" "time" + "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/tsdb" ) @@ -12,6 +15,7 @@ type ScenarioHandler func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.Q type Scenario struct { Id string `json:"id"` Name string `json:"name"` + StringInput string `json:"stringOption"` Description string `json:"description"` Handler ScenarioHandler `json:"-"` } @@ -20,15 +24,17 @@ var ScenarioRegistry map[string]*Scenario func init() { ScenarioRegistry = make(map[string]*Scenario) - //logger := log.New("tsdb.testdata") + logger := log.New("tsdb.testdata") + + logger.Debug("Initializing TestData Scenario") registerScenario(&Scenario{ Id: "random_walk", Name: "Random Walk", Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult { - to := context.TimeRange.MustGetTo().UnixNano() / int64(time.Millisecond) - timeWalkerMs := context.TimeRange.MustGetFrom().UnixNano() / int64(time.Millisecond) + timeWalkerMs := context.TimeRange.GetFromAsMsEpoch() + to := context.TimeRange.GetToAsMsEpoch() series := newSeriesForQuery(query) @@ -44,7 +50,7 @@ func init() { series.Points = points - queryRes := &tsdb.QueryResult{} + queryRes := tsdb.NewQueryResult() queryRes.Series = append(queryRes.Series, series) return queryRes }, @@ -54,9 +60,7 @@ func init() { Id: "no_data_points", Name: "No Data Points", Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult { - return &tsdb.QueryResult{ - Series: make(tsdb.TimeSeriesSlice, 0), - } + return tsdb.NewQueryResult() }, }) @@ -64,7 +68,7 @@ func init() { Id: "datapoints_outside_range", Name: "Datapoints Outside Range", Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult { - queryRes := &tsdb.QueryResult{} + queryRes := tsdb.NewQueryResult() series := newSeriesForQuery(query) outsideTime := context.TimeRange.MustGetFrom().Add(-1*time.Hour).Unix() * 1000 @@ -75,6 +79,41 @@ func init() { return queryRes }, }) + + registerScenario(&Scenario{ + Id: "csv_metric_values", + Name: "CSV Metric Values", + StringInput: "1,20,90,30,5,0", + Handler: func(query *tsdb.Query, context *tsdb.QueryContext) *tsdb.QueryResult { + queryRes := tsdb.NewQueryResult() + + stringInput := query.Model.Get("stringInput").MustString() + values := []float64{} + for _, strVal := range strings.Split(stringInput, ",") { + if val, err := strconv.ParseFloat(strVal, 64); err == nil { + values = append(values, val) + } + } + + if len(values) == 0 { + return queryRes + } + + series := newSeriesForQuery(query) + startTime := context.TimeRange.GetFromAsMsEpoch() + endTime := context.TimeRange.GetToAsMsEpoch() + step := (endTime - startTime) / int64(len(values)-1) + + for _, val := range values { + series.Points = append(series.Points, tsdb.NewTimePoint(val, float64(startTime))) + startTime += step + } + + queryRes.Series = append(queryRes.Series, series) + + return queryRes + }, + }) } func registerScenario(scenario *Scenario) { diff --git a/pkg/tsdb/time_range.go b/pkg/tsdb/time_range.go index 67227b834dc..cf6bc6a5048 100644 --- a/pkg/tsdb/time_range.go +++ b/pkg/tsdb/time_range.go @@ -21,6 +21,14 @@ type TimeRange struct { Now time.Time } +func (tr *TimeRange) GetFromAsMsEpoch() int64 { + return tr.MustGetFrom().UnixNano() / int64(time.Millisecond) +} + +func (tr *TimeRange) GetToAsMsEpoch() int64 { + return tr.MustGetTo().UnixNano() / int64(time.Millisecond) +} + func (tr *TimeRange) MustGetFrom() time.Time { if res, err := tr.ParseFrom(); err != nil { return time.Unix(0, 0) diff --git a/public/app/plugins/app/testdata/dashboards/alerts.json b/public/app/plugins/app/testdata/dashboards/alerts.json new file mode 100644 index 00000000000..c510ec220ea --- /dev/null +++ b/public/app/plugins/app/testdata/dashboards/alerts.json @@ -0,0 +1,286 @@ +{ + "title": "TestData - Alerts", + "tags": [ + "grafana-test" + ], + "style": "dark", + "timezone": "browser", + "editable": true, + "hideControls": false, + "sharedCrosshair": false, + "rows": [ + { + "collapse": false, + "editable": true, + "height": 255.625, + "panels": [ + { + "alert": { + "conditions": [ + { + "evaluator": { + "params": [ + 111 + ], + "type": "gt" + }, + "query": { + "params": [ + "A", + "5m", + "now" + ] + }, + "reducer": { + "params": [], + "type": "avg" + }, + "type": "query" + } + ], + "enabled": true, + "frequency": "60s", + "handler": 1, + "name": "Should always be green", + "noDataState": "no_data", + "notifications": [] + }, + "aliasColors": {}, + "bars": false, + "datasource": "Grafana TestData", + "editable": true, + "error": false, + "fill": 1, + "id": 3, + "isNew": true, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 2, + "links": [], + "nullPointMode": "connected", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "span": 6, + "stack": false, + "steppedLine": false, + "targets": [ + { + "refId": "A", + "scenario": "random_walk", + "scenarioId": "csv_metric_values", + "target": "", + "stringInput": "1,20,90,30,5,0" + } + ], + "thresholds": [ + { + "colorMode": "critical", + "fill": true, + "line": true, + "op": "gt", + "value": 111 + } + ], + "timeFrom": null, + "timeShift": null, + "title": "Always OK", + "tooltip": { + "msResolution": false, + "shared": true, + "sort": 0, + "value_type": "cumulative" + }, + "type": "graph", + "xaxis": { + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "short", + "label": "", + "logBase": 1, + "max": "125", + "min": "0", + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ] + }, + { + "alert": { + "conditions": [ + { + "evaluator": { + "params": [ + 177 + ], + "type": "gt" + }, + "query": { + "params": [ + "A", + "5m", + "now" + ] + }, + "reducer": { + "params": [], + "type": "avg" + }, + "type": "query" + } + ], + "enabled": true, + "frequency": "60s", + "handler": 1, + "name": "Always Alerting", + "noDataState": "no_data", + "notifications": [] + }, + "aliasColors": {}, + "bars": false, + "datasource": "Grafana TestData", + "editable": true, + "error": false, + "fill": 1, + "id": 4, + "isNew": true, + "legend": { + "avg": false, + "current": false, + "max": false, + "min": false, + "show": true, + "total": false, + "values": false + }, + "lines": true, + "linewidth": 2, + "links": [], + "nullPointMode": "connected", + "percentage": false, + "pointradius": 5, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "span": 6, + "stack": false, + "steppedLine": false, + "targets": [ + { + "refId": "A", + "scenario": "random_walk", + "scenarioId": "csv_metric_values", + "target": "", + "stringInput": "200,445,100,150,200,220,190" + } + ], + "thresholds": [ + { + "value": 177, + "op": "gt", + "fill": true, + "line": true, + "colorMode": "critical" + } + ], + "timeFrom": null, + "timeShift": null, + "title": "Always Alerting", + "tooltip": { + "msResolution": false, + "shared": true, + "sort": 0, + "value_type": "cumulative" + }, + "type": "graph", + "xaxis": { + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "short", + "label": "", + "logBase": 1, + "max": null, + "min": "0", + "show": true + }, + { + "format": "short", + "label": "", + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ] + } + ], + "title": "New row" + } + ], + "time": { + "from": "now-6h", + "to": "now" + }, + "timepicker": { + "refresh_intervals": [ + "5s", + "10s", + "30s", + "1m", + "5m", + "15m", + "30m", + "1h", + "2h", + "1d" + ], + "time_options": [ + "5m", + "15m", + "1h", + "6h", + "12h", + "24h", + "2d", + "7d", + "30d" + ] + }, + "templating": { + "list": [] + }, + "annotations": { + "list": [] + }, + "schemaVersion": 13, + "version": 10, + "links": [], + "gnetId": null +} diff --git a/public/app/plugins/app/testdata/dashboards/graph_last_1h.json b/public/app/plugins/app/testdata/dashboards/graph_last_1h.json index 0cca7b6c331..757dd48e50f 100644 --- a/public/app/plugins/app/testdata/dashboards/graph_last_1h.json +++ b/public/app/plugins/app/testdata/dashboards/graph_last_1h.json @@ -1,5 +1,5 @@ { - "revision": 3, + "revision": 4, "title": "TestData - Graph Panel Last 1h", "tags": [ "grafana-test" @@ -284,7 +284,7 @@ } ], "thresholds": [], - "timeFrom": "5d", + "timeFrom": "2s", "timeShift": null, "title": "Millisecond res x-axis and tooltip", "tooltip": { @@ -318,9 +318,126 @@ "show": true } ] + }, + { + "title": "", + "error": false, + "span": 4, + "editable": true, + "type": "text", + "isNew": true, + "id": 6, + "mode": "markdown", + "content": "Just verify that the tooltip time has millisecond resolution ", + "links": [] } ], "title": "New row" + }, + { + "title": "New row", + "height": 336, + "editable": true, + "collapse": false, + "panels": [ + { + "title": "2 yaxis and axis lables", + "error": false, + "span": 7.99561403508772, + "editable": true, + "type": "graph", + "isNew": true, + "id": 5, + "targets": [ + { + "target": "", + "refId": "A", + "scenarioId": "csv_metric_values", + "stringInput": "1,20,90,30,5,0" + }, + { + "target": "", + "refId": "B", + "scenarioId": "csv_metric_values", + "stringInput": "2000,3000,4000,1000,3000,10000" + } + ], + "datasource": "Grafana TestData", + "renderer": "flot", + "yaxes": [ + { + "label": "Perecent", + "show": true, + "logBase": 1, + "min": null, + "max": null, + "format": "percent" + }, + { + "label": "Pressure", + "show": true, + "logBase": 1, + "min": null, + "max": null, + "format": "short" + } + ], + "xaxis": { + "show": true, + "mode": "time", + "name": null, + "values": [] + }, + "lines": true, + "fill": 1, + "linewidth": 2, + "points": false, + "pointradius": 5, + "bars": false, + "stack": false, + "percentage": false, + "legend": { + "show": true, + "values": false, + "min": false, + "max": false, + "current": false, + "total": false, + "avg": false + }, + "nullPointMode": "connected", + "steppedLine": false, + "tooltip": { + "value_type": "cumulative", + "shared": true, + "sort": 0, + "msResolution": false + }, + "timeFrom": null, + "timeShift": null, + "aliasColors": {}, + "seriesOverrides": [ + { + "alias": "B-series", + "yaxis": 2 + } + ], + "thresholds": [], + "links": [] + }, + { + "title": "", + "error": false, + "span": 4.00438596491228, + "editable": true, + "type": "text", + "isNew": true, + "id": 7, + "mode": "markdown", + "content": "Verify that axis labels look ok", + "links": [] + } + ] } ], "time": { @@ -360,7 +477,7 @@ }, "refresh": false, "schemaVersion": 13, - "version": 4, + "version": 3, "links": [], "gnetId": null } diff --git a/public/app/plugins/app/testdata/datasource/datasource.ts b/public/app/plugins/app/testdata/datasource/datasource.ts index a06daa19262..e0846d99ab6 100644 --- a/public/app/plugins/app/testdata/datasource/datasource.ts +++ b/public/app/plugins/app/testdata/datasource/datasource.ts @@ -1,6 +1,7 @@ /// import _ from 'lodash'; +import angular from 'angular'; class TestDataDatasource { @@ -16,6 +17,8 @@ class TestDataDatasource { scenarioId: item.scenarioId, intervalMs: options.intervalMs, maxDataPoints: options.maxDataPoints, + stringInput: item.stringInput, + jsonInput: angular.fromJson(item.jsonInput), }; }); diff --git a/public/app/plugins/app/testdata/datasource/query_ctrl.ts b/public/app/plugins/app/testdata/datasource/query_ctrl.ts index f22ef8948ad..6b0ad93f26c 100644 --- a/public/app/plugins/app/testdata/datasource/query_ctrl.ts +++ b/public/app/plugins/app/testdata/datasource/query_ctrl.ts @@ -1,5 +1,7 @@ /// +import _ from 'lodash'; + import {TestDataDatasource} from './datasource'; import {QueryCtrl} from 'app/plugins/sdk'; @@ -7,6 +9,7 @@ export class TestDataQueryCtrl extends QueryCtrl { static templateUrl = 'partials/query.editor.html'; scenarioList: any; + scenario: any; /** @ngInject **/ constructor($scope, $injector, private backendSrv) { @@ -19,7 +22,14 @@ export class TestDataQueryCtrl extends QueryCtrl { $onInit() { return this.backendSrv.get('/api/tsdb/testdata/scenarios').then(res => { this.scenarioList = res; + this.scenario = _.find(this.scenarioList, {id: this.target.scenarioId}); }); } + + scenarioChanged() { + this.scenario = _.find(this.scenarioList, {id: this.target.scenarioId}); + this.target.stringInput = this.scenario.stringInput; + this.refresh(); + } } diff --git a/public/app/plugins/app/testdata/partials/query.editor.html b/public/app/plugins/app/testdata/partials/query.editor.html index 53f84309661..a39582d5397 100644 --- a/public/app/plugins/app/testdata/partials/query.editor.html +++ b/public/app/plugins/app/testdata/partials/query.editor.html @@ -2,17 +2,17 @@
-
- +
+
-
- - +
+ +
- +
diff --git a/public/app/plugins/app/testdata/plugin.json b/public/app/plugins/app/testdata/plugin.json index 0bd65a0735d..efd4e6bb739 100644 --- a/public/app/plugins/app/testdata/plugin.json +++ b/public/app/plugins/app/testdata/plugin.json @@ -9,7 +9,7 @@ "name": "Grafana Project", "url": "http://grafana.org" }, - "version": "1.0.6", + "version": "1.0.7", "updated": "2016-09-26" }, From 68370ba2bcc999a47048b137d709e239ccea8d8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Wed, 28 Sep 2016 10:44:21 +0200 Subject: [PATCH 28/32] feat(testdata): added alert dashboard --- .../app/testdata/dashboards/alerts.json | 29 ++++++++++--------- public/app/plugins/app/testdata/plugin.json | 7 ++++- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/public/app/plugins/app/testdata/dashboards/alerts.json b/public/app/plugins/app/testdata/dashboards/alerts.json index c510ec220ea..159df0f458b 100644 --- a/public/app/plugins/app/testdata/dashboards/alerts.json +++ b/public/app/plugins/app/testdata/dashboards/alerts.json @@ -1,4 +1,5 @@ { + "revision": 2, "title": "TestData - Alerts", "tags": [ "grafana-test" @@ -20,7 +21,7 @@ { "evaluator": { "params": [ - 111 + 60 ], "type": "gt" }, @@ -41,7 +42,7 @@ "enabled": true, "frequency": "60s", "handler": 1, - "name": "Should always be green", + "name": "TestData - Always OK", "noDataState": "no_data", "notifications": [] }, @@ -79,17 +80,17 @@ "refId": "A", "scenario": "random_walk", "scenarioId": "csv_metric_values", - "target": "", - "stringInput": "1,20,90,30,5,0" + "stringInput": "1,20,90,30,5,0", + "target": "" } ], "thresholds": [ { - "colorMode": "critical", + "value": 60, + "op": "gt", "fill": true, "line": true, - "op": "gt", - "value": 111 + "colorMode": "critical" } ], "timeFrom": null, @@ -154,7 +155,7 @@ "enabled": true, "frequency": "60s", "handler": 1, - "name": "Always Alerting", + "name": "TestData - Always Alerting", "noDataState": "no_data", "notifications": [] }, @@ -192,17 +193,17 @@ "refId": "A", "scenario": "random_walk", "scenarioId": "csv_metric_values", - "target": "", - "stringInput": "200,445,100,150,200,220,190" + "stringInput": "200,445,100,150,200,220,190", + "target": "" } ], "thresholds": [ { - "value": 177, - "op": "gt", + "colorMode": "critical", "fill": true, "line": true, - "colorMode": "critical" + "op": "gt", + "value": 177 } ], "timeFrom": null, @@ -280,7 +281,7 @@ "list": [] }, "schemaVersion": 13, - "version": 10, + "version": 4, "links": [], "gnetId": null } diff --git a/public/app/plugins/app/testdata/plugin.json b/public/app/plugins/app/testdata/plugin.json index efd4e6bb739..6742ad04ecb 100644 --- a/public/app/plugins/app/testdata/plugin.json +++ b/public/app/plugins/app/testdata/plugin.json @@ -9,7 +9,7 @@ "name": "Grafana Project", "url": "http://grafana.org" }, - "version": "1.0.7", + "version": "1.0.13", "updated": "2016-09-26" }, @@ -18,6 +18,11 @@ "type": "dashboard", "name": "TestData - Graph Last 1h", "path": "dashboards/graph_last_1h.json" + }, + { + "type": "dashboard", + "name": "TestData - Alerts", + "path": "dashboards/alerts.json" } ], From 593863fb9eb65b25ad7627e1ff77bbd79af4c883 Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Wed, 28 Sep 2016 18:54:25 +0900 Subject: [PATCH 29/32] (prometheus) check time range (#6137) --- public/app/plugins/datasource/prometheus/datasource.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index e2677b4b8f5..941591791ef 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/public/app/plugins/datasource/prometheus/datasource.ts @@ -123,6 +123,10 @@ export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateS }; this.performTimeSeriesQuery = function(query, start, end) { + if (start > end) { + throw { message: 'Invalid time range' }; + } + var url = '/api/v1/query_range?query=' + encodeURIComponent(query.expr) + '&start=' + start + '&end=' + end + '&step=' + query.step; return this._request('GET', url, query.requestId); }; From 599fe49944cbf28db1483801e99d02a6ca48ef98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Wed, 28 Sep 2016 12:12:38 +0200 Subject: [PATCH 30/32] fix(templating): fix to datasource variable, was not updated on dashboard load, added unit test for case as well --- .../templating/datasource_variable.ts | 3 +++ .../specs/variable_srv_init_specs.ts | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/public/app/features/templating/datasource_variable.ts b/public/app/features/templating/datasource_variable.ts index 3a297002900..d43c0dd486d 100644 --- a/public/app/features/templating/datasource_variable.ts +++ b/public/app/features/templating/datasource_variable.ts @@ -10,6 +10,7 @@ export class DatasourceVariable implements Variable { query: string; options: any; current: any; + refresh: any; defaults = { type: 'datasource', @@ -20,11 +21,13 @@ export class DatasourceVariable implements Variable { regex: '', options: [], query: '', + refresh: 1, }; /** @ngInject **/ constructor(private model, private datasourceSrv, private variableSrv) { assignModelProperties(this, model, this.defaults); + this.refresh = 1; } getModel() { diff --git a/public/app/features/templating/specs/variable_srv_init_specs.ts b/public/app/features/templating/specs/variable_srv_init_specs.ts index 8cac63135ca..533c70dfc25 100644 --- a/public/app/features/templating/specs/variable_srv_init_specs.ts +++ b/public/app/features/templating/specs/variable_srv_init_specs.ts @@ -62,6 +62,7 @@ describe('VariableSrv init', function() { options: [{text: "test", value: "test"}] }]; scenario.urlParams["var-apps"] = "new"; + scenario.metricSources = []; }); it('should update current value', () => { @@ -110,6 +111,30 @@ describe('VariableSrv init', function() { }); }); + describeInitScenario('when datasource variable is initialized', scenario => { + scenario.setup(() => { + scenario.variables = [{ + type: 'datasource', + query: 'graphite', + name: 'test', + current: {value: 'backend4_pee', text: 'backend4_pee'}, + regex: '/pee$/' + } + ]; + scenario.metricSources = [ + {name: 'backend1', meta: {id: 'influx'}}, + {name: 'backend2_pee', meta: {id: 'graphite'}}, + {name: 'backend3', meta: {id: 'graphite'}}, + {name: 'backend4_pee', meta: {id: 'graphite'}}, + ]; + }); + + it('should update current value', function() { + var variable = ctx.variableSrv.variables[0]; + expect(variable.options.length).to.be(2); + }); + }); + describeInitScenario('when template variable is present in url multiple times', scenario => { scenario.setup(() => { scenario.variables = [{ From 15299c2ac009c7624aff4e2b43b0baac0f36b97c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Wed, 28 Sep 2016 12:15:47 +0200 Subject: [PATCH 31/32] fix(govendor): added new null package --- vendor/gopkg.in/guregu/null.v3/LICENSE | 10 ++ vendor/gopkg.in/guregu/null.v3/README.md | 75 +++++++++++++ vendor/gopkg.in/guregu/null.v3/bool.go | 129 ++++++++++++++++++++++ vendor/gopkg.in/guregu/null.v3/float.go | 117 ++++++++++++++++++++ vendor/gopkg.in/guregu/null.v3/int.go | 118 ++++++++++++++++++++ vendor/gopkg.in/guregu/null.v3/string.go | 110 ++++++++++++++++++ vendor/gopkg.in/guregu/null.v3/time.go | 135 +++++++++++++++++++++++ vendor/vendor.json | 18 ++- 8 files changed, 706 insertions(+), 6 deletions(-) create mode 100644 vendor/gopkg.in/guregu/null.v3/LICENSE create mode 100644 vendor/gopkg.in/guregu/null.v3/README.md create mode 100644 vendor/gopkg.in/guregu/null.v3/bool.go create mode 100644 vendor/gopkg.in/guregu/null.v3/float.go create mode 100644 vendor/gopkg.in/guregu/null.v3/int.go create mode 100644 vendor/gopkg.in/guregu/null.v3/string.go create mode 100644 vendor/gopkg.in/guregu/null.v3/time.go diff --git a/vendor/gopkg.in/guregu/null.v3/LICENSE b/vendor/gopkg.in/guregu/null.v3/LICENSE new file mode 100644 index 00000000000..69062b45b16 --- /dev/null +++ b/vendor/gopkg.in/guregu/null.v3/LICENSE @@ -0,0 +1,10 @@ +Copyright (c) 2014, Greg Roseberry +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/vendor/gopkg.in/guregu/null.v3/README.md b/vendor/gopkg.in/guregu/null.v3/README.md new file mode 100644 index 00000000000..62bf48be93e --- /dev/null +++ b/vendor/gopkg.in/guregu/null.v3/README.md @@ -0,0 +1,75 @@ +## null [![GoDoc](https://godoc.org/github.com/guregu/null?status.svg)](https://godoc.org/github.com/guregu/null) [![Coverage](http://gocover.io/_badge/github.com/guregu/null)](http://gocover.io/github.com/guregu/null) +`import "gopkg.in/guregu/null.v3"` + +null is a library with reasonable options for dealing with nullable SQL and JSON values + +There are two packages: `null` and its subpackage `zero`. + +Types in `null` will only be considered null on null input, and will JSON encode to `null`. If you need zero and null be considered separate values, use these. + +Types in `zero` are treated like zero values in Go: blank string input will produce a null `zero.String`, and null Strings will JSON encode to `""`. Zero values of these types will be considered null to SQL. If you need zero and null treated the same, use these. + +All types implement `sql.Scanner` and `driver.Valuer`, so you can use this library in place of `sql.NullXXX`. All types also implement: `encoding.TextMarshaler`, `encoding.TextUnmarshaler`, `json.Marshaler`, and `json.Unmarshaler`. + +### null package + +`import "gopkg.in/guregu/null.v3"` + +#### null.String +Nullable string. + +Marshals to JSON null if SQL source data is null. Zero (blank) input will not produce a null String. Can unmarshal from `sql.NullString` JSON input or string input. + +#### null.Int +Nullable int64. + +Marshals to JSON null if SQL source data is null. Zero input will not produce a null Int. Can unmarshal from `sql.NullInt64` JSON input. + +#### null.Float +Nullable float64. + +Marshals to JSON null if SQL source data is null. Zero input will not produce a null Float. Can unmarshal from `sql.NullFloat64` JSON input. + +#### null.Bool +Nullable bool. + +Marshals to JSON null if SQL source data is null. False input will not produce a null Bool. Can unmarshal from `sql.NullBool` JSON input. + +#### null.Time + +Marshals to JSON null if SQL source data is null. Uses `time.Time`'s marshaler. Can unmarshal from `pq.NullTime` and similar JSON input. + +### zero package + +`import "gopkg.in/guregu/null.v3/zero"` + +#### zero.String +Nullable string. + +Will marshal to a blank string if null. Blank string input produces a null String. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullString` JSON input. + +#### zero.Int +Nullable int64. + +Will marshal to 0 if null. 0 produces a null Int. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullInt64` JSON input. + +#### zero.Float +Nullable float64. + +Will marshal to 0 if null. 0.0 produces a null Float. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullFloat64` JSON input. + +#### zero.Bool +Nullable bool. + +Will marshal to false if null. `false` produces a null Float. Null values and zero values are considered equivalent. Can unmarshal from `sql.NullBool` JSON input. + +#### zero.Time + +Will marshal to the zero time if null. Uses `time.Time`'s marshaler. Can unmarshal from `pq.NullTime` and similar JSON input. + + +### Bugs +`json`'s `",omitempty"` struct tag does not work correctly right now. It will never omit a null or empty String. This might be [fixed eventually](https://github.com/golang/go/issues/4357). + +### License +BSD diff --git a/vendor/gopkg.in/guregu/null.v3/bool.go b/vendor/gopkg.in/guregu/null.v3/bool.go new file mode 100644 index 00000000000..1ec13961bfe --- /dev/null +++ b/vendor/gopkg.in/guregu/null.v3/bool.go @@ -0,0 +1,129 @@ +package null + +import ( + "database/sql" + "encoding/json" + "errors" + "fmt" + "reflect" +) + +// Bool is a nullable bool. +// It does not consider false values to be null. +// It will decode to null, not false, if null. +type Bool struct { + sql.NullBool +} + +// NewBool creates a new Bool +func NewBool(b bool, valid bool) Bool { + return Bool{ + NullBool: sql.NullBool{ + Bool: b, + Valid: valid, + }, + } +} + +// BoolFrom creates a new Bool that will always be valid. +func BoolFrom(b bool) Bool { + return NewBool(b, true) +} + +// BoolFromPtr creates a new Bool that will be null if f is nil. +func BoolFromPtr(b *bool) Bool { + if b == nil { + return NewBool(false, false) + } + return NewBool(*b, true) +} + +// UnmarshalJSON implements json.Unmarshaler. +// It supports number and null input. +// 0 will not be considered a null Bool. +// It also supports unmarshalling a sql.NullBool. +func (b *Bool) UnmarshalJSON(data []byte) error { + var err error + var v interface{} + if err = json.Unmarshal(data, &v); err != nil { + return err + } + switch x := v.(type) { + case bool: + b.Bool = x + case map[string]interface{}: + err = json.Unmarshal(data, &b.NullBool) + case nil: + b.Valid = false + return nil + default: + err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Bool", reflect.TypeOf(v).Name()) + } + b.Valid = err == nil + return err +} + +// UnmarshalText implements encoding.TextUnmarshaler. +// It will unmarshal to a null Bool if the input is a blank or not an integer. +// It will return an error if the input is not an integer, blank, or "null". +func (b *Bool) UnmarshalText(text []byte) error { + str := string(text) + switch str { + case "", "null": + b.Valid = false + return nil + case "true": + b.Bool = true + case "false": + b.Bool = false + default: + b.Valid = false + return errors.New("invalid input:" + str) + } + b.Valid = true + return nil +} + +// MarshalJSON implements json.Marshaler. +// It will encode null if this Bool is null. +func (b Bool) MarshalJSON() ([]byte, error) { + if !b.Valid { + return []byte("null"), nil + } + if !b.Bool { + return []byte("false"), nil + } + return []byte("true"), nil +} + +// MarshalText implements encoding.TextMarshaler. +// It will encode a blank string if this Bool is null. +func (b Bool) MarshalText() ([]byte, error) { + if !b.Valid { + return []byte{}, nil + } + if !b.Bool { + return []byte("false"), nil + } + return []byte("true"), nil +} + +// SetValid changes this Bool's value and also sets it to be non-null. +func (b *Bool) SetValid(v bool) { + b.Bool = v + b.Valid = true +} + +// Ptr returns a pointer to this Bool's value, or a nil pointer if this Bool is null. +func (b Bool) Ptr() *bool { + if !b.Valid { + return nil + } + return &b.Bool +} + +// IsZero returns true for invalid Bools, for future omitempty support (Go 1.4?) +// A non-null Bool with a 0 value will not be considered zero. +func (b Bool) IsZero() bool { + return !b.Valid +} diff --git a/vendor/gopkg.in/guregu/null.v3/float.go b/vendor/gopkg.in/guregu/null.v3/float.go new file mode 100644 index 00000000000..1f57b959ab7 --- /dev/null +++ b/vendor/gopkg.in/guregu/null.v3/float.go @@ -0,0 +1,117 @@ +package null + +import ( + "database/sql" + "encoding/json" + "fmt" + "reflect" + "strconv" +) + +// Float is a nullable float64. +// It does not consider zero values to be null. +// It will decode to null, not zero, if null. +type Float struct { + sql.NullFloat64 +} + +// NewFloat creates a new Float +func NewFloat(f float64, valid bool) Float { + return Float{ + NullFloat64: sql.NullFloat64{ + Float64: f, + Valid: valid, + }, + } +} + +// FloatFrom creates a new Float that will always be valid. +func FloatFrom(f float64) Float { + return NewFloat(f, true) +} + +// FloatFromPtr creates a new Float that be null if f is nil. +func FloatFromPtr(f *float64) Float { + if f == nil { + return NewFloat(0, false) + } + return NewFloat(*f, true) +} + +// UnmarshalJSON implements json.Unmarshaler. +// It supports number and null input. +// 0 will not be considered a null Float. +// It also supports unmarshalling a sql.NullFloat64. +func (f *Float) UnmarshalJSON(data []byte) error { + var err error + var v interface{} + if err = json.Unmarshal(data, &v); err != nil { + return err + } + switch x := v.(type) { + case float64: + f.Float64 = float64(x) + case map[string]interface{}: + err = json.Unmarshal(data, &f.NullFloat64) + case nil: + f.Valid = false + return nil + default: + err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Float", reflect.TypeOf(v).Name()) + } + f.Valid = err == nil + return err +} + +// UnmarshalText implements encoding.TextUnmarshaler. +// It will unmarshal to a null Float if the input is a blank or not an integer. +// It will return an error if the input is not an integer, blank, or "null". +func (f *Float) UnmarshalText(text []byte) error { + str := string(text) + if str == "" || str == "null" { + f.Valid = false + return nil + } + var err error + f.Float64, err = strconv.ParseFloat(string(text), 64) + f.Valid = err == nil + return err +} + +// MarshalJSON implements json.Marshaler. +// It will encode null if this Float is null. +func (f Float) MarshalJSON() ([]byte, error) { + if !f.Valid { + return []byte("null"), nil + } + return []byte(strconv.FormatFloat(f.Float64, 'f', -1, 64)), nil +} + +// MarshalText implements encoding.TextMarshaler. +// It will encode a blank string if this Float is null. +func (f Float) MarshalText() ([]byte, error) { + if !f.Valid { + return []byte{}, nil + } + return []byte(strconv.FormatFloat(f.Float64, 'f', -1, 64)), nil +} + +// SetValid changes this Float's value and also sets it to be non-null. +func (f *Float) SetValid(n float64) { + f.Float64 = n + f.Valid = true +} + +// Ptr returns a pointer to this Float's value, or a nil pointer if this Float is null. +func (f Float) Ptr() *float64 { + if !f.Valid { + return nil + } + return &f.Float64 +} + +// IsZero returns true for invalid Floats, for future omitempty support (Go 1.4?) +// A non-null Float with a 0 value will not be considered zero. +func (f Float) IsZero() bool { + return !f.Valid +} diff --git a/vendor/gopkg.in/guregu/null.v3/int.go b/vendor/gopkg.in/guregu/null.v3/int.go new file mode 100644 index 00000000000..981d17b09aa --- /dev/null +++ b/vendor/gopkg.in/guregu/null.v3/int.go @@ -0,0 +1,118 @@ +package null + +import ( + "database/sql" + "encoding/json" + "fmt" + "reflect" + "strconv" +) + +// Int is an nullable int64. +// It does not consider zero values to be null. +// It will decode to null, not zero, if null. +type Int struct { + sql.NullInt64 +} + +// NewInt creates a new Int +func NewInt(i int64, valid bool) Int { + return Int{ + NullInt64: sql.NullInt64{ + Int64: i, + Valid: valid, + }, + } +} + +// IntFrom creates a new Int that will always be valid. +func IntFrom(i int64) Int { + return NewInt(i, true) +} + +// IntFromPtr creates a new Int that be null if i is nil. +func IntFromPtr(i *int64) Int { + if i == nil { + return NewInt(0, false) + } + return NewInt(*i, true) +} + +// UnmarshalJSON implements json.Unmarshaler. +// It supports number and null input. +// 0 will not be considered a null Int. +// It also supports unmarshalling a sql.NullInt64. +func (i *Int) UnmarshalJSON(data []byte) error { + var err error + var v interface{} + if err = json.Unmarshal(data, &v); err != nil { + return err + } + switch v.(type) { + case float64: + // Unmarshal again, directly to int64, to avoid intermediate float64 + err = json.Unmarshal(data, &i.Int64) + case map[string]interface{}: + err = json.Unmarshal(data, &i.NullInt64) + case nil: + i.Valid = false + return nil + default: + err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Int", reflect.TypeOf(v).Name()) + } + i.Valid = err == nil + return err +} + +// UnmarshalText implements encoding.TextUnmarshaler. +// It will unmarshal to a null Int if the input is a blank or not an integer. +// It will return an error if the input is not an integer, blank, or "null". +func (i *Int) UnmarshalText(text []byte) error { + str := string(text) + if str == "" || str == "null" { + i.Valid = false + return nil + } + var err error + i.Int64, err = strconv.ParseInt(string(text), 10, 64) + i.Valid = err == nil + return err +} + +// MarshalJSON implements json.Marshaler. +// It will encode null if this Int is null. +func (i Int) MarshalJSON() ([]byte, error) { + if !i.Valid { + return []byte("null"), nil + } + return []byte(strconv.FormatInt(i.Int64, 10)), nil +} + +// MarshalText implements encoding.TextMarshaler. +// It will encode a blank string if this Int is null. +func (i Int) MarshalText() ([]byte, error) { + if !i.Valid { + return []byte{}, nil + } + return []byte(strconv.FormatInt(i.Int64, 10)), nil +} + +// SetValid changes this Int's value and also sets it to be non-null. +func (i *Int) SetValid(n int64) { + i.Int64 = n + i.Valid = true +} + +// Ptr returns a pointer to this Int's value, or a nil pointer if this Int is null. +func (i Int) Ptr() *int64 { + if !i.Valid { + return nil + } + return &i.Int64 +} + +// IsZero returns true for invalid Ints, for future omitempty support (Go 1.4?) +// A non-null Int with a 0 value will not be considered zero. +func (i Int) IsZero() bool { + return !i.Valid +} diff --git a/vendor/gopkg.in/guregu/null.v3/string.go b/vendor/gopkg.in/guregu/null.v3/string.go new file mode 100644 index 00000000000..554aac820e3 --- /dev/null +++ b/vendor/gopkg.in/guregu/null.v3/string.go @@ -0,0 +1,110 @@ +// Package null contains SQL types that consider zero input and null input as separate values, +// with convenient support for JSON and text marshaling. +// Types in this package will always encode to their null value if null. +// Use the zero subpackage if you want zero values and null to be treated the same. +package null + +import ( + "database/sql" + "encoding/json" + "fmt" + "reflect" +) + +// String is a nullable string. It supports SQL and JSON serialization. +// It will marshal to null if null. Blank string input will be considered null. +type String struct { + sql.NullString +} + +// StringFrom creates a new String that will never be blank. +func StringFrom(s string) String { + return NewString(s, true) +} + +// StringFromPtr creates a new String that be null if s is nil. +func StringFromPtr(s *string) String { + if s == nil { + return NewString("", false) + } + return NewString(*s, true) +} + +// NewString creates a new String +func NewString(s string, valid bool) String { + return String{ + NullString: sql.NullString{ + String: s, + Valid: valid, + }, + } +} + +// UnmarshalJSON implements json.Unmarshaler. +// It supports string and null input. Blank string input does not produce a null String. +// It also supports unmarshalling a sql.NullString. +func (s *String) UnmarshalJSON(data []byte) error { + var err error + var v interface{} + if err = json.Unmarshal(data, &v); err != nil { + return err + } + switch x := v.(type) { + case string: + s.String = x + case map[string]interface{}: + err = json.Unmarshal(data, &s.NullString) + case nil: + s.Valid = false + return nil + default: + err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.String", reflect.TypeOf(v).Name()) + } + s.Valid = err == nil + return err +} + +// MarshalJSON implements json.Marshaler. +// It will encode null if this String is null. +func (s String) MarshalJSON() ([]byte, error) { + if !s.Valid { + return []byte("null"), nil + } + return json.Marshal(s.String) +} + +// MarshalText implements encoding.TextMarshaler. +// It will encode a blank string when this String is null. +func (s String) MarshalText() ([]byte, error) { + if !s.Valid { + return []byte{}, nil + } + return []byte(s.String), nil +} + +// UnmarshalText implements encoding.TextUnmarshaler. +// It will unmarshal to a null String if the input is a blank string. +func (s *String) UnmarshalText(text []byte) error { + s.String = string(text) + s.Valid = s.String != "" + return nil +} + +// SetValid changes this String's value and also sets it to be non-null. +func (s *String) SetValid(v string) { + s.String = v + s.Valid = true +} + +// Ptr returns a pointer to this String's value, or a nil pointer if this String is null. +func (s String) Ptr() *string { + if !s.Valid { + return nil + } + return &s.String +} + +// IsZero returns true for null strings, for potential future omitempty support. +func (s String) IsZero() bool { + return !s.Valid +} diff --git a/vendor/gopkg.in/guregu/null.v3/time.go b/vendor/gopkg.in/guregu/null.v3/time.go new file mode 100644 index 00000000000..a4d843920b4 --- /dev/null +++ b/vendor/gopkg.in/guregu/null.v3/time.go @@ -0,0 +1,135 @@ +package null + +import ( + "database/sql/driver" + "encoding/json" + "fmt" + "reflect" + "time" +) + +// Time is a nullable time.Time. It supports SQL and JSON serialization. +// It will marshal to null if null. +type Time struct { + Time time.Time + Valid bool +} + +// Scan implements the Scanner interface. +func (t *Time) Scan(value interface{}) error { + var err error + switch x := value.(type) { + case time.Time: + t.Time = x + case nil: + t.Valid = false + return nil + default: + err = fmt.Errorf("null: cannot scan type %T into null.Time: %v", value, value) + } + t.Valid = err == nil + return err +} + +// Value implements the driver Valuer interface. +func (t Time) Value() (driver.Value, error) { + if !t.Valid { + return nil, nil + } + return t.Time, nil +} + +// NewTime creates a new Time. +func NewTime(t time.Time, valid bool) Time { + return Time{ + Time: t, + Valid: valid, + } +} + +// TimeFrom creates a new Time that will always be valid. +func TimeFrom(t time.Time) Time { + return NewTime(t, true) +} + +// TimeFromPtr creates a new Time that will be null if t is nil. +func TimeFromPtr(t *time.Time) Time { + if t == nil { + return NewTime(time.Time{}, false) + } + return NewTime(*t, true) +} + +// MarshalJSON implements json.Marshaler. +// It will encode null if this time is null. +func (t Time) MarshalJSON() ([]byte, error) { + if !t.Valid { + return []byte("null"), nil + } + return t.Time.MarshalJSON() +} + +// UnmarshalJSON implements json.Unmarshaler. +// It supports string, object (e.g. pq.NullTime and friends) +// and null input. +func (t *Time) UnmarshalJSON(data []byte) error { + var err error + var v interface{} + if err = json.Unmarshal(data, &v); err != nil { + return err + } + switch x := v.(type) { + case string: + err = t.Time.UnmarshalJSON(data) + case map[string]interface{}: + ti, tiOK := x["Time"].(string) + valid, validOK := x["Valid"].(bool) + if !tiOK || !validOK { + return fmt.Errorf(`json: unmarshalling object into Go value of type null.Time requires key "Time" to be of type string and key "Valid" to be of type bool; found %T and %T, respectively`, x["Time"], x["Valid"]) + } + err = t.Time.UnmarshalText([]byte(ti)) + t.Valid = valid + return err + case nil: + t.Valid = false + return nil + default: + err = fmt.Errorf("json: cannot unmarshal %v into Go value of type null.Time", reflect.TypeOf(v).Name()) + } + t.Valid = err == nil + return err +} + +func (t Time) MarshalText() ([]byte, error) { + if !t.Valid { + return []byte("null"), nil + } + return t.Time.MarshalText() +} + +func (t *Time) UnmarshalText(text []byte) error { + str := string(text) + if str == "" || str == "null" { + t.Valid = false + return nil + } + if err := t.Time.UnmarshalText(text); err != nil { + return err + } + t.Valid = true + return nil +} + +// SetValid changes this Time's value and sets it to be non-null. +func (t *Time) SetValid(v time.Time) { + t.Time = v + t.Valid = true +} + +// Ptr returns a pointer to this Time's value, or a nil pointer if this Time is null. +func (t Time) Ptr() *time.Time { + if !t.Valid { + return nil + } + return &t.Time +} diff --git a/vendor/vendor.json b/vendor/vendor.json index 46aec8bd61b..05396911094 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -15,12 +15,6 @@ "revisionTime": "2016-09-17T18:44:01Z" }, { - "checksumSHA1": "WHc3uByvGaMcnSoI21fhzYgbOgg=", - "path": "golang.org/x/net/context/ctxhttp", - "revision": "71a035914f99bb58fe82eac0f1289f10963d876c", - "revisionTime": "2016-09-12T21:59:12Z" - }, - { "checksumSHA1": "6AYg4fjEvFuAVN3wHakGApjhZAM=", "path": "github.com/smartystreets/assertions", "revision": "2063fd1cc7c975db70502811a34b06ad034ccdf2", @@ -55,6 +49,18 @@ "path": "github.com/smartystreets/goconvey/convey/reporting", "revision": "5db88ed452e937f2fd557de6f4f1af7f2eabed0b", "revisionTime": "2016-08-23T18:01:44Z" + }, + { + "checksumSHA1": "WHc3uByvGaMcnSoI21fhzYgbOgg=", + "path": "golang.org/x/net/context/ctxhttp", + "revision": "71a035914f99bb58fe82eac0f1289f10963d876c", + "revisionTime": "2016-09-12T21:59:12Z" + }, + { + "checksumSHA1": "PoHLopxwkiXxa3uVhezeq/qJ/Vo=", + "path": "gopkg.in/guregu/null.v3", + "revision": "41961cea0328defc5f95c1c473f89ebf0d1813f6", + "revisionTime": "2016-02-28T00:53:16Z" } ], "rootPath": "github.com/grafana/grafana" From 7a6501640f44007823d1b54651050165525c9913 Mon Sep 17 00:00:00 2001 From: bergquist Date: Tue, 27 Sep 2016 16:06:06 +0200 Subject: [PATCH 32/32] tech(log): fixes extra param logging --- pkg/log/log.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/pkg/log/log.go b/pkg/log/log.go index fd18e9c65bf..9b4e8be31d0 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -58,7 +58,14 @@ func Debug2(message string, v ...interface{}) { } func Info(format string, v ...interface{}) { - Root.Info(fmt.Sprintf(format, v)) + var message string + if len(v) > 0 { + message = fmt.Sprintf(format, v) + } else { + message = format + } + + Root.Info(message) } func Info2(message string, v ...interface{}) { @@ -66,7 +73,14 @@ func Info2(message string, v ...interface{}) { } func Warn(format string, v ...interface{}) { - Root.Warn(fmt.Sprintf(format, v)) + var message string + if len(v) > 0 { + message = fmt.Sprintf(format, v) + } else { + message = format + } + + Root.Warn(message) } func Warn2(message string, v ...interface{}) {