ExploreToolbar.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import React, { PureComponent } from 'react';
  2. import { connect } from 'react-redux';
  3. import { hot } from 'react-hot-loader';
  4. import { ExploreId, ExploreMode } from 'app/types/explore';
  5. import { DataSourceSelectItem } from '@grafana/ui';
  6. import { RawTimeRange, TimeZone, TimeRange, LoadingState, SelectableValue } from '@grafana/data';
  7. import { DataSourcePicker } from 'app/core/components/Select/DataSourcePicker';
  8. import { StoreState } from 'app/types/store';
  9. import {
  10. changeDatasource,
  11. clearQueries,
  12. splitClose,
  13. runQueries,
  14. splitOpen,
  15. changeRefreshInterval,
  16. changeMode,
  17. } from './state/actions';
  18. import { getTimeZone } from '../profile/state/selectors';
  19. import ToggleButtonGroup, { ToggleButton } from 'app/core/components/ToggleButtonGroup/ToggleButtonGroup';
  20. import { ExploreTimeControls } from './ExploreTimeControls';
  21. enum IconSide {
  22. left = 'left',
  23. right = 'right',
  24. }
  25. const createResponsiveButton = (options: {
  26. splitted: boolean;
  27. title: string;
  28. onClick: () => void;
  29. buttonClassName?: string;
  30. iconClassName?: string;
  31. iconSide?: IconSide;
  32. disabled?: boolean;
  33. }) => {
  34. const defaultOptions = {
  35. iconSide: IconSide.left,
  36. };
  37. const props = { ...options, defaultOptions };
  38. const { title, onClick, buttonClassName, iconClassName, splitted, iconSide, disabled } = props;
  39. return (
  40. <button
  41. className={`btn navbar-button ${buttonClassName ? buttonClassName : ''}`}
  42. onClick={onClick}
  43. disabled={disabled || false}
  44. >
  45. {iconClassName && iconSide === IconSide.left ? <i className={`${iconClassName}`} /> : null}
  46. <span className="btn-title">{!splitted ? title : ''}</span>
  47. {iconClassName && iconSide === IconSide.right ? <i className={`${iconClassName}`} /> : null}
  48. </button>
  49. );
  50. };
  51. interface OwnProps {
  52. exploreId: ExploreId;
  53. onChangeTime: (range: RawTimeRange, changedByScanner?: boolean) => void;
  54. }
  55. interface StateProps {
  56. datasourceMissing: boolean;
  57. exploreDatasources: DataSourceSelectItem[];
  58. loading: boolean;
  59. range: TimeRange;
  60. timeZone: TimeZone;
  61. selectedDatasource: DataSourceSelectItem;
  62. splitted: boolean;
  63. refreshInterval: string;
  64. supportedModeOptions: Array<SelectableValue<ExploreMode>>;
  65. selectedModeOption: SelectableValue<ExploreMode>;
  66. hasLiveOption: boolean;
  67. isLive: boolean;
  68. }
  69. interface DispatchProps {
  70. changeDatasource: typeof changeDatasource;
  71. clearAll: typeof clearQueries;
  72. runQueries: typeof runQueries;
  73. closeSplit: typeof splitClose;
  74. split: typeof splitOpen;
  75. changeRefreshInterval: typeof changeRefreshInterval;
  76. changeMode: typeof changeMode;
  77. }
  78. type Props = StateProps & DispatchProps & OwnProps;
  79. export class UnConnectedExploreToolbar extends PureComponent<Props, {}> {
  80. constructor(props: Props) {
  81. super(props);
  82. }
  83. onChangeDatasource = async option => {
  84. this.props.changeDatasource(this.props.exploreId, option.value);
  85. };
  86. onClearAll = () => {
  87. this.props.clearAll(this.props.exploreId);
  88. };
  89. onRunQuery = () => {
  90. return this.props.runQueries(this.props.exploreId);
  91. };
  92. onChangeRefreshInterval = (item: string) => {
  93. const { changeRefreshInterval, exploreId } = this.props;
  94. changeRefreshInterval(exploreId, item);
  95. };
  96. onModeChange = (mode: ExploreMode) => {
  97. const { changeMode, exploreId } = this.props;
  98. changeMode(exploreId, mode);
  99. };
  100. render() {
  101. const {
  102. datasourceMissing,
  103. exploreDatasources,
  104. closeSplit,
  105. exploreId,
  106. loading,
  107. range,
  108. timeZone,
  109. selectedDatasource,
  110. splitted,
  111. refreshInterval,
  112. onChangeTime,
  113. split,
  114. supportedModeOptions,
  115. selectedModeOption,
  116. hasLiveOption,
  117. isLive,
  118. } = this.props;
  119. return (
  120. <div className={splitted ? 'explore-toolbar splitted' : 'explore-toolbar'}>
  121. <div className="explore-toolbar-item">
  122. <div className="explore-toolbar-header">
  123. <div className="explore-toolbar-header-title">
  124. {exploreId === 'left' && (
  125. <span className="navbar-page-btn">
  126. <i className="gicon gicon-explore" />
  127. Explore
  128. </span>
  129. )}
  130. </div>
  131. {splitted && (
  132. <a className="explore-toolbar-header-close" onClick={() => closeSplit(exploreId)}>
  133. <i className="fa fa-times fa-fw" />
  134. </a>
  135. )}
  136. </div>
  137. </div>
  138. <div className="explore-toolbar-item">
  139. <div className="explore-toolbar-content">
  140. {!datasourceMissing ? (
  141. <div className="explore-toolbar-content-item">
  142. <div className="datasource-picker">
  143. <DataSourcePicker
  144. onChange={this.onChangeDatasource}
  145. datasources={exploreDatasources}
  146. current={selectedDatasource}
  147. />
  148. </div>
  149. {supportedModeOptions.length > 1 ? (
  150. <div className="query-type-toggle">
  151. <ToggleButtonGroup label="" transparent={true}>
  152. <ToggleButton
  153. key={ExploreMode.Metrics}
  154. value={ExploreMode.Metrics}
  155. onChange={this.onModeChange}
  156. selected={selectedModeOption.value === ExploreMode.Metrics}
  157. >
  158. {'Metrics'}
  159. </ToggleButton>
  160. <ToggleButton
  161. key={ExploreMode.Logs}
  162. value={ExploreMode.Logs}
  163. onChange={this.onModeChange}
  164. selected={selectedModeOption.value === ExploreMode.Logs}
  165. >
  166. {'Logs'}
  167. </ToggleButton>
  168. </ToggleButtonGroup>
  169. </div>
  170. ) : null}
  171. </div>
  172. ) : null}
  173. {exploreId === 'left' && !splitted ? (
  174. <div className="explore-toolbar-content-item">
  175. {createResponsiveButton({
  176. splitted,
  177. title: 'Split',
  178. onClick: split,
  179. iconClassName: 'fa fa-fw fa-columns icon-margin-right',
  180. iconSide: IconSide.left,
  181. disabled: isLive,
  182. })}
  183. </div>
  184. ) : null}
  185. <div className="explore-toolbar-content-item">
  186. <ExploreTimeControls
  187. exploreId={exploreId}
  188. hasLiveOption={hasLiveOption}
  189. isLive={isLive}
  190. loading={loading}
  191. range={range}
  192. refreshInterval={refreshInterval}
  193. timeZone={timeZone}
  194. onChangeTime={onChangeTime}
  195. onChangeRefreshInterval={this.onChangeRefreshInterval}
  196. onRunQuery={this.onRunQuery}
  197. />
  198. </div>
  199. <div className="explore-toolbar-content-item">
  200. <button className="btn navbar-button" onClick={this.onClearAll}>
  201. Clear All
  202. </button>
  203. </div>
  204. <div className="explore-toolbar-content-item">
  205. {createResponsiveButton({
  206. splitted,
  207. title: 'Run Query',
  208. onClick: this.onRunQuery,
  209. buttonClassName: 'navbar-button--secondary',
  210. iconClassName:
  211. loading && !isLive ? 'fa fa-spinner fa-fw fa-spin run-icon' : 'fa fa-level-down fa-fw run-icon',
  212. iconSide: IconSide.right,
  213. })}
  214. </div>
  215. </div>
  216. </div>
  217. </div>
  218. );
  219. }
  220. }
  221. const mapStateToProps = (state: StoreState, { exploreId }: OwnProps): StateProps => {
  222. const splitted = state.explore.split;
  223. const exploreItem = state.explore[exploreId];
  224. const {
  225. datasourceInstance,
  226. datasourceMissing,
  227. exploreDatasources,
  228. range,
  229. refreshInterval,
  230. loadingState,
  231. supportedModes,
  232. mode,
  233. isLive,
  234. } = exploreItem;
  235. const selectedDatasource = datasourceInstance
  236. ? exploreDatasources.find(datasource => datasource.name === datasourceInstance.name)
  237. : undefined;
  238. const loading = loadingState === LoadingState.Loading || loadingState === LoadingState.Streaming;
  239. const hasLiveOption =
  240. datasourceInstance && datasourceInstance.meta && datasourceInstance.meta.streaming ? true : false;
  241. const supportedModeOptions: Array<SelectableValue<ExploreMode>> = [];
  242. let selectedModeOption = null;
  243. for (const supportedMode of supportedModes) {
  244. switch (supportedMode) {
  245. case ExploreMode.Metrics:
  246. const option1 = {
  247. value: ExploreMode.Metrics,
  248. label: ExploreMode.Metrics,
  249. };
  250. supportedModeOptions.push(option1);
  251. if (mode === ExploreMode.Metrics) {
  252. selectedModeOption = option1;
  253. }
  254. break;
  255. case ExploreMode.Logs:
  256. const option2 = {
  257. value: ExploreMode.Logs,
  258. label: ExploreMode.Logs,
  259. };
  260. supportedModeOptions.push(option2);
  261. if (mode === ExploreMode.Logs) {
  262. selectedModeOption = option2;
  263. }
  264. break;
  265. }
  266. }
  267. return {
  268. datasourceMissing,
  269. exploreDatasources,
  270. loading,
  271. range,
  272. timeZone: getTimeZone(state.user),
  273. selectedDatasource,
  274. splitted,
  275. refreshInterval,
  276. supportedModeOptions,
  277. selectedModeOption,
  278. hasLiveOption,
  279. isLive,
  280. };
  281. };
  282. const mapDispatchToProps: DispatchProps = {
  283. changeDatasource,
  284. changeRefreshInterval,
  285. clearAll: clearQueries,
  286. runQueries,
  287. closeSplit: splitClose,
  288. split: splitOpen,
  289. changeMode: changeMode,
  290. };
  291. export const ExploreToolbar = hot(module)(
  292. connect(
  293. mapStateToProps,
  294. mapDispatchToProps
  295. )(UnConnectedExploreToolbar)
  296. );