import React, { PureComponent } from 'react'; import { connect } from 'react-redux'; import { hot } from 'react-hot-loader'; import memoizeOne from 'memoize-one'; import classNames from 'classnames'; import { css } from 'emotion'; import { ExploreId, ExploreItemState } from 'app/types/explore'; import { ToggleButtonGroup, ToggleButton, Tooltip, LegacyForms, SetInterval, Icon, IconButton } from '@grafana/ui'; const { ButtonSelect } = LegacyForms; import { RawTimeRange, TimeZone, TimeRange, DataQuery, ExploreMode } from '@grafana/data'; import { DataSourcePicker } from 'app/core/components/Select/DataSourcePicker'; import { StoreState } from 'app/types/store'; import { changeDatasource, cancelQueries, clearQueries, splitClose, runQueries, splitOpen, syncTimes, changeRefreshInterval, changeMode, } from './state/actions'; import { updateLocation } from 'app/core/actions'; import { getTimeZone } from '../profile/state/selectors'; import { getDashboardSrv } from '../dashboard/services/DashboardSrv'; import kbn from '../../core/utils/kbn'; import { ExploreTimeControls } from './ExploreTimeControls'; import { LiveTailButton } from './LiveTailButton'; import { ResponsiveButton } from './ResponsiveButton'; import { RunButton } from './RunButton'; import { LiveTailControls } from './useLiveTailControls'; import { getExploreDatasources } from './state/selectors'; import { setDashboardQueriesToUpdateOnLoad } from '../dashboard/state/reducers'; const getStyles = memoizeOne(() => { return { liveTailButtons: css` margin-left: 10px; @media (max-width: 1110px) { margin-left: 4px; } `, }; }); interface OwnProps { exploreId: ExploreId; onChangeTime: (range: RawTimeRange, changedByScanner?: boolean) => void; } interface StateProps { datasourceMissing: boolean; loading: boolean; range: TimeRange; timeZone: TimeZone; splitted: boolean; syncedTimes: boolean; refreshInterval?: string; supportedModes: ExploreMode[]; selectedMode: ExploreMode; hasLiveOption: boolean; isLive: boolean; isPaused: boolean; originPanelId?: number; queries: DataQuery[]; datasourceLoading?: boolean; containerWidth: number; datasourceName?: string; } interface DispatchProps { changeDatasource: typeof changeDatasource; clearAll: typeof clearQueries; cancelQueries: typeof cancelQueries; runQueries: typeof runQueries; closeSplit: typeof splitClose; split: typeof splitOpen; syncTimes: typeof syncTimes; changeRefreshInterval: typeof changeRefreshInterval; changeMode: typeof changeMode; updateLocation: typeof updateLocation; setDashboardQueriesToUpdateOnLoad: typeof setDashboardQueriesToUpdateOnLoad; } type Props = StateProps & DispatchProps & OwnProps; export class UnConnectedExploreToolbar extends PureComponent { onChangeDatasource = async (option: { value: any }) => { this.props.changeDatasource(this.props.exploreId, option.value); }; onClearAll = () => { this.props.clearAll(this.props.exploreId); }; onRunQuery = (loading = false) => { if (loading) { return this.props.cancelQueries(this.props.exploreId); } else { return this.props.runQueries(this.props.exploreId); } }; onChangeRefreshInterval = (item: string) => { const { changeRefreshInterval, exploreId } = this.props; changeRefreshInterval(exploreId, item); }; onModeChange = (mode: ExploreMode) => { const { changeMode, exploreId } = this.props; changeMode(exploreId, mode); }; onChangeTimeSync = () => { const { syncTimes, exploreId } = this.props; syncTimes(exploreId); }; returnToPanel = async ({ withChanges = false } = {}) => { const { originPanelId, queries } = this.props; const dashboardSrv = getDashboardSrv(); const dash = dashboardSrv.getCurrent(); const titleSlug = kbn.slugifyForUrl(dash.title); if (withChanges) { this.props.setDashboardQueriesToUpdateOnLoad({ panelId: originPanelId, queries: this.cleanQueries(queries), }); } const query: any = {}; if (withChanges || dash.panelInEdit) { query.editPanel = originPanelId; } else if (dash.panelInView) { query.viewPanel = originPanelId; } this.props.updateLocation({ path: `/d/${dash.uid}/:${titleSlug}`, query }); }; // Remove explore specific parameters from queries private cleanQueries(queries: DataQuery[]) { return queries.map((query: DataQuery & { context?: string }) => { delete query.context; delete query.key; return query; }); } getSelectedDatasource = () => { const { datasourceName } = this.props; const exploreDatasources = getExploreDatasources(); return datasourceName ? exploreDatasources.find(datasource => datasource.name === datasourceName) : undefined; }; render() { const { datasourceMissing, closeSplit, exploreId, loading, range, timeZone, splitted, syncedTimes, refreshInterval, onChangeTime, split, supportedModes, selectedMode, hasLiveOption, isLive, isPaused, originPanelId, datasourceLoading, containerWidth, } = this.props; const styles = getStyles(); const originDashboardIsEditable = originPanelId && Number.isInteger(originPanelId); const panelReturnClasses = classNames('btn', 'navbar-button', { 'btn--radius-right-0': originDashboardIsEditable, 'navbar-button navbar-button--border-right-0': originDashboardIsEditable, }); const showSmallDataSourcePicker = (splitted ? containerWidth < 700 : containerWidth < 800) || false; const showSmallTimePicker = splitted || containerWidth < 1210; return (
{exploreId === 'left' && ( Explore )}
{splitted && ( closeSplit(exploreId)} name="times" /> )}
{!datasourceMissing ? (
{supportedModes.length > 1 ? (
{'Metrics'} {'Logs'}
) : null}
) : null} {originPanelId && Number.isInteger(originPanelId) && !splitted && (
{originDashboardIsEditable && ( this.returnToPanel({ withChanges: true })} maxMenuHeight={380} /> )}
)} {exploreId === 'left' && !splitted ? (
) : null} {!isLive && (
)} {!isLive && (
)}
{refreshInterval && }
{hasLiveOption && (
{controls => ( )}
)}
); } } const mapStateToProps = (state: StoreState, { exploreId }: OwnProps): StateProps => { const splitted = state.explore.split; const syncedTimes = state.explore.syncedTimes; const exploreItem: ExploreItemState = state.explore[exploreId]; const { datasourceInstance, datasourceMissing, range, refreshInterval, loading, supportedModes, mode, isLive, isPaused, originPanelId, queries, datasourceLoading, containerWidth, } = exploreItem; const hasLiveOption = !!(datasourceInstance?.meta?.streaming && mode === ExploreMode.Logs); return { datasourceMissing, datasourceName: datasourceInstance?.name, loading, range, timeZone: getTimeZone(state.user), splitted, refreshInterval, supportedModes, selectedMode: supportedModes.includes(mode) ? mode : supportedModes[0], hasLiveOption, isLive, isPaused, originPanelId, queries, syncedTimes, datasourceLoading, containerWidth, }; }; const mapDispatchToProps: DispatchProps = { changeDatasource, updateLocation, changeRefreshInterval, clearAll: clearQueries, cancelQueries, runQueries, closeSplit: splitClose, split: splitOpen, syncTimes, changeMode: changeMode, setDashboardQueriesToUpdateOnLoad, }; export const ExploreToolbar = hot(module)(connect(mapStateToProps, mapDispatchToProps)(UnConnectedExploreToolbar));