11// Libraries
2- import { useMemo , useState } from 'react'
2+ import { useEffect , useMemo , useState } from 'react'
33import { useSelector } from 'react-redux'
44import { NumericColumnData , fromFlux } from '@influxdata/giraffe'
5+ import { isEqual } from 'lodash'
56
67// API
78import { runQuery , RunQueryResult } from 'src/shared/apis/query'
@@ -11,11 +12,19 @@ import {useOneWayState} from 'src/shared/utils/useOneWayState'
1112import { extent } from 'src/shared/utils/vis'
1213import { getStartTime , getEndTime } from 'src/timeMachine/selectors/index'
1314import { getOrg } from 'src/organizations/selectors'
14- import { getAllVariablesForZoomRequery } from 'src/variables/selectors'
15+ import {
16+ // getAllVariables,
17+ getAllVariablesForZoomRequery ,
18+ } from 'src/variables/selectors'
1519import { buildUsedVarsOption } from 'src/variables/utils/buildVarsOption'
1620
21+ import {
22+ getWindowPeriodFromVariables ,
23+ getWindowVarsFromVariables ,
24+ } from 'src/variables/utils/getWindowVars'
25+
1726// Types
18- import { InternalFromFluxResult , TimeRange } from 'src/types'
27+ import { AppState , InternalFromFluxResult , TimeRange } from 'src/types'
1928/*
2029 This hook helps map the domain setting stored for line graph to the
2130 appropriate settings on a @influxdata/giraffe `Config` object.
@@ -121,6 +130,9 @@ interface ZoomRequeryArgs {
121130 timeRange ?: TimeRange
122131}
123132
133+ const isNotEqual = ( firstValue : any , secondValue : any ) : boolean =>
134+ isEqual ( firstValue , secondValue ) === false
135+
124136export const useZoomRequeryXDomainSettings = ( args : ZoomRequeryArgs ) => {
125137 const {
126138 parsedResult,
@@ -133,10 +145,6 @@ export const useZoomRequeryXDomainSettings = (args: ZoomRequeryArgs) => {
133145 timeRange = null ,
134146 } = args
135147
136- const orgId = useSelector ( getOrg ) ?. id
137- const variables = useSelector ( getAllVariablesForZoomRequery )
138- const extern = buildUsedVarsOption ( query , variables )
139-
140148 const initialDomain = useMemo ( ( ) => {
141149 if ( storedDomain ) {
142150 return storedDomain
@@ -148,20 +156,57 @@ export const useZoomRequeryXDomainSettings = (args: ZoomRequeryArgs) => {
148156 const [ domain , setDomain ] = useState ( initialDomain )
149157 const [ preZoomDomain , setPreZoomDomain ] = useState < Array < number > > ( null )
150158
159+ const getAllVariablesWithTimeDomain = ( state : AppState ) =>
160+ getAllVariablesForZoomRequery ( state , timeRange ? domain : [ ] )
161+ const orgId = useSelector ( getOrg ) ?. id
162+ const variables = useSelector ( getAllVariablesWithTimeDomain )
163+
164+ const [ windowPeriod , setWindowPeriod ] = useState < number > (
165+ getWindowPeriodFromVariables ( query , variables )
166+ )
167+
168+ /*
169+ * When the user zooms in, re-run the query
170+ * When the user un-zooms, do not re-run but revert back to old data
171+ * Hence re-run the query only when both conditions are met:
172+ * - the window period changes from the domain changing (zooming in)
173+ * - the domain does not equal the original pre-zoom domain (unzooming)
174+ */
175+ useEffect ( ( ) => {
176+ const updatedWindowPeriod = getWindowPeriodFromVariables ( query , variables )
177+ if ( isNotEqual ( windowPeriod , updatedWindowPeriod ) ) {
178+ setWindowPeriod ( getWindowPeriodFromVariables ( query , variables ) )
179+
180+ if ( isNotEqual ( preZoomDomain , domain ) ) {
181+ const zoomQueryWindowVariable = getWindowVarsFromVariables (
182+ query ,
183+ variables
184+ )
185+ const extern = buildUsedVarsOption (
186+ query ,
187+ variables ,
188+ zoomQueryWindowVariable
189+ )
190+ runQuery ( orgId , query , extern ) . promise . then (
191+ ( result : RunQueryResult ) => {
192+ if ( result . type === 'SUCCESS' ) {
193+ const parsed = fromFlux ( result . csv )
194+ setResult ( parsed )
195+ }
196+ }
197+ )
198+ }
199+ }
200+ } , [ domain ] ) // eslint-disable-line react-hooks/exhaustive-deps
201+
151202 const setZoomDomain = ( updatedDomain : number [ ] ) => {
152203 if ( ! preZoomResult ) {
153204 setPreZoomDomain ( initialDomain )
154205 setPreZoomResult ( parsedResult )
155206 }
156-
157- runQuery ( orgId , query , extern ) . promise . then ( ( result : RunQueryResult ) => {
158- if ( result . type === 'SUCCESS' ) {
159- const parsed = fromFlux ( result . csv )
160- setResult ( parsed )
161- setDomain ( updatedDomain )
162- }
163- } )
207+ setDomain ( updatedDomain )
164208 }
209+
165210 const resetDomain = ( ) => {
166211 if ( preZoomResult ) {
167212 setResult ( preZoomResult )
@@ -184,9 +229,6 @@ export const useZoomRequeryYDomainSettings = (args: ZoomRequeryArgs) => {
184229 data,
185230 timeRange = null ,
186231 } = args
187- const orgId = useSelector ( getOrg ) ?. id
188- const variables = useSelector ( getAllVariablesForZoomRequery )
189- const extern = buildUsedVarsOption ( query , variables )
190232
191233 const initialDomain = useMemo ( ( ) => {
192234 if (
@@ -204,20 +246,57 @@ export const useZoomRequeryYDomainSettings = (args: ZoomRequeryArgs) => {
204246 const [ domain , setDomain ] = useState ( initialDomain )
205247 const [ preZoomDomain , setPreZoomDomain ] = useState < Array < number > > ( null )
206248
249+ const getAllVariablesWithTimeDomain = ( state : AppState ) =>
250+ getAllVariablesForZoomRequery ( state , timeRange ? domain : [ ] )
251+ const orgId = useSelector ( getOrg ) ?. id
252+ const variables = useSelector ( getAllVariablesWithTimeDomain )
253+
254+ const [ windowPeriod , setWindowPeriod ] = useState < number > (
255+ getWindowPeriodFromVariables ( query , variables )
256+ )
257+
258+ /*
259+ * When the user zooms in, re-run the query
260+ * When the user un-zooms, do not re-run but revert back to old data
261+ * Hence re-run the query only when both conditions are met:
262+ * - the window period changes from the domain changing (zooming in)
263+ * - the domain does not equal the original pre-zoom domain (unzooming)
264+ */
265+ useEffect ( ( ) => {
266+ const updatedWindowPeriod = getWindowPeriodFromVariables ( query , variables )
267+ if ( isNotEqual ( windowPeriod , updatedWindowPeriod ) ) {
268+ setWindowPeriod ( getWindowPeriodFromVariables ( query , variables ) )
269+
270+ if ( isNotEqual ( preZoomDomain , domain ) ) {
271+ const zoomQueryWindowVariable = getWindowVarsFromVariables (
272+ query ,
273+ variables
274+ )
275+ const extern = buildUsedVarsOption (
276+ query ,
277+ variables ,
278+ zoomQueryWindowVariable
279+ )
280+ runQuery ( orgId , query , extern ) . promise . then (
281+ ( result : RunQueryResult ) => {
282+ if ( result . type === 'SUCCESS' ) {
283+ const parsed = fromFlux ( result . csv )
284+ setResult ( parsed )
285+ }
286+ }
287+ )
288+ }
289+ }
290+ } , [ domain ] ) // eslint-disable-line react-hooks/exhaustive-deps
291+
207292 const setZoomDomain = ( updatedDomain : number [ ] ) => {
208293 if ( ! preZoomResult ) {
209294 setPreZoomDomain ( initialDomain )
210295 setPreZoomResult ( parsedResult )
211296 }
212-
213- runQuery ( orgId , query , extern ) . promise . then ( ( result : RunQueryResult ) => {
214- if ( result . type === 'SUCCESS' ) {
215- const parsed = fromFlux ( result . csv )
216- setResult ( parsed )
217- setDomain ( updatedDomain )
218- }
219- } )
297+ setDomain ( updatedDomain )
220298 }
299+
221300 const resetDomain = ( ) => {
222301 if ( preZoomResult ) {
223302 setResult ( preZoomResult )
0 commit comments