2019-04-16 15:15:23 +08:00
|
|
|
// Libaries
|
|
|
|
import React, { Component } from 'react';
|
2019-08-01 16:17:27 +08:00
|
|
|
import { dateMath } from '@grafana/data';
|
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-07-06 14:05:53 +08:00
|
|
|
import { TimeRange, TimeOption, RawTimeRange } from '@grafana/data';
|
2019-04-16 15:15:23 +08:00
|
|
|
|
|
|
|
// State
|
|
|
|
import { updateLocation } from 'app/core/actions';
|
|
|
|
|
|
|
|
// Components
|
2019-07-06 14:05:53 +08:00
|
|
|
import { TimePicker, RefreshPicker } from '@grafana/ui';
|
2019-04-16 15:15:23 +08:00
|
|
|
|
|
|
|
// Utils & Services
|
|
|
|
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
2019-06-24 20:39:59 +08:00
|
|
|
import { defaultSelectOptions } from '@grafana/ui/src/components/TimePicker/TimePicker';
|
2019-04-16 15:15:23 +08:00
|
|
|
|
|
|
|
export interface Props {
|
2019-06-24 20:39:59 +08:00
|
|
|
$injector: any;
|
2019-04-16 15:15:23 +08:00
|
|
|
dashboard: DashboardModel;
|
|
|
|
updateLocation: typeof updateLocation;
|
|
|
|
location: LocationState;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DashNavTimeControls extends Component<Props> {
|
|
|
|
timeSrv: TimeSrv = getTimeSrv();
|
2019-06-24 20:39:59 +08:00
|
|
|
$rootScope = this.props.$injector.get('$rootScope');
|
2019-04-16 15:15:23 +08:00
|
|
|
|
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) => {
|
|
|
|
this.timeSrv.setAutoRefresh(interval);
|
|
|
|
this.forceUpdate();
|
|
|
|
};
|
|
|
|
|
|
|
|
onRefresh = () => {
|
|
|
|
this.timeSrv.refreshDashboard();
|
|
|
|
return Promise.resolve();
|
|
|
|
};
|
|
|
|
|
2019-08-01 16:17:27 +08:00
|
|
|
onMoveBack = () => {
|
2019-10-14 16:27:47 +08:00
|
|
|
this.$rootScope.appEvent(CoreEvents.shiftTime, -1);
|
2019-08-01 16:17:27 +08:00
|
|
|
};
|
|
|
|
onMoveForward = () => {
|
2019-10-14 16:27:47 +08:00
|
|
|
this.$rootScope.appEvent(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
|
|
|
};
|
|
|
|
|
|
|
|
this.timeSrv.setTime(nextRange);
|
|
|
|
};
|
|
|
|
|
|
|
|
onZoom = () => {
|
2019-10-14 16:27:47 +08:00
|
|
|
this.$rootScope.appEvent(CoreEvents.zoomOut, 2);
|
2019-06-24 20:39:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
setActiveTimeOption = (timeOptions: TimeOption[], rawTimeRange: RawTimeRange): TimeOption[] => {
|
|
|
|
return timeOptions.map(option => {
|
|
|
|
if (option.to === rawTimeRange.to && option.from === rawTimeRange.from) {
|
|
|
|
return {
|
|
|
|
...option,
|
|
|
|
active: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...option,
|
|
|
|
active: false,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-04-16 15:15:23 +08:00
|
|
|
render() {
|
|
|
|
const { dashboard } = this.props;
|
|
|
|
const intervals = dashboard.timepicker.refresh_intervals;
|
2019-06-24 20:39:59 +08:00
|
|
|
const timePickerValue = this.timeSrv.timeRange();
|
|
|
|
const timeZone = dashboard.getTimezone();
|
|
|
|
|
2019-04-16 15:15:23 +08:00
|
|
|
return (
|
2019-07-01 16:16:33 +08:00
|
|
|
<div className="dashboard-timepicker-wrapper">
|
2019-06-24 20:39:59 +08:00
|
|
|
<TimePicker
|
|
|
|
value={timePickerValue}
|
|
|
|
onChange={this.onChangeTimePicker}
|
|
|
|
timeZone={timeZone}
|
|
|
|
onMoveBackward={this.onMoveBack}
|
|
|
|
onMoveForward={this.onMoveForward}
|
|
|
|
onZoom={this.onZoom}
|
|
|
|
selectOptions={this.setActiveTimeOption(defaultSelectOptions, timePickerValue.raw)}
|
|
|
|
/>
|
|
|
|
<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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|