From 6f2ee822c9c1cf3e89003190204ad99af5d6716e Mon Sep 17 00:00:00 2001 From: Delmer Reed Date: Tue, 3 Jul 2018 11:40:00 -0400 Subject: [PATCH] Refactor AutoRefresh polling condition Refactoring to check for template values that do not have their localSelected status resolved allows us to avoid initially polling with default selections after mounting. This condition also allows AutoRefresh to ignore updates as template values resolve with their localSelected status. This introduces a delay in cell graphs display before template value local selections resolve. --- ui/src/shared/components/AutoRefresh.tsx | 28 ++++++++++++------------ 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/ui/src/shared/components/AutoRefresh.tsx b/ui/src/shared/components/AutoRefresh.tsx index fc2d924e9..26c065d2f 100644 --- a/ui/src/shared/components/AutoRefresh.tsx +++ b/ui/src/shared/components/AutoRefresh.tsx @@ -4,7 +4,7 @@ import _ from 'lodash' import {fetchTimeSeries} from 'src/shared/apis/query' import {DEFAULT_TIME_SERIES} from 'src/shared/constants/series' import {TimeSeriesServerResponse, TimeSeriesResponse} from 'src/types/series' -import {Template, Source} from 'src/types' +import {Template, Source, TemplateValue} from 'src/types' interface Axes { bounds: { @@ -69,11 +69,13 @@ const AutoRefresh = ( } public async componentDidMount() { - this.startNewPolling() + if (!this.isAwaitingSelectedValues) { + this.startNewPolling() + } } public async componentDidUpdate(prevProps: Props) { - if (!this.isPropsDifferent(prevProps)) { + if (this.isAwaitingSelectedValues || !this.isPropsDifferent(prevProps)) { return } this.startNewPolling() @@ -195,7 +197,7 @@ const AutoRefresh = ( return ( this.props.inView !== nextProps.inView || !!this.queryDifference(this.props.queries, nextProps.queries).length || - this.isActiveTemplatesDifferent(nextProps) || + !_.isEqual(this.props.templates, nextProps.templates) || this.props.autoRefresh !== nextProps.autoRefresh || isSourceDifferent ) @@ -233,18 +235,16 @@ const AutoRefresh = ( ) } - private isActiveTemplatesDifferent(prevProps: Props) { - const prevTemplates = this.getActiveTemplates(prevProps.templates) - const templates = this.getActiveTemplates(this.props.templates) + private get isAwaitingSelectedValues(): boolean { + const {templates} = this.props - return !_.isEqual(prevTemplates, templates) - } + const isAwaitingLocalSelection = (v: TemplateValue): boolean => + v.localSelected === undefined - private getActiveTemplates(tempVars: Template[]): Template[] { - return _.map(tempVars, tempVar => ({ - ...tempVar, - values: tempVar.values.filter(v => v.selected || v.localSelected), - })) + const allValues = _.flatMap(templates, t => t.values) + const valuesAwaitingSelection = allValues.filter(isAwaitingLocalSelection) + + return valuesAwaitingSelection.length !== 0 } }