feat(ui): pass templates to flux script renderer

pull/5688/head
Pavel Zavora 2021-03-07 09:49:59 +01:00
parent 42c574c544
commit fbde00bc6f
4 changed files with 38 additions and 7 deletions

View File

@ -1,4 +1,4 @@
import {TimeRange} from 'src/types'
import {Template, TimeRange} from 'src/types'
import {computeInterval} from 'src/tempVars/utils/replace'
import {DEFAULT_DURATION_MS} from 'src/shared/constants'
import {extractImports} from 'src/shared/parsing/flux/extractImports'
@ -11,21 +11,28 @@ export const WINDOW_PERIOD = 'v.windowPeriod'
const INTERVAL_REGEX = /autoInterval|v\.windowPeriod/g
function templateVariables(templates: Template[]) {
// TODO implement variables
return templates ? '' : ' '
}
function fluxVariables(
lower: string,
upper: string,
extraVars: string,
interval?: number
): string {
// dashboardTime, upperDashboardTime and autoInterval are added for bacward compatibility with 1.8.x
if (interval) {
return `dashboardTime = ${lower}\nupperDashboardTime = ${upper}\nautoInterval = ${interval}ms\nv = { timeRangeStart: dashboardTime , timeRangeStop: upperDashboardTime , windowPeriod: autoInterval }`
return `dashboardTime = ${lower}\nupperDashboardTime = ${upper}\nautoInterval = ${interval}ms\nv = {${extraVars} timeRangeStart: dashboardTime , timeRangeStop: upperDashboardTime , windowPeriod: autoInterval }`
}
return `dashboardTime = ${lower}\nupperDashboardTime = ${upper}\nv = { timeRangeStart: dashboardTime , timeRangeStop: upperDashboardTime }`
return `dashboardTime = ${lower}\nupperDashboardTime = ${upper}\nv = {${extraVars} timeRangeStart: dashboardTime , timeRangeStop: upperDashboardTime }`
}
export const renderTemplatesInScript = async (
script: string,
timeRange: TimeRange,
templates: Template[],
astLink: string
): Promise<string> => {
let dashboardTime: string
@ -41,7 +48,8 @@ export const renderTemplatesInScript = async (
const {imports, body} = await extractImports(astLink, script)
let variables = fluxVariables(dashboardTime, upperDashboardTime)
const extraVars = templateVariables(templates)
let variables = fluxVariables(dashboardTime, upperDashboardTime, extraVars)
let rendered = `${variables}\n\n${body}`
if (!script.match(INTERVAL_REGEX)) {
@ -58,7 +66,12 @@ export const renderTemplatesInScript = async (
}
const interval = computeInterval(duration)
variables = fluxVariables(dashboardTime, upperDashboardTime, interval)
variables = fluxVariables(
dashboardTime,
upperDashboardTime,
extraVars,
interval
)
rendered = `${imports}\n\n${variables}\n\n${body}`

View File

@ -80,12 +80,20 @@ class CSVExporter extends PureComponent<Props, State> {
}
private downloadFluxCSV = async (): Promise<void> => {
const {source, script, onNotify, timeRange, fluxASTLink} = this.props
const {
source,
script,
onNotify,
timeRange,
fluxASTLink,
templates,
} = this.props
const {didTruncate, rowCount} = await downloadFluxCSV(
source,
script,
timeRange,
templates,
fluxASTLink
)

View File

@ -268,13 +268,21 @@ class TimeSeries extends PureComponent<Props, State> {
}
private executeTemplatedFluxQuery = async (latestUUID: string) => {
const {queries, onNotify, source, timeRange, fluxASTLink} = this.props
const {
queries,
onNotify,
source,
timeRange,
fluxASTLink,
templates,
} = this.props
const script: string = _.get(queries, '0.text', '')
const renderedScript = await renderTemplatesInScript(
script,
timeRange,
templates,
fluxASTLink
)

View File

@ -32,11 +32,13 @@ export const downloadFluxCSV = async (
source: Source,
script: string,
timeRange: TimeRange,
templates: Template[],
fluxASTLink: string
): Promise<{didTruncate: boolean; rowCount: number}> => {
const renderedScript = await renderTemplatesInScript(
script,
timeRange,
templates,
fluxASTLink
)