diff --git a/grafonnet/grafana.libsonnet b/grafonnet/grafana.libsonnet index 88fe2778..67120fbd 100644 --- a/grafonnet/grafana.libsonnet +++ b/grafonnet/grafana.libsonnet @@ -27,4 +27,5 @@ gaugePanel:: import 'gauge_panel.libsonnet', barGaugePanel:: import 'bar_gauge_panel.libsonnet', statPanel:: import 'stat_panel.libsonnet', + transformation:: import 'transformation.libsonnet', } diff --git a/grafonnet/table_panel.libsonnet b/grafonnet/table_panel.libsonnet index 947ef676..4c686b3d 100644 --- a/grafonnet/table_panel.libsonnet +++ b/grafonnet/table_panel.libsonnet @@ -24,6 +24,8 @@ * @method addColumn(field, style) Adds a column * @method hideColumn(field) Hides a column * @method addLink(link) Adds a link + * @method addTransformation(transformation) Adds a transformation object + * @method addTransformations(transformations) Adds an array of transformations */ new( title, @@ -81,5 +83,9 @@ addLink(link):: self { links+: [link], }, + addTransformation(transformation):: self { + transformations+: [transformation], + }, + addTransformations(transformations):: std.foldl(function(p, t) p.addTransformation(t), transformations, self), }, } diff --git a/grafonnet/transformation.libsonnet b/grafonnet/transformation.libsonnet new file mode 100644 index 00000000..5e62adea --- /dev/null +++ b/grafonnet/transformation.libsonnet @@ -0,0 +1,12 @@ +{ + /** + * @name transformation.new + */ + new( + id='', + options={} + ):: { + id: id, + options: options, + }, +} diff --git a/tests/table_panel/test.jsonnet b/tests/table_panel/test.jsonnet index d3c0c0ea..7b945343 100644 --- a/tests/table_panel/test.jsonnet +++ b/tests/table_panel/test.jsonnet @@ -1,5 +1,6 @@ local grafana = import 'grafonnet/grafana.libsonnet'; local tablePanel = grafana.tablePanel; +local transformation = grafana.transformation; { basic: tablePanel.new( @@ -46,6 +47,45 @@ local tablePanel = grafana.tablePanel; tablePanel.new('with batch targets', span=12) .addTargets([{ a: 'foo' }, { b: 'foo' }]), ], + transformations: + [ + tablePanel.new('with transformations', span=12) + .addTransformation(transformation.new('seriesToColumns', options={ + byField: 'instance', + })) + .addTransformation(transformation.new('filterFieldsByName', options={ + include: { + names: [ + 'instance', + ], + }, + })), + tablePanel.new('with batch transformations', span=12) + .addTransformations([ + transformation.new('filterFieldsByName', options={ + include: { + names: [ + 'instance', + 'nodename', + 'Value #D', + 'Value #B', + 'Value #C', + 'Value #E', + 'Value #F', + 'Value #G', + 'Value #H', + 'Value #I', + 'Value #J', + 'Value #K', + 'Value #L', + ], + }, + }), + transformation.new('seriesToColumns', options={ + byField: 'instance', + }), + ]), + ], hideColumns: tablePanel.new( 'test', span=12, diff --git a/tests/table_panel/test_compiled.json b/tests/table_panel/test_compiled.json index a3bdc965..efcaa535 100644 --- a/tests/table_panel/test_compiled.json +++ b/tests/table_panel/test_compiled.json @@ -124,5 +124,79 @@ "title": "with batch targets", "type": "table" } + ], + "transformations": [ + { + "columns": [ ], + "datasource": null, + "links": [ ], + "span": 12, + "styles": [ ], + "targets": [ ], + "timeFrom": null, + "timeShift": null, + "title": "with transformations", + "transformations": [ + { + "id": "seriesToColumns", + "options": { + "byField": "instance" + } + }, + { + "id": "filterFieldsByName", + "options": { + "include": { + "names": [ + "instance" + ] + } + } + } + ], + "type": "table" + }, + { + "columns": [ ], + "datasource": null, + "links": [ ], + "span": 12, + "styles": [ ], + "targets": [ ], + "timeFrom": null, + "timeShift": null, + "title": "with batch transformations", + "transformations": [ + { + "id": "filterFieldsByName", + "options": { + "include": { + "names": [ + "instance", + "nodename", + "Value #D", + "Value #B", + "Value #C", + "Value #E", + "Value #F", + "Value #G", + "Value #H", + "Value #I", + "Value #J", + "Value #K", + "Value #L" + ] + } + } + }, + { + "id": "seriesToColumns", + "options": { + "byField": "instance" + } + } + ], + "type": "table" + } ] } diff --git a/tests/transformation/test.jsonnet b/tests/transformation/test.jsonnet new file mode 100644 index 00000000..cc80aadb --- /dev/null +++ b/tests/transformation/test.jsonnet @@ -0,0 +1,11 @@ +local grafana = import 'grafonnet/grafana.libsonnet'; +local transformation = grafana.transformation; + +{ + basic: transformation.new(), + advanced: transformation.new( + 'seriesToColumns', options={ + byField: 'instance', + } + ), +} diff --git a/tests/transformation/test_compiled.json b/tests/transformation/test_compiled.json new file mode 100644 index 00000000..99f6c9d4 --- /dev/null +++ b/tests/transformation/test_compiled.json @@ -0,0 +1,12 @@ +{ + "advanced": { + "id": "seriesToColumns", + "options": { + "byField": "instance" + } + }, + "basic": { + "id": "", + "options": { } + } +}