Skip to content

Commit 3f11cd3

Browse files
authored
feat: toggle window period calculation (#5423)
1 parent 92bf5ba commit 3f11cd3

File tree

1 file changed

+63
-5
lines changed

1 file changed

+63
-5
lines changed

src/shared/contexts/query.tsx

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import React, {FC, useEffect, useRef} from 'react'
22
import {useSelector} from 'react-redux'
33
import {nanoid} from 'nanoid'
44
import {parse, format_from_js_file} from '@influxdata/flux-lsp-browser'
5-
import {
6-
GATEWAY_TIMEOUT_STATUS,
7-
REQUEST_TIMEOUT_STATUS,
8-
} from 'src/cloud/constants'
95

106
import {getOrg} from 'src/organizations/selectors'
117
import {fromFlux} from '@influxdata/giraffe'
@@ -17,6 +13,8 @@ import {SELECTABLE_TIME_RANGES} from 'src/shared/constants/timeRanges'
1713
import {
1814
RATE_LIMIT_ERROR_STATUS,
1915
RATE_LIMIT_ERROR_TEXT,
16+
GATEWAY_TIMEOUT_STATUS,
17+
REQUEST_TIMEOUT_STATUS,
2018
} from 'src/cloud/constants'
2119
import {isFlagEnabled} from 'src/shared/utils/featureFlag'
2220

@@ -353,7 +351,7 @@ const updateWindowPeriod = (
353351
.join('\n\n')
354352

355353
const queryAST = parse(query)
356-
const optionAST = parse(optionTexts)
354+
let optionAST = parse(optionTexts)
357355

358356
// only run this if the query need a windowPeriod
359357
if (
@@ -370,7 +368,63 @@ const updateWindowPeriod = (
370368
}
371369

372370
return options
371+
} else if (isFlagEnabled('dontSolveWindowPeriod')) {
372+
if (options?.v?.timeRangeStart && options?.v?.timeRangeStop) {
373+
const NOW = Date.now()
374+
const range = find(
375+
optionAST,
376+
node =>
377+
node?.type === 'OptionStatement' && node?.assignment?.id?.name === 'v'
378+
).reduce(
379+
(acc, curr) => {
380+
acc.start =
381+
find(
382+
curr,
383+
n => n.type === 'Property' && n?.key?.name === 'timeRangeStart'
384+
)[0]?.value ?? acc.start
385+
386+
acc.stop =
387+
find(
388+
curr,
389+
n => n.type === 'Property' && n?.key?.name === 'timeRangeStop'
390+
)[0]?.value ?? acc.stop
391+
392+
return acc
393+
},
394+
{
395+
start: null,
396+
stop: null,
397+
}
398+
)
399+
const duration =
400+
propertyTime(queryAST, range.stop, NOW) -
401+
propertyTime(queryAST, range.start, NOW)
402+
const foundDuration = SELECTABLE_TIME_RANGES.find(
403+
tr => tr.seconds * 1000 === duration
404+
)
405+
406+
if (foundDuration) {
407+
options.v.windowPeriod = `${foundDuration.windowPeriod} ms`
408+
} else {
409+
options.v.windowPeriod = `${Math.round(
410+
duration / DESIRED_POINTS_PER_GRAPH
411+
)} ms`
412+
}
413+
} else {
414+
options.v.windowPeriod = `${FALLBACK_WINDOW_PERIOD} ms`
415+
}
416+
417+
// write the mutations back out into the AST
418+
optionAST = parse(
419+
Object.entries(options)
420+
.map(([k, v]) => {
421+
const vals = Object.entries(v).map(([_k, _v]) => ` ${_k}: ${_v}`)
422+
return `option ${k} = {${vals.join(',\n')}}`
423+
})
424+
.join('\n\n')
425+
)
373426
}
427+
374428
try {
375429
const _optionAST = JSON.parse(JSON.stringify(optionAST))
376430
// make sure there's a variable in there named windowPeriod so later logic doesnt bail
@@ -388,6 +442,10 @@ const updateWindowPeriod = (
388442
return
389443
}
390444

445+
if (isFlagEnabled('dontSolveWindowPeriod')) {
446+
throw new Error('v.windowPeriod is used and not defined')
447+
}
448+
391449
node.assignment.init.properties.push({
392450
type: 'Property',
393451
key: {

0 commit comments

Comments
 (0)