DashNavTimeControls.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Libaries
  2. import React, { Component } from 'react';
  3. import { toUtc } from '@grafana/ui/src/utils/moment_wrapper';
  4. // Types
  5. import { DashboardModel } from '../../state';
  6. import { LocationState } from 'app/types';
  7. import { TimeRange, TimeOption } from '@grafana/ui';
  8. // State
  9. import { updateLocation } from 'app/core/actions';
  10. // Components
  11. import { TimePicker, RefreshPicker, RawTimeRange } 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. onMoveTimePicker = (direction: number) => {
  36. const range = this.timeSrv.timeRange();
  37. const timespan = (range.to.valueOf() - range.from.valueOf()) / 2;
  38. let to: number, from: number;
  39. if (direction === -1) {
  40. to = range.to.valueOf() - timespan;
  41. from = range.from.valueOf() - timespan;
  42. } else if (direction === 1) {
  43. to = range.to.valueOf() + timespan;
  44. from = range.from.valueOf() + timespan;
  45. if (to > Date.now() && range.to.valueOf() < Date.now()) {
  46. to = Date.now();
  47. from = range.from.valueOf();
  48. }
  49. } else {
  50. to = range.to.valueOf();
  51. from = range.from.valueOf();
  52. }
  53. this.timeSrv.setTime({
  54. from: toUtc(from),
  55. to: toUtc(to),
  56. });
  57. };
  58. onMoveForward = () => this.onMoveTimePicker(1);
  59. onMoveBack = () => this.onMoveTimePicker(-1);
  60. onChangeTimePicker = (timeRange: TimeRange) => {
  61. const { dashboard } = this.props;
  62. const panel = dashboard.timepicker;
  63. const hasDelay = panel.nowDelay && timeRange.raw.to === 'now';
  64. const nextRange = {
  65. from: timeRange.raw.from,
  66. to: hasDelay ? 'now-' + panel.nowDelay : timeRange.raw.to,
  67. };
  68. this.timeSrv.setTime(nextRange);
  69. };
  70. onZoom = () => {
  71. this.$rootScope.appEvent('zoom-out', 2);
  72. };
  73. setActiveTimeOption = (timeOptions: TimeOption[], rawTimeRange: RawTimeRange): TimeOption[] => {
  74. return timeOptions.map(option => {
  75. if (option.to === rawTimeRange.to && option.from === rawTimeRange.from) {
  76. return {
  77. ...option,
  78. active: true,
  79. };
  80. }
  81. return {
  82. ...option,
  83. active: false,
  84. };
  85. });
  86. };
  87. render() {
  88. const { dashboard } = this.props;
  89. const intervals = dashboard.timepicker.refresh_intervals;
  90. const timePickerValue = this.timeSrv.timeRange();
  91. const timeZone = dashboard.getTimezone();
  92. return (
  93. <>
  94. <TimePicker
  95. value={timePickerValue}
  96. onChange={this.onChangeTimePicker}
  97. timeZone={timeZone}
  98. onMoveBackward={this.onMoveBack}
  99. onMoveForward={this.onMoveForward}
  100. onZoom={this.onZoom}
  101. selectOptions={this.setActiveTimeOption(defaultSelectOptions, timePickerValue.raw)}
  102. />
  103. <RefreshPicker
  104. onIntervalChanged={this.onChangeRefreshInterval}
  105. onRefresh={this.onRefresh}
  106. value={dashboard.refresh}
  107. intervals={intervals}
  108. tooltip="Refresh dashboard"
  109. />
  110. </>
  111. );
  112. }
  113. }