ExploreQueryInspector.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import React from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { CoreApp, TimeZone } from '@grafana/data';
  4. import { TabbedContainer, TabConfig } from '@grafana/ui';
  5. import { ExploreDrawer } from 'app/features/explore/ExploreDrawer';
  6. import { InspectDataTab } from 'app/features/inspector/InspectDataTab';
  7. import { InspectErrorTab } from 'app/features/inspector/InspectErrorTab';
  8. import { InspectJSONTab } from 'app/features/inspector/InspectJSONTab';
  9. import { InspectStatsTab } from 'app/features/inspector/InspectStatsTab';
  10. import { QueryInspector } from 'app/features/inspector/QueryInspector';
  11. import { StoreState, ExploreItemState, ExploreId } from 'app/types';
  12. import { runQueries } from './state/query';
  13. interface DispatchProps {
  14. width: number;
  15. exploreId: ExploreId;
  16. timeZone: TimeZone;
  17. onClose: () => void;
  18. }
  19. type Props = DispatchProps & ConnectedProps<typeof connector>;
  20. export function ExploreQueryInspector(props: Props) {
  21. const { loading, width, onClose, queryResponse, timeZone } = props;
  22. const dataFrames = queryResponse?.series || [];
  23. const error = queryResponse?.error;
  24. const statsTab: TabConfig = {
  25. label: 'Stats',
  26. value: 'stats',
  27. icon: 'chart-line',
  28. content: <InspectStatsTab data={queryResponse!} timeZone={queryResponse?.request?.timezone as TimeZone} />,
  29. };
  30. const jsonTab: TabConfig = {
  31. label: 'JSON',
  32. value: 'json',
  33. icon: 'brackets-curly',
  34. content: <InspectJSONTab data={queryResponse} onClose={onClose} />,
  35. };
  36. const dataTab: TabConfig = {
  37. label: 'Data',
  38. value: 'data',
  39. icon: 'database',
  40. content: (
  41. <InspectDataTab
  42. data={dataFrames}
  43. isLoading={loading}
  44. options={{ withTransforms: false, withFieldConfig: false }}
  45. timeZone={timeZone}
  46. app={CoreApp.Explore}
  47. />
  48. ),
  49. };
  50. const queryTab: TabConfig = {
  51. label: 'Query',
  52. value: 'query',
  53. icon: 'info-circle',
  54. content: <QueryInspector data={dataFrames} onRefreshQuery={() => props.runQueries(props.exploreId)} />,
  55. };
  56. const tabs = [statsTab, queryTab, jsonTab, dataTab];
  57. if (error) {
  58. const errorTab: TabConfig = {
  59. label: 'Error',
  60. value: 'error',
  61. icon: 'exclamation-triangle',
  62. content: <InspectErrorTab error={error} />,
  63. };
  64. tabs.push(errorTab);
  65. }
  66. return (
  67. <ExploreDrawer width={width}>
  68. <TabbedContainer tabs={tabs} onClose={onClose} closeIconTooltip="Close query inspector" />
  69. </ExploreDrawer>
  70. );
  71. }
  72. function mapStateToProps(state: StoreState, { exploreId }: { exploreId: ExploreId }) {
  73. const explore = state.explore;
  74. const item: ExploreItemState = explore[exploreId]!;
  75. const { loading, queryResponse } = item;
  76. return {
  77. loading,
  78. queryResponse,
  79. };
  80. }
  81. const mapDispatchToProps = {
  82. runQueries,
  83. };
  84. const connector = connect(mapStateToProps, mapDispatchToProps);
  85. export default connector(ExploreQueryInspector);