| 
									
										
										
										
											2019-06-28 18:07:55 +08:00
										 |  |  | import React, { Component } from 'react'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-27 15:55:22 +08:00
										 |  |  | import { TimeRange, TimeZone, RawTimeRange, dateTimeForTimeZone, dateMath } from '@grafana/data'; | 
					
						
							| 
									
										
										
										
											2022-09-20 18:50:23 +08:00
										 |  |  | import { reportInteraction } from '@grafana/runtime'; | 
					
						
							| 
									
										
										
										
											2019-12-20 22:31:58 +08:00
										 |  |  | import { TimePickerWithHistory } from 'app/core/components/TimePicker/TimePickerWithHistory'; | 
					
						
							| 
									
										
										
										
											2019-06-28 18:07:55 +08:00
										 |  |  | import { getShiftedTimeRange, getZoomedTimeRange } from 'app/core/utils/timePicker'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { TimeSyncButton } from './TimeSyncButton'; | 
					
						
							| 
									
										
										
										
											2019-06-28 18:07:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export interface Props { | 
					
						
							| 
									
										
										
										
											2023-06-21 17:06:28 +08:00
										 |  |  |   exploreId: string; | 
					
						
							| 
									
										
										
										
											2019-11-07 03:19:50 +08:00
										 |  |  |   hideText?: boolean; | 
					
						
							| 
									
										
										
										
											2019-06-28 18:07:55 +08:00
										 |  |  |   range: TimeRange; | 
					
						
							|  |  |  |   timeZone: TimeZone; | 
					
						
							| 
									
										
										
										
											2021-09-30 15:40:02 +08:00
										 |  |  |   fiscalYearStartMonth: number; | 
					
						
							| 
									
										
										
										
											2019-10-09 00:55:53 +08:00
										 |  |  |   splitted: boolean; | 
					
						
							|  |  |  |   syncedTimes: boolean; | 
					
						
							|  |  |  |   onChangeTimeSync: () => void; | 
					
						
							| 
									
										
										
										
											2019-06-28 18:07:55 +08:00
										 |  |  |   onChangeTime: (range: RawTimeRange) => void; | 
					
						
							| 
									
										
										
										
											2020-06-26 15:08:15 +08:00
										 |  |  |   onChangeTimeZone: (timeZone: TimeZone) => void; | 
					
						
							| 
									
										
										
										
											2021-09-30 15:40:02 +08:00
										 |  |  |   onChangeFiscalYearStartMonth: (fiscalYearStartMonth: number) => void; | 
					
						
							| 
									
										
										
										
											2019-06-28 18:07:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class ExploreTimeControls extends Component<Props> { | 
					
						
							|  |  |  |   onMoveTimePicker = (direction: number) => { | 
					
						
							|  |  |  |     const { range, onChangeTime, timeZone } = this.props; | 
					
						
							|  |  |  |     const { from, to } = getShiftedTimeRange(direction, range); | 
					
						
							|  |  |  |     const nextTimeRange = { | 
					
						
							| 
									
										
										
										
											2019-08-06 16:43:53 +08:00
										 |  |  |       from: dateTimeForTimeZone(timeZone, from), | 
					
						
							|  |  |  |       to: dateTimeForTimeZone(timeZone, to), | 
					
						
							| 
									
										
										
										
											2019-06-28 18:07:55 +08:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     onChangeTime(nextTimeRange); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   onMoveForward = () => this.onMoveTimePicker(1); | 
					
						
							|  |  |  |   onMoveBack = () => this.onMoveTimePicker(-1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   onChangeTimePicker = (timeRange: TimeRange) => { | 
					
						
							| 
									
										
										
										
											2021-04-27 15:55:22 +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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     this.props.onChangeTime({ | 
					
						
							|  |  |  |       from: adjustedFrom, | 
					
						
							|  |  |  |       to: adjustedTo, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2022-09-20 18:50:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     reportInteraction('grafana_explore_time_picker_time_range_changed', { | 
					
						
							|  |  |  |       timeRangeFrom: adjustedFrom, | 
					
						
							|  |  |  |       timeRangeTo: adjustedTo, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-06-28 18:07:55 +08:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   onZoom = () => { | 
					
						
							|  |  |  |     const { range, onChangeTime, timeZone } = this.props; | 
					
						
							|  |  |  |     const { from, to } = getZoomedTimeRange(range, 2); | 
					
						
							|  |  |  |     const nextTimeRange = { | 
					
						
							| 
									
										
										
										
											2019-08-06 16:43:53 +08:00
										 |  |  |       from: dateTimeForTimeZone(timeZone, from), | 
					
						
							|  |  |  |       to: dateTimeForTimeZone(timeZone, to), | 
					
						
							| 
									
										
										
										
											2019-06-28 18:07:55 +08:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     onChangeTime(nextTimeRange); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   render() { | 
					
						
							| 
									
										
										
										
											2021-09-30 15:40:02 +08:00
										 |  |  |     const { | 
					
						
							|  |  |  |       range, | 
					
						
							|  |  |  |       timeZone, | 
					
						
							|  |  |  |       fiscalYearStartMonth, | 
					
						
							|  |  |  |       splitted, | 
					
						
							|  |  |  |       syncedTimes, | 
					
						
							|  |  |  |       onChangeTimeSync, | 
					
						
							|  |  |  |       hideText, | 
					
						
							|  |  |  |       onChangeTimeZone, | 
					
						
							|  |  |  |       onChangeFiscalYearStartMonth, | 
					
						
							|  |  |  |     } = this.props; | 
					
						
							| 
									
										
										
										
											2019-11-26 17:01:32 +08:00
										 |  |  |     const timeSyncButton = splitted ? <TimeSyncButton onClick={onChangeTimeSync} isSynced={syncedTimes} /> : undefined; | 
					
						
							| 
									
										
										
										
											2019-10-09 00:55:53 +08:00
										 |  |  |     const timePickerCommonProps = { | 
					
						
							|  |  |  |       value: range, | 
					
						
							|  |  |  |       timeZone, | 
					
						
							| 
									
										
										
										
											2021-09-30 15:40:02 +08:00
										 |  |  |       fiscalYearStartMonth, | 
					
						
							| 
									
										
										
										
											2019-10-09 00:55:53 +08:00
										 |  |  |       onMoveBackward: this.onMoveBack, | 
					
						
							|  |  |  |       onMoveForward: this.onMoveForward, | 
					
						
							|  |  |  |       onZoom: this.onZoom, | 
					
						
							| 
									
										
										
										
											2019-11-07 03:19:50 +08:00
										 |  |  |       hideText, | 
					
						
							| 
									
										
										
										
											2019-10-09 00:55:53 +08:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-06-28 18:07:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-20 22:31:58 +08:00
										 |  |  |     return ( | 
					
						
							|  |  |  |       <TimePickerWithHistory | 
					
						
							| 
									
										
										
										
											2023-01-06 06:26:58 +08:00
										 |  |  |         isOnCanvas | 
					
						
							| 
									
										
										
										
											2019-12-20 22:31:58 +08:00
										 |  |  |         {...timePickerCommonProps} | 
					
						
							|  |  |  |         timeSyncButton={timeSyncButton} | 
					
						
							|  |  |  |         isSynced={syncedTimes} | 
					
						
							| 
									
										
										
										
											2022-04-22 01:49:52 +08:00
										 |  |  |         widthOverride={splitted ? window.innerWidth / 2 : undefined} | 
					
						
							| 
									
										
										
										
											2019-12-20 22:31:58 +08:00
										 |  |  |         onChange={this.onChangeTimePicker} | 
					
						
							| 
									
										
										
										
											2020-06-26 15:08:15 +08:00
										 |  |  |         onChangeTimeZone={onChangeTimeZone} | 
					
						
							| 
									
										
										
										
											2021-09-30 15:40:02 +08:00
										 |  |  |         onChangeFiscalYearStartMonth={onChangeFiscalYearStartMonth} | 
					
						
							| 
									
										
										
										
											2019-12-20 22:31:58 +08:00
										 |  |  |       /> | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2019-06-28 18:07:55 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } |