2021-06-01 19:52:08 +08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-04-22 21:33:13 +08:00
|
|
|
|
2021-10-21 04:01:21 +08:00
|
|
|
import { RefreshEvent } from '@grafana/runtime';
|
2022-04-22 21:33:13 +08:00
|
|
|
import { PanelChrome } from '@grafana/ui';
|
2021-06-01 19:52:08 +08:00
|
|
|
import { applyPanelTimeOverrides } from 'app/features/dashboard/utils/panel';
|
2022-04-22 21:33:13 +08:00
|
|
|
import { PanelRenderer } from 'app/features/panel/components/PanelRenderer';
|
2023-03-01 23:48:36 +08:00
|
|
|
import { PanelOptions } from 'app/plugins/panel/table/panelcfg.gen';
|
2022-04-22 21:33:13 +08:00
|
|
|
|
2022-01-31 13:02:24 +08:00
|
|
|
import PanelHeaderCorner from '../../dashgrid/PanelHeader/PanelHeaderCorner';
|
2022-04-22 21:33:13 +08:00
|
|
|
import { getTimeSrv } from '../../services/TimeSrv';
|
|
|
|
|
import { DashboardModel, PanelModel } from '../../state';
|
|
|
|
|
|
|
|
|
|
import { usePanelLatestData } from './usePanelLatestData';
|
2022-01-31 13:02:24 +08:00
|
|
|
|
2022-08-26 21:36:42 +08:00
|
|
|
export interface Props {
|
2021-05-07 23:09:06 +08:00
|
|
|
width: number;
|
|
|
|
|
height: number;
|
|
|
|
|
panel: PanelModel;
|
2021-06-01 19:52:08 +08:00
|
|
|
dashboard: DashboardModel;
|
2021-05-07 23:09:06 +08:00
|
|
|
}
|
|
|
|
|
|
2021-06-01 19:52:08 +08:00
|
|
|
export function PanelEditorTableView({ width, height, panel, dashboard }: Props) {
|
|
|
|
|
const { data } = usePanelLatestData(panel, { withTransforms: true, withFieldConfig: false }, false);
|
2021-05-07 23:09:06 +08:00
|
|
|
const [options, setOptions] = useState<PanelOptions>({
|
|
|
|
|
frameIndex: 0,
|
|
|
|
|
showHeader: true,
|
2021-09-08 15:44:41 +08:00
|
|
|
showTypeIcons: true,
|
2021-05-07 23:09:06 +08:00
|
|
|
});
|
|
|
|
|
|
2021-06-01 19:52:08 +08:00
|
|
|
// Subscribe to panel event
|
|
|
|
|
useEffect(() => {
|
2022-01-31 13:02:24 +08:00
|
|
|
const timeSrv = getTimeSrv();
|
2021-06-01 19:52:08 +08:00
|
|
|
|
|
|
|
|
const sub = panel.events.subscribe(RefreshEvent, () => {
|
2022-08-26 21:36:42 +08:00
|
|
|
const timeData = applyPanelTimeOverrides(panel, timeSrv.timeRange());
|
2022-08-10 16:36:19 +08:00
|
|
|
panel.runAllPanelQueries({
|
|
|
|
|
dashboardUID: dashboard.uid,
|
|
|
|
|
dashboardTimezone: dashboard.getTimezone(),
|
|
|
|
|
timeData,
|
|
|
|
|
width,
|
|
|
|
|
});
|
2021-06-01 19:52:08 +08:00
|
|
|
});
|
|
|
|
|
return () => {
|
|
|
|
|
sub.unsubscribe();
|
|
|
|
|
};
|
|
|
|
|
}, [panel, dashboard, width]);
|
|
|
|
|
|
2021-05-07 23:09:06 +08:00
|
|
|
if (!data) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-03-08 23:11:38 +08:00
|
|
|
|
|
|
|
|
const errorMessage = data?.errors
|
|
|
|
|
? data.errors.length > 1
|
|
|
|
|
? 'Multiple errors found. Click for more details'
|
|
|
|
|
: data.errors[0].message
|
|
|
|
|
: data?.error?.message;
|
2021-05-07 23:09:06 +08:00
|
|
|
return (
|
|
|
|
|
<PanelChrome width={width} height={height} padding="none">
|
|
|
|
|
{(innerWidth, innerHeight) => (
|
2022-01-31 13:02:24 +08:00
|
|
|
<>
|
2023-03-08 23:11:38 +08:00
|
|
|
<PanelHeaderCorner panel={panel} error={errorMessage} />
|
2022-01-31 13:02:24 +08:00
|
|
|
<PanelRenderer
|
|
|
|
|
title="Raw data"
|
|
|
|
|
pluginId="table"
|
|
|
|
|
width={innerWidth}
|
|
|
|
|
height={innerHeight}
|
|
|
|
|
data={data}
|
|
|
|
|
options={options}
|
|
|
|
|
onOptionsChange={setOptions}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
2021-05-07 23:09:06 +08:00
|
|
|
)}
|
|
|
|
|
</PanelChrome>
|
|
|
|
|
);
|
|
|
|
|
}
|