Graphite: Never escape asPercent function params as string (#56593)

* handle asPercent func by never esc params as string

* add test and clean up
This commit is contained in:
Brendan O'Handley 2022-10-20 15:49:40 -05:00 committed by GitHub
parent 5128659d19
commit c34c1d0cb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 2 deletions

View File

@ -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)');
});
});

View File

@ -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;
}