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 NONE_VARIABLE_TEXT = 'None';
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 { variableAdapters } from './adapters';
import { VARIABLE_PREFIX } from './constants';
export function getVariablesUrlParams(scopedVars?: ScopedVars): UrlQueryMap {
const params: UrlQueryMap = {};
@ -13,13 +14,13 @@ export function getVariablesUrlParams(scopedVars?: ScopedVars): UrlQueryMap {
if (scopedVars[variable.name].skipUrlSync) {
continue;
}
params['var-' + variable.name] = scopedVars[variable.name].value;
params[VARIABLE_PREFIX + variable.name] = scopedVars[variable.name].value;
} else {
// @ts-ignore
if (variable.skipUrlSync) {
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 { StoreState, ThunkDispatch } from 'app/types';
import { VARIABLE_PREFIX } from '../../constants';
import { isMulti } from '../../guard';
import { getVariableQueryRunner } from '../../query/VariableQueryRunner';
import { formatVariableLabel } from '../../shared/formatVariable';
@ -125,7 +126,7 @@ export const optionPickerFactory = <Model extends VariableWithOptions | Variable
return (
<VariableLink
id={`var-${variable.id}`}
id={VARIABLE_PREFIX + variable.id}
text={linkText}
onClick={this.onShowOptions}
loading={loading}
@ -144,7 +145,7 @@ export const optionPickerFactory = <Model extends VariableWithOptions | Variable
return (
<ClickOutsideWrapper onClick={this.onHideOptions}>
<VariableInput
id={`var-${id}`}
id={VARIABLE_PREFIX + id}
value={picker.queryValue}
onChange={this.onFilterOrSearchOptions}
onNavigate={this.onNavigate}

View File

@ -4,6 +4,7 @@ import { selectors } from '@grafana/e2e-selectors';
import { Tooltip } from '@grafana/ui';
import { variableAdapters } from '../adapters';
import { VARIABLE_PREFIX } from '../constants';
import { VariableHide, VariableModel } from '../types';
interface Props {
@ -35,7 +36,7 @@ function PickerLabel({ variable }: PropsWithChildren<Props>): ReactElement | nul
return null;
}
const elementId = `var-${variable.id}`;
const elementId = VARIABLE_PREFIX + variable.id;
if (variable.description) {
return (
<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 { getTemplateSrv, TemplateSrv } from '../../templating/template_srv';
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 {
hasCurrent,
@ -359,7 +359,7 @@ export const processVariable = (
const variable = getVariable(identifier, getState());
await processVariableDependencies(variable, getState());
const urlValue = queryParams['var-' + variable.name];
const urlValue = queryParams[VARIABLE_PREFIX + variable.name];
if (urlValue !== void 0) {
const stringUrlValue = ensureStringValues(urlValue);
await variableAdapters.get(variable.type).setValueFromUrl(variable, stringUrlValue);
@ -705,7 +705,7 @@ export const templateVarsChangedInUrl =
const variables = getVariablesByKey(key, getState());
for (const variable of variables) {
const key = `var-${variable.name}`;
const key = VARIABLE_PREFIX + variable.name;
if (!vars.hasOwnProperty(key)) {
// key not found quick exit
continue;
@ -735,7 +735,7 @@ export const templateVarsChangedInUrl =
}
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);
});
const varGraph = createGraph(variables);
@ -772,7 +772,7 @@ const getQueryWithVariables = (key: string, getState: () => StoreState): UrlQuer
const queryParams = locationService.getSearchObject();
const queryParamsNew = Object.keys(queryParams)
.filter((key) => key.indexOf('var-') === -1)
.filter((key) => key.indexOf(VARIABLE_PREFIX) === -1)
.reduce((obj, key) => {
obj[key] = queryParams[key];
return obj;
@ -784,7 +784,7 @@ const getQueryWithVariables = (key: string, getState: () => StoreState): UrlQuer
}
const adapter = variableAdapters.get(variable.type);
queryParamsNew['var-' + variable.name] = adapter.getValueForUrl(variable);
queryParamsNew[VARIABLE_PREFIX + variable.name] = adapter.getValueForUrl(variable);
}
return queryParamsNew;

View File

@ -5,6 +5,7 @@ import { t } from 'app/core/internationalization';
import { useDispatch } from 'app/types';
import { variableAdapters } from '../adapters';
import { VARIABLE_PREFIX } from '../constants';
import { VariablePickerProps } from '../pickers/types';
import { toKeyedAction } from '../state/keyedVariablesReducer';
import { changeVariableProp } from '../state/sharedReducer';
@ -72,7 +73,7 @@ export function TextBoxVariablePicker({ variable, onVariableChange, readOnly }:
disabled={readOnly}
onKeyDown={onKeyDown}
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 { 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 { KeyedVariableIdentifier, VariableIdentifier, VariablePayload } from './state/types';
import { QueryVariableModel, TransactionStatus, VariableModel, VariableRefresh, VariableWithOptions } from './types';
@ -224,7 +224,7 @@ export function findTemplateVarChanges(query: UrlQueryMap, old: UrlQueryMap): Ex
const changes: ExtendedUrlQueryMap = {};
for (const key in query) {
if (!key.startsWith('var-')) {
if (!key.startsWith(VARIABLE_PREFIX)) {
continue;
}
@ -238,7 +238,7 @@ export function findTemplateVarChanges(query: UrlQueryMap, old: UrlQueryMap): Ex
}
for (const key in old) {
if (!key.startsWith('var-')) {
if (!key.startsWith(VARIABLE_PREFIX)) {
continue;
}