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

Improved dashboard component extensibility. #60

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions src/components/DashboardsCard/DashboardsCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,9 @@ import { render } from '@testing-library/react';
import { DashboardsTable } from './DashboardsCard';

describe('DashboardsTable', () => {
const entityMock = {
metadata: {
namespace: 'default',
annotations: {},
name: 'mocked-service',
},
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
spec: {
type: 'service',
owner: 'John Doe',
lifecycle: 'experimental',
},
}

it('should render even with no dashboards', async () => {
const rendered = render(<DashboardsTable dashboards={[]} entity={entityMock} opts={{}} />);
const rendered = render(<DashboardsTable dashboards={[]} tags={""} opts={{}} />);

expect(await rendered.findByText('No records to display')).toBeInTheDocument();
});
Expand Down
32 changes: 18 additions & 14 deletions src/components/DashboardsCard/DashboardsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import React from 'react';
import { Progress, TableColumn, Table, MissingAnnotationEmptyState, Link } from '@backstage/core-components';
import { Entity } from '@backstage/catalog-model';
import { useEntity } from '@backstage/plugin-catalog-react';
import { useApi } from '@backstage/core-plugin-api';
import { grafanaApiRef } from '../../api';
Expand All @@ -26,7 +25,7 @@ import { Tooltip } from '@material-ui/core';
import { Dashboard } from '../../types';
import { dashboardSelectorFromEntity, GRAFANA_ANNOTATION_DASHBOARD_SELECTOR, isDashboardSelectorAvailable } from '../grafanaData';

export const DashboardsTable = ({entity, dashboards, opts}: {entity: Entity, dashboards: Dashboard[], opts: DashboardCardOpts}) => {
export const DashboardsTable = ({tags, dashboards, opts}: {tags: string, dashboards: Dashboard[], opts: DashboardCardOpts}) => {
const columns: TableColumn<Dashboard>[] = [
{
title: 'Title',
Expand All @@ -41,7 +40,7 @@ export const DashboardsTable = ({entity, dashboards, opts}: {entity: Entity, das
];

const titleElm = (
<Tooltip title={`Note: only dashboard with the "${dashboardSelectorFromEntity(entity)}" selector are displayed.`}>
<Tooltip title={`Note: only dashboard with the "${tags}" tags are displayed.`}>
<span>{opts.title || 'Dashboards'}</span>
</Tooltip>
);
Expand All @@ -64,9 +63,9 @@ export const DashboardsTable = ({entity, dashboards, opts}: {entity: Entity, das
);
};

const Dashboards = ({entity, opts}: {entity: Entity, opts: DashboardCardOpts}) => {
const Dashboards = ({tags, opts}: {tags: string, opts: DashboardCardOpts}) => {
const grafanaApi = useApi(grafanaApiRef);
const { value, loading, error } = useAsync(async () => await grafanaApi.listDashboards(dashboardSelectorFromEntity(entity)));
const { value, loading, error } = useAsync(async () => await grafanaApi.listDashboards(tags));

if (loading) {
return <Progress />;
Expand All @@ -75,7 +74,7 @@ const Dashboards = ({entity, opts}: {entity: Entity, opts: DashboardCardOpts}) =
}

return (
<DashboardsTable entity={entity} dashboards={value || []} opts={opts} />
<DashboardsTable tags={tags} dashboards={value || []} opts={opts} />
);
};

Expand All @@ -87,12 +86,17 @@ export type DashboardCardOpts = {
title?: string;
};

export const DashboardsCard = (opts?: DashboardCardOpts) => {
const { entity } = useEntity();

return !isDashboardSelectorAvailable(entity) ? (
<MissingAnnotationEmptyState annotation={GRAFANA_ANNOTATION_DASHBOARD_SELECTOR} />
) : (
<Dashboards entity={entity} opts={opts || {}} />
);
export const DashboardsCard = ({tags, opts}: {tags?: string, opts?: DashboardCardOpts}) => {
if(isAbsent(tags)){
const { entity } = useEntity();
if(!isDashboardSelectorAvailable(entity)){
return <MissingAnnotationEmptyState annotation={GRAFANA_ANNOTATION_DASHBOARD_SELECTOR} />;
}
tags = dashboardSelectorFromEntity(entity);
}
return <Dashboards tags={tags || ""} opts={opts || {}} />;
};

function isAbsent(tags? : string) : boolean {
return (tags === null || tags === undefined);
}