mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
show all columns in singlestats
This commit is contained in:
parent
30f0c35006
commit
c4a503dbc5
@ -4,6 +4,7 @@ import Papa, { ParseError, ParseMeta } from 'papaparse';
|
||||
|
||||
// Types
|
||||
import { TableData, Column, TimeSeries } from '../types';
|
||||
import { isString } from 'util';
|
||||
|
||||
// Subset of all parse options
|
||||
export interface TableParseOptions {
|
||||
@ -146,6 +147,57 @@ function convertTimeSeriesToTableData(timeSeries: TimeSeries): TableData {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns a table Returns a copy of the table with the best guess for each column type
|
||||
*/
|
||||
export const guessColumnTypes = (table: TableData): TableData => {
|
||||
let changed = false;
|
||||
const columns = table.columns.map((column, index) => {
|
||||
if (!column.type) {
|
||||
// 1. Use the column name to guess
|
||||
if (column.text) {
|
||||
const name = column.text.toLowerCase();
|
||||
if (name === 'date' || name === 'time') {
|
||||
changed = true;
|
||||
return {
|
||||
...column,
|
||||
type: 'time',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Check the first non-null value
|
||||
for (let i = 0; i < table.rows.length; i++) {
|
||||
const v = table.rows[i][index];
|
||||
if (v !== null) {
|
||||
let type;
|
||||
if (isNumber(v)) {
|
||||
type = 'number';
|
||||
} else if (isString(v)) {
|
||||
type = 'string';
|
||||
}
|
||||
if (type) {
|
||||
changed = true;
|
||||
return {
|
||||
...column,
|
||||
type,
|
||||
};
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return column;
|
||||
});
|
||||
if (changed) {
|
||||
return {
|
||||
...table,
|
||||
columns,
|
||||
};
|
||||
}
|
||||
return table;
|
||||
};
|
||||
|
||||
export const isTableData = (data: any): data is TableData => data && data.hasOwnProperty('columns');
|
||||
|
||||
export const toTableData = (results?: any[]): TableData[] => {
|
||||
|
@ -4,7 +4,7 @@ import React, { PureComponent, CSSProperties } from 'react';
|
||||
// Types
|
||||
import { SingleStatOptions, SingleStatBaseOptions } from './types';
|
||||
|
||||
import { DisplayValue, PanelProps, processTimeSeries, NullValueMode } from '@grafana/ui';
|
||||
import { DisplayValue, PanelProps, processTimeSeries, NullValueMode, guessColumnTypes } from '@grafana/ui';
|
||||
import { config } from 'app/core/config';
|
||||
import { getDisplayProcessor } from '@grafana/ui';
|
||||
import { ProcessedValuesRepeater } from './ProcessedValuesRepeater';
|
||||
@ -24,13 +24,31 @@ export const getSingleStatValues = (props: PanelProps<SingleStatBaseOptions>): D
|
||||
theme: config.theme,
|
||||
});
|
||||
|
||||
return processTimeSeries({
|
||||
data,
|
||||
nullValueMode: NullValueMode.Null,
|
||||
}).map((series, index) => {
|
||||
const value = stat !== 'name' ? series.stats[stat] : series.label;
|
||||
return processor(value);
|
||||
const values: DisplayValue[] = [];
|
||||
data.forEach(t => {
|
||||
const table = guessColumnTypes(t);
|
||||
for (let i = 0; i < table.columns.length; i++) {
|
||||
const column = table.columns[i];
|
||||
|
||||
// Show all columns that are not 'time'
|
||||
if (column.type === 'number') {
|
||||
const series = processTimeSeries({
|
||||
data: [table],
|
||||
xColumn: i,
|
||||
yColumn: i,
|
||||
nullValueMode: NullValueMode.Null,
|
||||
})[0];
|
||||
|
||||
const value = stat !== 'name' ? series.stats[stat] : series.label;
|
||||
values.push(processor(value));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (values.length === 0) {
|
||||
throw { message: 'Could not find numeric data' };
|
||||
}
|
||||
return values;
|
||||
};
|
||||
|
||||
export class SingleStatPanel extends PureComponent<PanelProps<SingleStatOptions>> {
|
||||
|
Loading…
Reference in New Issue
Block a user