From 2dd40eb4e4b865b3769dae8eef6d0fe0a3feac68 Mon Sep 17 00:00:00 2001 From: Stuart McLean Date: Mon, 14 May 2018 20:50:08 +0200 Subject: [PATCH 1/6] improve alerting api docs sample responses --- docs/sources/http_api/alerting.md | 53 +++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/docs/sources/http_api/alerting.md b/docs/sources/http_api/alerting.md index 3860ae490b1..305bfccebe8 100644 --- a/docs/sources/http_api/alerting.md +++ b/docs/sources/http_api/alerting.md @@ -50,19 +50,16 @@ Content-Type: application/json "id": 1, "dashboardId": 1, "panelId": 1, + "panelUId": "ABcdEFghij" + "dashboardSlug": "sensors", "name": "fire place sensor", "message": "Someone is trying to break in through the fire place", "state": "alerting", + "newStateDate": "2018-05-14T05:55:20+02:00", "evalDate": "0001-01-01T00:00:00Z", - "evalData": [ - { - "metric": "fire", - "tags": null, - "value": 5.349999999999999 - } - "newStateDate": "2016-12-25", + "evalData": null, "executionError": "", - "url": "http://grafana.com/dashboard/db/sensors" + "dashboardUri": "http://grafana.com/dashboard/db/sensors" } ] ``` @@ -86,15 +83,37 @@ Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk HTTP/1.1 200 Content-Type: application/json { - "id": 1, - "dashboardId": 1, - "panelId": 1, - "name": "fire place sensor", - "message": "Someone is trying to break in through the fire place", - "state": "alerting", - "newStateDate": "2016-12-25", - "executionError": "", - "url": "http://grafana.com/dashboard/db/sensors" + "Id": 1, + "Version": 0, + "OrgId": 1, + "DashboardId": 55, + "PanelId": 2, + "Name": "my mem alert", + "Message": "", + "Severity": "", + "State": "alerting", + "Handler": 1, + "Silenced": false, + "ExecutionError": " ", + "Frequency": 60, + "EvalData": { + "evalMatches": [ + { + "metric": "mem_usage", + "tags": { + "name": "server.grafana.com" + }, + "value": 98.765 + } + ] + }, + "NewStateDate": "2018-05-14T17:12:45+02:00", + "StateChanges": 3, + "Created": "2018-05-14T17:01:25+02:00", + "Updated": "2018-05-14T17:11:18+02:00", + "Settings": { + ... + } } ``` From 295169b94f79af4d8316b9cc3a193fce0706a772 Mon Sep 17 00:00:00 2001 From: Stuart McLean Date: Mon, 14 May 2018 20:50:44 +0200 Subject: [PATCH 2/6] add useful note to alerting api docs --- docs/sources/http_api/alerting.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/sources/http_api/alerting.md b/docs/sources/http_api/alerting.md index 305bfccebe8..3b9eceddd5e 100644 --- a/docs/sources/http_api/alerting.md +++ b/docs/sources/http_api/alerting.md @@ -117,6 +117,13 @@ Content-Type: application/json } ``` +**Important Note**: +"evalMatches" data is cached in the db when and only when the state of the alert changes +(e.g. transitioning from "ok" to "alerting" state). + +If data from one server triggers the alert first and, before that server is seen leaving alerting state, +a second server also enters a state that would trigger the alert, the second server will not be visible in "evalMatches" data. + ## Pause alert `POST /api/alerts/:id/pause` From c09c00a1f5eacf6d06e57d1c4d4fe001861e182e Mon Sep 17 00:00:00 2001 From: Stuart McLean Date: Tue, 15 May 2018 10:06:26 +0200 Subject: [PATCH 3/6] fixes following first code review --- docs/sources/http_api/alerting.md | 60 +++++++++++++------------------ 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/docs/sources/http_api/alerting.md b/docs/sources/http_api/alerting.md index 3b9eceddd5e..4d52105cf3c 100644 --- a/docs/sources/http_api/alerting.md +++ b/docs/sources/http_api/alerting.md @@ -49,17 +49,17 @@ Content-Type: application/json { "id": 1, "dashboardId": 1, - "panelId": 1, - "panelUId": "ABcdEFghij" + "dashboardUId": "ABcdEFghij" "dashboardSlug": "sensors", + "panelId": 1, "name": "fire place sensor", - "message": "Someone is trying to break in through the fire place", "state": "alerting", + "message": "Someone is trying to break in through the fire place", "newStateDate": "2018-05-14T05:55:20+02:00", "evalDate": "0001-01-01T00:00:00Z", "evalData": null, "executionError": "", - "dashboardUri": "http://grafana.com/dashboard/db/sensors" + "url": "http://grafana.com/dashboard/db/sensors" } ] ``` @@ -83,37 +83,27 @@ Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk HTTP/1.1 200 Content-Type: application/json { - "Id": 1, - "Version": 0, - "OrgId": 1, - "DashboardId": 55, - "PanelId": 2, - "Name": "my mem alert", - "Message": "", - "Severity": "", - "State": "alerting", - "Handler": 1, - "Silenced": false, - "ExecutionError": " ", - "Frequency": 60, - "EvalData": { - "evalMatches": [ - { - "metric": "mem_usage", - "tags": { - "name": "server.grafana.com" - }, - "value": 98.765 - } - ] - }, - "NewStateDate": "2018-05-14T17:12:45+02:00", - "StateChanges": 3, - "Created": "2018-05-14T17:01:25+02:00", - "Updated": "2018-05-14T17:11:18+02:00", - "Settings": { - ... - } + "id": 1, + "dashboardId": 1, + "dashboardUId": "ABcdEFghij" + "dashboardSlug": "sensors", + "panelId": 1, + "name": "fire place sensor", + "state": "alerting", + "message": "Someone is trying to break in through the fire place", + "newStateDate": "2018-05-14T05:55:20+02:00", + "evalDate": "0001-01-01T00:00:00Z", + "evalData": "evalMatches": [ + { + "metric": "movement", + "tags": { + "name": "fireplace_chimney" + }, + "value": 98.765 + } + ], + "executionError": "", + "url": "http://grafana.com/dashboard/db/sensors" } ``` From f15919555b039322dfb5043f13df9ee11c13e974 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Thu, 17 May 2018 14:56:15 +0200 Subject: [PATCH 4/6] explore: fixes #11953 --- .../app/features/panel/metrics_panel_ctrl.ts | 2 +- .../panel/specs/metrics_panel_ctrl.jest.ts | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 public/app/features/panel/specs/metrics_panel_ctrl.jest.ts diff --git a/public/app/features/panel/metrics_panel_ctrl.ts b/public/app/features/panel/metrics_panel_ctrl.ts index acf46a193e8..d460b27a679 100644 --- a/public/app/features/panel/metrics_panel_ctrl.ts +++ b/public/app/features/panel/metrics_panel_ctrl.ts @@ -312,7 +312,7 @@ class MetricsPanelCtrl extends PanelCtrl { getAdditionalMenuItems() { const items = []; - if (this.datasource.supportsExplore) { + if (this.datasource && this.datasource.supportsExplore) { items.push({ text: 'Explore', click: 'ctrl.explore();', diff --git a/public/app/features/panel/specs/metrics_panel_ctrl.jest.ts b/public/app/features/panel/specs/metrics_panel_ctrl.jest.ts new file mode 100644 index 00000000000..f2e5199b57d --- /dev/null +++ b/public/app/features/panel/specs/metrics_panel_ctrl.jest.ts @@ -0,0 +1,65 @@ +jest.mock('app/core/core', () => ({})); + +import { MetricsPanelCtrl } from '../metrics_panel_ctrl'; +import q from 'q'; +import { PanelModel } from 'app/features/dashboard/panel_model'; + +describe('MetricsPanelCtrl', () => { + let ctrl; + + beforeEach(() => { + ctrl = setupController(); + }); + + describe('when getting additional menu items', () => { + let additionalItems; + + describe('and has no datasource set', () => { + beforeEach(() => { + additionalItems = ctrl.getAdditionalMenuItems(); + }); + + it('should not return any items', () => { + expect(additionalItems.length).toBe(0); + }); + }); + + describe('and has datasource set that supports explore', () => { + beforeEach(() => { + ctrl.datasource = { supportsExplore: true }; + additionalItems = ctrl.getAdditionalMenuItems(); + }); + + it('should not return any items', () => { + expect(additionalItems.length).toBe(1); + }); + }); + }); +}); + +function setupController() { + const injectorStub = { + get: type => { + switch (type) { + case '$q': { + return q; + } + default: { + return jest.fn(); + } + } + }, + }; + + const scope = { + panel: { events: [] }, + appEvent: jest.fn(), + onAppEvent: jest.fn(), + $on: jest.fn(), + colors: [], + }; + + MetricsPanelCtrl.prototype.panel = new PanelModel({ type: 'test' }); + + return new MetricsPanelCtrl(scope, injectorStub); +} From 80f8fb1e02829d5a09a1510a2b9ce06553f47ca6 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Mon, 21 May 2018 15:46:23 +0200 Subject: [PATCH 5/6] Revert "Fix ResponseParser for InfluxDB to return only string values" This reverts commit 92e5968f91472abdba17405092e092a532de5eb4. Ref #12009 --- public/app/plugins/datasource/influxdb/response_parser.ts | 2 +- .../datasource/influxdb/specs/response_parser.jest.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/public/app/plugins/datasource/influxdb/response_parser.ts b/public/app/plugins/datasource/influxdb/response_parser.ts index 746a8c0e05a..33b883f1d64 100644 --- a/public/app/plugins/datasource/influxdb/response_parser.ts +++ b/public/app/plugins/datasource/influxdb/response_parser.ts @@ -38,7 +38,7 @@ export default class ResponseParser { }); return _.map(res, value => { - return { text: value.toString() }; + return { text: value }; }); } } diff --git a/public/app/plugins/datasource/influxdb/specs/response_parser.jest.ts b/public/app/plugins/datasource/influxdb/specs/response_parser.jest.ts index c0652f5fca3..ba1546c37e9 100644 --- a/public/app/plugins/datasource/influxdb/specs/response_parser.jest.ts +++ b/public/app/plugins/datasource/influxdb/specs/response_parser.jest.ts @@ -105,9 +105,9 @@ describe('influxdb response parser', () => { it('should return second column', () => { expect(_.size(result)).toBe(3); - expect(result[0].text).toBe('0'); - expect(result[1].text).toBe('15'); - expect(result[2].text).toBe('20.2'); + expect(result[0].text).toBe(0.0); + expect(result[1].text).toBe(15.0); + expect(result[2].text).toBe(20.2); }); }); From de7a880d66e284b8def5bb27e87d8786e789a2ef Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Mon, 21 May 2018 15:47:29 +0200 Subject: [PATCH 6/6] Revert "Conditionally select a field to return in ResponseParser for InfluxDB" This reverts commit a101177b5521bfdd8ce67f6a60216fb7fb8d3c8f. Ref #12009 --- .../datasource/influxdb/response_parser.ts | 17 +++--------- .../influxdb/specs/response_parser.jest.ts | 26 ------------------- 2 files changed, 4 insertions(+), 39 deletions(-) diff --git a/public/app/plugins/datasource/influxdb/response_parser.ts b/public/app/plugins/datasource/influxdb/response_parser.ts index 33b883f1d64..78ce67e7a37 100644 --- a/public/app/plugins/datasource/influxdb/response_parser.ts +++ b/public/app/plugins/datasource/influxdb/response_parser.ts @@ -11,23 +11,14 @@ export default class ResponseParser { return []; } + var influxdb11format = query.toLowerCase().indexOf('show tag values') >= 0; + var res = {}; _.each(influxResults.series, serie => { _.each(serie.values, value => { if (_.isArray(value)) { - // In general, there are 2 possible shapes for the returned value. - // The first one is a two-element array, - // where the first element is somewhat a metadata value: - // the tag name for SHOW TAG VALUES queries, - // the time field for SELECT queries, etc. - // The second shape is an one-element array, - // that is containing an immediate value. - // For example, SHOW FIELD KEYS queries return such shape. - // Note, pre-0.11 versions return - // the second shape for SHOW TAG VALUES queries - // (while the newer versions—first). - if (value[1] !== undefined) { - addUnique(res, value[1]); + if (influxdb11format) { + addUnique(res, value[1] || value[0]); } else { addUnique(res, value[0]); } diff --git a/public/app/plugins/datasource/influxdb/specs/response_parser.jest.ts b/public/app/plugins/datasource/influxdb/specs/response_parser.jest.ts index ba1546c37e9..8ddc0fcdaf1 100644 --- a/public/app/plugins/datasource/influxdb/specs/response_parser.jest.ts +++ b/public/app/plugins/datasource/influxdb/specs/response_parser.jest.ts @@ -85,32 +85,6 @@ describe('influxdb response parser', () => { }); }); - describe('SELECT response', () => { - var query = 'SELECT "usage_iowait" FROM "cpu" LIMIT 10'; - var response = { - results: [ - { - series: [ - { - name: 'cpu', - columns: ['time', 'usage_iowait'], - values: [[1488465190006040638, 0.0], [1488465190006040638, 15.0], [1488465190006040638, 20.2]], - }, - ], - }, - ], - }; - - var result = parser.parse(query, response); - - it('should return second column', () => { - expect(_.size(result)).toBe(3); - expect(result[0].text).toBe(0.0); - expect(result[1].text).toBe(15.0); - expect(result[2].text).toBe(20.2); - }); - }); - describe('SHOW FIELD response', () => { var query = 'SHOW FIELD KEYS FROM "cpu"'; describe('response from 0.10.0', () => {