From 645d36ca2cb182d6b473f6555a81ba82fbf0617b Mon Sep 17 00:00:00 2001 From: Sergey Starostin Date: Thu, 3 Sep 2020 20:41:22 +0300 Subject: [PATCH 1/4] AddTransformations and AddTransformation methods for table panel --- grafonnet/grafana.libsonnet | 1 + grafonnet/table_panel.libsonnet | 6 +++++ grafonnet/transformation.libsonnet | 12 +++++++++ tests/table_panel/test.jsonnet | 31 +++++++++++++++++++++-- tests/table_panel/test_compiled.json | 38 ++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 grafonnet/transformation.libsonnet 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..923dbbcb --- /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..220db0cc 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( @@ -42,9 +43,35 @@ local tablePanel = grafana.tablePanel; [ tablePanel.new('with targets', span=12) .addTarget({ a: 'foo' }) - .addTarget({ b: 'foo' }), + .addTarget({ b: 'foo' }) + .addTransformation(transformation.new("seriesToColumns", options={ + "byField": "instance" + })), tablePanel.new('with batch targets', span=12) - .addTargets([{ a: 'foo' }, { b: 'foo' }]), + .addTargets([{ a: 'foo' }, { b: 'foo' }]) + .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', diff --git a/tests/table_panel/test_compiled.json b/tests/table_panel/test_compiled.json index a3bdc965..61934d03 100644 --- a/tests/table_panel/test_compiled.json +++ b/tests/table_panel/test_compiled.json @@ -101,6 +101,14 @@ "timeFrom": null, "timeShift": null, "title": "with targets", + "transformations": [ + { + "id": "seriesToColumns", + "options": { + "byField": "instance" + } + } + ], "type": "table" }, { @@ -122,6 +130,36 @@ "timeFrom": null, "timeShift": null, "title": "with batch targets", + "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" } ] From 564d5717a7a68956b61d8f5e9a7bba28fd04a869 Mon Sep 17 00:00:00 2001 From: Sergey Starostin Date: Thu, 3 Sep 2020 20:54:07 +0300 Subject: [PATCH 2/4] Added test for transformation object --- tests/transformation/test.jsonnet | 11 +++++++++++ tests/transformation/test_compiled.json | 12 ++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 tests/transformation/test.jsonnet create mode 100644 tests/transformation/test_compiled.json diff --git a/tests/transformation/test.jsonnet b/tests/transformation/test.jsonnet new file mode 100644 index 00000000..755dcfba --- /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": { } + } +} From 1e06c704b2c4fe12c5c43643810f434a3fa6818e Mon Sep 17 00:00:00 2001 From: Sergey Starostin Date: Thu, 3 Sep 2020 21:16:10 +0300 Subject: [PATCH 3/4] Updated table_panel test --- tests/table_panel/test.jsonnet | 20 ++++++++--- tests/table_panel/test_compiled.json | 52 +++++++++++++++++++++++----- 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/tests/table_panel/test.jsonnet b/tests/table_panel/test.jsonnet index 220db0cc..2c3bef8c 100644 --- a/tests/table_panel/test.jsonnet +++ b/tests/table_panel/test.jsonnet @@ -43,14 +43,26 @@ local transformation = grafana.transformation; [ tablePanel.new('with targets', span=12) .addTarget({ a: 'foo' }) - .addTarget({ b: 'foo' }) + .addTarget({ b: 'foo' }), + 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 targets', span=12) - .addTargets([{ a: 'foo' }, { b: 'foo' }]) + tablePanel.new('with batch transformations', span=12) .addTransformations([ - transformation.new("filterFieldsByName", options={ + transformation.new("filterFieldsByName", options={ "include": { "names": [ "instance", diff --git a/tests/table_panel/test_compiled.json b/tests/table_panel/test_compiled.json index 61934d03..efcaa535 100644 --- a/tests/table_panel/test_compiled.json +++ b/tests/table_panel/test_compiled.json @@ -101,14 +101,6 @@ "timeFrom": null, "timeShift": null, "title": "with targets", - "transformations": [ - { - "id": "seriesToColumns", - "options": { - "byField": "instance" - } - } - ], "type": "table" }, { @@ -130,6 +122,50 @@ "timeFrom": null, "timeShift": null, "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", From 42b4aa00e3c10c3afd41e02c782924b32ece9235 Mon Sep 17 00:00:00 2001 From: Sergey Starostin Date: Thu, 3 Sep 2020 21:25:40 +0300 Subject: [PATCH 4/4] Updated formatting --- grafonnet/transformation.libsonnet | 2 +- tests/table_panel/test.jsonnet | 65 +++++++++++++++--------------- tests/transformation/test.jsonnet | 6 +-- 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/grafonnet/transformation.libsonnet b/grafonnet/transformation.libsonnet index 923dbbcb..5e62adea 100644 --- a/grafonnet/transformation.libsonnet +++ b/grafonnet/transformation.libsonnet @@ -7,6 +7,6 @@ options={} ):: { id: id, - options: options + options: options, }, } diff --git a/tests/table_panel/test.jsonnet b/tests/table_panel/test.jsonnet index 2c3bef8c..7b945343 100644 --- a/tests/table_panel/test.jsonnet +++ b/tests/table_panel/test.jsonnet @@ -50,40 +50,41 @@ local transformation = grafana.transformation; transformations: [ tablePanel.new('with transformations', span=12) - .addTransformation(transformation.new("seriesToColumns", options={ - "byField": "instance" - })) - .addTransformation(transformation.new("filterFieldsByName", options={ - "include": { - "names": [ - "instance", - ] - } - })), + .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" - })]) + 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', diff --git a/tests/transformation/test.jsonnet b/tests/transformation/test.jsonnet index 755dcfba..cc80aadb 100644 --- a/tests/transformation/test.jsonnet +++ b/tests/transformation/test.jsonnet @@ -4,8 +4,8 @@ local transformation = grafana.transformation; { basic: transformation.new(), advanced: transformation.new( - "seriesToColumns", options={ - "byField": "instance" - } + 'seriesToColumns', options={ + byField: 'instance', + } ), }