Skip to content

Commit

Permalink
handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaliidm committed Dec 2, 2024
1 parent 798698d commit 1fe0ffe
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@

import type { ElasticsearchClient, Logger } from '@kbn/core/server';

import type { IndexVersionsByIndex } from './get_index_versions_by_index';
import { getIndexVersionsByIndex } from './get_index_versions_by_index';
import { getSignalVersionsByIndex } from './get_signal_versions_by_index';
import {
getSignalVersionsByIndex,
type SignalVersionsByIndex,
} from './get_signal_versions_by_index';
import { isOutdated as getIsOutdated, signalsAreOutdated } from './helpers';
import { getLatestIndexTemplateVersion } from './get_latest_index_template_version';
import { getIndexAliasPerSpace } from './get_index_alias_per_space';
Expand Down Expand Up @@ -56,15 +60,27 @@ export const getNonMigratedSignalsInfo = async ({
};
}

const indexVersionsByIndex = await getIndexVersionsByIndex({
esClient,
index: indices,
});
let indexVersionsByIndex: IndexVersionsByIndex = {};
try {
indexVersionsByIndex = await getIndexVersionsByIndex({
esClient,
index: indices,
});
} catch (e) {
logger.debug(
`Getting information about legacy siem signals index version failed:"${e?.message}"`
);
}

const signalVersionsByIndex = await getSignalVersionsByIndex({
esClient,
index: indices,
});
let signalVersionsByIndex: SignalVersionsByIndex = {};
try {
signalVersionsByIndex = await getSignalVersionsByIndex({
esClient,
index: indices,
});
} catch (e) {
logger.debug(`Getting information about legacy siem signals versions failed:"${e?.message}"`);
}

const outdatedIndices = indices.reduce<Array<{ indexName: string; space: string }>>(
(acc, indexName) => {
Expand Down Expand Up @@ -99,6 +115,7 @@ export const getNonMigratedSignalsInfo = async ({

const fromRange = await getOldestSignalTimestamp({
esClient,
logger,
index: outdatedIndexNames,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
*/
import { elasticsearchServiceMock } from '@kbn/core/server/mocks';
import { getOldestSignalTimestamp } from './get_oldest_signal_timestamp';
import { loggerMock } from '@kbn/logging-mocks';

describe('getOldestSignalTimestamp', () => {
let esClient: ReturnType<typeof elasticsearchServiceMock.createElasticsearchClient>;
const logger = loggerMock.create();

beforeEach(() => {
esClient = elasticsearchServiceMock.createElasticsearchClient();
Expand All @@ -29,6 +31,7 @@ describe('getOldestSignalTimestamp', () => {

const result = await getOldestSignalTimestamp({
esClient,
logger,
index: ['index1'],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { ElasticsearchClient } from '@kbn/core/server';
import type { ElasticsearchClient, Logger } from '@kbn/core/server';
import type { AggregationsMinAggregate } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';

/**
Expand All @@ -13,25 +13,32 @@ import type { AggregationsMinAggregate } from '@elastic/elasticsearch/lib/api/ty
export const getOldestSignalTimestamp = async ({
esClient,
index,
logger,
}: {
esClient: ElasticsearchClient;
logger: Logger;
index: string[];
}): Promise<string | undefined> => {
const response = await esClient.search<unknown, { min_timestamp: AggregationsMinAggregate }>({
index,
size: 0,
body: {
aggs: {
min_timestamp: {
min: {
field: '@timestamp',
try {
const response = await esClient.search<unknown, { min_timestamp: AggregationsMinAggregate }>({
index,
size: 0,
body: {
aggs: {
min_timestamp: {
min: {
field: '@timestamp',
},
},
},
},
},
});
});

const minTimestamp = response.aggregations?.min_timestamp?.value_as_string;
const minTimestamp = response.aggregations?.min_timestamp?.value_as_string;

return minTimestamp;
return minTimestamp;
} catch (e) {
logger.debug(`Getting information about oldest legacy siem signal failed:"${e?.message}"`);
return undefined;
}
};

0 comments on commit 1fe0ffe

Please sign in to comment.