import React, { PureComponent, ReactNode } from 'react'; import { Alert, Table } from 'reactstrap'; import SeriesName from './SeriesName'; export interface QueryResult { data: null | { resultType: 'vector', result: InstantSample[], } | { resultType: 'matrix', result: RangeSamples[], } | { resultType: 'scalar', result: SampleValue, } | { resultType: 'string', result: string, }, }; interface InstantSample { metric: Metric, value: SampleValue, } interface RangeSamples { metric: Metric, values: SampleValue[], } interface Metric { [key: string]: string, } type SampleValue = [number, string]; class DataTable extends PureComponent { limitSeries(series: InstantSample[] | RangeSamples[]): InstantSample[] | RangeSamples[] { const maxSeries = 10000; if (series.length > maxSeries) { return series.slice(0, maxSeries); } return series; } render() { const data = this.props.data; if (data === null) { return No data queried yet; } if (data.result === null || data.result.length === 0) { return Empty query result; } let rows: ReactNode[] = []; let limited = false; switch(data.resultType) { case 'vector': rows = (this.limitSeries(data.result) as InstantSample[]) .map((s: InstantSample, index: number): ReactNode => { return {s.value[1]}; }); limited = rows.length !== data.result.length; break; case 'matrix': rows = (this.limitSeries(data.result) as RangeSamples[]) .map((s, index) => { const valueText = s.values.map((v) => { return [1] + ' @' + v[0]; }).join('\n'); return {valueText}; }); limited = rows.length !== data.result.length; break; case 'scalar': rows.push(scalar{data.result[1]}); break; case 'string': rows.push(scalar{data.result[1]}); break; default: return Unsupported result value type; } return ( <> {limited && Warning: Fetched {data.result.length} metrics, only displaying first {rows.length}. } {rows}
); } } export default DataTable;