Skip to content

Commit

Permalink
refactoring menu section
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes committed Jul 5, 2021
1 parent 5301403 commit 05eae3b
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 55 deletions.
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));
}

0 comments on commit 05eae3b

Please sign in to comment.