Skip to content

Commit

Permalink
Index routes by name
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar committed Aug 14, 2019
1 parent 59b41a3 commit 528c62e
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 340 deletions.
Expand Up @@ -20,17 +20,14 @@ import styled from 'styled-components';
import { idx } from '@kbn/elastic-idx';
import { NOT_AVAILABLE_LABEL } from '../../../../common/i18n';
import { useFetcher } from '../../../hooks/useFetcher';
import {
loadErrorDistribution,
loadErrorGroupDetails
} from '../../../services/rest/apm/error_groups';
import { fontFamilyCode, fontSizes, px, units } from '../../../style/variables';
import { ApmHeader } from '../../shared/ApmHeader';
import { DetailView } from './DetailView';
import { ErrorDistribution } from './Distribution';
import { useLocation } from '../../../hooks/useLocation';
import { useUrlParams } from '../../../hooks/useUrlParams';
import { useTrackPageview } from '../../../../../infra/public';
import { calls } from '../../../services/rest/callApi';

const Titles = styled.div`
margin-bottom: ${px(units.plus)};
Expand Down Expand Up @@ -68,24 +65,28 @@ export function ErrorGroupDetails() {

const { data: errorGroupData } = useFetcher(() => {
if (serviceName && start && end && errorGroupId) {
return loadErrorGroupDetails({
serviceName,
start,
end,
errorGroupId,
uiFilters
return calls.getErrorsByGroupId({
query: {
serviceName,
groupId: errorGroupId,
start,
end,
uiFilters: JSON.stringify(uiFilters)
}
});
}
}, [serviceName, start, end, errorGroupId, uiFilters]);

const { data: errorDistributionData } = useFetcher(() => {
if (serviceName && start && end && errorGroupId) {
return loadErrorDistribution({
serviceName,
start,
end,
errorGroupId,
uiFilters
return calls.getErrorsDistribution({
query: {
serviceName,
start,
end,
groupId: errorGroupId,
uiFilters: JSON.stringify(uiFilters)
}
});
}
}, [serviceName, start, end, errorGroupId, uiFilters]);
Expand Down
Expand Up @@ -14,14 +14,11 @@ import {
import { i18n } from '@kbn/i18n';
import React from 'react';
import { useFetcher } from '../../../hooks/useFetcher';
import {
loadErrorDistribution,
loadErrorGroupList
} from '../../../services/rest/apm/error_groups';
import { ErrorDistribution } from '../ErrorGroupDetails/Distribution';
import { ErrorGroupList } from './List';
import { useUrlParams } from '../../../hooks/useUrlParams';
import { useTrackPageview } from '../../../../../infra/public';
import { calls } from '../../../services/rest/callApi';

const ErrorGroupOverview: React.SFC = () => {
const {
Expand All @@ -31,24 +28,28 @@ const ErrorGroupOverview: React.SFC = () => {

const { data: errorDistributionData } = useFetcher(() => {
if (serviceName && start && end) {
return loadErrorDistribution({
serviceName,
start,
end,
uiFilters
return calls.getErrorsDistribution({
query: {
serviceName,
start,
end,
uiFilters: JSON.stringify(uiFilters)
}
});
}
}, [serviceName, start, end, uiFilters]);

const { data: errorGroupListData } = useFetcher(() => {
if (serviceName && start && end) {
return loadErrorGroupList({
serviceName,
start,
end,
sortField,
sortDirection,
uiFilters
return calls.getErrorGroupList({
query: {
serviceName,
start,
end,
sortField,
sortDirection,
uiFilters: JSON.stringify(uiFilters)
}
});
}
}, [serviceName, start, end, sortField, sortDirection, uiFilters]);
Expand Down
101 changes: 0 additions & 101 deletions x-pack/legacy/plugins/apm/public/services/rest/apm/error_groups.ts

This file was deleted.

55 changes: 25 additions & 30 deletions x-pack/legacy/plugins/apm/public/services/rest/callApi.ts
Expand Up @@ -10,9 +10,8 @@ import LRU from 'lru-cache';
import hash from 'object-hash';
import { kfetch, KFetchOptions } from 'ui/kfetch';
import { KFetchKibanaOptions } from 'ui/kfetch/kfetch';
import { idx } from '@kbn/elastic-idx';
import { APICall, RouteParams } from '../../../server/routes/typings';
import { api } from '../../../server/routes';
import { APMAPI } from '../../../server/routes/create_apm_api';
import { Client, HttpMethod } from '../../../server/routes/typings';

function fetchOptionsWithDebug(fetchOptions: KFetchOptions) {
const debugEnabled =
Expand Down Expand Up @@ -58,33 +57,29 @@ export async function callApi<T = void>(
return res;
}

export const callApmApi: APICall<typeof api> = ({
method,
pathname,
params
}: {
method: any;
pathname: string;
params?: {
[key in keyof RouteParams]: any;
};
}) => {
const path = idx(params, _ => _.path) || {};
const query = idx(params, _ => _.query) || {};
const body = idx(params, _ => _.body) || {};

const formattedPathname = pathname
.split('/')
.map(part => (part.startsWith(':') ? path[part.substr(1)] : part))
.join('/');

return callApi({
method,
pathname: formattedPathname,
query,
body: body ? JSON.stringify(body) : undefined
}) as any;
};
export const calls: Client<APMAPI> = new Proxy(
{},
{
get: (obj, prop) => {
return ({
query = {},
body = {},
method = 'GET'
}: {
query?: any;
body?: any;
method?: HttpMethod;
}) => {
return callApi({
method,
pathname: `/api/apm/${prop.toString()}`,
query,
body: body ? JSON.stringify(body) : undefined
}) as any;
};
}
}
) as Client<APMAPI>;

// only cache items that has a time range with `start` and `end` params,
// and where `end` is not a timestamp in the future
Expand Down
17 changes: 17 additions & 0 deletions x-pack/legacy/plugins/apm/public/utils/createTypedObjectBuilder.ts
@@ -0,0 +1,17 @@
/*
* 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.
*/

type ExtraKeys<T, U> = Exclude<keyof U, keyof T>;

type Exact<T, U> = ExtraKeys<T, U> extends never
? U
: 'Unknown keys specified:' & { [key in ExtraKeys<T, U>]: U[key] };

export function createTypedObjectBuilder<T>() {
return function build<U extends T>(obj: U): Exact<T, U> {
return obj as any;
};
}
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/apm/server/new-platform/plugin.ts
Expand Up @@ -13,7 +13,7 @@ import { initTracesApi } from '../routes/traces';
import { initTransactionGroupsApi } from '../routes/transaction_groups';
import { initUIFiltersApi } from '../routes/ui_filters';
import { initSettingsApi } from '../routes/settings';
import { api } from '../routes';
import { createApmApi } from '../routes/create_apm_api';

export class Plugin {
public setup(core: InternalCoreSetup) {
Expand All @@ -23,7 +23,7 @@ export class Plugin {
initServicesApi(core);
initSettingsApi(core);
initMetricsApi(core);
api.init(core);
createApmApi().init(core);

makeApmUsageCollector(core as CoreSetupWithUsageCollector);
}
Expand Down

0 comments on commit 528c62e

Please sign in to comment.