mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
chore: more TableData to SeriesData renaming (#16206)
This commit is contained in:
parent
dbc2f2761a
commit
941b770c46
@ -12,7 +12,7 @@ import {
|
|||||||
} from 'react-virtualized';
|
} from 'react-virtualized';
|
||||||
import { Themeable } from '../../types/theme';
|
import { Themeable } from '../../types/theme';
|
||||||
|
|
||||||
import { sortSeriesData } from '../../utils/processTableData';
|
import { sortSeriesData } from '../../utils/processSeriesData';
|
||||||
|
|
||||||
import { SeriesData, InterpolateFunction } from '@grafana/ui';
|
import { SeriesData, InterpolateFunction } from '@grafana/ui';
|
||||||
import {
|
import {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import debounce from 'lodash/debounce';
|
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 { SeriesData } from '../../types/data';
|
||||||
import { AutoSizer } from 'react-virtualized';
|
import { AutoSizer } from 'react-virtualized';
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
export * from './processTableData';
|
export * from './processSeriesData';
|
||||||
export * from './valueFormats/valueFormats';
|
export * from './valueFormats/valueFormats';
|
||||||
export * from './colors';
|
export * from './colors';
|
||||||
export * from './namedColorsPalette';
|
export * from './namedColorsPalette';
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { parseCSV, toSeriesData, guessFieldTypes, guessFieldTypeFromValue } from './processTableData';
|
import { parseCSV, toSeriesData, guessFieldTypes, guessFieldTypeFromValue } from './processSeriesData';
|
||||||
import { FieldType } from '../types/data';
|
import { FieldType } from '../types/data';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
|
|
||||||
@ -11,25 +11,25 @@ describe('processSeriesData', () => {
|
|||||||
|
|
||||||
it('should generate a header and fix widths', () => {
|
it('should generate a header and fix widths', () => {
|
||||||
const text = '1\n2,3,4\n5,6';
|
const text = '1\n2,3,4\n5,6';
|
||||||
const table = parseCSV(text, {
|
const series = parseCSV(text, {
|
||||||
headerIsFirstLine: false,
|
headerIsFirstLine: false,
|
||||||
});
|
});
|
||||||
expect(table.rows.length).toBe(3);
|
expect(series.rows.length).toBe(3);
|
||||||
|
|
||||||
expect(table).toMatchSnapshot();
|
expect(series).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('toSeriesData', () => {
|
describe('toSeriesData', () => {
|
||||||
it('converts timeseries to table ', () => {
|
it('converts timeseries to series', () => {
|
||||||
const input1 = {
|
const input1 = {
|
||||||
target: 'Field Name',
|
target: 'Field Name',
|
||||||
datapoints: [[100, 1], [200, 2]],
|
datapoints: [[100, 1], [200, 2]],
|
||||||
};
|
};
|
||||||
let table = toSeriesData(input1);
|
let series = toSeriesData(input1);
|
||||||
expect(table.fields[0].name).toBe(input1.target);
|
expect(series.fields[0].name).toBe(input1.target);
|
||||||
expect(table.rows).toBe(input1.datapoints);
|
expect(series.rows).toBe(input1.datapoints);
|
||||||
|
|
||||||
// Should fill a default name if target is empty
|
// Should fill a default name if target is empty
|
||||||
const input2 = {
|
const input2 = {
|
||||||
@ -37,17 +37,17 @@ describe('toSeriesData', () => {
|
|||||||
target: '',
|
target: '',
|
||||||
datapoints: [[100, 1], [200, 2]],
|
datapoints: [[100, 1], [200, 2]],
|
||||||
};
|
};
|
||||||
table = toSeriesData(input2);
|
series = toSeriesData(input2);
|
||||||
expect(table.fields[0].name).toEqual('Value');
|
expect(series.fields[0].name).toEqual('Value');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('keeps tableData unchanged', () => {
|
it('keeps seriesData unchanged', () => {
|
||||||
const input = {
|
const input = {
|
||||||
fields: [{ text: 'A' }, { text: 'B' }, { text: 'C' }],
|
fields: [{ text: 'A' }, { text: 'B' }, { text: 'C' }],
|
||||||
rows: [[100, 'A', 1], [200, 'B', 2], [300, 'C', 3]],
|
rows: [[100, 'A', 1], [200, 'B', 2], [300, 'C', 3]],
|
||||||
};
|
};
|
||||||
const table = toSeriesData(input);
|
const series = toSeriesData(input);
|
||||||
expect(table).toBe(input);
|
expect(series).toBe(input);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Guess Colum Types from value', () => {
|
it('Guess Colum Types from value', () => {
|
||||||
@ -70,12 +70,12 @@ describe('toSeriesData', () => {
|
|||||||
expect(guessFieldTypeFromValue('xxxx')).toBe(FieldType.string);
|
expect(guessFieldTypeFromValue('xxxx')).toBe(FieldType.string);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Guess Colum Types from table', () => {
|
it('Guess Colum Types from series', () => {
|
||||||
const table = {
|
const series = {
|
||||||
fields: [{ name: 'A (number)' }, { name: 'B (strings)' }, { name: 'C (nulls)' }, { name: 'Time' }],
|
fields: [{ name: 'A (number)' }, { name: 'B (strings)' }, { name: 'C (nulls)' }, { name: 'Time' }],
|
||||||
rows: [[123, null, null, '2000'], [null, 'Hello', null, 'XXX']],
|
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[0].type).toBe(FieldType.number);
|
||||||
expect(norm.fields[1].type).toBe(FieldType.string);
|
expect(norm.fields[1].type).toBe(FieldType.string);
|
||||||
expect(norm.fields[2].type).toBeUndefined();
|
expect(norm.fields[2].type).toBeUndefined();
|
@ -7,7 +7,7 @@ import moment from 'moment';
|
|||||||
import Papa, { ParseError, ParseMeta } from 'papaparse';
|
import Papa, { ParseError, ParseMeta } from 'papaparse';
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
import { SeriesData, Field, TimeSeries, FieldType, TableData } from '../types';
|
import { SeriesData, Field, TimeSeries, FieldType, TableData } from '../types/index';
|
||||||
|
|
||||||
// Subset of all parse options
|
// Subset of all parse options
|
||||||
export interface TableParseOptions {
|
export interface TableParseOptions {
|
||||||
@ -27,13 +27,13 @@ export interface TableParseDetails {
|
|||||||
/**
|
/**
|
||||||
* This makes sure the header and all rows have equal length.
|
* This makes sure the header and all rows have equal length.
|
||||||
*
|
*
|
||||||
* @param table (immutable)
|
* @param series (immutable)
|
||||||
* @returns a new table that has equal length rows, or the same
|
* @returns a series that has equal length rows, or the same
|
||||||
* table if no changes were needed
|
* series if no changes were needed
|
||||||
*/
|
*/
|
||||||
export function matchRowSizes(table: SeriesData): SeriesData {
|
export function matchRowSizes(series: SeriesData): SeriesData {
|
||||||
const { rows } = table;
|
const { rows } = series;
|
||||||
let { fields } = table;
|
let { fields } = series;
|
||||||
|
|
||||||
let sameSize = true;
|
let sameSize = true;
|
||||||
let size = fields.length;
|
let size = fields.length;
|
||||||
@ -44,7 +44,7 @@ export function matchRowSizes(table: SeriesData): SeriesData {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (sameSize) {
|
if (sameSize) {
|
||||||
return table;
|
return series;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pad Fields
|
// Pad Fields
|
||||||
@ -164,8 +164,8 @@ function convertTimeSeriesToSeriesData(timeSeries: TimeSeries): SeriesData {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getFirstTimeField = (table: SeriesData): number => {
|
export const getFirstTimeField = (series: SeriesData): number => {
|
||||||
const { fields } = table;
|
const { fields } = series;
|
||||||
for (let i = 0; i < fields.length; i++) {
|
for (let i = 0; i < fields.length; i++) {
|
||||||
if (fields[i].type === FieldType.time) {
|
if (fields[i].type === FieldType.time) {
|
||||||
return i;
|
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
|
* Looks at the data to guess the column type. This ignores any existing setting
|
||||||
*/
|
*/
|
||||||
function guessFieldTypeFromTable(table: SeriesData, index: number): FieldType | undefined {
|
function guessFieldTypeFromTable(series: SeriesData, index: number): FieldType | undefined {
|
||||||
const column = table.fields[index];
|
const column = series.fields[index];
|
||||||
|
|
||||||
// 1. Use the column name to guess
|
// 1. Use the column name to guess
|
||||||
if (column.name) {
|
if (column.name) {
|
||||||
@ -226,8 +226,8 @@ function guessFieldTypeFromTable(table: SeriesData, index: number): FieldType |
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 2. Check the first non-null value
|
// 2. Check the first non-null value
|
||||||
for (let i = 0; i < table.rows.length; i++) {
|
for (let i = 0; i < series.rows.length; i++) {
|
||||||
const v = table.rows[i][index];
|
const v = series.rows[i][index];
|
||||||
if (v !== null) {
|
if (v !== null) {
|
||||||
return guessFieldTypeFromValue(v);
|
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
|
* @returns a copy of the series with the best guess for each field type
|
||||||
* If the table already has column types defined, they will be used
|
* If the series already has field types defined, they will be used
|
||||||
*/
|
*/
|
||||||
export const guessFieldTypes = (table: SeriesData): SeriesData => {
|
export const guessFieldTypes = (series: SeriesData): SeriesData => {
|
||||||
for (let i = 0; i < table.fields.length; i++) {
|
for (let i = 0; i < series.fields.length; i++) {
|
||||||
if (!table.fields[i].type) {
|
if (!series.fields[i].type) {
|
||||||
// Somethign is missing a type return a modified copy
|
// Somethign is missing a type return a modified copy
|
||||||
return {
|
return {
|
||||||
...table,
|
...series,
|
||||||
fields: table.fields.map((column, index) => {
|
fields: series.fields.map((field, index) => {
|
||||||
if (column.type) {
|
if (field.type) {
|
||||||
return column;
|
return field;
|
||||||
}
|
}
|
||||||
// Replace it with a calculated version
|
// Replace it with a calculated version
|
||||||
return {
|
return {
|
||||||
...column,
|
...field,
|
||||||
type: guessFieldTypeFromTable(table, index),
|
type: guessFieldTypeFromTable(series, index),
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// No changes necessary
|
// No changes necessary
|
||||||
return table;
|
return series;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const isTableData = (data: any): data is SeriesData => data && data.hasOwnProperty('columns');
|
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')) {
|
if (data.hasOwnProperty('columns')) {
|
||||||
return convertTableToSeriesData(data);
|
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);
|
console.warn('Can not convert', data);
|
||||||
throw new Error('Unsupported data format');
|
throw new Error('Unsupported data format');
|
||||||
};
|
};
|
@ -1,4 +1,4 @@
|
|||||||
import { parseCSV } from './processTableData';
|
import { parseCSV } from './processSeriesData';
|
||||||
import { getStatsCalculators, StatID, calculateStats } from './statsCalculator';
|
import { getStatsCalculators, StatID, calculateStats } from './statsCalculator';
|
||||||
|
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
Loading…
Reference in New Issue
Block a user