Skip to content

Commit

Permalink
Simplify a bit getSummaryIndices - more refactoring to come
Browse files Browse the repository at this point in the history
  • Loading branch information
kdelemme committed Feb 7, 2025
1 parent 57cdfaa commit f33fc07
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
* 2.0.
*/

import { getListOfSloSummaryIndices } from './summary_indices';
import { getSLOSummaryIndices } from './get_slo_summary_indices';
import { DEFAULT_STALE_SLO_THRESHOLD_HOURS, SUMMARY_DESTINATION_INDEX_PATTERN } from './constants';

describe('getListOfSloSummaryIndices', () => {
it('should return default index if disabled', function () {
describe('getSLOSummaryIndices', () => {
it('should return default local index if disabled', function () {
const settings = {
useAllRemoteClusters: false,
selectedRemoteClusters: [],
staleThresholdInHours: DEFAULT_STALE_SLO_THRESHOLD_HOURS,
};
const result = getListOfSloSummaryIndices(settings, []);
expect(result).toBe(SUMMARY_DESTINATION_INDEX_PATTERN);
const result = getSLOSummaryIndices(settings, []);
expect(result).toBe([SUMMARY_DESTINATION_INDEX_PATTERN]);
});

it('should return all remote clusters when enabled', function () {
Expand All @@ -29,10 +29,12 @@ describe('getListOfSloSummaryIndices', () => {
{ name: 'cluster1', isConnected: true },
{ name: 'cluster2', isConnected: true },
];
const result = getListOfSloSummaryIndices(settings, clustersByName);
expect(result).toBe(
`${SUMMARY_DESTINATION_INDEX_PATTERN},cluster1:${SUMMARY_DESTINATION_INDEX_PATTERN},cluster2:${SUMMARY_DESTINATION_INDEX_PATTERN}`
);
const result = getSLOSummaryIndices(settings, clustersByName);
expect(result).toBe([
SUMMARY_DESTINATION_INDEX_PATTERN,
`cluster1:${SUMMARY_DESTINATION_INDEX_PATTERN}`,
`cluster2:${SUMMARY_DESTINATION_INDEX_PATTERN}`,
]);
});

it('should return selected when enabled', function () {
Expand All @@ -45,9 +47,10 @@ describe('getListOfSloSummaryIndices', () => {
{ name: 'cluster1', isConnected: true },
{ name: 'cluster2', isConnected: true },
];
const result = getListOfSloSummaryIndices(settings, clustersByName);
expect(result).toBe(
`${SUMMARY_DESTINATION_INDEX_PATTERN},cluster1:${SUMMARY_DESTINATION_INDEX_PATTERN}`
);
const result = getSLOSummaryIndices(settings, clustersByName);
expect(result).toBe([
SUMMARY_DESTINATION_INDEX_PATTERN,
`cluster1:${SUMMARY_DESTINATION_INDEX_PATTERN}`,
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@
import { GetSLOSettingsResponse } from '@kbn/slo-schema';
import { SUMMARY_DESTINATION_INDEX_PATTERN } from './constants';

export const getListOfSloSummaryIndices = (
export const getSLOSummaryIndices = (
settings: GetSLOSettingsResponse,
clustersByName: Array<{ name: string; isConnected: boolean }>
) => {
remoteClusters: Array<{ name: string; isConnected: boolean }>
): string[] => {
const { useAllRemoteClusters, selectedRemoteClusters } = settings;
if (!useAllRemoteClusters && selectedRemoteClusters.length === 0) {
return SUMMARY_DESTINATION_INDEX_PATTERN;
return [SUMMARY_DESTINATION_INDEX_PATTERN];
}

const indices: string[] = [SUMMARY_DESTINATION_INDEX_PATTERN];
clustersByName.forEach(({ name, isConnected }) => {
if (isConnected && (useAllRemoteClusters || selectedRemoteClusters.includes(name))) {
indices.push(`${name}:${SUMMARY_DESTINATION_INDEX_PATTERN}`);
}
});

return indices.join(',');
return remoteClusters.reduce(
(acc, { name, isConnected }) => {
if (isConnected && (useAllRemoteClusters || selectedRemoteClusters.includes(name))) {
acc.push(`${name}:${SUMMARY_DESTINATION_INDEX_PATTERN}`);
}
return acc;
},
[SUMMARY_DESTINATION_INDEX_PATTERN]
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { useFetcher } from '@kbn/observability-shared-plugin/public';
import { useEffect, useState } from 'react';
import { getListOfSloSummaryIndices } from '../../../../common/summary_indices';
import { getSLOSummaryIndices } from '../../../../common/get_slo_summary_indices';
import { useCreateDataView } from '../../../hooks/use_create_data_view';
import { useKibana } from '../../../hooks/use_kibana';
import { useGetSettings } from '../../slo_settings/hooks/use_get_settings';
Expand All @@ -23,8 +23,8 @@ export const useSloSummaryDataView = () => {

useEffect(() => {
if (settings && remoteClusters) {
const summaryIndices = getListOfSloSummaryIndices(settings, remoteClusters);
setIndexPattern(summaryIndices);
const summaryIndices = getSLOSummaryIndices(settings, remoteClusters);
setIndexPattern(summaryIndices.join(','));
}
}, [settings, remoteClusters]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import type {
KibanaRequest,
SavedObjectsClientContract,
} from '@kbn/core/server';
import { castArray, once } from 'lodash';
import { getListOfSummaryIndices, getSloSettings } from '../services/slo_settings';
import { once } from 'lodash';
import { getSloSettings, getSummaryIndices } from '../services/slo_settings';

export interface SloClient {
getSummaryIndices(): Promise<string[]>;
Expand All @@ -24,17 +24,17 @@ export function getSloClientWithRequest({
esClient: ElasticsearchClient;
soClient: SavedObjectsClientContract;
}): SloClient {
const getListOfSummaryIndicesOnce = once(async () => {
const getSummaryIndicesOnce = once(async () => {
const settings = await getSloSettings(soClient);

const { indices } = await getListOfSummaryIndices(esClient, settings);
const { indices } = await getSummaryIndices(esClient, settings);

return castArray(indices);
return indices;
});

return {
getSummaryIndices: async () => {
return await getListOfSummaryIndicesOnce();
return await getSummaryIndicesOnce();
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
Pagination,
sloGroupWithSummaryResponseSchema,
} from '@kbn/slo-schema';
import { getListOfSummaryIndices, getSloSettings } from './slo_settings';
import { getSummaryIndices, getSloSettings } from './slo_settings';
import { DEFAULT_SLO_GROUPS_PAGE_SIZE } from '../../common/constants';
import { IllegalArgumentError } from '../errors';
import { typedSearch } from '../utils/queries';
Expand Down Expand Up @@ -53,7 +53,7 @@ export class FindSLOGroups {
const parsedFilters = parseStringFilters(filters, this.logger);

const settings = await getSloSettings(this.soClient);
const { indices } = await getListOfSummaryIndices(this.esClient, settings);
const { indices } = await getSummaryIndices(this.esClient, settings);

const hasSelectedTags = groupBy === 'slo.tags' && groupsFilter.length > 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { AlertsClient } from '@kbn/rule-registry-plugin/server';
import { GetSLOStatsOverviewParams, GetSLOStatsOverviewResponse } from '@kbn/slo-schema';
import moment from 'moment';
import { typedSearch } from '../utils/queries';
import { getListOfSummaryIndices, getSloSettings } from './slo_settings';
import { getSummaryIndices, getSloSettings } from './slo_settings';
import { getElasticsearchQueryOrThrow, parseStringFilters } from './transform_generators';

export class GetSLOStatsOverview {
Expand All @@ -29,7 +29,7 @@ export class GetSLOStatsOverview {

public async execute(params: GetSLOStatsOverviewParams): Promise<GetSLOStatsOverviewResponse> {
const settings = await getSloSettings(this.soClient);
const { indices } = await getListOfSummaryIndices(this.esClient, settings);
const { indices } = await getSummaryIndices(this.esClient, settings);

const kqlQuery = params.kqlQuery ?? '';
const filters = params.filters ?? '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
DEFAULT_STALE_SLO_THRESHOLD_HOURS,
SUMMARY_DESTINATION_INDEX_PATTERN,
} from '../../common/constants';
import { getListOfSloSummaryIndices } from '../../common/summary_indices';
import { getSLOSummaryIndices } from '../../common/get_slo_summary_indices';
import { SLOSettings, StoredSLOSettings } from '../domain/models';
import { SO_SLO_SETTINGS_TYPE, sloSettingsObjectId } from '../saved_objects/slo_settings';

Expand Down Expand Up @@ -56,13 +56,13 @@ export const storeSloSettings = async (
return sloSettingsSchema.encode(object.attributes);
};

export const getListOfSummaryIndices = async (
export const getSummaryIndices = async (
esClient: ElasticsearchClient,
settings: StoredSLOSettings
) => {
): Promise<{ indices: string[] }> => {
const { useAllRemoteClusters, selectedRemoteClusters } = settings;
if (!useAllRemoteClusters && selectedRemoteClusters.length === 0) {
return { indices: [SUMMARY_DESTINATION_INDEX_PATTERN], settings };
return { indices: [SUMMARY_DESTINATION_INDEX_PATTERN] };
}

const clustersByName = await esClient.cluster.remoteInfo();
Expand All @@ -72,5 +72,5 @@ export const getListOfSummaryIndices = async (
isConnected: clustersByName[clusterName].connected,
}));

return { indices: getListOfSloSummaryIndices(settings, clusterInfo) };
return { indices: getSLOSummaryIndices(settings, clusterInfo) };
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { SUMMARY_DESTINATION_INDEX_PATTERN } from '../../../common/constants';
import { StoredSLOSettings } from '../../domain/models';
import { toHighPrecision } from '../../utils/number';
import { createEsParams, typedSearch } from '../../utils/queries';
import { getListOfSummaryIndices, getSloSettings } from '../slo_settings';
import { getSummaryIndices, getSloSettings } from '../slo_settings';
import { EsSummaryDocument } from '../summary_transform_generator/helpers/create_temp_summary';
import { getElasticsearchQueryOrThrow, parseStringFilters } from '../transform_generators';
import { fromRemoteSummaryDocumentToSloDefinition } from '../unsafe_federated/remote_summary_doc_to_slo';
Expand Down Expand Up @@ -47,7 +47,7 @@ export class DefaultSummarySearchClient implements SummarySearchClient {
): Promise<Paginated<SummaryResult>> {
const parsedFilters = parseStringFilters(filters, this.logger);
const settings = await getSloSettings(this.soClient);
const { indices } = await getListOfSummaryIndices(this.esClient, settings);
const { indices } = await getSummaryIndices(this.esClient, settings);

const esParams = createEsParams({
index: indices,
Expand Down

0 comments on commit f33fc07

Please sign in to comment.