From aced9de56d1cd0952acf39b4ea35943f1a15c6dc Mon Sep 17 00:00:00 2001 From: Dominik Prokop Date: Wed, 15 Apr 2020 14:36:35 +0200 Subject: [PATCH] Move field display value proxy to grafana-data --- ...sx => getFieldDisplayValuesProxy.test.tsx} | 0 .../fieldDisplayValuesProxy.test.ts | 66 ------------------- .../panellinks/fieldDisplayValuesProxy.ts | 35 ---------- 3 files changed, 101 deletions(-) rename packages/grafana-data/src/field/{getFieldDisplayValuesProxt.test.tsx => getFieldDisplayValuesProxy.test.tsx} (100%) delete mode 100644 public/app/features/panel/panellinks/fieldDisplayValuesProxy.test.ts delete mode 100644 public/app/features/panel/panellinks/fieldDisplayValuesProxy.ts diff --git a/packages/grafana-data/src/field/getFieldDisplayValuesProxt.test.tsx b/packages/grafana-data/src/field/getFieldDisplayValuesProxy.test.tsx similarity index 100% rename from packages/grafana-data/src/field/getFieldDisplayValuesProxt.test.tsx rename to packages/grafana-data/src/field/getFieldDisplayValuesProxy.test.tsx diff --git a/public/app/features/panel/panellinks/fieldDisplayValuesProxy.test.ts b/public/app/features/panel/panellinks/fieldDisplayValuesProxy.test.ts deleted file mode 100644 index f69de4cd36f..00000000000 --- a/public/app/features/panel/panellinks/fieldDisplayValuesProxy.test.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { toDataFrame, applyFieldOverrides, GrafanaTheme } from '@grafana/data'; -import { getFieldDisplayValuesProxy } from './fieldDisplayValuesProxy'; - -describe('getFieldDisplayValuesProxy', () => { - const data = applyFieldOverrides({ - data: [ - toDataFrame({ - fields: [ - { name: 'Time', values: [1, 2, 3] }, - { - name: 'power', - values: [100, 200, 300], - config: { - title: 'The Power', - }, - }, - { - name: 'Last', - values: ['a', 'b', 'c'], - }, - ], - }), - ], - fieldConfig: { - defaults: {}, - overrides: [], - }, - replaceVariables: (val: string) => val, - timeZone: 'utc', - theme: {} as GrafanaTheme, - autoMinMax: true, - })[0]; - - it('should define all display functions', () => { - // Field display should be set - for (const field of data.fields) { - expect(field.display).toBeDefined(); - } - }); - - it('should format the time values in UTC', () => { - // Test Proxies in general - const p = getFieldDisplayValuesProxy(data, 0); - const time = p.Time; - expect(time.numeric).toEqual(1); - expect(time.text).toEqual('1970-01-01 00:00:00'); - - // Should get to the same values by name or index - const time2 = p[0]; - expect(time2.toString()).toEqual(time.toString()); - }); - - it('Lookup by name, index, or title', () => { - const p = getFieldDisplayValuesProxy(data, 2); - expect(p.power.numeric).toEqual(300); - expect(p['power'].numeric).toEqual(300); - expect(p['The Power'].numeric).toEqual(300); - expect(p[1].numeric).toEqual(300); - }); - - it('should return undefined when missing', () => { - const p = getFieldDisplayValuesProxy(data, 0); - expect(p.xyz).toBeUndefined(); - expect(p[100]).toBeUndefined(); - }); -}); diff --git a/public/app/features/panel/panellinks/fieldDisplayValuesProxy.ts b/public/app/features/panel/panellinks/fieldDisplayValuesProxy.ts deleted file mode 100644 index ec7f06dea8e..00000000000 --- a/public/app/features/panel/panellinks/fieldDisplayValuesProxy.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { DisplayValue, DataFrame, formattedValueToString, getDisplayProcessor } from '@grafana/data'; -import { config } from '@grafana/runtime'; -import toNumber from 'lodash/toNumber'; - -export function getFieldDisplayValuesProxy(frame: DataFrame, rowIndex: number): Record { - return new Proxy({} as Record, { - get: (obj: any, key: string) => { - // 1. Match the name - let field = frame.fields.find(f => key === f.name); - if (!field) { - // 2. Match the array index - const k = toNumber(key); - field = frame.fields[k]; - } - if (!field) { - // 3. Match the title - field = frame.fields.find(f => key === f.config.title); - } - if (!field) { - return undefined; - } - if (!field.display) { - // Lazy load the display processor - field.display = getDisplayProcessor({ - field, - theme: config.theme, - }); - } - const raw = field.values.get(rowIndex); - const disp = field.display(raw); - disp.toString = () => formattedValueToString(disp); - return disp; - }, - }); -}