2019-11-05 03:53:44 +08:00
|
|
|
import React, { PureComponent } from 'react';
|
2020-01-16 18:11:33 +08:00
|
|
|
import AutoSizer from 'react-virtualized-auto-sizer';
|
2020-01-17 19:55:21 +08:00
|
|
|
import { saveAs } from 'file-saver';
|
|
|
|
|
import { css } from 'emotion';
|
2019-11-05 03:53:44 +08:00
|
|
|
|
|
|
|
|
import { DashboardModel, PanelModel } from 'app/features/dashboard/state';
|
2020-01-17 19:55:21 +08:00
|
|
|
import { JSONFormatter, Drawer, Select, Table, TabsBar, Tab, TabContent, Forms, stylesFactory } from '@grafana/ui';
|
2020-01-10 14:59:23 +08:00
|
|
|
import { getLocationSrv, getDataSourceSrv } from '@grafana/runtime';
|
2020-01-17 19:55:21 +08:00
|
|
|
import { DataFrame, DataSourceApi, SelectableValue, applyFieldOverrides, toCSV } from '@grafana/data';
|
2020-01-10 14:59:23 +08:00
|
|
|
import { config } from 'app/core/config';
|
2019-11-05 03:53:44 +08:00
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
dashboard: DashboardModel;
|
|
|
|
|
panel: PanelModel;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-10 14:59:23 +08:00
|
|
|
enum InspectTab {
|
|
|
|
|
Data = 'data',
|
|
|
|
|
Raw = 'raw',
|
|
|
|
|
Issue = 'issue',
|
|
|
|
|
Meta = 'meta', // When result metadata exists
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
|
// The last raw response
|
|
|
|
|
last?: any;
|
|
|
|
|
|
|
|
|
|
// Data frem the last response
|
|
|
|
|
data: DataFrame[];
|
|
|
|
|
|
|
|
|
|
// The selected data frame
|
|
|
|
|
selected: number;
|
|
|
|
|
|
|
|
|
|
// The Selected Tab
|
|
|
|
|
tab: InspectTab;
|
|
|
|
|
|
|
|
|
|
// If the datasource supports custom metadata
|
|
|
|
|
metaDS?: DataSourceApi;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-17 19:55:21 +08:00
|
|
|
const getStyles = stylesFactory(() => {
|
|
|
|
|
return {
|
|
|
|
|
toolbar: css`
|
|
|
|
|
display: flex;
|
|
|
|
|
margin: 8px 0;
|
|
|
|
|
justify-content: flex-end;
|
|
|
|
|
align-items: center;
|
|
|
|
|
`,
|
|
|
|
|
dataFrameSelect: css`
|
|
|
|
|
flex-grow: 2;
|
|
|
|
|
`,
|
|
|
|
|
downloadCsv: css`
|
|
|
|
|
margin-left: 16px;
|
|
|
|
|
`,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2020-01-10 14:59:23 +08:00
|
|
|
export class PanelInspector extends PureComponent<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
data: [],
|
|
|
|
|
selected: 0,
|
|
|
|
|
tab: InspectTab.Data,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async componentDidMount() {
|
|
|
|
|
const { panel } = this.props;
|
|
|
|
|
if (!panel) {
|
|
|
|
|
this.onDismiss(); // Try to close the component
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO? should we get the result with an observable once?
|
|
|
|
|
const lastResult = (panel.getQueryRunner() as any).lastResult;
|
|
|
|
|
if (!lastResult) {
|
|
|
|
|
this.onDismiss(); // Usually opened from refresh?
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find the first DataSource wanting to show custom metadata
|
|
|
|
|
let metaDS: DataSourceApi;
|
|
|
|
|
const data = lastResult?.series as DataFrame[];
|
|
|
|
|
if (data) {
|
|
|
|
|
for (const frame of data) {
|
|
|
|
|
const key = frame.meta?.datasource;
|
|
|
|
|
if (key) {
|
|
|
|
|
const ds = await getDataSourceSrv().get(key);
|
|
|
|
|
if (ds && ds.components.MetadataInspector) {
|
|
|
|
|
metaDS = ds;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set last result, but no metadata inspector
|
|
|
|
|
this.setState({
|
|
|
|
|
last: lastResult,
|
|
|
|
|
data,
|
|
|
|
|
metaDS,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 03:53:44 +08:00
|
|
|
onDismiss = () => {
|
|
|
|
|
getLocationSrv().update({
|
|
|
|
|
query: { inspect: null },
|
|
|
|
|
partial: true,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-10 14:59:23 +08:00
|
|
|
onSelectTab = (item: SelectableValue<InspectTab>) => {
|
|
|
|
|
this.setState({ tab: item.value || InspectTab.Data });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onSelectedFrameChanged = (item: SelectableValue<number>) => {
|
|
|
|
|
this.setState({ selected: item.value || 0 });
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-17 19:55:21 +08:00
|
|
|
exportCsv = (dataFrame: DataFrame) => {
|
|
|
|
|
const dataFrameCsv = toCSV([dataFrame]);
|
|
|
|
|
|
|
|
|
|
const blob = new Blob([dataFrameCsv], {
|
|
|
|
|
type: 'application/csv;charset=utf-8',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
saveAs(blob, dataFrame.name + '-' + new Date().getUTCDate() + '.csv');
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-10 14:59:23 +08:00
|
|
|
renderMetadataInspector() {
|
|
|
|
|
const { metaDS, data } = this.state;
|
|
|
|
|
if (!metaDS || !metaDS.components?.MetadataInspector) {
|
|
|
|
|
return <div>No Metadata Inspector</div>;
|
|
|
|
|
}
|
|
|
|
|
return <metaDS.components.MetadataInspector datasource={metaDS} data={data} />;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-18 15:24:17 +08:00
|
|
|
renderDataTab(width: number, height: number) {
|
2020-01-10 14:59:23 +08:00
|
|
|
const { data, selected } = this.state;
|
2020-01-17 19:55:21 +08:00
|
|
|
const styles = getStyles();
|
2020-01-10 14:59:23 +08:00
|
|
|
if (!data || !data.length) {
|
|
|
|
|
return <div>No Data</div>;
|
|
|
|
|
}
|
|
|
|
|
const choices = data.map((frame, index) => {
|
|
|
|
|
return {
|
|
|
|
|
value: index,
|
|
|
|
|
label: `${frame.name} (${index})`,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Apply dummy styles
|
|
|
|
|
const processed = applyFieldOverrides({
|
|
|
|
|
data,
|
|
|
|
|
theme: config.theme,
|
|
|
|
|
fieldOptions: { defaults: {}, overrides: [] },
|
|
|
|
|
replaceVariables: (value: string) => {
|
|
|
|
|
return value;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
2020-01-17 19:55:21 +08:00
|
|
|
<div className={styles.toolbar}>
|
|
|
|
|
{choices.length > 1 && (
|
|
|
|
|
<div className={styles.dataFrameSelect}>
|
|
|
|
|
<Select
|
|
|
|
|
options={choices}
|
|
|
|
|
value={choices.find(t => t.value === selected)}
|
|
|
|
|
onChange={this.onSelectedFrameChanged}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
<div className={styles.downloadCsv}>
|
|
|
|
|
<Forms.Button variant="primary" onClick={() => this.exportCsv(processed[selected])}>
|
|
|
|
|
Download CSV
|
|
|
|
|
</Forms.Button>
|
2020-01-10 14:59:23 +08:00
|
|
|
</div>
|
2020-01-17 19:55:21 +08:00
|
|
|
</div>
|
2020-01-18 15:24:17 +08:00
|
|
|
<Table width={width} height={height} data={processed[selected]} />
|
2020-01-10 14:59:23 +08:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderIssueTab() {
|
|
|
|
|
return <div>TODO: show issue form</div>;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 03:53:44 +08:00
|
|
|
render() {
|
|
|
|
|
const { panel } = this.props;
|
2020-01-10 14:59:23 +08:00
|
|
|
const { last, tab } = this.state;
|
2019-11-05 03:53:44 +08:00
|
|
|
if (!panel) {
|
|
|
|
|
this.onDismiss(); // Try to close the component
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-10 14:59:23 +08:00
|
|
|
const tabs = [
|
|
|
|
|
{ label: 'Data', value: InspectTab.Data },
|
|
|
|
|
{ label: 'Issue', value: InspectTab.Issue },
|
|
|
|
|
{ label: 'Raw JSON', value: InspectTab.Raw },
|
|
|
|
|
];
|
|
|
|
|
if (this.state.metaDS) {
|
|
|
|
|
tabs.push({ label: 'Meta Data', value: InspectTab.Meta });
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 03:53:44 +08:00
|
|
|
return (
|
2019-12-18 20:57:07 +08:00
|
|
|
<Drawer title={panel.title} onClose={this.onDismiss}>
|
2020-01-14 16:03:14 +08:00
|
|
|
<TabsBar>
|
|
|
|
|
{tabs.map(t => {
|
|
|
|
|
return <Tab label={t.label} active={t.value === tab} onChangeTab={() => this.onSelectTab(t)} />;
|
|
|
|
|
})}
|
|
|
|
|
</TabsBar>
|
|
|
|
|
<TabContent>
|
2020-01-18 15:24:17 +08:00
|
|
|
<AutoSizer>
|
|
|
|
|
{({ width, height }) => {
|
2020-01-16 18:11:33 +08:00
|
|
|
if (width === 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-01-18 15:24:17 +08:00
|
|
|
|
2020-01-16 18:11:33 +08:00
|
|
|
return (
|
|
|
|
|
<div style={{ width }}>
|
2020-01-18 15:24:17 +08:00
|
|
|
{tab === InspectTab.Data && this.renderDataTab(width, height)}
|
2020-01-16 18:11:33 +08:00
|
|
|
|
|
|
|
|
{tab === InspectTab.Meta && this.renderMetadataInspector()}
|
|
|
|
|
|
|
|
|
|
{tab === InspectTab.Issue && this.renderIssueTab()}
|
|
|
|
|
|
|
|
|
|
{tab === InspectTab.Raw && (
|
|
|
|
|
<div>
|
|
|
|
|
<JSONFormatter json={last} open={2} />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}}
|
|
|
|
|
</AutoSizer>
|
2020-01-14 16:03:14 +08:00
|
|
|
</TabContent>
|
2019-12-18 20:57:07 +08:00
|
|
|
</Drawer>
|
2019-11-05 03:53:44 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|