Commit 7ab11027 by Ryan McKinley Committed by GitHub

TemplateSrv: expose limited templateSrv in grafana/runtime (#23339)

parent c89ad9b0
...@@ -19,6 +19,7 @@ export * from './datasource'; ...@@ -19,6 +19,7 @@ export * from './datasource';
export * from './panel'; export * from './panel';
export * from './plugin'; export * from './plugin';
export * from './thresholds'; export * from './thresholds';
export * from './templateVars';
export * from './fieldColor'; export * from './fieldColor';
export * from './theme'; export * from './theme';
export * from './orgs'; export * from './orgs';
......
export type VariableType = 'query' | 'adhoc' | 'constant' | 'datasource' | 'interval' | 'textbox' | 'custom';
export interface VariableModel {
type: VariableType;
name: string;
label: string | null;
}
...@@ -3,3 +3,4 @@ export * from './AngularLoader'; ...@@ -3,3 +3,4 @@ export * from './AngularLoader';
export * from './dataSourceSrv'; export * from './dataSourceSrv';
export * from './LocationSrv'; export * from './LocationSrv';
export * from './EchoSrv'; export * from './EchoSrv';
export * from './templateSrv';
import { VariableModel } from '@grafana/data';
export interface TemplateSrv {
getVariables(): VariableModel[];
}
let singletonInstance: TemplateSrv;
export const setTemplateSrv = (instance: TemplateSrv) => {
singletonInstance = instance;
};
export const getTemplateSrv = (): TemplateSrv => singletonInstance;
...@@ -8,13 +8,13 @@ import { cleanUpAction } from '../actions/cleanUp'; ...@@ -8,13 +8,13 @@ import { cleanUpAction } from '../actions/cleanUp';
import { initialTeamsState, teamsLoaded } from '../../features/teams/state/reducers'; import { initialTeamsState, teamsLoaded } from '../../features/teams/state/reducers';
jest.mock('@grafana/runtime', () => ({ jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'),
config: { config: {
bootData: { bootData: {
navTree: [] as NavModelItem[], navTree: [] as NavModelItem[],
user: {}, user: {},
}, },
}, },
DataSourceWithBackend: jest.fn(),
})); }));
describe('recursiveCleanState', () => { describe('recursiveCleanState', () => {
......
...@@ -12,6 +12,7 @@ jest.mock('app/core/store', () => { ...@@ -12,6 +12,7 @@ jest.mock('app/core/store', () => {
}); });
jest.mock('@grafana/runtime', () => ({ jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'),
getDataSourceSrv: () => ({ getDataSourceSrv: () => ({
get: jest.fn(arg => getStub(arg)), get: jest.fn(arg => getStub(arg)),
}), }),
...@@ -22,7 +23,6 @@ jest.mock('@grafana/runtime', () => ({ ...@@ -22,7 +23,6 @@ jest.mock('@grafana/runtime', () => ({
newVariables: false, newVariables: false,
}, },
}, },
DataSourceWithBackend: jest.fn(),
})); }));
describe('given dashboard with repeated panels', () => { describe('given dashboard with repeated panels', () => {
......
...@@ -4,10 +4,10 @@ import { ...@@ -4,10 +4,10 @@ import {
VariableActions, VariableActions,
VariableHide, VariableHide,
VariableOption, VariableOption,
VariableType,
variableTypes, variableTypes,
} from './types'; } from './types';
import { VariableSrv } from './variable_srv'; import { VariableSrv } from './variable_srv';
import { VariableType } from '@grafana/data';
export class TextBoxVariable implements TextBoxVariableModel, VariableActions { export class TextBoxVariable implements TextBoxVariableModel, VariableActions {
type: VariableType; type: VariableType;
......
...@@ -5,10 +5,11 @@ import { ...@@ -5,10 +5,11 @@ import {
assignModelProperties, assignModelProperties,
VariableActions, VariableActions,
VariableHide, VariableHide,
VariableType,
variableTypes, variableTypes,
} from './types'; } from './types';
import { VariableType } from '@grafana/data';
export class AdhocVariable implements AdHocVariableModel, VariableActions { export class AdhocVariable implements AdHocVariableModel, VariableActions {
type: VariableType; type: VariableType;
name: string; name: string;
......
...@@ -4,10 +4,10 @@ import { ...@@ -4,10 +4,10 @@ import {
VariableActions, VariableActions,
VariableHide, VariableHide,
VariableOption, VariableOption,
VariableType,
variableTypes, variableTypes,
} from './types'; } from './types';
import { VariableSrv } from './all'; import { VariableSrv } from './all';
import { VariableType } from '@grafana/data';
export class ConstantVariable implements ConstantVariableModel, VariableActions { export class ConstantVariable implements ConstantVariableModel, VariableActions {
type: VariableType; type: VariableType;
......
...@@ -5,10 +5,10 @@ import { ...@@ -5,10 +5,10 @@ import {
VariableActions, VariableActions,
VariableHide, VariableHide,
VariableOption, VariableOption,
VariableType,
variableTypes, variableTypes,
} from './types'; } from './types';
import { VariableSrv } from './variable_srv'; import { VariableSrv } from './variable_srv';
import { VariableType } from '@grafana/data';
export class CustomVariable implements CustomVariableModel, VariableActions { export class CustomVariable implements CustomVariableModel, VariableActions {
type: VariableType; type: VariableType;
......
...@@ -5,10 +5,9 @@ import { ...@@ -5,10 +5,9 @@ import {
VariableHide, VariableHide,
VariableOption, VariableOption,
VariableRefresh, VariableRefresh,
VariableType,
variableTypes, variableTypes,
} from './types'; } from './types';
import { stringToJsRegex } from '@grafana/data'; import { VariableType, stringToJsRegex } from '@grafana/data';
import { VariableSrv } from './variable_srv'; import { VariableSrv } from './variable_srv';
import { TemplateSrv } from './template_srv'; import { TemplateSrv } from './template_srv';
import { DatasourceSrv } from '../plugins/datasource_srv'; import { DatasourceSrv } from '../plugins/datasource_srv';
......
...@@ -7,12 +7,12 @@ import { ...@@ -7,12 +7,12 @@ import {
VariableHide, VariableHide,
VariableOption, VariableOption,
VariableRefresh, VariableRefresh,
VariableType,
variableTypes, variableTypes,
} from './types'; } from './types';
import { TimeSrv } from '../dashboard/services/TimeSrv'; import { TimeSrv } from '../dashboard/services/TimeSrv';
import { TemplateSrv } from './template_srv'; import { TemplateSrv } from './template_srv';
import { VariableSrv } from './variable_srv'; import { VariableSrv } from './variable_srv';
import { VariableType } from '@grafana/data';
export class IntervalVariable implements IntervalVariableModel, VariableActions { export class IntervalVariable implements IntervalVariableModel, VariableActions {
type: VariableType; type: VariableType;
......
...@@ -8,10 +8,9 @@ import { ...@@ -8,10 +8,9 @@ import {
VariableRefresh, VariableRefresh,
VariableSort, VariableSort,
VariableTag, VariableTag,
VariableType,
variableTypes, variableTypes,
} from './types'; } from './types';
import { DataSourceApi, stringToJsRegex } from '@grafana/data'; import { VariableType, DataSourceApi, stringToJsRegex } from '@grafana/data';
import DatasourceSrv from '../plugins/datasource_srv'; import DatasourceSrv from '../plugins/datasource_srv';
import { TemplateSrv } from './template_srv'; import { TemplateSrv } from './template_srv';
import { VariableSrv } from './variable_srv'; import { VariableSrv } from './variable_srv';
......
...@@ -7,6 +7,7 @@ import { getConfig } from 'app/core/config'; ...@@ -7,6 +7,7 @@ import { getConfig } from 'app/core/config';
import { variableRegex } from './utils'; import { variableRegex } from './utils';
import { isAdHoc } from '../variables/guard'; import { isAdHoc } from '../variables/guard';
import { VariableModel } from './types'; import { VariableModel } from './types';
import { setTemplateSrv, TemplateSrv as BaseTemplateSrv } from '@grafana/runtime';
function luceneEscape(value: string) { function luceneEscape(value: string) {
return value.replace(/([\!\*\+\-\=<>\s\&\|\(\)\[\]\{\}\^\~\?\:\\/"])/g, '\\$1'); return value.replace(/([\!\*\+\-\=<>\s\&\|\(\)\[\]\{\}\^\~\?\:\\/"])/g, '\\$1');
...@@ -28,7 +29,7 @@ const runtimeDependencies: TemplateSrvDependencies = { ...@@ -28,7 +29,7 @@ const runtimeDependencies: TemplateSrvDependencies = {
getVariableWithName, getVariableWithName,
}; };
export class TemplateSrv { export class TemplateSrv implements BaseTemplateSrv {
private _variables: any[]; private _variables: any[];
private regex = variableRegex; private regex = variableRegex;
private index: any = {}; private index: any = {};
...@@ -448,4 +449,7 @@ export class TemplateSrv { ...@@ -448,4 +449,7 @@ export class TemplateSrv {
}; };
} }
export default new TemplateSrv(); // Expose the template srv
const srv = new TemplateSrv();
setTemplateSrv(srv);
export default srv;
import { assignModelProperties } from 'app/core/utils/model_utils'; import { assignModelProperties } from 'app/core/utils/model_utils';
import { Deferred } from '../../core/utils/deferred'; import { Deferred } from '../../core/utils/deferred';
import { VariableModel as BaseVariableModel } from '@grafana/data';
export enum VariableRefresh { export enum VariableRefresh {
never, never,
...@@ -38,8 +39,6 @@ export interface VariableOption { ...@@ -38,8 +39,6 @@ export interface VariableOption {
tags?: VariableTag[]; tags?: VariableTag[];
} }
export type VariableType = 'query' | 'adhoc' | 'constant' | 'datasource' | 'interval' | 'textbox' | 'custom';
export interface AdHocVariableFilter { export interface AdHocVariableFilter {
key: string; key: string;
operator: string; operator: string;
...@@ -93,12 +92,9 @@ export interface VariableWithOptions extends VariableModel { ...@@ -93,12 +92,9 @@ export interface VariableWithOptions extends VariableModel {
query: string; query: string;
} }
export interface VariableModel { export interface VariableModel extends BaseVariableModel {
id?: string; // only exists for variables in redux state id?: string; // only exists for variables in redux state
global?: boolean; // only exists for variables in redux state global?: boolean; // only exists for variables in redux state
type: VariableType;
name: string;
label: string | null;
hide: VariableHide; hide: VariableHide;
skipUrlSync: boolean; skipUrlSync: boolean;
index?: number; index?: number;
......
...@@ -12,12 +12,11 @@ import { ...@@ -12,12 +12,11 @@ import {
TextBoxVariableModel, TextBoxVariableModel,
VariableModel, VariableModel,
VariableOption, VariableOption,
VariableType,
} from '../templating/types'; } from '../templating/types';
import { VariableEditorProps } from './editor/types'; import { VariableEditorProps } from './editor/types';
import { VariablesState } from './state/variablesReducer'; import { VariablesState } from './state/variablesReducer';
import { VariablePickerProps } from './pickers/types'; import { VariablePickerProps } from './pickers/types';
import { Registry } from '@grafana/data'; import { Registry, VariableType } from '@grafana/data';
import { createQueryVariableAdapter } from './query/adapter'; import { createQueryVariableAdapter } from './query/adapter';
import { createCustomVariableAdapter } from './custom/adapter'; import { createCustomVariableAdapter } from './custom/adapter';
import { createTextBoxVariableAdapter } from './textbox/adapter'; import { createTextBoxVariableAdapter } from './textbox/adapter';
......
import React, { ChangeEvent, FormEvent, PureComponent } from 'react'; import React, { ChangeEvent, FormEvent, PureComponent } from 'react';
import isEqual from 'lodash/isEqual'; import isEqual from 'lodash/isEqual';
import { AppEvents } from '@grafana/data'; import { AppEvents, VariableType } from '@grafana/data';
import { FormLabel } from '@grafana/ui'; import { FormLabel } from '@grafana/ui';
import { e2e } from '@grafana/e2e'; import { e2e } from '@grafana/e2e';
import { variableAdapters } from '../adapters'; import { variableAdapters } from '../adapters';
import { NEW_VARIABLE_ID, toVariablePayload, VariableIdentifier } from '../state/types'; import { NEW_VARIABLE_ID, toVariablePayload, VariableIdentifier } from '../state/types';
import { VariableHide, VariableModel, VariableType } from '../../templating/types'; import { VariableHide, VariableModel } from '../../templating/types';
import { appEvents } from '../../../core/core'; import { appEvents } from '../../../core/core';
import { VariableValuesPreview } from './VariableValuesPreview'; import { VariableValuesPreview } from './VariableValuesPreview';
import { changeVariableName, onEditorAdd, onEditorUpdate, variableEditorMount, variableEditorUnMount } from './actions'; import { changeVariableName, onEditorAdd, onEditorUpdate, variableEditorMount, variableEditorUnMount } from './actions';
......
...@@ -17,7 +17,7 @@ import { ...@@ -17,7 +17,7 @@ import {
VariableIdentifier, VariableIdentifier,
} from '../state/types'; } from '../state/types';
import cloneDeep from 'lodash/cloneDeep'; import cloneDeep from 'lodash/cloneDeep';
import { VariableType } from '../../templating/types'; import { VariableType } from '@grafana/data';
import { addVariable, removeVariable, storeNewVariable } from '../state/sharedReducer'; import { addVariable, removeVariable, storeNewVariable } from '../state/sharedReducer';
export const variableEditorMount = (identifier: VariableIdentifier): ThunkResult<void> => { export const variableEditorMount = (identifier: VariableIdentifier): ThunkResult<void> => {
......
import { reducerTester } from '../../../../test/core/redux/reducerTester'; import { reducerTester } from '../../../../test/core/redux/reducerTester';
import { cleanUpDashboard } from 'app/features/dashboard/state/reducers'; import { cleanUpDashboard } from 'app/features/dashboard/state/reducers';
import { QueryVariableModel, VariableHide, VariableType } from '../../templating/types'; import { QueryVariableModel, VariableHide } from '../../templating/types';
import { VariableAdapter, variableAdapters } from '../adapters'; import { VariableAdapter, variableAdapters } from '../adapters';
import { createAction } from '@reduxjs/toolkit'; import { createAction } from '@reduxjs/toolkit';
import { variablesReducer, VariablesState } from './variablesReducer'; import { variablesReducer, VariablesState } from './variablesReducer';
import { toVariablePayload, VariablePayload } from './types'; import { toVariablePayload, VariablePayload } from './types';
import { VariableType } from '@grafana/data';
const variableAdapter: VariableAdapter<QueryVariableModel> = { const variableAdapter: VariableAdapter<QueryVariableModel> = {
id: ('mock' as unknown) as VariableType, id: ('mock' as unknown) as VariableType,
......
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import cloneDeep from 'lodash/cloneDeep'; import cloneDeep from 'lodash/cloneDeep';
import { VariableModel, VariableOption, VariableType, VariableWithOptions } from '../../templating/types'; import { VariableType } from '@grafana/data';
import { VariableModel, VariableOption, VariableWithOptions } from '../../templating/types';
import { AddVariable, ALL_VARIABLE_VALUE, getInstanceState, NEW_VARIABLE_ID, VariablePayload } from './types'; import { AddVariable, ALL_VARIABLE_VALUE, getInstanceState, NEW_VARIABLE_ID, VariablePayload } from './types';
import { variableAdapters } from '../adapters'; import { variableAdapters } from '../adapters';
import { changeVariableNameSucceeded } from '../editor/reducer'; import { changeVariableNameSucceeded } from '../editor/reducer';
......
import { VariableModel, VariableType } from '../../templating/types'; import { VariableModel } from '../../templating/types';
import { VariablesState } from './variablesReducer'; import { VariablesState } from './variablesReducer';
import { VariableType } from '@grafana/data';
export const NEW_VARIABLE_ID = '00000000-0000-0000-0000-000000000000'; export const NEW_VARIABLE_ID = '00000000-0000-0000-0000-000000000000';
export const ALL_VARIABLE_TEXT = 'All'; export const ALL_VARIABLE_TEXT = 'All';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment