From 0a572cae4ba1b1ab225f86bcc8159bffdbd7d3f8 Mon Sep 17 00:00:00 2001 From: Tharun Rajendran Date: Thu, 24 Feb 2022 17:15:51 +0530 Subject: [PATCH] Explore: fix object value parsing for downloading traces as csv (#44492) * Explore: fix object value parsing for downloading traces as csv Signed-off-by: tharun * add replacer function to fix circular objects Signed-off-by: tharun --- .../src/field/displayProcessor.test.ts | 2 +- .../grafana-data/src/field/displayProcessor.ts | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/grafana-data/src/field/displayProcessor.test.ts b/packages/grafana-data/src/field/displayProcessor.test.ts index 3bae9138192..7cb49137e7a 100644 --- a/packages/grafana-data/src/field/displayProcessor.test.ts +++ b/packages/grafana-data/src/field/displayProcessor.test.ts @@ -460,7 +460,7 @@ describe('getRawDisplayProcessor', () => { ${'a string'} | ${'a string'} ${null} | ${'null'} ${undefined} | ${'undefined'} - ${{ value: 0, label: 'a label' }} | ${'[object Object]'} + ${{ value: 0, label: 'a label' }} | ${'{"value":0,"label":"a label"}'} `('when called with value:{$value}', ({ value, expected }) => { const result = processor(value); diff --git a/packages/grafana-data/src/field/displayProcessor.ts b/packages/grafana-data/src/field/displayProcessor.ts index 7e09960c4a7..a8bab9f7ac6 100644 --- a/packages/grafana-data/src/field/displayProcessor.ts +++ b/packages/grafana-data/src/field/displayProcessor.ts @@ -11,6 +11,7 @@ import { KeyValue, TimeZone } from '../types'; import { getScaleCalculator } from './scale'; import { GrafanaTheme2 } from '../themes/types'; import { anyToNumber } from '../utils/anyToNumber'; +import { getFieldTypeFromValue } from '../dataframe/processDataFrame'; interface DisplayProcessorOptions { field: Partial; @@ -168,7 +169,20 @@ function toStringProcessor(value: any): DisplayValue { export function getRawDisplayProcessor(): DisplayProcessor { return (value: any) => ({ - text: `${value}`, + text: getFieldTypeFromValue(value) === 'other' ? `${JSON.stringify(value, getCircularReplacer())}` : `${value}`, numeric: null as unknown as number, }); } + +const getCircularReplacer = () => { + const seen = new WeakSet(); + return (_key: any, value: object | null) => { + if (typeof value === 'object' && value !== null) { + if (seen.has(value)) { + return; + } + seen.add(value); + } + return value; + }; +};