DashNavTimeControls.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Libaries
  2. import React, { Component } from 'react';
  3. import { dateMath } from '@grafana/data';
  4. // Types
  5. import { DashboardModel } from '../../state';
  6. import { LocationState } from 'app/types';
  7. import { TimeRange, TimeOption, RawTimeRange } from '@grafana/data';
  8. // State
  9. import { updateLocation } from 'app/core/actions';
  10. // Components
  11. import { TimePicker, RefreshPicker } from '@grafana/ui';
  12. // Utils & Services
  13. import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
  14. import { defaultSelectOptions } from '@grafana/ui/src/components/TimePicker/TimePicker';
  15. export interface Props {
  16. $injector: any;
  17. dashboard: DashboardModel;
  18. updateLocation: typeof updateLocation;
  19. location: LocationState;
  20. }
  21. export class DashNavTimeControls extends Component<Props> {
  22. timeSrv: TimeSrv = getTimeSrv();
  23. $rootScope = this.props.$injector.get('$rootScope');
  24. get refreshParamInUrl(): string {
  25. return this.props.location.query.refresh as string;
  26. }
  27. onChangeRefreshInterval = (interval: string) => {
  28. this.timeSrv.setAutoRefresh(interval);
  29. this.forceUpdate();
  30. };
  31. onRefresh = () => {
  32. this.timeSrv.refreshDashboard();
  33. return Promise.resolve();
  34. };
  35. onMoveBack = () => {
  36. this.$rootScope.appEvent('shift-time', -1);
  37. };
  38. onMoveForward = () => {
  39. this.$rootScope.appEvent('shift-time', 1);
  40. };
  41. onChangeTimePicker = (timeRange: TimeRange) => {
  42. const { dashboard } = this.props;
  43. const panel = dashboard.timepicker;
  44. const hasDelay = panel.nowDelay && timeRange.raw.to === 'now';
  45. const adjustedFrom = dateMath.isMathString(timeRange.raw.from) ? timeRange.raw.from : timeRange.from;
  46. const adjustedTo = dateMath.isMathString(timeRange.raw.to) ? timeRange.raw.to : timeRange.to;
  47. const nextRange = {
  48. from: adjustedFrom,
  49. to: hasDelay ? 'now-' + panel.nowDelay : adjustedTo,
  50. };
  51. this.timeSrv.setTime(nextRange);
  52. };
  53. onZoom = () => {
  54. this.$rootScope.appEvent('zoom-out', 2);
  55. };
  56. setActiveTimeOption = (timeOptions: TimeOption[], rawTimeRange: RawTimeRange): TimeOption[] => {
  57. return timeOptions.map(option => {
  58. if (option.to === rawTimeRange.to && option.from === rawTimeRange.from) {
  59. return {
  60. ...option,
  61. active: true,
  62. };
  63. }
  64. return {
  65. ...option,
  66. active: false,
  67. };
  68. });
  69. };
  70. render() {
  71. const { dashboard } = this.props;
  72. const intervals = dashboard.timepicker.refresh_intervals;
  73. const timePickerValue = this.timeSrv.timeRange();
  74. const timeZone = dashboard.getTimezone();
  75. return (
  76. <div className="dashboard-timepicker-wrapper">
  77. <TimePicker
  78. value={timePickerValue}
  79. onChange={this.onChangeTimePicker}
  80. timeZone={timeZone}
  81. onMoveBackward={this.onMoveBack}
  82. onMoveForward={this.onMoveForward}
  83. onZoom={this.onZoom}
  84. selectOptions={this.setActiveTimeOption(defaultSelectOptions, timePickerValue.raw)}
  85. />
  86. <RefreshPicker
  87. onIntervalChanged={this.onChangeRefreshInterval}
  88. onRefresh={this.onRefresh}
  89. value={dashboard.refresh}
  90. intervals={intervals}
  91. tooltip="Refresh dashboard"
  92. />
  93. </div>
  94. );
  95. }
  96. }