2022-04-22 08:33:13 -05:00
|
|
|
import { useMemo } from 'react';
|
2020-06-18 07:31:30 -05:00
|
|
|
import useAsync from 'react-use/lib/useAsync';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
|
|
|
import { DataQueryError, DataSourceApi, PanelData, PanelPlugin } from '@grafana/data';
|
2020-06-18 07:31:30 -05:00
|
|
|
import { getDataSourceSrv } from '@grafana/runtime';
|
2022-10-06 10:34:04 -05:00
|
|
|
import { t } from 'app/core/internationalization';
|
2021-05-18 15:55:35 -05:00
|
|
|
import { DashboardModel, PanelModel } from 'app/features/dashboard/state';
|
2021-03-18 11:38:09 -05:00
|
|
|
import { InspectTab } from 'app/features/inspector/types';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
|
|
|
import { supportsDataQuery } from '../PanelEditor/utils';
|
|
|
|
|
2020-06-18 07:31:30 -05:00
|
|
|
/**
|
|
|
|
* Given PanelData return first data source supporting metadata inspector
|
|
|
|
*/
|
|
|
|
export const useDatasourceMetadata = (data?: PanelData) => {
|
2021-08-05 07:32:51 -05:00
|
|
|
const state = useAsync(async () => {
|
2020-06-18 07:31:30 -05:00
|
|
|
const targets = data?.request?.targets || [];
|
|
|
|
|
|
|
|
if (data && data.series && targets.length) {
|
|
|
|
for (const frame of data.series) {
|
|
|
|
if (frame.meta && frame.meta.custom) {
|
|
|
|
// get data source from first query
|
|
|
|
const dataSource = await getDataSourceSrv().get(targets[0].datasource);
|
|
|
|
if (dataSource && dataSource.components?.MetadataInspector) {
|
|
|
|
return dataSource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}, [data]);
|
|
|
|
return state.value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configures tabs for PanelInspector
|
|
|
|
*/
|
|
|
|
export const useInspectTabs = (
|
2021-05-18 15:55:35 -05:00
|
|
|
panel: PanelModel,
|
2020-06-18 07:31:30 -05:00
|
|
|
dashboard: DashboardModel,
|
2021-03-25 06:42:14 -05:00
|
|
|
plugin: PanelPlugin | undefined | null,
|
2020-06-18 07:31:30 -05:00
|
|
|
error?: DataQueryError,
|
|
|
|
metaDs?: DataSourceApi
|
|
|
|
) => {
|
|
|
|
return useMemo(() => {
|
|
|
|
const tabs = [];
|
|
|
|
if (supportsDataQuery(plugin)) {
|
2022-10-06 10:34:04 -05:00
|
|
|
tabs.push({ label: t('dashboard.inspect.data-tab', 'Data'), value: InspectTab.Data });
|
|
|
|
tabs.push({ label: t('dashboard.inspect.stats-tab', 'Stats'), value: InspectTab.Stats });
|
2020-06-18 07:31:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (metaDs) {
|
2022-10-06 10:34:04 -05:00
|
|
|
tabs.push({ label: t('dashboard.inspect.meta-tab', 'Meta Data'), value: InspectTab.Meta });
|
2020-06-18 07:31:30 -05:00
|
|
|
}
|
|
|
|
|
2022-10-06 10:34:04 -05:00
|
|
|
tabs.push({ label: t('dashboard.inspect.json-tab', 'JSON'), value: InspectTab.JSON });
|
2020-06-18 07:31:30 -05:00
|
|
|
|
|
|
|
if (error && error.message) {
|
2022-10-06 10:34:04 -05:00
|
|
|
tabs.push({ label: t('dashboard.inspect.error-tab', 'Error'), value: InspectTab.Error });
|
2020-06-18 07:31:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dashboard.meta.canEdit && supportsDataQuery(plugin)) {
|
2022-10-06 10:34:04 -05:00
|
|
|
tabs.push({ label: t('dashboard.inspect.query-tab', 'Query'), value: InspectTab.Query });
|
2020-06-18 07:31:30 -05:00
|
|
|
}
|
|
|
|
return tabs;
|
2023-03-13 10:07:36 -05:00
|
|
|
}, [plugin, metaDs, dashboard, error]);
|
2020-06-18 07:31:30 -05:00
|
|
|
};
|