2019-01-13 16:10:23 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
2021-06-24 01:21:19 -05:00
|
|
|
import { connect, ConnectedProps } from 'react-redux';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2023-04-12 11:58:08 -05:00
|
|
|
import { ValueLinkConfig, applyFieldOverrides, TimeZone, SplitOpen, DataFrame, LoadingState } from '@grafana/data';
|
|
|
|
import { Table, AdHocFilterItem, PanelChrome } from '@grafana/ui';
|
2019-12-27 02:11:16 -06:00
|
|
|
import { config } from 'app/core/config';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { StoreState } from 'app/types';
|
2023-06-21 04:06:28 -05:00
|
|
|
import { ExploreItemState } from 'app/types/explore';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2023-05-23 08:42:38 -05:00
|
|
|
import { MetaInfoText } from '../MetaInfoText';
|
2023-06-20 10:38:05 -05:00
|
|
|
import { selectIsWaitingForData } from '../state/query';
|
2023-05-23 08:42:38 -05:00
|
|
|
import { getFieldLinksForExplore } from '../utils/links';
|
2019-01-13 16:10:23 -06:00
|
|
|
|
|
|
|
interface TableContainerProps {
|
2020-08-28 11:59:56 -05:00
|
|
|
ariaLabel?: string;
|
2023-06-21 04:06:28 -05:00
|
|
|
exploreId: string;
|
2019-12-18 01:38:50 -06:00
|
|
|
width: number;
|
2021-12-02 05:07:23 -06:00
|
|
|
timeZone: TimeZone;
|
2023-01-19 07:03:13 -06:00
|
|
|
onCellFilterAdded?: (filter: AdHocFilterItem) => void;
|
2022-11-02 05:22:09 -05:00
|
|
|
splitOpenFn: SplitOpen;
|
2019-01-13 16:10:23 -06:00
|
|
|
}
|
|
|
|
|
2021-06-24 01:21:19 -05:00
|
|
|
function mapStateToProps(state: StoreState, { exploreId }: TableContainerProps) {
|
|
|
|
const explore = state.explore;
|
2023-05-03 10:45:11 -05:00
|
|
|
const item: ExploreItemState = explore.panes[exploreId]!;
|
2023-06-20 10:38:05 -05:00
|
|
|
const { tableResult, range } = item;
|
|
|
|
const loadingInState = selectIsWaitingForData(exploreId);
|
2021-06-24 01:21:19 -05:00
|
|
|
const loading = tableResult && tableResult.length > 0 ? false : loadingInState;
|
|
|
|
return { loading, tableResult, range };
|
|
|
|
}
|
|
|
|
|
2022-11-02 05:22:09 -05:00
|
|
|
const connector = connect(mapStateToProps, {});
|
2021-06-24 01:21:19 -05:00
|
|
|
|
|
|
|
type Props = TableContainerProps & ConnectedProps<typeof connector>;
|
|
|
|
|
|
|
|
export class TableContainer extends PureComponent<Props> {
|
2023-07-13 06:44:42 -05:00
|
|
|
getMainFrames(frames: DataFrame[] | null) {
|
|
|
|
return frames?.filter((df) => df.meta?.custom?.parentRowIndex === undefined) || [frames?.[0]];
|
2022-11-23 11:49:32 -06:00
|
|
|
}
|
|
|
|
|
2023-07-13 06:44:42 -05:00
|
|
|
getTableHeight(rowCount: number, isSingleTable = true) {
|
|
|
|
if (rowCount === 0) {
|
2019-12-18 01:38:50 -06:00
|
|
|
return 200;
|
|
|
|
}
|
2023-07-13 06:44:42 -05:00
|
|
|
// tries to estimate table height, with a min of 300 and a max of 600
|
|
|
|
// if there are multiple tables, there is no min
|
|
|
|
return Math.min(600, Math.max(rowCount * 36, isSingleTable ? 300 : 0) + 40 + 46);
|
2019-12-18 01:38:50 -06:00
|
|
|
}
|
|
|
|
|
2019-01-13 16:10:23 -06:00
|
|
|
render() {
|
2022-11-02 05:22:09 -05:00
|
|
|
const { loading, onCellFilterAdded, tableResult, width, splitOpenFn, range, ariaLabel, timeZone } = this.props;
|
2019-01-28 06:09:51 -06:00
|
|
|
|
2022-11-23 11:49:32 -06:00
|
|
|
let dataFrames = tableResult;
|
2021-12-02 05:07:23 -06:00
|
|
|
|
2022-11-23 11:49:32 -06:00
|
|
|
if (dataFrames?.length) {
|
|
|
|
dataFrames = applyFieldOverrides({
|
|
|
|
data: dataFrames,
|
2021-12-02 05:07:23 -06:00
|
|
|
timeZone,
|
|
|
|
theme: config.theme2,
|
|
|
|
replaceVariables: (v: string) => v,
|
|
|
|
fieldConfig: {
|
|
|
|
defaults: {},
|
|
|
|
overrides: [],
|
|
|
|
},
|
2022-11-23 11:49:32 -06:00
|
|
|
});
|
2020-06-30 07:51:04 -05:00
|
|
|
// Bit of code smell here. We need to add links here to the frame modifying the frame on every render.
|
|
|
|
// Should work fine in essence but still not the ideal way to pass props. In logs container we do this
|
|
|
|
// differently and sidestep this getLinks API on a dataframe
|
2022-11-23 11:49:32 -06:00
|
|
|
for (const frame of dataFrames) {
|
|
|
|
for (const field of frame.fields) {
|
|
|
|
field.getLinks = (config: ValueLinkConfig) => {
|
|
|
|
return getFieldLinksForExplore({
|
|
|
|
field,
|
|
|
|
rowIndex: config.valueRowIndex!,
|
|
|
|
splitOpenFn,
|
|
|
|
range,
|
|
|
|
dataFrame: frame!,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2020-06-30 07:51:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 06:44:42 -05:00
|
|
|
// move dataframes to be grouped by table, with optional sub-tables for a row
|
|
|
|
const tableData: Array<{ main: DataFrame; sub?: DataFrame[] }> = [];
|
|
|
|
const mainFrames = this.getMainFrames(dataFrames).filter(
|
|
|
|
(frame: DataFrame | undefined): frame is DataFrame => !!frame && frame.length !== 0
|
|
|
|
);
|
|
|
|
|
|
|
|
mainFrames?.forEach((frame) => {
|
|
|
|
const subFrames =
|
|
|
|
dataFrames?.filter((df) => frame.refId === df.refId && df.meta?.custom?.parentRowIndex !== undefined) ||
|
|
|
|
undefined;
|
|
|
|
tableData.push({ main: frame, sub: subFrames });
|
|
|
|
});
|
2022-11-23 11:49:32 -06:00
|
|
|
|
2019-01-13 16:10:23 -06:00
|
|
|
return (
|
2023-07-13 06:44:42 -05:00
|
|
|
<>
|
|
|
|
{tableData.length === 0 && (
|
|
|
|
<PanelChrome title={'Table'} width={width} height={200}>
|
|
|
|
{() => <MetaInfoText metaItems={[{ value: '0 series returned' }]} />}
|
|
|
|
</PanelChrome>
|
2020-01-19 21:53:59 -06:00
|
|
|
)}
|
2023-07-13 06:44:42 -05:00
|
|
|
{tableData.length > 0 &&
|
|
|
|
tableData.map((data, i) => (
|
|
|
|
<PanelChrome
|
|
|
|
key={data.main.refId || `table-${i}`}
|
|
|
|
title={tableData.length > 1 ? `Table - ${data.main.name || data.main.refId || i}` : 'Table'}
|
|
|
|
width={width}
|
|
|
|
height={this.getTableHeight(data.main.length, tableData.length === 1)}
|
|
|
|
loadingState={loading ? LoadingState.Loading : undefined}
|
|
|
|
>
|
|
|
|
{(innerWidth, innerHeight) => (
|
|
|
|
<Table
|
|
|
|
ariaLabel={ariaLabel}
|
|
|
|
data={data.main}
|
|
|
|
subData={data.sub}
|
|
|
|
width={innerWidth}
|
|
|
|
height={innerHeight}
|
|
|
|
onCellFilterAdded={onCellFilterAdded}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</PanelChrome>
|
|
|
|
))}
|
|
|
|
</>
|
2019-01-13 16:10:23 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 05:55:05 -05:00
|
|
|
export default connector(TableContainer);
|