Skip to content

Commit

Permalink
Link suppliers: getLinks API update (#29757)
Browse files Browse the repository at this point in the history
* ContextMenuPlugin WIP

* Remove Add annotations menu item from graph context menu

* ts ifx

* WIP

* Tests updates

* ts check fix

* Fix rebase

* Use replace function in angular graph data links
  • Loading branch information
dprokop committed Dec 15, 2020
1 parent 5f4b528 commit 683ce69
Show file tree
Hide file tree
Showing 25 changed files with 455 additions and 445 deletions.
4 changes: 1 addition & 3 deletions packages/grafana-data/src/types/ScopedVars.ts
Expand Up @@ -4,6 +4,4 @@ export interface ScopedVar<T = any> {
[key: string]: any;
}

export interface ScopedVars {
[key: string]: ScopedVar;
}
export interface ScopedVars extends Record<string, ScopedVar> {}
6 changes: 3 additions & 3 deletions packages/grafana-data/src/types/dataLink.ts
@@ -1,12 +1,12 @@
import { ScopedVars } from './ScopedVars';
import { DataQuery } from './datasource';
import { InterpolateFunction } from './panel';

/**
* Callback info for DataLink click events
*/
export interface DataLinkClickEvent<T = any> {
origin: T;
scopedVars?: ScopedVars;
replaceVariables: InterpolateFunction | undefined;
e?: any; // mouse|react event
}

Expand Down Expand Up @@ -67,7 +67,7 @@ export interface LinkModel<T = any> {
* TODO: ScopedVars in in GrafanaUI package!
*/
export interface LinkModelSupplier<T extends object> {
getLinks(scopedVars?: any): Array<LinkModel<T>>;
getLinks(replaceVariables?: InterpolateFunction): Array<LinkModel<T>>;
}

export enum VariableOrigin {
Expand Down
@@ -1,7 +1,11 @@
import React from 'react';
import { shallow, ShallowWrapper } from 'enzyme';
import { setTemplateSrv } from '@grafana/runtime';
import config from 'app/core/config';
import { ShareLink, Props, State } from './ShareLink';
import { initTemplateSrv } from '../../../../../test/helpers/initTemplateSrv';
import { variableAdapters } from '../../../variables/adapters';
import { createQueryVariableAdapter } from '../../../variables/query/adapter';

jest.mock('app/features/dashboard/services/TimeSrv', () => ({
getTimeSrv: () => ({
Expand All @@ -11,16 +15,6 @@ jest.mock('app/features/dashboard/services/TimeSrv', () => ({
}),
}));

let fillVariableValuesForUrlMock = (params: any) => {};

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

function mockLocationHref(href: string) {
const location = window.location;

Expand Down Expand Up @@ -98,6 +92,13 @@ function shareLinkScenario(description: string, scenarioFn: (ctx: ScenarioContex
}

describe('ShareModal', () => {
let templateSrv = initTemplateSrv([]);

beforeAll(() => {
variableAdapters.register(createQueryVariableAdapter());
setTemplateSrv(templateSrv);
});

shareLinkScenario('shareUrl with current time range and panel', ctx => {
ctx.setup(() => {
mockLocationHref('http://server/#!/test');
Expand Down Expand Up @@ -174,33 +175,35 @@ describe('ShareModal', () => {
expect(state?.imageUrl).toContain('?from=1000&to=2000&orgId=1&panelId=1&width=1000&height=500&tz=UTC');
});

it('should include template variables in url', async () => {
mockLocationHref('http://server/#!/test');
fillVariableValuesForUrlMock = (params: any) => {
params['var-app'] = 'mupp';
params['var-server'] = 'srv-01';
};
ctx.mount();
ctx.wrapper?.setState({ includeTemplateVars: true });
describe('template variables', () => {
beforeEach(() => {
templateSrv = initTemplateSrv([
{ type: 'query', name: 'app', current: { value: 'mupp' } },
{ type: 'query', name: 'server', current: { value: 'srv-01' } },
]);
setTemplateSrv(templateSrv);
});

await ctx.wrapper?.instance().buildUrl();
const state = ctx.wrapper?.state();
expect(state?.shareUrl).toContain(
'http://server/#!/test?from=1000&to=2000&orgId=1&var-app=mupp&var-server=srv-01'
);
});
it('should include template variables in url', async () => {
mockLocationHref('http://server/#!/test');
ctx.mount();
ctx.wrapper?.setState({ includeTemplateVars: true });

it('should shorten url', () => {
mockLocationHref('http://server/#!/test');
fillVariableValuesForUrlMock = (params: any) => {
params['var-app'] = 'mupp';
params['var-server'] = 'srv-01';
};
ctx.mount();
ctx.wrapper?.setState({ includeTemplateVars: true, useShortUrl: true }, async () => {
await ctx.wrapper?.instance().buildUrl();
const state = ctx.wrapper?.state();
expect(state?.shareUrl).toContain(`/goto/${mockUid}`);
expect(state?.shareUrl).toContain(
'http://server/#!/test?from=1000&to=2000&orgId=1&var-app=mupp&var-server=srv-01'
);
});

it('should shorten url', () => {
mockLocationHref('http://server/#!/test');
ctx.mount();
ctx.wrapper?.setState({ includeTemplateVars: true, useShortUrl: true }, async () => {
await ctx.wrapper?.instance().buildUrl();
const state = ctx.wrapper?.state();
expect(state?.shareUrl).toContain(`/goto/${mockUid}`);
});
});
});
});
Expand Down
9 changes: 6 additions & 3 deletions public/app/features/dashboard/components/ShareModal/utils.ts
@@ -1,16 +1,16 @@
import { config } from '@grafana/runtime';
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { getTemplateSrv } from 'app/features/templating/template_srv';
import { createShortLink } from 'app/core/utils/shortLinks';
import { PanelModel, dateTime, urlUtil } from '@grafana/data';
import { getAllVariableValuesForUrl } from 'app/features/variables/getAllVariableValuesForUrl';

export function buildParams(
useCurrentTimeRange: boolean,
includeTemplateVars: boolean,
selectedTheme?: string,
panel?: PanelModel
) {
const params = urlUtil.getUrlSearchParams();
let params = urlUtil.getUrlSearchParams();

const range = getTimeSrv().timeRange();
params.from = range.from.valueOf();
Expand All @@ -23,7 +23,10 @@ export function buildParams(
}

if (includeTemplateVars) {
getTemplateSrv().fillVariableValuesForUrl(params);
params = {
...params,
...getAllVariableValuesForUrl(),
};
}

if (selectedTheme !== 'current') {
Expand Down
1 change: 0 additions & 1 deletion public/app/features/dashboard/dashgrid/PanelChrome.tsx
Expand Up @@ -332,7 +332,6 @@ export class PanelChrome extends PureComponent<Props, State> {
dashboard={dashboard}
title={panel.title}
description={panel.description}
scopedVars={panel.scopedVars}
links={panel.links}
error={errorMessage}
isEditing={isEditing}
Expand Down
Expand Up @@ -247,7 +247,6 @@ export class PanelChromeAngularUnconnected extends PureComponent<Props, State> {
dashboard={dashboard}
title={panel.title}
description={panel.description}
scopedVars={panel.scopedVars}
angularComponent={angularComponent}
links={panel.links}
error={errorMessage}
Expand Down
@@ -1,7 +1,7 @@
import React, { PureComponent } from 'react';
import classNames from 'classnames';
import { DataLink, LoadingState, PanelData, PanelMenuItem, QueryResultMetaNotice, ScopedVars } from '@grafana/data';
import { AngularComponent, config, getTemplateSrv } from '@grafana/runtime';
import { DataLink, LoadingState, PanelData, PanelMenuItem, QueryResultMetaNotice } from '@grafana/data';
import { AngularComponent, config } from '@grafana/runtime';
import { ClickOutsideWrapper, Icon, IconName, Tooltip, stylesFactory } from '@grafana/ui';
import { selectors } from '@grafana/e2e-selectors';

Expand All @@ -20,7 +20,6 @@ export interface Props {
dashboard: DashboardModel;
title?: string;
description?: string;
scopedVars?: ScopedVars;
angularComponent?: AngularComponent | null;
links?: DataLink[];
error?: string;
Expand Down Expand Up @@ -148,9 +147,9 @@ export class PanelHeader extends PureComponent<Props, State> {
};

render() {
const { panel, scopedVars, error, isViewing, isEditing, data, alertState } = this.props;
const { panel, error, isViewing, isEditing, data, alertState } = this.props;
const { menuItems } = this.state;
const title = getTemplateSrv().replace(panel.title, scopedVars, 'text');
const title = panel.replaceVariables(panel.title, {}, 'text');

const panelHeaderClass = classNames({
'panel-header': true,
Expand Down
Expand Up @@ -46,7 +46,7 @@ export class PanelHeaderCorner extends Component<Props> {
const markdown = panel.description || '';
const interpolatedMarkdown = getTemplateSrv().replace(markdown, panel.scopedVars);
const markedInterpolatedMarkdown = renderMarkdown(interpolatedMarkdown);
const links = this.props.links && this.props.links.getLinks(panel);
const links = this.props.links && this.props.links.getLinks(panel.replaceVariables);

return (
<div className="panel-info-content markdown-html">
Expand Down

0 comments on commit 683ce69

Please sign in to comment.