Skip to content

Commit

Permalink
Merge branch 'main' into 162051-fix-link-test-hosts-view
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Jul 18, 2023
2 parents 40193ee + 41a8a1d commit 1c1976a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
44 changes: 40 additions & 4 deletions x-pack/plugins/uptime/server/legacy_uptime/lib/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import { UptimeEsClient } from './lib';
import { savedObjectsClientMock, uiSettingsServiceMock } from '@kbn/core/server/mocks';
import { elasticsearchClientMock } from '@kbn/core-elasticsearch-client-server-mocks';
import { savedObjectsAdapter } from './saved_objects';
import { SavedObjectsErrorHelpers } from '@kbn/core-saved-objects-server';
import { settingsObjectId, umDynamicSettings } from './saved_objects/uptime_settings';

describe('UptimeEsClient', () => {
let uptimeEsClient: UptimeEsClient;
Expand Down Expand Up @@ -140,9 +141,44 @@ describe('UptimeEsClient', () => {
});
describe('heartbeatIndices', () => {
it('appends synthetics-* in index for legacy alerts', async () => {
savedObjectsAdapter.getUptimeDynamicSettings = jest.fn().mockResolvedValue({
heartbeatIndices: 'heartbeat-8*,heartbeat-7*',
syntheticsIndexRemoved: true,
savedObjectsClient.get = jest.fn().mockResolvedValue({
attributes: {
heartbeatIndices: 'heartbeat-8*,heartbeat-7*',
syntheticsIndexRemoved: true,
},
});
uptimeEsClient = new UptimeEsClient(savedObjectsClient, esClient, { isLegacyAlert: true });

const mockSearchParams = {
body: {
query: {
match_all: {},
},
},
};

await uptimeEsClient.search({
body: {
query: {
match_all: {},
},
},
});

expect(esClient.search).toHaveBeenCalledWith(
{
index: 'heartbeat-8*,heartbeat-7*,synthetics-*',
...mockSearchParams,
},
{ meta: true }
);
});
it('appends synthetics-* in index for legacy alerts when settings are never saved', async () => {
savedObjectsClient.get = jest.fn().mockImplementation(() => {
throw SavedObjectsErrorHelpers.createGenericNotFoundError(
umDynamicSettings.name,
settingsObjectId
);
});
uptimeEsClient = new UptimeEsClient(savedObjectsClient, esClient, { isLegacyAlert: true });

Expand Down
26 changes: 23 additions & 3 deletions x-pack/plugins/uptime/server/legacy_uptime/lib/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
SavedObjectsClientContract,
KibanaRequest,
CoreRequestHandlerContext,
SavedObjectsErrorHelpers,
} from '@kbn/core/server';
import chalk from 'chalk';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
Expand All @@ -18,9 +19,11 @@ import { RequestStatus } from '@kbn/inspector-plugin/common';
import { InspectResponse } from '@kbn/observability-plugin/typings/common';
import { enableInspectEsQueries } from '@kbn/observability-plugin/common';
import { getInspectResponse } from '@kbn/observability-shared-plugin/common';
import { DYNAMIC_SETTINGS_DEFAULT_ATTRIBUTES } from '../../constants/settings';
import { DynamicSettingsAttributes } from '../../runtime_types/settings';
import { settingsObjectId, umDynamicSettings } from './saved_objects/uptime_settings';
import { API_URLS } from '../../../common/constants';
import { UptimeServerSetup } from './adapters';
import { savedObjectsAdapter } from './saved_objects/saved_objects';

export type { UMServerLibs } from '../uptime_server';

Expand Down Expand Up @@ -202,14 +205,31 @@ export class UptimeEsClient {
// if isLegacyAlert appends synthetics-* if it's not already there
let indices = '';
let syntheticsIndexRemoved = false;
let settingsChangedByUser = true;
let settings: DynamicSettingsAttributes = DYNAMIC_SETTINGS_DEFAULT_ATTRIBUTES;
if (this.heartbeatIndices) {
indices = this.heartbeatIndices;
} else {
const settings = await savedObjectsAdapter.getUptimeDynamicSettings(this.savedObjectsClient);
try {
const obj = await this.savedObjectsClient.get<DynamicSettingsAttributes>(
umDynamicSettings.name,
settingsObjectId
);
settings = obj.attributes;
} catch (getErr) {
if (SavedObjectsErrorHelpers.isNotFoundError(getErr)) {
settingsChangedByUser = false;
}
}

indices = settings?.heartbeatIndices || '';
syntheticsIndexRemoved = settings.syntheticsIndexRemoved ?? false;
}
if (this.isLegacyAlert && !indices.includes('synthetics-') && syntheticsIndexRemoved) {
if (
this.isLegacyAlert &&
!indices.includes('synthetics-') &&
(syntheticsIndexRemoved || !settingsChangedByUser)
) {
indices = indices + ',synthetics-*';
}
return indices;
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/uptime/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"@kbn/core-http-server",
"@kbn/core-http-router-server-internal",
"@kbn/actions-plugin",
"@kbn/core-saved-objects-server",
],
"exclude": [
"target/**/*",
Expand Down

0 comments on commit 1c1976a

Please sign in to comment.