Skip to content

Commit

Permalink
[Dataset quality] Prevent an extra call to ES for degraded docs (elas…
Browse files Browse the repository at this point in the history
…tic#185975)

The PR prevents an extra call to ES, thus improving the response time
the endpoint `/internal/dataset_quality/data_streams/degraded_docs`
takes.

**Before**
<img width="1792" alt="prevent-extra-call-before"
src="https://github.com/elastic/kibana/assets/2748376/0c6bbfa0-d84e-4ad4-9787-4b53169aa9db">

**After**

![prevent-extra-call-after](https://github.com/elastic/kibana/assets/2748376/312b2600-f5a5-4865-8ba4-9a54f0f52317)
  • Loading branch information
awahab07 authored Jun 19, 2024
1 parent 8a7de8c commit ca083d2
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface ResultBucket {
count: number;
}

const SIZE_LIMIT = 10000;

export async function getDegradedDocsPaginated(options: {
esClient: ElasticsearchClient;
type?: DataStreamType;
Expand Down Expand Up @@ -65,7 +67,7 @@ export async function getDegradedDocsPaginated(options: {
datasets: {
composite: {
...(afterKey ? { after: afterKey } : {}),
size: 10000,
size: SIZE_LIMIT,
sources: [
{ dataset: { terms: { field: 'data_stream.dataset' } } },
{ namespace: { terms: { field: 'data_stream.namespace' } } },
Expand Down Expand Up @@ -99,26 +101,27 @@ export async function getDegradedDocsPaginated(options: {
aggs: aggs(after?.docsCount),
},
]);
const [degradedDocsResponse, totalDocsResponse] = response.responses;

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

const degradedDocs = [...prevResults.degradedDocs, ...currDegradedDocs];

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

const docsCount = [...prevResults.docsCount, ...currTotalDocs];

if (
response.responses[0].aggregations?.datasets.after_key ||
response.responses[1].aggregations?.datasets.after_key
totalDocsResponse.aggregations?.datasets.after_key &&
totalDocsResponse.aggregations?.datasets.buckets.length === SIZE_LIMIT
) {
return getDegradedDocsPaginated({
esClient,
Expand All @@ -128,12 +131,12 @@ export async function getDegradedDocsPaginated(options: {
datasetQuery,
after: {
degradedDocs:
(response.responses[0].aggregations?.datasets.after_key as {
(degradedDocsResponse.aggregations?.datasets.after_key as {
dataset: string;
namespace: string;
}) || after?.degradedDocs,
docsCount:
(response.responses[1].aggregations?.datasets.after_key as {
(totalDocsResponse.aggregations?.datasets.after_key as {
dataset: string;
namespace: string;
}) || after?.docsCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ export default function ApiTest({ getService }: FtrProviderContext) {
.message('This is a log message')
.timestamp(timestamp)
.dataset('synth.2')
.logLevel(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?'
)
.logLevel(MORE_THAN_1024_CHARS)
.defaults({
'log.file.path': '/my-service.log',
})
Expand Down Expand Up @@ -112,5 +110,175 @@ export default function ApiTest({ getService }: FtrProviderContext) {
expect(stats.body.degradedDocs.length).to.be(0);
});
});

describe('when there are data streams of different spaces', () => {
const spaces = ['default', 'space1', 'space2'];
const datasetsWithNoDegradedDocs = ['nginx.access', 'apache.access', 'mysql.access'];
const datasetsWithDegradedDocs = ['nginx.error', 'apache.error', 'mysql.error'];

before(async () => {
for (const space of spaces) {
for (const dataset of datasetsWithNoDegradedDocs) {
await synthtrace.index([
timerange(start, end)
.interval('1m')
.rate(1)
.generator((timestamp) =>
log
.create()
.message('This is a log message')
.timestamp(timestamp)
.dataset(dataset)
.namespace(space)
),
]);
}

for (const dataset of datasetsWithDegradedDocs) {
await synthtrace.index([
timerange(start, end)
.interval('1m')
.rate(2)
.generator((timestamp: number, index: number) =>
log
.create()
.message('This is a log message')
.timestamp(timestamp)
.dataset(dataset)
.namespace(space)
.logLevel(index % 2 === 0 ? MORE_THAN_1024_CHARS : 'This is a log message')
),
]);
}
}
});

it('returns counts and list of datasets correctly', async () => {
const stats = await callApiAs('datasetQualityLogsUser');
expect(stats.body.degradedDocs.length).to.be(18);

const expected = {
degradedDocs: [
{
dataset: 'logs-apache.access-default',
count: 0,
docsCount: 1,
percentage: 0,
},
{
dataset: 'logs-apache.access-space1',
count: 0,
docsCount: 1,
percentage: 0,
},
{
dataset: 'logs-apache.access-space2',
count: 0,
docsCount: 1,
percentage: 0,
},
{
dataset: 'logs-apache.error-default',
count: 1,
docsCount: 2,
percentage: 50,
},
{
dataset: 'logs-apache.error-space1',
count: 1,
docsCount: 2,
percentage: 50,
},
{
dataset: 'logs-apache.error-space2',
count: 1,
docsCount: 2,
percentage: 50,
},
{
dataset: 'logs-mysql.access-default',
count: 0,
docsCount: 1,
percentage: 0,
},
{
dataset: 'logs-mysql.access-space1',
count: 0,
docsCount: 1,
percentage: 0,
},
{
dataset: 'logs-mysql.access-space2',
count: 0,
docsCount: 1,
percentage: 0,
},
{
dataset: 'logs-mysql.error-default',
count: 1,
docsCount: 2,
percentage: 50,
},
{
dataset: 'logs-mysql.error-space1',
count: 1,
docsCount: 2,
percentage: 50,
},
{
dataset: 'logs-mysql.error-space2',
count: 1,
docsCount: 2,
percentage: 50,
},
{
dataset: 'logs-nginx.access-default',
count: 0,
docsCount: 1,
percentage: 0,
},
{
dataset: 'logs-nginx.access-space1',
count: 0,
docsCount: 1,
percentage: 0,
},
{
dataset: 'logs-nginx.access-space2',
count: 0,
docsCount: 1,
percentage: 0,
},
{
dataset: 'logs-nginx.error-default',
count: 1,
docsCount: 2,
percentage: 50,
},
{
dataset: 'logs-nginx.error-space1',
count: 1,
docsCount: 2,
percentage: 50,
},
{
dataset: 'logs-nginx.error-space2',
count: 1,
docsCount: 2,
percentage: 50,
},
],
};

expect(stats.body).to.eql(expected);
});

after(async () => {
await synthtrace.clean();
});
});
});
}

const MORE_THAN_1024_CHARS =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?';

0 comments on commit ca083d2

Please sign in to comment.