Clarify template selection resolution
parent
ab5f1ded78
commit
dddbf8b5fb
|
@ -19,9 +19,9 @@ import {notify} from 'src/shared/actions/notifications'
|
|||
import {errorThrown} from 'src/shared/actions/errors'
|
||||
|
||||
import {
|
||||
applySelections,
|
||||
applyLocalSelections,
|
||||
templateSelectionsFromQueryParams,
|
||||
queryParamsFromTemplates,
|
||||
templateSelectionsFromTemplates,
|
||||
} from 'src/dashboards/utils/tempVars'
|
||||
import {validTimeRange, validAbsoluteTimeRange} from 'src/dashboards/utils/time'
|
||||
import {
|
||||
|
@ -584,7 +584,7 @@ export const getDashboardWithTemplatesAsync = (
|
|||
.map(t => hydrateTemplate(proxyLink, t, []))
|
||||
)
|
||||
|
||||
applySelections(nonNestedTemplates, templateSelections)
|
||||
applyLocalSelections(nonNestedTemplates, templateSelections)
|
||||
|
||||
const nestedTemplates = await Promise.all(
|
||||
dashboard.templates
|
||||
|
@ -592,7 +592,7 @@ export const getDashboardWithTemplatesAsync = (
|
|||
.map(t => hydrateTemplate(proxyLink, t, nonNestedTemplates))
|
||||
)
|
||||
|
||||
applySelections(nestedTemplates, templateSelections)
|
||||
applyLocalSelections(nestedTemplates, templateSelections)
|
||||
|
||||
const templates = [...nonNestedTemplates, ...nestedTemplates]
|
||||
|
||||
|
@ -615,7 +615,7 @@ export const rehydrateNestedTemplatesAsync = (
|
|||
.map(t => hydrateTemplate(proxyLink, t, dashboard.templates))
|
||||
)
|
||||
|
||||
applySelections(nestedTemplates, templateSelections)
|
||||
applyLocalSelections(nestedTemplates, templateSelections)
|
||||
|
||||
dispatch(updateTemplates(nestedTemplates))
|
||||
dispatch<any>(updateTemplateQueryParams(dashboardId))
|
||||
|
@ -627,7 +627,7 @@ export const updateTemplateQueryParams = (dashboardId: number) => (
|
|||
): void => {
|
||||
const templates = getDashboard(getState(), dashboardId).templates
|
||||
const updatedQueryParams = {
|
||||
tempVars: queryParamsFromTemplates(templates),
|
||||
tempVars: templateSelectionsFromTemplates(templates),
|
||||
}
|
||||
|
||||
dispatch(updateQueryParams(updatedQueryParams))
|
||||
|
|
|
@ -4,7 +4,7 @@ import qs from 'qs'
|
|||
import {formatTempVar} from 'src/tempVars/utils'
|
||||
|
||||
import {Template, TemplateQuery} from 'src/types'
|
||||
import {TemplateQPSelections} from 'src/types/dashboards'
|
||||
import {TemplateSelections} from 'src/types/dashboards'
|
||||
|
||||
export const makeQueryForTemplate = ({
|
||||
influxql,
|
||||
|
@ -17,7 +17,7 @@ export const makeQueryForTemplate = ({
|
|||
.replace(':measurement:', `"${measurement}"`)
|
||||
.replace(':tagKey:', `"${tagKey}"`)
|
||||
|
||||
export const templateSelectionsFromQueryParams = (): TemplateQPSelections => {
|
||||
export const templateSelectionsFromQueryParams = (): TemplateSelections => {
|
||||
const queryParams = qs.parse(window.location.search, {
|
||||
ignoreQueryPrefix: true,
|
||||
})
|
||||
|
@ -29,7 +29,9 @@ export const templateSelectionsFromQueryParams = (): TemplateQPSelections => {
|
|||
)
|
||||
}
|
||||
|
||||
export const queryParamsFromTemplates = (templates: Template[]) => {
|
||||
export const templateSelectionsFromTemplates = (
|
||||
templates: Template[]
|
||||
): TemplateSelections => {
|
||||
return templates.reduce((acc, template) => {
|
||||
const tempVar = stripTempVar(template.tempVar)
|
||||
const selection = template.values.find(t => t.localSelected)
|
||||
|
@ -45,18 +47,22 @@ export const queryParamsFromTemplates = (templates: Template[]) => {
|
|||
}, {})
|
||||
}
|
||||
|
||||
export const applySelections = (
|
||||
export const applyLocalSelections = (
|
||||
templates: Template[],
|
||||
selections: TemplateQPSelections
|
||||
selections: TemplateSelections
|
||||
): void => {
|
||||
// Ensure that every supplied template has an appropriately set
|
||||
// `localSelected` value.
|
||||
for (const {tempVar, values} of templates) {
|
||||
if (!values.length) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Attempt to use supplied selection
|
||||
let selection = selections[tempVar]
|
||||
|
||||
if (!selection || !values.find(v => v.value === selection)) {
|
||||
// Default to template's `selected` value if no valid selection is found
|
||||
selection = values.find(v => v.selected).value
|
||||
}
|
||||
|
||||
|
|
|
@ -135,7 +135,7 @@ export interface DashboardSwitcherLink {
|
|||
to: string
|
||||
}
|
||||
|
||||
export interface TemplateQPSelections {
|
||||
export interface TemplateSelections {
|
||||
// e.g. {':my-db:': 'telegraf'}
|
||||
[tempVar: string]: string
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue