Commit 1bec6c2a by Torkel Ödegaard

feat(tablepanel): made annotations transform work

parent a66825c7
...@@ -9,7 +9,7 @@ import {TableModel} from './table_model'; ...@@ -9,7 +9,7 @@ import {TableModel} from './table_model';
export class TablePanelCtrl { export class TablePanelCtrl {
constructor($scope, $rootScope, $q, panelSrv, panelHelper) { constructor($scope, $rootScope, $q, panelSrv, panelHelper, annotationsSrv) {
$scope.ctrl = this; $scope.ctrl = this;
$scope.pageIndex = 0; $scope.pageIndex = 0;
...@@ -36,19 +36,22 @@ export class TablePanelCtrl { ...@@ -36,19 +36,22 @@ export class TablePanelCtrl {
$scope.init = function() { $scope.init = function() {
_.defaults($scope.panel, panelDefaults); _.defaults($scope.panel, panelDefaults);
if ($scope.panel.columns.length === 0) {
}
panelSrv.init($scope); panelSrv.init($scope);
}; };
$scope.refreshData = function(datasource) { $scope.refreshData = function(datasource) {
panelHelper.updateTimeRange($scope); panelHelper.updateTimeRange($scope);
if ($scope.panel.transform === 'annotations') {
return annotationsSrv.getAnnotations($scope.dashboard).then(annotations => {
$scope.dataRaw = annotations;
$scope.render();
});
}
return panelHelper.issueMetricQuery($scope, datasource) return panelHelper.issueMetricQuery($scope, datasource)
.then($scope.dataHandler, function(err) { .then($scope.dataHandler, function(err) {
$scope.seriesList = []; $scope.render();
$scope.render([]);
throw err; throw err;
}); });
}; };
......
...@@ -24,9 +24,22 @@ export class TableRenderer { ...@@ -24,9 +24,22 @@ export class TableRenderer {
return null; return null;
} }
defaultCellFormater(v) {
if (v === null || v === void 0) {
return '';
}
if (_.isArray(v)) {
v = v.join(', ');
}
return v;
}
createColumnFormater(style) { createColumnFormater(style) {
if (!style) { if (!style) {
return v => v; return this.defaultCellFormater;
} }
if (style.type === 'date') { if (style.type === 'date') {
...@@ -60,17 +73,7 @@ export class TableRenderer { ...@@ -60,17 +73,7 @@ export class TableRenderer {
}; };
} }
return v => { return this.defaultCellFormater;
if (v === null || v === void 0) {
return '-';
}
if (_.isArray(v)) {
v = v.join(', ');
}
return v;
};
} }
formatColumnValue(colIndex, value) { formatColumnValue(colIndex, value) {
...@@ -88,10 +91,7 @@ export class TableRenderer { ...@@ -88,10 +91,7 @@ export class TableRenderer {
} }
} }
this.formaters[colIndex] = function(v) { this.formaters[colIndex] = this.defaultCellFormater;
return v;
};
return this.formaters[colIndex](value); return this.formaters[colIndex](value);
} }
......
...@@ -9,7 +9,8 @@ describe('when rendering table', () => { ...@@ -9,7 +9,8 @@ describe('when rendering table', () => {
table.columns = [ table.columns = [
{text: 'Time'}, {text: 'Time'},
{text: 'Value'}, {text: 'Value'},
{text: 'Colored'} {text: 'Colored'},
{text: 'Undefined'},
]; ];
var panel = { var panel = {
...@@ -59,6 +60,11 @@ describe('when rendering table', () => { ...@@ -59,6 +60,11 @@ describe('when rendering table', () => {
var html = renderer.renderCell(2, 55); var html = renderer.renderCell(2, 55);
expect(html).to.be('<td style="color:orange">55.0</td>'); expect(html).to.be('<td style="color:orange">55.0</td>');
}); });
it('unformated undefined should be rendered as -', () => {
var html = renderer.renderCell(3, undefined);
expect(html).to.be('<td></td>');
});
}); });
}); });
......
...@@ -103,8 +103,37 @@ describe('when transforming time series table', () => { ...@@ -103,8 +103,37 @@ describe('when transforming time series table', () => {
expect(table.rows[0][0]).to.be('time'); expect(table.rows[0][0]).to.be('time');
expect(table.rows[0][1]).to.be('message'); expect(table.rows[0][1]).to.be('message');
}); });
});
describe('Annnotations', () => {
var panel = {transform: 'annotations'};
var rawData = [
{
min: 1000,
text: 'hej',
tags: ['tags', 'asd'],
title: 'title',
}
];
beforeEach(() => {
table = TableModel.transform(rawData, panel);
});
it ('should return 4 columns', () => {
expect(table.columns.length).to.be(4);
expect(table.columns[0].text).to.be('Time');
expect(table.columns[1].text).to.be('Title');
expect(table.columns[2].text).to.be('Text');
expect(table.columns[3].text).to.be('Tags');
});
it ('should return 1 rows', () => {
expect(table.rows.length).to.be(1);
expect(table.rows[0][0]).to.be(1000);
});
}); });
}); });
}); });
...@@ -66,6 +66,21 @@ transformers['timeseries_to_columns'] = { ...@@ -66,6 +66,21 @@ transformers['timeseries_to_columns'] = {
transformers['annotations'] = { transformers['annotations'] = {
description: 'Annotations', description: 'Annotations',
transform: function(data, panel, model) {
model.columns.push({text: 'Time', type: 'date'});
model.columns.push({text: 'Title'});
model.columns.push({text: 'Text'});
model.columns.push({text: 'Tags'});
if (!data || data.length === 0) {
return;
}
for (var i = 0; i < data.length; i++) {
var evt = data[i];
model.rows.push([evt.min, evt.title, evt.text, evt.tags]);
}
}
}; };
transformers['json'] = { transformers['json'] = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment