From c34c1d0cb4665ef74c1c935d760c08c300625c2b Mon Sep 17 00:00:00 2001 From: Brendan O'Handley Date: Thu, 20 Oct 2022 15:49:40 -0500 Subject: [PATCH] Graphite: Never escape asPercent function params as string (#56593) * handle asPercent func by never esc params as string * add test and clean up --- .../plugins/datasource/graphite/gfunc.test.ts | 33 ++++++++++++++++++- .../app/plugins/datasource/graphite/gfunc.ts | 8 ++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/datasource/graphite/gfunc.test.ts b/public/app/plugins/datasource/graphite/gfunc.test.ts index 0b9d80b472b..1c543b0d020 100644 --- a/public/app/plugins/datasource/graphite/gfunc.test.ts +++ b/public/app/plugins/datasource/graphite/gfunc.test.ts @@ -1,4 +1,4 @@ -import gfunc from './gfunc'; +import gfunc, { FuncInstance } from './gfunc'; describe('gfunc', () => { const INDEX = { @@ -20,4 +20,35 @@ describe('gfunc', () => { unknown: true, }); }); + + it('renders the version < .9 asPercent function parameters by not escaping them as a string', () => { + // this function is returned from the graphite functions endpoint + const asPercentDef = { + name: 'asPercent', + description: 'Calculates a percentage.', + category: 'Combine', + params: [ + { + name: 'total', + type: 'string', + optional: true, + multiple: false, + }, + { + name: 'nodes', + type: 'node_or_tag', + optional: true, + multiple: true, + options: [], + }, + ], + defaultParams: [], + }; + + const asPercent = new FuncInstance(asPercentDef); + + const asPercentRendered = asPercent.render('#A', () => '#A'); + + expect(asPercentRendered).toEqual('asPercent(#A)'); + }); }); diff --git a/public/app/plugins/datasource/graphite/gfunc.ts b/public/app/plugins/datasource/graphite/gfunc.ts index 44609e30824..ffec5cfa703 100644 --- a/public/app/plugins/datasource/graphite/gfunc.ts +++ b/public/app/plugins/datasource/graphite/gfunc.ts @@ -1028,7 +1028,13 @@ export class FuncInstance { } // param types that should never be quoted - if (includes(['value_or_series', 'boolean', 'int', 'float', 'node', 'int_or_infinity'], paramType)) { + const neverQuotedParams = ['value_or_series', 'boolean', 'int', 'float', 'node', 'int_or_infinity']; + + // functions that should not have param types quoted + // https://github.com/grafana/grafana/issues/54924 + const neverQuotedFunctions = ['asPercent']; + // params or functions that should never be quoted + if (includes(neverQuotedParams, paramType) || includes(neverQuotedFunctions, this.def.name)) { return value; }