Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dataset quality] 🐞 Rely solely on _index instead of data_stream properties #210329

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,82 +7,9 @@

import type { ElasticsearchClient } from '@kbn/core/server';
import { DataStreamDocsStat } from '../../../../common/api_types';
import { FAILURE_STORE_SELECTOR } from '../../../../common/constants';
import { DataStreamType } from '../../../../common/types';
import {
extractIndexNameFromBackingIndex,
streamPartsToIndexPattern,
} from '../../../../common/utils';
import { createDatasetQualityESClient } from '../../../utils';
import { DatasetQualityESClient } from '../../../utils/create_dataset_quality_es_client';
import { rangeQuery } from '../../../utils/queries';

const SIZE_LIMIT = 10000;

async function getPaginatedResults(options: {
datasetQualityESClient: DatasetQualityESClient;
index: string;
start: number;
end: number;
after?: { dataset: string };
prevResults?: Record<string, number>;
}) {
const { datasetQualityESClient, index, start, end, after, prevResults = {} } = options;

const bool = {
filter: [...rangeQuery(start, end)],
};

const response = await datasetQualityESClient.search({
index: `${index}${FAILURE_STORE_SELECTOR}`,
size: 0,
query: {
bool,
},
aggs: {
datasets: {
composite: {
...(after ? { after } : {}),
size: SIZE_LIMIT,
sources: [{ dataset: { terms: { field: '_index' } } }],
},
},
},
});

const currResults = (response.aggregations?.datasets.buckets ?? []).reduce((acc, curr) => {
const datasetName = extractIndexNameFromBackingIndex(curr.key.dataset as string);

return {
...acc,
[datasetName]: (acc[datasetName] ?? 0) + curr.doc_count,
};
}, {} as Record<string, number>);

const results = {
...prevResults,
...currResults,
};

if (
response.aggregations?.datasets.after_key &&
response.aggregations?.datasets.buckets.length === SIZE_LIMIT
) {
return getPaginatedResults({
datasetQualityESClient,
index,
start,
end,
after:
(response.aggregations?.datasets.after_key as {
dataset: string;
}) || after,
prevResults: results,
});
}

return results;
}
import { streamPartsToIndexPattern } from '../../../../common/utils';
import { getAggregatedDatasetPaginatedResults } from '../get_dataset_aggregated_paginated_results';

export async function getFailedDocsPaginated(options: {
esClient: ElasticsearchClient;
Expand All @@ -102,17 +29,10 @@ export async function getFailedDocsPaginated(options: {
})
);

const datasetQualityESClient = createDatasetQualityESClient(esClient);

const datasets = await getPaginatedResults({
datasetQualityESClient,
return await getAggregatedDatasetPaginatedResults({
esClient,
index: datasetNames.join(','),
start,
end,
});

return Object.entries(datasets).map(([dataset, count]) => ({
dataset,
count,
}));
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@

import { QueryDslBoolQuery } from '@elastic/elasticsearch/lib/api/types';
import type { ElasticsearchClient } from '@kbn/core/server';
import { extractIndexNameFromBackingIndex } from '../../../common/utils';
import { DataStreamDocsStat } from '../../../common/api_types';
import { createDatasetQualityESClient } from '../../utils';
import { rangeQuery } from '../../utils/queries';

interface Dataset {
type: string;
dataset: string;
namespace: string;
}

const SIZE_LIMIT = 10000;
Expand All @@ -37,11 +36,7 @@ export async function getAggregatedDatasetPaginatedResults(options: {
composite: {
...(afterKey ? { after: afterKey } : {}),
size: SIZE_LIMIT,
sources: [
{ type: { terms: { field: 'data_stream.type' } } },
{ dataset: { terms: { field: 'data_stream.dataset' } } },
{ namespace: { terms: { field: 'data_stream.namespace' } } },
],
sources: [{ dataset: { terms: { field: '_index' } } }],
},
},
});
Expand All @@ -65,7 +60,7 @@ export async function getAggregatedDatasetPaginatedResults(options: {

const currResults =
response.aggregations?.datasets.buckets.map((bucket) => ({
dataset: `${bucket.key.type}-${bucket.key.dataset}-${bucket.key.namespace}`,
dataset: bucket.key.dataset as string,
count: bucket.doc_count,
})) ?? [];

Expand All @@ -82,13 +77,17 @@ export async function getAggregatedDatasetPaginatedResults(options: {
end,
after:
(response.aggregations?.datasets.after_key as {
type: string;
dataset: string;
namespace: string;
}) || after,
prevResults: results,
});
}

return results;
return Object.entries(
results.reduce((acc, curr) => {
const dataset = extractIndexNameFromBackingIndex(curr.dataset);
acc[dataset] = (acc[dataset] ?? 0) + curr.count;
return acc;
}, {} as Record<string, number>)
).map(([dataset, count]) => ({ dataset, count }));
}