2019-04-16 15:15:23 +08:00
|
|
|
// Libaries
|
|
|
|
import React, { Component } from 'react';
|
2019-12-20 22:31:58 +08:00
|
|
|
import { dateMath, GrafanaTheme } from '@grafana/data';
|
|
|
|
import { css } from 'emotion';
|
2019-04-16 15:15:23 +08:00
|
|
|
|
|
|
|
// Types
|
|
|
|
import { DashboardModel } from '../../state';
|
2019-10-14 16:27:47 +08:00
|
|
|
import { LocationState, CoreEvents } from 'app/types';
|
2019-12-20 22:31:58 +08:00
|
|
|
import { TimeRange } from '@grafana/data';
|
2019-04-16 15:15:23 +08:00
|
|
|
|
|
|
|
// State
|
|
|
|
import { updateLocation } from 'app/core/actions';
|
|
|
|
|
|
|
|
// Components
|
2019-12-20 22:31:58 +08:00
|
|
|
import { RefreshPicker, withTheme, stylesFactory, Themeable } from '@grafana/ui';
|
|
|
|
import { TimePickerWithHistory } from 'app/core/components/TimePicker/TimePickerWithHistory';
|
2019-04-16 15:15:23 +08:00
|
|
|
|
|
|
|
// Utils & Services
|
2020-02-09 21:36:49 +08:00
|
|
|
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
2020-02-28 21:32:01 +08:00
|
|
|
import { defaultIntervals } from '@grafana/ui/src/components/RefreshPicker/RefreshPicker';
|
2020-02-09 21:36:49 +08:00
|
|
|
import { appEvents } from 'app/core/core';
|
2019-04-16 15:15:23 +08:00
|
|
|
|
2019-12-20 22:31:58 +08:00
|
|
|
const getStyles = stylesFactory((theme: GrafanaTheme) => {
|
|
|
|
return {
|
|
|
|
container: css`
|
|
|
|
position: relative;
|
|
|
|
display: flex;
|
|
|
|
`,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
export interface Props extends Themeable {
|
2019-04-16 15:15:23 +08:00
|
|
|
dashboard: DashboardModel;
|
|
|
|
updateLocation: typeof updateLocation;
|
|
|
|
location: LocationState;
|
|
|
|
}
|
2019-12-20 22:31:58 +08:00
|
|
|
class UnthemedDashNavTimeControls extends Component<Props> {
|
2019-11-20 04:19:10 +08:00
|
|
|
componentDidMount() {
|
|
|
|
// Only reason for this is that sometimes time updates can happen via redux location changes
|
|
|
|
// and this happens before timeSrv has had chance to update state (as it listens to angular route-updated)
|
|
|
|
// This can be removed after timeSrv listens redux location
|
|
|
|
this.props.dashboard.on(CoreEvents.timeRangeUpdated, this.triggerForceUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.props.dashboard.off(CoreEvents.timeRangeUpdated, this.triggerForceUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
triggerForceUpdate = () => {
|
|
|
|
this.forceUpdate();
|
|
|
|
};
|
|
|
|
|
2019-04-16 15:15:23 +08:00
|
|
|
get refreshParamInUrl(): string {
|
|
|
|
return this.props.location.query.refresh as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
onChangeRefreshInterval = (interval: string) => {
|
2020-02-09 21:36:49 +08:00
|
|
|
getTimeSrv().setAutoRefresh(interval);
|
2019-04-16 15:15:23 +08:00
|
|
|
this.forceUpdate();
|
|
|
|
};
|
|
|
|
|
|
|
|
onRefresh = () => {
|
2020-02-09 21:36:49 +08:00
|
|
|
getTimeSrv().refreshDashboard();
|
2019-04-16 15:15:23 +08:00
|
|
|
return Promise.resolve();
|
|
|
|
};
|
|
|
|
|
2019-08-01 16:17:27 +08:00
|
|
|
onMoveBack = () => {
|
2020-02-09 21:36:49 +08:00
|
|
|
appEvents.emit(CoreEvents.shiftTime, -1);
|
2019-08-01 16:17:27 +08:00
|
|
|
};
|
2020-02-11 21:57:16 +08:00
|
|
|
|
2019-08-01 16:17:27 +08:00
|
|
|
onMoveForward = () => {
|
2020-02-09 21:36:49 +08:00
|
|
|
appEvents.emit(CoreEvents.shiftTime, 1);
|
2019-06-24 20:39:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
onChangeTimePicker = (timeRange: TimeRange) => {
|
|
|
|
const { dashboard } = this.props;
|
|
|
|
const panel = dashboard.timepicker;
|
|
|
|
const hasDelay = panel.nowDelay && timeRange.raw.to === 'now';
|
|
|
|
|
2019-07-24 21:09:52 +08:00
|
|
|
const adjustedFrom = dateMath.isMathString(timeRange.raw.from) ? timeRange.raw.from : timeRange.from;
|
|
|
|
const adjustedTo = dateMath.isMathString(timeRange.raw.to) ? timeRange.raw.to : timeRange.to;
|
2019-06-24 20:39:59 +08:00
|
|
|
const nextRange = {
|
2019-07-24 21:09:52 +08:00
|
|
|
from: adjustedFrom,
|
|
|
|
to: hasDelay ? 'now-' + panel.nowDelay : adjustedTo,
|
2019-06-24 20:39:59 +08:00
|
|
|
};
|
|
|
|
|
2020-02-09 21:36:49 +08:00
|
|
|
getTimeSrv().setTime(nextRange);
|
2019-06-24 20:39:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
onZoom = () => {
|
2020-02-09 21:36:49 +08:00
|
|
|
appEvents.emit(CoreEvents.zoomOut, 2);
|
2019-06-24 20:39:59 +08:00
|
|
|
};
|
|
|
|
|
2019-04-16 15:15:23 +08:00
|
|
|
render() {
|
2019-12-20 22:31:58 +08:00
|
|
|
const { dashboard, theme } = this.props;
|
2020-02-28 21:32:01 +08:00
|
|
|
const { refresh_intervals } = dashboard.timepicker;
|
|
|
|
const intervals = getTimeSrv().getValidIntervals(refresh_intervals || defaultIntervals);
|
|
|
|
|
2020-02-09 21:36:49 +08:00
|
|
|
const timePickerValue = getTimeSrv().timeRange();
|
2019-06-24 20:39:59 +08:00
|
|
|
const timeZone = dashboard.getTimezone();
|
2019-12-20 22:31:58 +08:00
|
|
|
const styles = getStyles(theme);
|
2019-06-24 20:39:59 +08:00
|
|
|
|
2019-04-16 15:15:23 +08:00
|
|
|
return (
|
2019-12-20 22:31:58 +08:00
|
|
|
<div className={styles.container}>
|
|
|
|
<TimePickerWithHistory
|
2019-06-24 20:39:59 +08:00
|
|
|
value={timePickerValue}
|
|
|
|
onChange={this.onChangeTimePicker}
|
|
|
|
timeZone={timeZone}
|
|
|
|
onMoveBackward={this.onMoveBack}
|
|
|
|
onMoveForward={this.onMoveForward}
|
|
|
|
onZoom={this.onZoom}
|
|
|
|
/>
|
|
|
|
<RefreshPicker
|
|
|
|
onIntervalChanged={this.onChangeRefreshInterval}
|
|
|
|
onRefresh={this.onRefresh}
|
|
|
|
value={dashboard.refresh}
|
|
|
|
intervals={intervals}
|
|
|
|
tooltip="Refresh dashboard"
|
|
|
|
/>
|
2019-07-01 16:16:33 +08:00
|
|
|
</div>
|
2019-04-16 15:15:23 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-12-20 22:31:58 +08:00
|
|
|
|
|
|
|
export const DashNavTimeControls = withTheme(UnthemedDashNavTimeControls);
|