Skip to content

Commit

Permalink
replace useServiceTransactionTypes with useTransactionTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar committed Aug 1, 2019
1 parent 30863ad commit bae7fa4
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { MLJobLink } from '../../../../shared/Links/MachineLearningLinks/MLJobLi
import { MLLink } from '../../../../shared/Links/MachineLearningLinks/MLLink';
import { TransactionSelect } from './TransactionSelect';
import { IUrlParams } from '../../../../../context/UrlParamsContext/types';
import { useServiceTransactionTypes } from '../../../../../hooks/useServiceTransactionTypes';
import { useTransactionTypes } from '../../../../../hooks/useTransactionTypes';

interface Props {
hasIndexPattern: boolean;
Expand All @@ -47,7 +47,7 @@ export function MachineLearningFlyoutView({
urlParams
}: Props) {
const { serviceName } = urlParams;
const transactionTypes = useServiceTransactionTypes(urlParams);
const transactionTypes = useTransactionTypes(urlParams);

const [selectedTransactionType, setSelectedTransactionType] = useState<
string | undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { TransactionOverview } from '..';
import * as useLocationHook from '../../../../hooks/useLocation';
import { history } from '../../../../utils/history';
import { IUrlParams } from '../../../../context/UrlParamsContext/types';
import * as useServiceTransactionTypesHook from '../../../../hooks/useServiceTransactionTypes';
import * as useTransactionTypesHook from '../../../../hooks/useTransactionTypes';

jest.mock('ui/kfetch');

Expand All @@ -37,7 +37,7 @@ function setup({
.mockReturnValue({ pathname: '' } as any);

jest
.spyOn(useServiceTransactionTypesHook, 'useServiceTransactionTypes')
.spyOn(useTransactionTypesHook, 'useTransactionTypes')
.mockReturnValue(serviceTransactionTypes);

const { container } = render(<TransactionOverview urlParams={urlParams} />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { useLocation } from '../../../hooks/useLocation';
import { ChartsSyncContextProvider } from '../../../context/ChartsSyncContext';
import { useTrackPageview } from '../../../../../infra/public';
import { fromQuery, toQuery } from '../../shared/Links/url_helpers';
import { useServiceTransactionTypes } from '../../../hooks/useServiceTransactionTypes';
import { useTransactionTypes } from '../../../hooks/useTransactionTypes';

interface Props {
urlParams: IUrlParams;
Expand Down Expand Up @@ -63,7 +63,7 @@ export function TransactionOverview({ urlParams }: Props) {
const { serviceName, transactionType } = urlParams;

// TODO: fetching of transaction types should perhaps be lifted since it is needed in several places. Context?
const serviceTransactionTypes = useServiceTransactionTypes(urlParams);
const serviceTransactionTypes = useTransactionTypes(urlParams);

// redirect to first transaction type
useRedirect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { loadServiceTransactionTypes } from '../services/rest/apm/services';
import { loadTransactionTypes } from '../services/rest/apm/transaction_types';
import { IUrlParams } from '../context/UrlParamsContext/types';
import { useFetcher } from './useFetcher';

export function useServiceTransactionTypes(urlParams: IUrlParams) {
export function useTransactionTypes(urlParams: IUrlParams) {
const { serviceName, start, end } = urlParams;
const { data: transactionTypes = [] } = useFetcher(() => {
if (serviceName && start && end) {
return loadServiceTransactionTypes({ serviceName, start, end });
if (start && end) {
return loadTransactionTypes({ serviceName, start, end });
}
}, [serviceName, start, end]);

Expand Down
22 changes: 0 additions & 22 deletions x-pack/legacy/plugins/apm/public/services/rest/apm/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { ServiceAgentNameAPIResponse } from '../../../../server/lib/services/get
import { ServiceListAPIResponse } from '../../../../server/lib/services/get_services';
import { callApi } from '../callApi';
import { UIFilters } from '../../../../typings/ui-filters';
import { ServiceTransactionTypesAPIResponse } from '../../../../server/lib/services/get_service_transaction_types';

export async function loadServiceList({
start,
Expand Down Expand Up @@ -48,24 +47,3 @@ export async function loadServiceAgentName({

return agentName;
}

export async function loadServiceTransactionTypes({
serviceName,
start,
end
}: {
serviceName: string;
start: string;
end: string;
}) {
const { transactionTypes } = await callApi<
ServiceTransactionTypesAPIResponse
>({
pathname: `/api/apm/services/${serviceName}/transaction_types`,
query: {
start,
end
}
});
return transactionTypes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { TransactionTypesAPIResponse } from '../../../../server/lib/transaction_types/get_transaction_types';
import { callApi } from '../callApi';

export async function loadTransactionTypes({
serviceName,
start,
end
}: {
serviceName: string | undefined;
start: string;
end: string;
}) {
const { transactionTypes } = await callApi<TransactionTypesAPIResponse>({
pathname: `/api/apm/transaction_types`,
query: {
start,
end,
serviceName
}
});
return transactionTypes;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions x-pack/legacy/plugins/apm/server/lib/services/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { getServiceAgentName } from './get_service_agent_name';
import { getServiceTransactionTypes } from './get_service_transaction_types';
import { getServicesItems } from './get_services/get_services_items';
import { getLegacyDataStatus } from './get_services/get_legacy_data_status';
import { getAgentStatus } from './get_services/get_agent_status';
Expand All @@ -29,14 +28,6 @@ describe('services queries', () => {
expect(mock.params).toMatchSnapshot();
});

it('fetches the service transaction types', async () => {
mock = await inspectSearchParams(setup =>
getServiceTransactionTypes('foo', setup)
);

expect(mock.params).toMatchSnapshot();
});

it('fetches the service items', async () => {
mock = await inspectSearchParams(setup => getServicesItems(setup));

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,30 @@ import { PromiseReturnType } from '../../../typings/common';
import { rangeFilter } from '../helpers/range_filter';
import { Setup } from '../helpers/setup_request';

export type ServiceTransactionTypesAPIResponse = PromiseReturnType<
typeof getServiceTransactionTypes
export type TransactionTypesAPIResponse = PromiseReturnType<
typeof getTransactionTypes
>;
export async function getServiceTransactionTypes(
serviceName: string,
setup: Setup
) {
export async function getTransactionTypes({
setup,
serviceName
}: {
setup: Setup;
serviceName?: string;
}) {
const { start, end, client, config } = setup;

const serviceNameFilter = serviceName
? [{ term: { [SERVICE_NAME]: serviceName } }]
: [];

const params = {
index: [config.get<string>('apm_oss.transactionIndices')],
body: {
size: 0,
query: {
bool: {
filter: [
{ term: { [SERVICE_NAME]: serviceName } },
...serviceNameFilter,
{ terms: { [PROCESSOR_EVENT]: ['transaction'] } },
{ range: rangeFilter(start, end) }
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { getTransactionTypes } from './get_transaction_types';
import {
SearchParamsMock,
inspectSearchParams
} from '../../../public/utils/testHelpers';

describe('services queries', () => {
let mock: SearchParamsMock;

afterEach(() => {
mock.teardown();
});

it('fetches the transaction types without service name', async () => {
mock = await inspectSearchParams(setup => getTransactionTypes({ setup }));

expect(mock.params).toMatchSnapshot();
});

it('fetches the transaction types with a service name', async () => {
mock = await inspectSearchParams(setup =>
getTransactionTypes({ setup, serviceName: 'foo' })
);

expect(mock.params).toMatchSnapshot();
});
});
2 changes: 2 additions & 0 deletions x-pack/legacy/plugins/apm/server/new-platform/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { initTransactionGroupsApi } from '../routes/transaction_groups';
import { initUIFiltersApi } from '../routes/ui_filters';
import { initIndexPatternApi } from '../routes/index_pattern';
import { initSettingsApi } from '../routes/settings';
import { initTransactionTypesApi } from '../routes/transaction_types';

export class Plugin {
public setup(core: InternalCoreSetup) {
Expand All @@ -26,6 +27,7 @@ export class Plugin {
initErrorsApi(core);
initMetricsApi(core);
initIndexPatternApi(core);
initTransactionTypesApi(core);
makeApmUsageCollector(core as CoreSetupWithUsageCollector);
}
}
Loading

0 comments on commit bae7fa4

Please sign in to comment.