Skip to content

Commit

Permalink
[APM] Fix missing apm indicies (#53541)
Browse files Browse the repository at this point in the history
* [APM] Fix missing apm indicies

* Fix infinite loop in ui indices

* Add test for empty settings
  • Loading branch information
sorenlouv committed Dec 23, 2019
1 parent 08ff024 commit 8b0d5f5
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 { render, wait } from '@testing-library/react';
import React from 'react';
import { ApmIndices } from '.';
import { MockApmPluginContextWrapper } from '../../../../utils/testHelpers';
import * as hooks from '../../../../hooks/useFetcher';

describe('ApmIndices', () => {
it('should not get stuck in infinite loop', async () => {
spyOn(hooks, 'useFetcher').and.returnValue({
data: undefined,
status: 'loading'
});
const { getByText } = render(
<MockApmPluginContextWrapper>
<ApmIndices />
</MockApmPluginContextWrapper>
);

expect(getByText('Indices')).toMatchInlineSnapshot(`
<h2
class="euiTitle euiTitle--medium"
>
Indices
</h2>
`);

await wait();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ async function saveApmIndices({
clearCache();
}

// avoid infinite loop by initializing the state outside the component
const INITIAL_STATE = [] as [];

export function ApmIndices() {
const { toasts } = useApmPluginContext().core.notifications;

Expand All @@ -93,7 +96,7 @@ export function ApmIndices() {

const callApmApiFromHook = useCallApmApi();

const { data = [], status, refetch } = useFetcher(
const { data = INITIAL_STATE, status, refetch } = useFetcher(
callApmApi =>
callApmApi({ pathname: `/api/apm/settings/apm-index-settings` }),
[]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 { saveApmIndices } from './save_apm_indices';

describe('saveApmIndices', () => {
it('should trim and strip empty settings', async () => {
const context = {
core: {
savedObjects: {
client: {
create: jest.fn()
}
}
}
} as any;

const apmIndices = {
settingA: 'aa',
settingB: '',
settingC: undefined,
settingD: null,
settingE: ' ',
settingF: 'ff',
settingG: ' gg '
} as any;
await saveApmIndices(context, apmIndices);
expect(context.core.savedObjects.client.create).toHaveBeenCalledWith(
expect.any(String),
{ settingA: 'aa', settingF: 'ff', settingG: 'gg' },
expect.any(Object)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@ import { APMRequestHandlerContext } from '../../../routes/typings';

export async function saveApmIndices(
context: APMRequestHandlerContext,
apmIndicesSavedObject: Partial<ApmIndicesConfig>
apmIndices: Partial<ApmIndicesConfig>
) {
return await context.core.savedObjects.client.create(
APM_INDICES_SAVED_OBJECT_TYPE,
apmIndicesSavedObject,
removeEmpty(apmIndices),
{
id: APM_INDICES_SAVED_OBJECT_ID,
overwrite: true
}
);
}

// remove empty/undefined values
function removeEmpty(apmIndices: Partial<ApmIndicesConfig>) {
return Object.fromEntries(
Object.entries(apmIndices)
.map(([key, value]) => [key, value?.trim()])
.filter(([key, value]) => !!value)
);
}

0 comments on commit 8b0d5f5

Please sign in to comment.