grafana/public/app/features/plugins/sql/ResponseParser.ts
Kyle Cunningham bf687fff45
SQL Datasources: Prevent Call Stack Overflows with Large Numbers of Values for Variable (#64937)
* Push values with every map call to avoid hitting the maximum call stack size.

* Add test and refactor to for of

* Use native fill instead of lodash

---------

Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com>
2023-03-22 14:50:54 +01:00

27 lines
909 B
TypeScript

import { uniqBy } from 'lodash';
import { DataFrame, MetricFindValue } from '@grafana/data';
import { ResponseParser as ResponseParserType } from 'app/features/plugins/sql/types';
export class ResponseParser implements ResponseParserType {
transformMetricFindResponse(frame: DataFrame): MetricFindValue[] {
const values: MetricFindValue[] = [];
const textField = frame.fields.find((f) => f.name === '__text');
const valueField = frame.fields.find((f) => f.name === '__value');
if (textField && valueField) {
for (let i = 0; i < textField.values.length; i++) {
values.push({ text: '' + textField.values.get(i), value: '' + valueField.values.get(i) });
}
} else {
for (const field of frame.fields) {
for (const value of field.values.toArray()) {
values.push({ text: value });
}
}
}
return uniqBy(values, 'text');
}
}