1- import React , {
2- FC ,
3- useContext ,
4- useMemo ,
5- useEffect ,
6- useState ,
7- useRef ,
8- } from 'react'
1+ import React , { FC , useContext , useEffect , useState , useRef } from 'react'
92import { useDispatch , useSelector } from 'react-redux'
10- import { getVariables } from 'src/variables/selectors'
113import { FlowContext } from 'src/flows/context/flow.current'
124import { ResultsContext } from 'src/flows/context/results'
135import { QueryContext , simplify } from 'src/shared/contexts/query'
@@ -16,7 +8,6 @@ import {FluxResult, QueryScope} from 'src/types/flows'
168import { PIPE_DEFINITIONS , PROJECT_NAME } from 'src/flows'
179import { notify } from 'src/shared/actions/notifications'
1810import EmptyGraphMessage from 'src/shared/components/EmptyGraphMessage'
19- import { parseDuration , timeRangeToDuration } from 'src/shared/utils/duration'
2011import { useEvent , sendEvent } from 'src/users/hooks/useEvent'
2112import { getOrg } from 'src/organizations/selectors'
2213
@@ -25,10 +16,9 @@ import {
2516 notebookRunSuccess ,
2617 notebookRunFail ,
2718} from 'src/shared/copy/notifications'
28- import { TIME_RANGE_START , TIME_RANGE_STOP } from 'src/variables/constants'
2919
3020// Types
31- import { AppState , RemoteDataState , Variable } from 'src/types'
21+ import { RemoteDataState } from 'src/types'
3222
3323export interface Stage {
3424 id : string
@@ -59,7 +49,7 @@ export const DEFAULT_CONTEXT: FlowQueryContextType = {
5949 queryDependents : ( ) => { } ,
6050 getPanelQueries : _ => ( {
6151 id : '' ,
62- scope : { vars : { } } ,
52+ scope : { } ,
6353 source : '' ,
6454 visual : '' ,
6555 } ) ,
@@ -70,19 +60,6 @@ export const FlowQueryContext = React.createContext<FlowQueryContextType>(
7060 DEFAULT_CONTEXT
7161)
7262
73- const generateTimeVar = ( which , value ) : Variable =>
74- ( {
75- orgID : '' ,
76- id : which ,
77- name : which ,
78- arguments : {
79- type : 'system' ,
80- values : [ value ] ,
81- } ,
82- status : RemoteDataState . Done ,
83- labels : [ ] ,
84- } as Variable )
85-
8663export const FlowQueryProvider : FC = ( { children} ) => {
8764 const { flow} = useContext ( FlowContext )
8865 const { setResult, setStatuses, statuses} = useContext ( ResultsContext )
@@ -92,7 +69,6 @@ export const FlowQueryProvider: FC = ({children}) => {
9269
9370 const dispatch = useDispatch ( )
9471 const notebookQueryKey = `queryAll-${ flow ?. name } `
95- const variables = useSelector ( ( state : AppState ) => getVariables ( state ) )
9672
9773 useEffect ( ( ) => {
9874 if ( flow ?. range ?. lower !== prevLower ) {
@@ -113,42 +89,31 @@ export const FlowQueryProvider: FC = ({children}) => {
11389 }
11490 useEvent ( 'storage' , handleStorageEvent )
11591
116- const vars = useMemo ( ( ) => {
117- const _vars = [ ... variables ]
92+ const _map = useRef ( [ ] )
93+ let timeRangeStart , timeRangeStop
11894
119- if ( ! flow ?. range ) {
120- return _vars . reduce ( ( acc , curr ) => {
121- acc [ curr . name ] = curr
122- return acc
123- } , { } )
95+ if ( ! flow ?. range ) {
96+ timeRangeStart = timeRangeStop = null
97+ } else {
98+ if ( flow . range . type === 'selectable-duration' ) {
99+ timeRangeStart = '-' + flow . range . duration
100+ } else if ( flow . range . type === 'duration' ) {
101+ timeRangeStart = '-' + flow . range . lower
102+ } else if ( isNaN ( Date . parse ( flow . range . lower ) ) ) {
103+ timeRangeStart = null
104+ } else {
105+ timeRangeStart = new Date ( flow . range . lower ) . toISOString ( )
124106 }
125107
126- // Here we add in those optional time range variables if we have them
127108 if ( ! flow . range . upper ) {
128- _vars . push ( generateTimeVar ( TIME_RANGE_STOP , 'now()' ) )
109+ timeRangeStop = 'now()'
129110 } else if ( isNaN ( Date . parse ( flow . range . upper ) ) ) {
130- _vars . push ( generateTimeVar ( TIME_RANGE_STOP , null ) )
131- } else {
132- _vars . push ( generateTimeVar ( TIME_RANGE_STOP , flow . range . upper ) )
133- }
134-
135- if ( ( flow . range . type as any ) !== 'custom' ) {
136- const duration = parseDuration ( timeRangeToDuration ( flow . range ) )
137-
138- _vars . push ( generateTimeVar ( TIME_RANGE_START , duration ) )
139- } else if ( isNaN ( Date . parse ( flow . range . lower ) ) ) {
140- _vars . push ( generateTimeVar ( TIME_RANGE_START , null ) )
111+ timeRangeStop = null
141112 } else {
142- _vars . push ( generateTimeVar ( TIME_RANGE_START , flow . range . lower ) )
113+ timeRangeStop = new Date ( flow . range . upper ) . toISOString ( )
143114 }
115+ }
144116
145- return _vars . reduce ( ( acc , curr ) => {
146- acc [ curr . name ] = curr
147- return acc
148- } , { } )
149- } , [ variables , flow ?. range ] )
150-
151- const _map = useRef ( [ ] )
152117 const _generateMap = ( ) : Stage [ ] => {
153118 const stages = ( flow ?. data ?. allIDs ?? [ ] ) . reduce ( ( acc , panelID ) => {
154119 const panel = flow . data . byID [ panelID ]
@@ -161,6 +126,10 @@ export const FlowQueryProvider: FC = ({children}) => {
161126 scope : {
162127 region : window . location . origin ,
163128 org : org . id ,
129+ vars : {
130+ timeRangeStart,
131+ timeRangeStop,
132+ } ,
164133 } ,
165134 source : '' ,
166135 visual : '' ,
@@ -278,10 +247,6 @@ export const FlowQueryProvider: FC = ({children}) => {
278247 region : window . location . origin ,
279248 org : org . id ,
280249 ...( override || { } ) ,
281- vars : {
282- ...vars ,
283- ...( override ?. vars || { } ) ,
284- } ,
285250 }
286251
287252 return queryAPI ( text , _override )
@@ -292,10 +257,6 @@ export const FlowQueryProvider: FC = ({children}) => {
292257 region : window . location . origin ,
293258 org : org . id ,
294259 ...( override || { } ) ,
295- vars : {
296- ...vars ,
297- ...( override ?. vars || { } ) ,
298- } ,
299260 }
300261
301262 return basicAPI ( text , _override )
@@ -428,7 +389,10 @@ export const FlowQueryProvider: FC = ({children}) => {
428389 }
429390
430391 const simple = ( text : string ) => {
431- return simplify ( text , vars )
392+ return simplify ( text , {
393+ timeRangeStart,
394+ timeRangeStop,
395+ } )
432396 }
433397
434398 if ( ! flow ) {
0 commit comments