diff --git a/packages/grafana-ui/src/components/Table/Table.tsx b/packages/grafana-ui/src/components/Table/Table.tsx index f4010ae436a..d2a65bdf9b0 100644 --- a/packages/grafana-ui/src/components/Table/Table.tsx +++ b/packages/grafana-ui/src/components/Table/Table.tsx @@ -12,7 +12,7 @@ import { } from 'react-virtualized'; import { Themeable } from '../../types/theme'; -import { sortSeriesData } from '../../utils/processTableData'; +import { sortSeriesData } from '../../utils/processSeriesData'; import { SeriesData, InterpolateFunction } from '@grafana/ui'; import { diff --git a/packages/grafana-ui/src/components/Table/TableInputCSV.tsx b/packages/grafana-ui/src/components/Table/TableInputCSV.tsx index a6da6eb0209..ff49673042f 100644 --- a/packages/grafana-ui/src/components/Table/TableInputCSV.tsx +++ b/packages/grafana-ui/src/components/Table/TableInputCSV.tsx @@ -1,6 +1,6 @@ import React from 'react'; import debounce from 'lodash/debounce'; -import { parseCSV, TableParseOptions, TableParseDetails } from '../../utils/processTableData'; +import { parseCSV, TableParseOptions, TableParseDetails } from '../../utils/processSeriesData'; import { SeriesData } from '../../types/data'; import { AutoSizer } from 'react-virtualized'; diff --git a/packages/grafana-ui/src/utils/__snapshots__/processTableData.test.ts.snap b/packages/grafana-ui/src/utils/__snapshots__/processSeriesData.test.ts.snap similarity index 100% rename from packages/grafana-ui/src/utils/__snapshots__/processTableData.test.ts.snap rename to packages/grafana-ui/src/utils/__snapshots__/processSeriesData.test.ts.snap diff --git a/packages/grafana-ui/src/utils/index.ts b/packages/grafana-ui/src/utils/index.ts index 45b8d049a63..ca7c4a7dd8d 100644 --- a/packages/grafana-ui/src/utils/index.ts +++ b/packages/grafana-ui/src/utils/index.ts @@ -1,4 +1,4 @@ -export * from './processTableData'; +export * from './processSeriesData'; export * from './valueFormats/valueFormats'; export * from './colors'; export * from './namedColorsPalette'; diff --git a/packages/grafana-ui/src/utils/processTableData.test.ts b/packages/grafana-ui/src/utils/processSeriesData.test.ts similarity index 78% rename from packages/grafana-ui/src/utils/processTableData.test.ts rename to packages/grafana-ui/src/utils/processSeriesData.test.ts index 4add1b2ff00..1f244d1ed0b 100644 --- a/packages/grafana-ui/src/utils/processTableData.test.ts +++ b/packages/grafana-ui/src/utils/processSeriesData.test.ts @@ -1,4 +1,4 @@ -import { parseCSV, toSeriesData, guessFieldTypes, guessFieldTypeFromValue } from './processTableData'; +import { parseCSV, toSeriesData, guessFieldTypes, guessFieldTypeFromValue } from './processSeriesData'; import { FieldType } from '../types/data'; import moment from 'moment'; @@ -11,25 +11,25 @@ describe('processSeriesData', () => { it('should generate a header and fix widths', () => { const text = '1\n2,3,4\n5,6'; - const table = parseCSV(text, { + const series = parseCSV(text, { headerIsFirstLine: false, }); - expect(table.rows.length).toBe(3); + expect(series.rows.length).toBe(3); - expect(table).toMatchSnapshot(); + expect(series).toMatchSnapshot(); }); }); }); describe('toSeriesData', () => { - it('converts timeseries to table ', () => { + it('converts timeseries to series', () => { const input1 = { target: 'Field Name', datapoints: [[100, 1], [200, 2]], }; - let table = toSeriesData(input1); - expect(table.fields[0].name).toBe(input1.target); - expect(table.rows).toBe(input1.datapoints); + let series = toSeriesData(input1); + expect(series.fields[0].name).toBe(input1.target); + expect(series.rows).toBe(input1.datapoints); // Should fill a default name if target is empty const input2 = { @@ -37,17 +37,17 @@ describe('toSeriesData', () => { target: '', datapoints: [[100, 1], [200, 2]], }; - table = toSeriesData(input2); - expect(table.fields[0].name).toEqual('Value'); + series = toSeriesData(input2); + expect(series.fields[0].name).toEqual('Value'); }); - it('keeps tableData unchanged', () => { + it('keeps seriesData unchanged', () => { const input = { fields: [{ text: 'A' }, { text: 'B' }, { text: 'C' }], rows: [[100, 'A', 1], [200, 'B', 2], [300, 'C', 3]], }; - const table = toSeriesData(input); - expect(table).toBe(input); + const series = toSeriesData(input); + expect(series).toBe(input); }); it('Guess Colum Types from value', () => { @@ -70,12 +70,12 @@ describe('toSeriesData', () => { expect(guessFieldTypeFromValue('xxxx')).toBe(FieldType.string); }); - it('Guess Colum Types from table', () => { - const table = { + it('Guess Colum Types from series', () => { + const series = { fields: [{ name: 'A (number)' }, { name: 'B (strings)' }, { name: 'C (nulls)' }, { name: 'Time' }], rows: [[123, null, null, '2000'], [null, 'Hello', null, 'XXX']], }; - const norm = guessFieldTypes(table); + const norm = guessFieldTypes(series); expect(norm.fields[0].type).toBe(FieldType.number); expect(norm.fields[1].type).toBe(FieldType.string); expect(norm.fields[2].type).toBeUndefined(); diff --git a/packages/grafana-ui/src/utils/processTableData.ts b/packages/grafana-ui/src/utils/processSeriesData.ts similarity index 84% rename from packages/grafana-ui/src/utils/processTableData.ts rename to packages/grafana-ui/src/utils/processSeriesData.ts index 76044201921..c2b844adef4 100644 --- a/packages/grafana-ui/src/utils/processTableData.ts +++ b/packages/grafana-ui/src/utils/processSeriesData.ts @@ -7,7 +7,7 @@ import moment from 'moment'; import Papa, { ParseError, ParseMeta } from 'papaparse'; // Types -import { SeriesData, Field, TimeSeries, FieldType, TableData } from '../types'; +import { SeriesData, Field, TimeSeries, FieldType, TableData } from '../types/index'; // Subset of all parse options export interface TableParseOptions { @@ -27,13 +27,13 @@ export interface TableParseDetails { /** * This makes sure the header and all rows have equal length. * - * @param table (immutable) - * @returns a new table that has equal length rows, or the same - * table if no changes were needed + * @param series (immutable) + * @returns a series that has equal length rows, or the same + * series if no changes were needed */ -export function matchRowSizes(table: SeriesData): SeriesData { - const { rows } = table; - let { fields } = table; +export function matchRowSizes(series: SeriesData): SeriesData { + const { rows } = series; + let { fields } = series; let sameSize = true; let size = fields.length; @@ -44,7 +44,7 @@ export function matchRowSizes(table: SeriesData): SeriesData { } }); if (sameSize) { - return table; + return series; } // Pad Fields @@ -164,8 +164,8 @@ function convertTimeSeriesToSeriesData(timeSeries: TimeSeries): SeriesData { }; } -export const getFirstTimeField = (table: SeriesData): number => { - const { fields } = table; +export const getFirstTimeField = (series: SeriesData): number => { + const { fields } = series; for (let i = 0; i < fields.length; i++) { if (fields[i].type === FieldType.time) { return i; @@ -214,8 +214,8 @@ export function guessFieldTypeFromValue(v: any): FieldType { /** * Looks at the data to guess the column type. This ignores any existing setting */ -function guessFieldTypeFromTable(table: SeriesData, index: number): FieldType | undefined { - const column = table.fields[index]; +function guessFieldTypeFromTable(series: SeriesData, index: number): FieldType | undefined { + const column = series.fields[index]; // 1. Use the column name to guess if (column.name) { @@ -226,8 +226,8 @@ function guessFieldTypeFromTable(table: SeriesData, index: number): FieldType | } // 2. Check the first non-null value - for (let i = 0; i < table.rows.length; i++) { - const v = table.rows[i][index]; + for (let i = 0; i < series.rows.length; i++) { + const v = series.rows[i][index]; if (v !== null) { return guessFieldTypeFromValue(v); } @@ -238,30 +238,30 @@ function guessFieldTypeFromTable(table: SeriesData, index: number): FieldType | } /** - * @returns a table Returns a copy of the table with the best guess for each column type - * If the table already has column types defined, they will be used + * @returns a copy of the series with the best guess for each field type + * If the series already has field types defined, they will be used */ -export const guessFieldTypes = (table: SeriesData): SeriesData => { - for (let i = 0; i < table.fields.length; i++) { - if (!table.fields[i].type) { +export const guessFieldTypes = (series: SeriesData): SeriesData => { + for (let i = 0; i < series.fields.length; i++) { + if (!series.fields[i].type) { // Somethign is missing a type return a modified copy return { - ...table, - fields: table.fields.map((column, index) => { - if (column.type) { - return column; + ...series, + fields: series.fields.map((field, index) => { + if (field.type) { + return field; } // Replace it with a calculated version return { - ...column, - type: guessFieldTypeFromTable(table, index), + ...field, + type: guessFieldTypeFromTable(series, index), }; }), }; } } // No changes necessary - return table; + return series; }; export const isTableData = (data: any): data is SeriesData => data && data.hasOwnProperty('columns'); @@ -278,7 +278,7 @@ export const toSeriesData = (data: any): SeriesData => { if (data.hasOwnProperty('columns')) { return convertTableToSeriesData(data); } - // TODO, try to convert JSON/Array to table? + // TODO, try to convert JSON/Array to seriesta? console.warn('Can not convert', data); throw new Error('Unsupported data format'); }; diff --git a/packages/grafana-ui/src/utils/statsCalculator.test.ts b/packages/grafana-ui/src/utils/statsCalculator.test.ts index 03697c37f5b..2cd3fd0abdc 100644 --- a/packages/grafana-ui/src/utils/statsCalculator.test.ts +++ b/packages/grafana-ui/src/utils/statsCalculator.test.ts @@ -1,4 +1,4 @@ -import { parseCSV } from './processTableData'; +import { parseCSV } from './processSeriesData'; import { getStatsCalculators, StatID, calculateStats } from './statsCalculator'; import _ from 'lodash';