Chore: Refactor to introduce VARIABLE_PREFIX constant (#62980)

Co-authored-by: Ivan Ortega Alba <ivanortegaalba@gmail.com>
This commit is contained in:
Christy O'Reilly 2023-03-21 09:46:18 +00:00 committed by GitHub
parent 1328a32a31
commit a7bb87d15d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 20 additions and 15 deletions

View File

@ -3,3 +3,4 @@ export const ALL_VARIABLE_TEXT = 'All';
export const ALL_VARIABLE_VALUE = '$__all'; export const ALL_VARIABLE_VALUE = '$__all';
export const NONE_VARIABLE_TEXT = 'None'; export const NONE_VARIABLE_TEXT = 'None';
export const NONE_VARIABLE_VALUE = ''; export const NONE_VARIABLE_VALUE = '';
export const VARIABLE_PREFIX = 'var-';

View File

@ -2,6 +2,7 @@ import { ScopedVars, UrlQueryMap } from '@grafana/data';
import { getTemplateSrv } from '@grafana/runtime'; import { getTemplateSrv } from '@grafana/runtime';
import { variableAdapters } from './adapters'; import { variableAdapters } from './adapters';
import { VARIABLE_PREFIX } from './constants';
export function getVariablesUrlParams(scopedVars?: ScopedVars): UrlQueryMap { export function getVariablesUrlParams(scopedVars?: ScopedVars): UrlQueryMap {
const params: UrlQueryMap = {}; const params: UrlQueryMap = {};
@ -13,13 +14,13 @@ export function getVariablesUrlParams(scopedVars?: ScopedVars): UrlQueryMap {
if (scopedVars[variable.name].skipUrlSync) { if (scopedVars[variable.name].skipUrlSync) {
continue; continue;
} }
params['var-' + variable.name] = scopedVars[variable.name].value; params[VARIABLE_PREFIX + variable.name] = scopedVars[variable.name].value;
} else { } else {
// @ts-ignore // @ts-ignore
if (variable.skipUrlSync) { if (variable.skipUrlSync) {
continue; continue;
} }
params['var-' + variable.name] = variableAdapters.get(variable.type).getValueForUrl(variable as any); params[VARIABLE_PREFIX + variable.name] = variableAdapters.get(variable.type).getValueForUrl(variable as any);
} }
} }

View File

