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

[APM] Refactoring menu section #104338

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,17 @@

import { i18n } from '@kbn/i18n';
import { IBasePath } from 'kibana/public';
import { isEmpty } from 'lodash';
import moment from 'moment';
import { APIReturnType } from '../../../../../services/rest/createCallApmApi';
import { getInfraHref } from '../../../../shared/Links/InfraLink';
import {
Action,
getNonEmptySections,
SectionRecord,
} from '../../../../shared/transaction_action_menu/sections_helper';

type InstaceDetails = APIReturnType<'GET /api/apm/services/{serviceName}/service_overview_instances/details/{serviceNodeName}'>;

interface Action {
key: string;
label: string;
href?: string;
onClick?: () => void;
condition: boolean;
}

interface Section {
key: string;
title?: string;
subtitle?: string;
actions: Action[];
}

type SectionRecord = Record<string, Section[]>;

function getInfraMetricsQuery(timestamp?: string) {
if (!timestamp) {
return { from: 0, to: 0 };
Expand Down Expand Up @@ -189,15 +176,5 @@ export function getMenuSections({
apm: [{ key: 'apm', actions: apmActions }],
};

// Filter out actions that shouldnt be shown and sections without any actions.
return Object.values(sectionRecord)
.map((sections) =>
sections
.map((section) => ({
...section,
actions: section.actions.filter((action) => action.condition),
}))
.filter((section) => !isEmpty(section.actions))
)
.filter((sections) => !isEmpty(sections));
return getNonEmptySections(sectionRecord);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getDiscoverHref } from '../Links/DiscoverLinks/DiscoverLink';
import { getDiscoverQuery } from '../Links/DiscoverLinks/DiscoverTransactionLink';
import { getInfraHref } from '../Links/InfraLink';
import { fromQuery } from '../Links/url_helpers';
import { SectionRecord, getNonEmptySections, Action } from './sections_helper';

function getInfraMetricsQuery(transaction: Transaction) {
const timestamp = new Date(transaction['@timestamp']).getTime();
Expand All @@ -28,22 +29,6 @@ function getInfraMetricsQuery(transaction: Transaction) {
};
}

interface Action {
key: string;
label: string;
href: string;
condition: boolean;
}

interface Section {
key: string;
title?: string;
subtitle?: string;
actions: Action[];
}

type SectionRecord = Record<string, Section[]>;

export const getSections = ({
transaction,
basePath,
Expand Down Expand Up @@ -296,14 +281,5 @@ export const getSections = ({
};

// Filter out actions that shouldnt be shown and sections without any actions.
return Object.values(sectionRecord)
.map((sections) =>
sections
.map((section) => ({
...section,
actions: section.actions.filter((action) => action.condition),
}))
.filter((section) => !isEmpty(section.actions))
)
.filter((sections) => !isEmpty(sections));
return getNonEmptySections(sectionRecord);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { getNonEmptySections } from './sections_helper';

describe('getNonEmptySections', () => {
it('returns empty when no section is available', () => {
expect(getNonEmptySections({})).toEqual([]);
});
it("returns empty when section doesn't have actions", () => {
expect(
getNonEmptySections({
foo: [
{
key: 'foo',
title: 'Foo',
subtitle: 'Foo bar',
actions: [],
},
],
})
).toEqual([]);
});

it('returns only sections with actions with condition true', () => {
expect(
getNonEmptySections({
foo: [
{
key: 'foo',
title: 'Foo',
subtitle: 'Foo bar',
actions: [],
},
],
bar: [
{
key: 'bar',
title: 'Bar',
subtitle: 'Bar foo',
actions: [
{
key: 'bar_action',
label: 'Bar Action',
condition: true,
},
{
key: 'bar_action_2',
label: 'Bar Action 2',
condition: false,
},
],
},
],
})
).toEqual([
[
{
key: 'bar',
title: 'Bar',
subtitle: 'Bar foo',
actions: [
{
key: 'bar_action',
label: 'Bar Action',
condition: true,
},
],
},
],
]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { isEmpty } from 'lodash';

export interface Action {
key: string;
label: string;
href?: string;
onClick?: () => void;
condition: boolean;
}

interface Section {
key: string;
title?: string;
subtitle?: string;
actions: Action[];
}

export type SectionRecord = Record<string, Section[]>;

/** Filter out actions that shouldnt be shown and sections without any actions. */
export function getNonEmptySections(sectionRecord: SectionRecord) {
return Object.values(sectionRecord)
.map((sections) =>
sections
.map((section) => ({
...section,
actions: section.actions.filter((action) => action.condition),
}))
.filter((section) => !isEmpty(section.actions))
)
.filter((sections) => !isEmpty(sections));
}