chore: more TableData to SeriesData renaming (#16206)

This commit is contained in:
Ryan McKinley 2019-03-25 09:30:53 -07:00 committed by Torkel Ödegaard
parent dbc2f2761a
commit 941b770c46
7 changed files with 47 additions and 47 deletions

View File

@ -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 {

View File

@ -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';

View File

@ -1,4 +1,4 @@
export * from './processTableData';
export * from './processSeriesData';
export * from './valueFormats/valueFormats';
export * from './colors';
export * from './namedColorsPalette';

View File

@ -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();

View File

@ -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');
};

View File

@ -1,4 +1,4 @@
import { parseCSV } from './processTableData';
import { parseCSV } from './processSeriesData';
import { getStatsCalculators, StatID, calculateStats } from './statsCalculator';
import _ from 'lodash';