@ -6,6 +6,7 @@ import { LoadingState } from '@grafana/data';
import { ClickOutsideWrapper } from '@grafana/ui'; import { ClickOutsideWrapper } from '@grafana/ui';
import { StoreState, ThunkDispatch } from 'app/types'; import { StoreState, ThunkDispatch } from 'app/types';
import { VARIABLE_PREFIX } from '../../constants';
import { isMulti } from '../../guard'; import { isMulti } from '../../guard';
import { getVariableQueryRunner } from '../../query/VariableQueryRunner'; import { getVariableQueryRunner } from '../../query/VariableQueryRunner';
import { formatVariableLabel } from '../../shared/formatVariable'; import { formatVariableLabel } from '../../shared/formatVariable';
@ -125,7 +126,7 @@ export const optionPickerFactory = <Model extends VariableWithOptions | Variable
return ( return (
<VariableLink <VariableLink
id={`var-${variable.id}`} id={VARIABLE_PREFIX + variable.id}
text={linkText} text={linkText}
onClick={this.onShowOptions} onClick={this.onShowOptions}
loading={loading} loading={loading}
@ -144,7 +145,7 @@ export const optionPickerFactory = <Model extends VariableWithOptions | Variable
return ( return (
<ClickOutsideWrapper onClick={this.onHideOptions}> <ClickOutsideWrapper onClick={this.onHideOptions}>
<VariableInput <VariableInput
id={`var-${id}`} id={VARIABLE_PREFIX + id}
value={picker.queryValue} value={picker.queryValue}
onChange={this.onFilterOrSearchOptions} onChange={this.onFilterOrSearchOptions}
onNavigate={this.onNavigate} onNavigate={this.onNavigate}

View File

@ -4,6 +4,7 @@ import { selectors } from '@grafana/e2e-selectors';
import { Tooltip } from '@grafana/ui'; import { Tooltip } from '@grafana/ui';
import { variableAdapters } from '../adapters'; import { variableAdapters } from '../adapters';
import { VARIABLE_PREFIX } from '../constants';
import { VariableHide, VariableModel } from '../types'; import { VariableHide, VariableModel } from '../types';
interface Props { interface Props {
@ -35,7 +36,7 @@ function PickerLabel({ variable }: PropsWithChildren<Props>): ReactElement | nul
return null; return null;
} }
const elementId = `var-${variable.id}`; const elementId = VARIABLE_PREFIX + variable.id;
if (variable.description) { if (variable.description) {
return ( return (
<Tooltip content={variable.description} placement={'bottom'}> <Tooltip content={variable.description} placement={'bottom'}>

View File

@ -24,7 +24,7 @@ import { AppNotification, StoreState, ThunkResult } from '../../../types';
import { getDatasourceSrv } from '../../plugins/datasource_srv'; import { getDatasourceSrv } from '../../plugins/datasource_srv';
import { getTemplateSrv, TemplateSrv } from '../../templating/template_srv'; import { getTemplateSrv, TemplateSrv } from '../../templating/template_srv';
import { variableAdapters } from '../adapters'; import { variableAdapters } from '../adapters';
import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE } from '../constants'; import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE, VARIABLE_PREFIX } from '../constants';
import { cleanEditorState } from '../editor/reducer'; import { cleanEditorState } from '../editor/reducer';
import { import {
hasCurrent, hasCurrent,
@ -359,7 +359,7 @@ export const processVariable = (
const variable = getVariable(identifier, getState()); const variable = getVariable(identifier, getState());
await processVariableDependencies(variable, getState()); await processVariableDependencies(variable, getState());
const urlValue = queryParams['var-' + variable.name]; const urlValue = queryParams[VARIABLE_PREFIX + variable.name];
if (urlValue !== void 0) { if (urlValue !== void 0) {
const stringUrlValue = ensureStringValues(urlValue); const stringUrlValue = ensureStringValues(urlValue);
await variableAdapters.get(variable.type).setValueFromUrl(variable, stringUrlValue); await variableAdapters.get(variable.type).setValueFromUrl(variable, stringUrlValue);
@ -705,7 +705,7 @@ export const templateVarsChangedInUrl =
const variables = getVariablesByKey(key, getState()); const variables = getVariablesByKey(key, getState());
for (const variable of variables) { for (const variable of variables) {
const key = `var-${variable.name}`; const key = VARIABLE_PREFIX + variable.name;
if (!vars.hasOwnProperty(key)) { if (!vars.hasOwnProperty(key)) {
// key not found quick exit // key not found quick exit
continue; continue;
@ -735,7 +735,7 @@ export const templateVarsChangedInUrl =
} }
const filteredVars = variables.filter((v) => { const filteredVars = variables.filter((v) => {
const key = `var-${v.name}`; const key = VARIABLE_PREFIX + v.name;
return vars.hasOwnProperty(key) && isVariableUrlValueDifferentFromCurrent(v, vars[key].value) && !isAdHoc(v); return vars.hasOwnProperty(key) && isVariableUrlValueDifferentFromCurrent(v, vars[key].value) && !isAdHoc(v);
}); });
const varGraph = createGraph(variables); const varGraph = createGraph(variables);
@ -772,7 +772,7 @@ const getQueryWithVariables = (key: string, getState: () => StoreState): UrlQuer
const queryParams = locationService.getSearchObject(); const queryParams = locationService.getSearchObject();
const queryParamsNew = Object.keys(queryParams) const queryParamsNew = Object.keys(queryParams)
.filter((key) => key.indexOf('var-') === -1) .filter((key) => key.indexOf(VARIABLE_PREFIX) === -1)
.reduce((obj, key) => { .reduce((obj, key) => {
obj[key] = queryParams[key]; obj[key] = queryParams[key];
return obj; return obj;
@ -784,7 +784,7 @@ const getQueryWithVariables = (key: string, getState: () => StoreState): UrlQuer
} }
const adapter = variableAdapters.get(variable.type); const adapter = variableAdapters.get(variable.type);
queryParamsNew['var-' + variable.name] = adapter.getValueForUrl(variable); queryParamsNew[VARIABLE_PREFIX + variable.name] = adapter.getValueForUrl(variable);
} }
return queryParamsNew; return queryParamsNew;

View File

@ -5,6 +5,7 @@ import { t } from 'app/core/internationalization';
import { useDispatch } from 'app/types'; import { useDispatch } from 'app/types';
import { variableAdapters } from '../adapters'; import { variableAdapters } from '../adapters';
import { VARIABLE_PREFIX } from '../constants';
import { VariablePickerProps } from '../pickers/types'; import { VariablePickerProps } from '../pickers/types';
import { toKeyedAction } from '../state/keyedVariablesReducer'; import { toKeyedAction } from '../state/keyedVariablesReducer';
import { changeVariableProp } from '../state/sharedReducer'; import { changeVariableProp } from '../state/sharedReducer';
@ -72,7 +73,7 @@ export function TextBoxVariablePicker({ variable, onVariableChange, readOnly }:
disabled={readOnly} disabled={readOnly}
onKeyDown={onKeyDown} onKeyDown={onKeyDown}
placeholder={t('variable.textbox.placeholder', 'Enter variable value')} placeholder={t('variable.textbox.placeholder', 'Enter variable value')}
id={`var-${variable.id}`} id={VARIABLE_PREFIX + variable.id}
/> />
); );
} }

View File

@ -9,7 +9,7 @@ import { StoreState } from '../../types';
import { getTimeSrv } from '../dashboard/services/TimeSrv'; import { getTimeSrv } from '../dashboard/services/TimeSrv';
import { variableAdapters } from './adapters'; import { variableAdapters } from './adapters';
import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE } from './constants'; import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE, VARIABLE_PREFIX } from './constants';
import { getVariablesState } from './state/selectors'; import { getVariablesState } from './state/selectors';
import { KeyedVariableIdentifier, VariableIdentifier, VariablePayload } from './state/types'; import { KeyedVariableIdentifier, VariableIdentifier, VariablePayload } from './state/types';
import { QueryVariableModel, TransactionStatus, VariableModel, VariableRefresh, VariableWithOptions } from './types'; import { QueryVariableModel, TransactionStatus, VariableModel, VariableRefresh, VariableWithOptions } from './types';
@ -224,7 +224,7 @@ export function findTemplateVarChanges(query: UrlQueryMap, old: UrlQueryMap): Ex
const changes: ExtendedUrlQueryMap = {}; const changes: ExtendedUrlQueryMap = {};
for (const key in query) { for (const key in query) {
if (!key.startsWith('var-')) { if (!key.startsWith(VARIABLE_PREFIX)) {
continue; continue;
} }
@ -238,7 +238,7 @@ export function findTemplateVarChanges(query: UrlQueryMap, old: UrlQueryMap): Ex
} }
for (const key in old) { for (const key in old) {
if (!key.startsWith('var-')) { if (!key.startsWith(VARIABLE_PREFIX)) {
continue; continue;
} }