Migrate to UID: Stop using search result ID (#54099)

* DashList: Stop using IDs

* DashLinks: Stop using IDs

* BackendSrv: Do not use ID for search endpoint

* DashboardDataDTO: Remove ID

* Remove unused properties from DashboardSearchItem
This commit is contained in:
Ivan Ortega Alba
2022-08-26 09:42:46 +02:00
committed by GitHub
parent e27769da9c
commit c332bf885c
15 changed files with 90 additions and 65 deletions

View File

@@ -37,7 +37,7 @@ export const DashboardLinks: FC<Props> = ({ dashboard, links }) => {
const key = `${link.title}-$${index}`;
if (link.type === 'dashboards') {
return <DashboardLinksDashboard key={key} link={link} linkInfo={linkInfo} dashboardId={dashboard.id} />;
return <DashboardLinksDashboard key={key} link={link} linkInfo={linkInfo} dashboardUID={dashboard.uid} />;
}
const linkElement = (

View File

@@ -1,4 +1,4 @@
import { DashboardSearchHit, DashboardSearchItemType } from '../../../search/types';
import { DashboardSearchItem, DashboardSearchItemType } from '../../../search/types';
import { DashboardLink } from '../../state/DashboardModel';
import { resolveLinks, searchForTags } from './DashboardLinksDashboard';
@@ -39,7 +39,7 @@ describe('searchForTags', () => {
});
describe('resolveLinks', () => {
const setupTestContext = (dashboardId: number, searchHitId: number) => {
const setupTestContext = (dashboardUID: string, searchHitId: string) => {
const link: DashboardLink = {
targetBlank: false,
keepTime: false,
@@ -52,9 +52,9 @@ describe('resolveLinks', () => {
type: 'dashboards',
url: '/d/6ieouugGk/DashLinks',
};
const searchHits: DashboardSearchHit[] = [
const searchHits: DashboardSearchItem[] = [
{
id: searchHitId,
uid: searchHitId,
title: 'DashLinks',
url: '/d/6ieouugGk/DashLinks',
isStarred: false,
@@ -70,14 +70,18 @@ describe('resolveLinks', () => {
const sanitize = jest.fn((args) => args);
const sanitizeUrl = jest.fn((args) => args);
return { dashboardId, link, searchHits, linkSrv, sanitize, sanitizeUrl };
return { dashboardUID, link, searchHits, linkSrv, sanitize, sanitizeUrl };
};
describe('when called', () => {
it('should filter out the calling dashboardId', () => {
const { dashboardId, link, searchHits, linkSrv, sanitize, sanitizeUrl } = setupTestContext(1, 1);
it('should filter out the calling dashboardUID', () => {
const { dashboardUID, link, searchHits, linkSrv, sanitize, sanitizeUrl } = setupTestContext('1', '1');
const results = resolveLinks(dashboardId, link, searchHits, { getLinkSrv: () => linkSrv, sanitize, sanitizeUrl });
const results = resolveLinks(dashboardUID, link, searchHits, {
getLinkSrv: () => linkSrv,
sanitize,
sanitizeUrl,
});
expect(results.length).toEqual(0);
expect(linkSrv.getLinkUrl).toHaveBeenCalledTimes(0);
@@ -86,9 +90,13 @@ describe('resolveLinks', () => {
});
it('should resolve link url', () => {
const { dashboardId, link, searchHits, linkSrv, sanitize, sanitizeUrl } = setupTestContext(1, 2);
const { dashboardUID, link, searchHits, linkSrv, sanitize, sanitizeUrl } = setupTestContext('1', '2');
const results = resolveLinks(dashboardId, link, searchHits, { getLinkSrv: () => linkSrv, sanitize, sanitizeUrl });
const results = resolveLinks(dashboardUID, link, searchHits, {
getLinkSrv: () => linkSrv,
sanitize,
sanitizeUrl,
});
expect(results.length).toEqual(1);
expect(linkSrv.getLinkUrl).toHaveBeenCalledTimes(1);
@@ -96,9 +104,13 @@ describe('resolveLinks', () => {
});
it('should sanitize title', () => {
const { dashboardId, link, searchHits, linkSrv, sanitize, sanitizeUrl } = setupTestContext(1, 2);
const { dashboardUID, link, searchHits, linkSrv, sanitize, sanitizeUrl } = setupTestContext('1', '2');
const results = resolveLinks(dashboardId, link, searchHits, { getLinkSrv: () => linkSrv, sanitize, sanitizeUrl });
const results = resolveLinks(dashboardUID, link, searchHits, {
getLinkSrv: () => linkSrv,
sanitize,
sanitizeUrl,
});
expect(results.length).toEqual(1);
expect(sanitize).toHaveBeenCalledTimes(1);
@@ -106,9 +118,13 @@ describe('resolveLinks', () => {
});
it('should sanitize url', () => {
const { dashboardId, link, searchHits, linkSrv, sanitize, sanitizeUrl } = setupTestContext(1, 2);
const { dashboardUID, link, searchHits, linkSrv, sanitize, sanitizeUrl } = setupTestContext('1', '2');
const results = resolveLinks(dashboardId, link, searchHits, { getLinkSrv: () => linkSrv, sanitize, sanitizeUrl });
const results = resolveLinks(dashboardUID, link, searchHits, {
getLinkSrv: () => linkSrv,
sanitize,
sanitizeUrl,
});
expect(results.length).toEqual(1);
expect(sanitizeUrl).toHaveBeenCalledTimes(1);

View File

@@ -7,7 +7,7 @@ import { sanitize, sanitizeUrl } from '@grafana/data/src/text/sanitize';
import { selectors } from '@grafana/e2e-selectors';
import { Icon, ToolbarButton, Tooltip, useStyles2 } from '@grafana/ui';
import { getBackendSrv } from 'app/core/services/backend_srv';
import { DashboardSearchHit } from 'app/features/search/types';
import { DashboardSearchItem } from 'app/features/search/types';
import { getLinkSrv } from '../../../panel/panellinks/link_srv';
import { DashboardLink } from '../../state/DashboardModel';
@@ -15,7 +15,7 @@ import { DashboardLink } from '../../state/DashboardModel';
interface Props {
link: DashboardLink;
linkInfo: { title: string; href: string };
dashboardId: number;
dashboardUID: string;
}
export const DashboardLinksDashboard = (props: Props) => {
@@ -55,7 +55,7 @@ export const DashboardLinksDashboard = (props: Props) => {
{resolvedLinks.length > 0 &&
resolvedLinks.map((resolvedLink, index) => {
return (
<li role="none" key={`dashlinks-dropdown-item-${resolvedLink.id}-${index}`}>
<li role="none" key={`dashlinks-dropdown-item-${resolvedLink.uid}-${index}`}>
<a
role="menuitem"
href={resolvedLink.url}
@@ -82,7 +82,7 @@ export const DashboardLinksDashboard = (props: Props) => {
return (
<LinkElement
link={link}
key={`dashlinks-list-item-${resolvedLink.id}-${index}`}
key={`dashlinks-list-item-${resolvedLink.uid}-${index}`}
data-testid={selectors.components.DashboardLinks.container}
>
<a
@@ -120,17 +120,17 @@ const LinkElement: React.FC<LinkElementProps> = (props) => {
);
};
const useResolvedLinks = ({ link, dashboardId }: Props, opened: number): ResolvedLinkDTO[] => {
const useResolvedLinks = ({ link, dashboardUID }: Props, opened: number): ResolvedLinkDTO[] => {
const { tags } = link;
const result = useAsync(() => searchForTags(tags), [tags, opened]);
if (!result.value) {
return [];
}
return resolveLinks(dashboardId, link, result.value);
return resolveLinks(dashboardUID, link, result.value);
};
interface ResolvedLinkDTO {
id: number;
uid: string;
url: string;
title: string;
}
@@ -138,17 +138,17 @@ interface ResolvedLinkDTO {
export async function searchForTags(
tags: string[],
dependencies: { getBackendSrv: typeof getBackendSrv } = { getBackendSrv }
): Promise<DashboardSearchHit[]> {
): Promise<DashboardSearchItem[]> {
const limit = 100;
const searchHits: DashboardSearchHit[] = await dependencies.getBackendSrv().search({ tag: tags, limit });
const searchHits: DashboardSearchItem[] = await dependencies.getBackendSrv().search({ tag: tags, limit });
return searchHits;
}
export function resolveLinks(
dashboardId: number,
dashboardUID: string,
link: DashboardLink,
searchHits: DashboardSearchHit[],
searchHits: DashboardSearchItem[],
dependencies: { getLinkSrv: typeof getLinkSrv; sanitize: typeof sanitize; sanitizeUrl: typeof sanitizeUrl } = {
getLinkSrv,
sanitize,
@@ -156,14 +156,14 @@ export function resolveLinks(
}
): ResolvedLinkDTO[] {
return searchHits
.filter((searchHit) => searchHit.id !== dashboardId)
.filter((searchHit) => searchHit.uid !== dashboardUID)
.map((searchHit) => {
const id = searchHit.id;
const uid = searchHit.uid;
const title = dependencies.sanitize(searchHit.title);
const resolvedLink = dependencies.getLinkSrv().getLinkUrl({ ...link, url: searchHit.url });
const url = dependencies.sanitizeUrl(resolvedLink);
return { id, title, url };
return { uid, title, url };
});
}