Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Remove angular dependency from data sources #27735

Merged
merged 5 commits into from Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions public/app/core/filters/filters.ts
@@ -1,7 +1,7 @@
import _ from 'lodash';
import angular from 'angular';
import coreModule from '../core_module';
import { TemplateSrv } from 'app/features/templating/template_srv';
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
import { dateTime } from '@grafana/data';

coreModule.filter('stringSort', () => {
Expand Down Expand Up @@ -39,8 +39,7 @@ coreModule.filter('moment', () => {
};
});

/** @ngInject */
function interpolateTemplateVars(templateSrv: TemplateSrv) {
function interpolateTemplateVars(templateSrv: TemplateSrv = getTemplateSrv()) {
const filterFunc: any = (text: string, scope: any) => {
let scopedVars;
if (scope.ctrl) {
Expand Down
Expand Up @@ -3,10 +3,10 @@ import classNames from 'classnames';
import { Icon } from '@grafana/ui';
import { PanelModel } from '../../state/PanelModel';
import { DashboardModel } from '../../state/DashboardModel';
import templateSrv from 'app/features/templating/template_srv';
import appEvents from 'app/core/app_events';
import { CoreEvents } from 'app/types';
import { RowOptionsButton } from '../RowOptions/RowOptionsButton';
import { getTemplateSrv } from '@grafana/runtime';

export interface DashboardRowProps {
panel: PanelModel;
Expand Down Expand Up @@ -69,7 +69,7 @@ export class DashboardRow extends React.Component<DashboardRowProps, any> {
'dashboard-row--collapsed': this.state.collapsed,
});

const title = templateSrv.replace(this.props.panel.title, this.props.panel.scopedVars, 'text');
const title = getTemplateSrv().replace(this.props.panel.title, this.props.panel.scopedVars, 'text');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This component uses only the replace function, should be ok to just import getTemplateSrv from grafana/runtime?

const count = this.props.panel.panels ? this.props.panel.panels.length : 0;
const panels = count === 1 ? 'panel' : 'panels';
const canEdit = this.props.dashboard.meta.canEdit === true;
Expand Down
Expand Up @@ -14,9 +14,11 @@ jest.mock('app/features/dashboard/services/TimeSrv', () => ({
let fillVariableValuesForUrlMock = (params: any) => {};

jest.mock('app/features/templating/template_srv', () => ({
fillVariableValuesForUrl: (params: any) => {
fillVariableValuesForUrlMock(params);
},
getTemplateSrv: () => ({
fillVariableValuesForUrl: (params: any) => {
fillVariableValuesForUrlMock(params);
},
}),
}));

function mockLocationHref(href: string) {
Expand Down
@@ -1,6 +1,6 @@
import { config } from '@grafana/runtime';
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
import templateSrv from 'app/features/templating/template_srv';
import { getTemplateSrv } from 'app/features/templating/template_srv';
import { PanelModel, dateTime, urlUtil } from '@grafana/data';

export function buildParams(
Expand All @@ -22,7 +22,7 @@ export function buildParams(
}

if (includeTemplateVars) {
templateSrv.fillVariableValuesForUrl(params);
getTemplateSrv().fillVariableValuesForUrl(params);
}

if (selectedTheme !== 'current') {
Expand Down
Expand Up @@ -2,13 +2,12 @@ import React, { Component } from 'react';
import classNames from 'classnames';
import { isEqual } from 'lodash';
import { DataLink, LoadingState, PanelData, PanelMenuItem, QueryResultMetaNotice, ScopedVars } from '@grafana/data';
import { AngularComponent } from '@grafana/runtime';
import { AngularComponent, getTemplateSrv } from '@grafana/runtime';
import { ClickOutsideWrapper, Icon, IconName, Tooltip } from '@grafana/ui';
import { selectors } from '@grafana/e2e-selectors';

import PanelHeaderCorner from './PanelHeaderCorner';
import { PanelHeaderMenu } from './PanelHeaderMenu';
import templateSrv from 'app/features/templating/template_srv';

import { DashboardModel } from 'app/features/dashboard/state/DashboardModel';
import { PanelModel } from 'app/features/dashboard/state/PanelModel';
Expand Down Expand Up @@ -137,7 +136,7 @@ export class PanelHeader extends Component<Props, State> {
render() {
const { panel, scopedVars, error, isViewing, isEditing, data, alertState } = this.props;
const { menuItems } = this.state;
const title = templateSrv.replace(panel.title, scopedVars, 'text');
const title = getTemplateSrv().replace(panel.title, scopedVars, 'text');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This component uses only the replace function, should be ok to just import getTemplateSrv from grafana/runtime?


const panelHeaderClass = classNames({
'panel-header': true,
Expand Down
Expand Up @@ -2,10 +2,9 @@ import React, { Component } from 'react';

import { renderMarkdown, LinkModelSupplier, ScopedVars } from '@grafana/data';
import { Tooltip, PopoverContent } from '@grafana/ui';
import { getLocationSrv } from '@grafana/runtime';
import { getLocationSrv, getTemplateSrv } from '@grafana/runtime';

import { PanelModel } from 'app/features/dashboard/state/PanelModel';
import templateSrv from 'app/features/templating/template_srv';
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { InspectTab } from '../../components/Inspector/types';

Expand Down Expand Up @@ -45,7 +44,7 @@ export class PanelHeaderCorner extends Component<Props> {
getInfoContent = (): JSX.Element => {
const { panel } = this.props;
const markdown = panel.description || '';
const interpolatedMarkdown = templateSrv.replace(markdown, panel.scopedVars);
const interpolatedMarkdown = getTemplateSrv().replace(markdown, panel.scopedVars);
const markedInterpolatedMarkdown = renderMarkdown(interpolatedMarkdown);
const links = this.props.links && this.props.links.getLinks(panel);

Expand Down
4 changes: 2 additions & 2 deletions public/app/features/dashboard/state/PanelModel.ts
@@ -1,9 +1,9 @@
// Libraries
import _ from 'lodash';
// Utils
import { getTemplateSrv } from '@grafana/runtime';
import { Emitter } from 'app/core/utils/emitter';
import { getNextRefIdChar } from 'app/core/utils/query';
import templateSrv from 'app/features/templating/template_srv';
// Types
import {
AppEvent,
Expand Down Expand Up @@ -478,7 +478,7 @@ export class PanelModel implements DataConfigSource {
if (extraVars) {
vars = vars ? { ...vars, ...extraVars } : extraVars;
}
return templateSrv.replace(value, vars, format);
return getTemplateSrv().replace(value, vars, format);
}

resendLastResult() {
Expand Down
4 changes: 2 additions & 2 deletions public/app/features/dashboard/state/PanelQueryRunner.ts
Expand Up @@ -4,8 +4,8 @@ import { ReplaySubject, Unsubscribable, Observable } from 'rxjs';
import { map } from 'rxjs/operators';

// Services & Utils
import { getTemplateSrv } from '@grafana/runtime';
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
import templateSrv from 'app/features/templating/template_srv';
import { runRequest, preProcessPanelData } from './runRequest';
import { runSharedRequest, isSharedDashboardQuery } from '../../../plugins/datasource/dashboard';

Expand Down Expand Up @@ -164,7 +164,7 @@ export class PanelQueryRunner {
return query;
});

const lowerIntervalLimit = minInterval ? templateSrv.replace(minInterval, request.scopedVars) : ds.interval;
const lowerIntervalLimit = minInterval ? getTemplateSrv().replace(minInterval, request.scopedVars) : ds.interval;
const norm = rangeUtil.calculateInterval(timeRange, maxDataPoints, lowerIntervalLimit);

// make shallow copy of scoped vars,
Expand Down
6 changes: 3 additions & 3 deletions public/app/features/dashboard/utils/panel.ts
Expand Up @@ -14,7 +14,7 @@ import appEvents from 'app/core/app_events';
import config from 'app/core/config';

// Services
import templateSrv from 'app/features/templating/template_srv';
import { getTemplateSrv } from '@grafana/runtime';

// Constants
import { LS_PANEL_COPY_KEY, PANEL_BORDER } from 'app/core/constants';
Expand Down Expand Up @@ -84,7 +84,7 @@ export function applyPanelTimeOverrides(panel: PanelModel, timeRange: TimeRange)
};

if (panel.timeFrom) {
const timeFromInterpolated = templateSrv.replace(panel.timeFrom, panel.scopedVars);
const timeFromInterpolated = getTemplateSrv().replace(panel.timeFrom, panel.scopedVars);
const timeFromInfo = rangeUtil.describeTextRange(timeFromInterpolated);
if (timeFromInfo.invalid) {
newTimeData.timeInfo = 'invalid time override';
Expand All @@ -106,7 +106,7 @@ export function applyPanelTimeOverrides(panel: PanelModel, timeRange: TimeRange)
}

if (panel.timeShift) {
const timeShiftInterpolated = templateSrv.replace(panel.timeShift, panel.scopedVars);
const timeShiftInterpolated = getTemplateSrv().replace(panel.timeShift, panel.scopedVars);
const timeShiftInfo = rangeUtil.describeTextRange(timeShiftInterpolated);
if (timeShiftInfo.invalid) {
newTimeData.timeInfo = 'invalid timeshift';
Expand Down
26 changes: 15 additions & 11 deletions public/app/features/panel/panellinks/link_srv.ts
@@ -1,6 +1,6 @@
import _ from 'lodash';
import { TimeSrv } from 'app/features/dashboard/services/TimeSrv';
import templateSrv, { TemplateSrv } from 'app/features/templating/template_srv';
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
import coreModule from 'app/core/core_module';
import { getConfig } from 'app/core/config';
import {
Expand Down Expand Up @@ -80,11 +80,13 @@ const buildLabelPath = (label: string) => {
};

export const getPanelLinksVariableSuggestions = (): VariableSuggestion[] => [
...templateSrv.getVariables().map(variable => ({
value: variable.name as string,
label: variable.name,
origin: VariableOrigin.Template,
})),
...getTemplateSrv()
.getVariables()
.map(variable => ({
value: variable.name as string,
label: variable.name,
origin: VariableOrigin.Template,
})),
{
value: `${DataLinkBuiltInVars.includeVars}`,
label: 'All variables',
Expand Down Expand Up @@ -240,11 +242,13 @@ export const getPanelOptionsVariableSuggestions = (plugin: PanelPlugin, data?: D
const dataVariables = plugin.meta.skipDataQuery ? [] : getDataFrameVars(data || []);
return [
...dataVariables, // field values
...templateSrv.getVariables().map(variable => ({
value: variable.name as string,
label: variable.name,
origin: VariableOrigin.Template,
})),
...getTemplateSrv()
.getVariables()
.map(variable => ({
value: variable.name as string,
label: variable.name,
origin: VariableOrigin.Template,
})),
];
};

Expand Down
7 changes: 5 additions & 2 deletions public/app/features/plugins/datasource_srv.ts
Expand Up @@ -4,11 +4,14 @@ import coreModule from 'app/core/core_module';
// Services & Utils
import config from 'app/core/config';
import { importDataSourcePlugin } from './plugin_loader';
import { DataSourceSrv as DataSourceService, getDataSourceSrv as getDataSourceService } from '@grafana/runtime';
import {
DataSourceSrv as DataSourceService,
getDataSourceSrv as getDataSourceService,
TemplateSrv,
} from '@grafana/runtime';
// Types
import { AppEvents, DataSourceApi, DataSourceInstanceSettings, DataSourceSelectItem, ScopedVars } from '@grafana/data';
import { auto } from 'angular';
import { TemplateSrv } from '../templating/template_srv';
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
// Pretend Datasource
import { expressionDatasource } from 'app/features/expressions/ExpressionDatasource';
Expand Down
Expand Up @@ -4,7 +4,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import DefaultVariableQueryEditor from '../variables/editor/DefaultVariableQueryEditor';
import { DataSourcePluginMeta } from '@grafana/data';
import { TemplateSrv } from '../templating/template_srv';
import { TemplateSrv } from '@grafana/runtime';

async function loadComponent(meta: DataSourcePluginMeta) {
const dsPlugin = await importDataSourcePlugin(meta);
Expand Down
4 changes: 2 additions & 2 deletions public/app/features/templating/all.ts
@@ -1,4 +1,4 @@
import coreModule from 'app/core/core_module';
import templateSrv from './template_srv';
import { getTemplateSrv } from './template_srv';

coreModule.factory('templateSrv', () => templateSrv);
coreModule.factory('templateSrv', () => getTemplateSrv());
2 changes: 1 addition & 1 deletion public/app/features/templating/template_srv.ts
Expand Up @@ -336,4 +336,4 @@ export class TemplateSrv implements BaseTemplateSrv {
// Expose the template srv
const srv = new TemplateSrv();
setTemplateSrv(srv);
export default srv;
export const getTemplateSrv = () => srv;
4 changes: 2 additions & 2 deletions public/app/features/variables/datasource/actions.ts
Expand Up @@ -6,7 +6,7 @@ import { DataSourceSelectItem, stringToJsRegex } from '@grafana/data';
import { getDatasourceSrv } from '../../plugins/datasource_srv';
import { getVariable } from '../state/selectors';
import { DataSourceVariableModel } from '../types';
import templateSrv from '../../templating/template_srv';
import { getTemplateSrv } from '@grafana/runtime';
import _ from 'lodash';
import { changeVariableEditorExtended } from '../editor/reducer';

Expand All @@ -23,7 +23,7 @@ export const updateDataSourceVariableOptions = (
let regex;

if (variableInState.regex) {
regex = templateSrv.replace(variableInState.regex, undefined, 'regex');
regex = getTemplateSrv().replace(variableInState.regex, undefined, 'regex');
regex = stringToJsRegex(regex);
}

Expand Down
6 changes: 3 additions & 3 deletions public/app/features/variables/interval/actions.ts
Expand Up @@ -7,7 +7,7 @@ import { validateVariableSelectionState } from '../state/actions';
import { getVariable } from '../state/selectors';
import { IntervalVariableModel } from '../types';
import { getTimeSrv } from '../../dashboard/services/TimeSrv';
import templateSrv from '../../templating/template_srv';
import { getTemplateSrv, TemplateSrv } from '../../templating/template_srv';
import appEvents from '../../../core/app_events';

export interface UpdateIntervalVariableOptionsDependencies {
Expand All @@ -30,15 +30,15 @@ export const updateIntervalVariableOptions = (
export interface UpdateAutoValueDependencies {
calculateInterval: typeof rangeUtil.calculateInterval;
getTimeSrv: typeof getTimeSrv;
templateSrv: typeof templateSrv;
templateSrv: TemplateSrv;
}

export const updateAutoValue = (
identifier: VariableIdentifier,
dependencies: UpdateAutoValueDependencies = {
calculateInterval: rangeUtil.calculateInterval,
getTimeSrv: getTimeSrv,
templateSrv: templateSrv,
templateSrv: getTemplateSrv(),
}
): ThunkResult<void> => (dispatch, getState) => {
const variableInState = getVariable<IntervalVariableModel>(identifier.id, getState());
Expand Down
4 changes: 2 additions & 2 deletions public/app/features/variables/query/QueryVariableEditor.tsx
Expand Up @@ -2,7 +2,7 @@ import React, { ChangeEvent, PureComponent } from 'react';
import { InlineFormLabel, LegacyForms } from '@grafana/ui';
import { selectors } from '@grafana/e2e-selectors';

import templateSrv from '../../templating/template_srv';
import { getTemplateSrv } from '@grafana/runtime';
import { SelectionOptionsEditor } from '../editor/SelectionOptionsEditor';
import { QueryVariableModel, VariableRefresh, VariableSort, VariableWithMultiSupport } from '../types';
import { QueryVariableEditorState } from './reducer';
Expand Down Expand Up @@ -185,7 +185,7 @@ export class QueryVariableEditorUnConnected extends PureComponent<Props, State>
<VariableQueryEditor
datasource={this.props.editor.extended?.dataSource}
query={this.props.variable.query}
templateSrv={templateSrv}
templateSrv={getTemplateSrv()}
onChange={this.onQueryChange}
/>
)}
Expand Down
4 changes: 2 additions & 2 deletions public/app/features/variables/query/actions.ts
@@ -1,9 +1,9 @@
import { AppEvents, DataSourcePluginMeta, DataSourceSelectItem } from '@grafana/data';
import { getTemplateSrv } from '@grafana/runtime';
import { validateVariableSelectionState } from '../state/actions';
import { QueryVariableModel, VariableRefresh } from '../types';
import { ThunkResult } from '../../../types';
import { getDatasourceSrv } from '../../plugins/datasource_srv';
import templateSrv from '../../templating/template_srv';
import { getTimeSrv } from '../../dashboard/services/TimeSrv';
import appEvents from '../../../core/app_events';
import { importDataSourcePlugin } from '../../plugins/plugin_loader';
Expand Down Expand Up @@ -138,5 +138,5 @@ const getTemplatedRegex = (variable: QueryVariableModel): string => {
return '';
}

return templateSrv.replace(variable.regex, {}, 'regex');
return getTemplateSrv().replace(variable.regex, {}, 'regex');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe runtime import for this one?

};
8 changes: 4 additions & 4 deletions public/app/features/variables/state/actions.ts
Expand Up @@ -30,7 +30,7 @@ import {
import { toVariableIdentifier, toVariablePayload, VariableIdentifier } from './types';
import { appEvents } from 'app/core/core';
import { contextSrv } from 'app/core/services/context_srv';
import templateSrv from '../../templating/template_srv';
import { getTemplateSrv, TemplateSrv } from '../../templating/template_srv';
import { alignCurrentWithMulti } from '../shared/multiOptions';
import { isMulti } from '../guard';
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
Expand Down Expand Up @@ -89,7 +89,7 @@ export const initDashboardTemplating = (list: VariableModel[]): ThunkResult<void
dispatch(addVariable(toVariablePayload(model, { global: false, index: orderIndex++, model })));
}

templateSrv.updateTimeRange(getTimeSrv().timeRange());
getTemplateSrv().updateTimeRange(getTimeSrv().timeRange());

for (let index = 0; index < getVariables(getState()).length; index++) {
dispatch(addInitLock(toVariablePayload(getVariables(getState())[index])));
Expand Down Expand Up @@ -458,13 +458,13 @@ export const variableUpdated = (
};

export interface OnTimeRangeUpdatedDependencies {
templateSrv: typeof templateSrv;
templateSrv: TemplateSrv;
appEvents: typeof appEvents;
}

export const onTimeRangeUpdated = (
timeRange: TimeRange,
dependencies: OnTimeRangeUpdatedDependencies = { templateSrv: templateSrv, appEvents: appEvents }
dependencies: OnTimeRangeUpdatedDependencies = { templateSrv: getTemplateSrv(), appEvents: appEvents }
): ThunkResult<Promise<void>> => async (dispatch, getState) => {
dependencies.templateSrv.updateTimeRange(timeRange);
const variablesThatNeedRefresh = getVariables(getState()).filter(variable => {
Expand Down
@@ -1,12 +1,9 @@
import { TemplateSrv } from 'app/features/templating/template_srv';
import { AnnotationTarget } from './types';

export class CloudMonitoringAnnotationsQueryCtrl {
static templateUrl = 'partials/annotations.editor.html';
annotation: any;
templateSrv: TemplateSrv;

/** @ngInject */
constructor() {
this.annotation.target = this.annotation.target || {};
this.onQueryChange = this.onQueryChange.bind(this);
Expand Down