-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Infra UI] Add time controls and live streaming to waffle map ui (#22637
) This adds a datepicker and an accompanying auto-refresh toggle button to the toolbar of the waffle map.
- Loading branch information
1 parent
491d4cc
commit 3773fe2
Showing
16 changed files
with
328 additions
and
12 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
x-pack/plugins/infra/public/components/waffle/waffle_time_controls.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { EuiButtonEmpty, EuiDatePicker, EuiFormControlLayout } from '@elastic/eui'; | ||
import moment, { Moment } from 'moment'; | ||
import React from 'react'; | ||
|
||
interface WaffleTimeControlsProps { | ||
currentTime: number; | ||
isLiveStreaming?: boolean; | ||
onChangeTime?: (time: number) => void; | ||
startLiveStreaming?: () => void; | ||
stopLiveStreaming?: () => void; | ||
} | ||
|
||
export class WaffleTimeControls extends React.Component<WaffleTimeControlsProps> { | ||
public render() { | ||
const { currentTime, isLiveStreaming } = this.props; | ||
|
||
const currentMoment = moment(currentTime); | ||
|
||
const liveStreamingButton = isLiveStreaming ? ( | ||
<EuiButtonEmpty | ||
color="primary" | ||
iconSide="left" | ||
iconType="pause" | ||
onClick={this.stopLiveStreaming} | ||
> | ||
Stop refreshing | ||
</EuiButtonEmpty> | ||
) : ( | ||
<EuiButtonEmpty iconSide="left" iconType="play" onClick={this.startLiveStreaming}> | ||
Auto-refresh | ||
</EuiButtonEmpty> | ||
); | ||
|
||
return ( | ||
<EuiFormControlLayout append={liveStreamingButton}> | ||
<EuiDatePicker | ||
className="euiFieldText--inGroup" | ||
dateFormat="L LTS" | ||
disabled={isLiveStreaming} | ||
injectTimes={currentMoment ? [currentMoment] : []} | ||
isLoading={isLiveStreaming} | ||
onChange={this.handleChangeDate} | ||
popperPlacement="top-end" | ||
selected={currentMoment} | ||
shouldCloseOnSelect | ||
showTimeSelect | ||
timeFormat="LT" | ||
/> | ||
</EuiFormControlLayout> | ||
); | ||
} | ||
|
||
private handleChangeDate = (time: Moment) => { | ||
const { onChangeTime } = this.props; | ||
|
||
if (onChangeTime) { | ||
onChangeTime(time.valueOf()); | ||
} | ||
}; | ||
|
||
private startLiveStreaming = () => { | ||
const { startLiveStreaming } = this.props; | ||
|
||
if (startLiveStreaming) { | ||
startLiveStreaming(); | ||
} | ||
}; | ||
|
||
private stopLiveStreaming = () => { | ||
const { stopLiveStreaming } = this.props; | ||
|
||
if (stopLiveStreaming) { | ||
stopLiveStreaming(); | ||
} | ||
}; | ||
} |
26 changes: 26 additions & 0 deletions
26
x-pack/plugins/infra/public/containers/waffle/with_waffle_time.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { connect } from 'react-redux'; | ||
|
||
import { State, waffleTimeActions, waffleTimeSelectors } from '../../store'; | ||
import { asChildFunctionRenderer } from '../../utils/typed_react'; | ||
import { bindPlainActionCreators } from '../../utils/typed_redux'; | ||
|
||
export const withWaffleTime = connect( | ||
(state: State) => ({ | ||
currentTime: waffleTimeSelectors.selectCurrentTime(state), | ||
currentTimeRange: waffleTimeSelectors.selectCurrentTimeRange(state), | ||
isAutoReloading: waffleTimeSelectors.selectIsAutoReloading(state), | ||
}), | ||
bindPlainActionCreators({ | ||
jumpToTime: waffleTimeActions.jumpToTime, | ||
startAutoReload: waffleTimeActions.startAutoReload, | ||
stopAutoReload: waffleTimeActions.stopAutoReload, | ||
}) | ||
); | ||
|
||
export const WithWaffleTime = asChildFunctionRenderer(withWaffleTime); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
x-pack/plugins/infra/public/store/local/waffle_time/actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import actionCreatorFactory from 'typescript-fsa'; | ||
|
||
const actionCreator = actionCreatorFactory('x-pack/infra/local/waffle_time'); | ||
|
||
export const jumpToTime = actionCreator<number>('JUMP_TO_TIME'); | ||
|
||
export const startAutoReload = actionCreator('START_AUTO_RELOAD'); | ||
|
||
export const stopAutoReload = actionCreator('STOP_AUTO_RELOAD'); |
41 changes: 41 additions & 0 deletions
41
x-pack/plugins/infra/public/store/local/waffle_time/epic.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { Action } from 'redux'; | ||
import { Epic } from 'redux-observable'; | ||
import { timer } from 'rxjs'; | ||
import { exhaustMap, filter, map, takeUntil, withLatestFrom } from 'rxjs/operators'; | ||
|
||
import { jumpToTime, startAutoReload, stopAutoReload } from './actions'; | ||
|
||
interface WaffleTimeEpicDependencies<State> { | ||
selectWaffleTimeUpdatePolicyInterval: (state: State) => number | null; | ||
} | ||
|
||
export const createWaffleTimeEpic = <State>(): Epic< | ||
Action, | ||
Action, | ||
State, | ||
WaffleTimeEpicDependencies<State> | ||
> => (action$, state$, { selectWaffleTimeUpdatePolicyInterval }) => { | ||
const updateInterval$ = state$.pipe( | ||
map(selectWaffleTimeUpdatePolicyInterval), | ||
filter(isNotNull) | ||
); | ||
|
||
return action$.pipe( | ||
filter(startAutoReload.match), | ||
withLatestFrom(updateInterval$), | ||
exhaustMap(([action, updateInterval]) => | ||
timer(0, updateInterval).pipe( | ||
map(() => jumpToTime(Date.now())), | ||
takeUntil(action$.pipe(filter(stopAutoReload.match))) | ||
) | ||
) | ||
); | ||
}; | ||
|
||
const isNotNull = <T>(value: T | null): value is T => value !== null; |
12 changes: 12 additions & 0 deletions
12
x-pack/plugins/infra/public/store/local/waffle_time/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import * as waffleTimeActions from './actions'; | ||
import * as waffleTimeSelectors from './selectors'; | ||
|
||
export { waffleTimeActions, waffleTimeSelectors }; | ||
export * from './epic'; | ||
export * from './reducer'; |
52 changes: 52 additions & 0 deletions
52
x-pack/plugins/infra/public/store/local/waffle_time/reducer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { combineReducers } from 'redux'; | ||
import { reducerWithInitialState } from 'typescript-fsa-reducers'; | ||
|
||
import { jumpToTime, startAutoReload, stopAutoReload } from './actions'; | ||
|
||
interface ManualTimeUpdatePolicy { | ||
policy: 'manual'; | ||
} | ||
|
||
interface IntervalTimeUpdatePolicy { | ||
policy: 'interval'; | ||
interval: number; | ||
} | ||
|
||
type TimeUpdatePolicy = ManualTimeUpdatePolicy | IntervalTimeUpdatePolicy; | ||
|
||
export interface WaffleTimeState { | ||
currentTime: number; | ||
updatePolicy: TimeUpdatePolicy; | ||
} | ||
|
||
export const initialWaffleTimeState: WaffleTimeState = { | ||
currentTime: Date.now(), | ||
updatePolicy: { | ||
policy: 'manual', | ||
}, | ||
}; | ||
|
||
const currentTimeReducer = reducerWithInitialState(initialWaffleTimeState.currentTime).case( | ||
jumpToTime, | ||
(currentTime, targetTime) => targetTime | ||
); | ||
|
||
const updatePolicyReducer = reducerWithInitialState(initialWaffleTimeState.updatePolicy) | ||
.case(startAutoReload, () => ({ | ||
policy: 'interval', | ||
interval: 5000, | ||
})) | ||
.case(stopAutoReload, () => ({ | ||
policy: 'manual', | ||
})); | ||
|
||
export const waffleTimeReducer = combineReducers<WaffleTimeState>({ | ||
currentTime: currentTimeReducer, | ||
updatePolicy: updatePolicyReducer, | ||
}); |
Oops, something went wrong.