Skip to content

[Backport 2.19] [BUG FIX] Enable Correct Sorting for Metrics in Query Insights Dashboard + dependent PRs #201

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

Merged
merged 1 commit into from
May 28, 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
164 changes: 142 additions & 22 deletions cypress/e2e/1_top_queries.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
const indexName = 'sample_index';

/**
Helper function to clean up the environment:
- Deletes the test index.
- Disables the top queries features.
Helper function to clean up the environment:
- Deletes the test index.
- Disables the top queries features.
*/
const clearAll = () => {
cy.deleteIndexByName(indexName);
cy.disableTopQueries(METRICS.LATENCY);
cy.disableTopQueries(METRICS.CPU);
cy.disableTopQueries(METRICS.MEMORY);
cy.disableGrouping();
};

describe('Query Insights Dashboard', () => {
Expand All @@ -43,7 +44,7 @@
/**
* Validate the main overview page loads correctly
*/
it('should display the main overview page', () => {

Check warning on line 47 in cypress/e2e/1_top_queries.cy.js

View workflow job for this annotation

GitHub Actions / Run lint

Test has no assertions
cy.get('.euiBasicTable').should('be.visible');
cy.contains('Query insights - Top N queries');
cy.url().should('include', '/queryInsights');
Expand Down Expand Up @@ -81,23 +82,7 @@
});
});

/**
* Validate pagination works as expected
*/
it('should paginate the query table', () => {
for (let i = 0; i < 20; i++) {
cy.searchOnIndex(indexName);
}
// waiting for the query insights queue to drain
cy.wait(10000);
cy.reload();
cy.get('.euiPagination').should('be.visible');
cy.get('.euiPagination__item').contains('2').click();
// Verify rows on the second page
cy.get('.euiTableRow').should('have.length.greaterThan', 0);
});

it('should switch between tabs', () => {

Check warning on line 85 in cypress/e2e/1_top_queries.cy.js

View workflow job for this annotation

GitHub Actions / Run lint

Test has no assertions
// Click Configuration tab
cy.getElementByText('.euiTab', 'Configuration').click({ force: true });
cy.contains('Query insights - Configuration');
Expand All @@ -108,27 +93,162 @@
cy.url().should('include', '/queryInsights');
});

it('should filter queries', () => {

Check warning on line 96 in cypress/e2e/1_top_queries.cy.js

View workflow job for this annotation

GitHub Actions / Run lint

Test has no assertions
cy.get('.euiFieldSearch').should('be.visible');
cy.get('.euiFieldSearch').type('sample_index');
// Add assertions for filtered results
cy.get('.euiTableRow').should('have.length.greaterThan', 0);
});

it('should clear the search input and reset results', () => {

Check warning on line 103 in cypress/e2e/1_top_queries.cy.js

View workflow job for this annotation

GitHub Actions / Run lint

Test has no assertions
cy.get('.euiFieldSearch').type('random_string');
cy.get('.euiTableRow').should('have.length.greaterThan', 0);
cy.get('.euiFieldSearch').clear();
cy.get('.euiTableRow').should('have.length.greaterThan', 0); // Validate reset
cy.get('.euiTableRow').should('have.length.greaterThan', 0);
});

it('should display a message when no top queries are found', () => {

Check warning on line 110 in cypress/e2e/1_top_queries.cy.js

View workflow job for this annotation

GitHub Actions / Run lint

Test has no assertions
clearAll(); // disable top n queries
// waiting for the query insights queue to drain
clearAll();
cy.wait(10000);
cy.reload();
cy.contains('No items found');
});

it('should paginate the query table', () => {

Check warning on line 117 in cypress/e2e/1_top_queries.cy.js

View workflow job for this annotation

GitHub Actions / Run lint

Test has no assertions
for (let i = 0; i < 20; i++) {
cy.searchOnIndex(indexName);
}
cy.wait(10000);
cy.reload();
cy.get('.euiPagination').should('be.visible');
cy.get('.euiPagination__item').contains('2').click();
// Verify rows on the second page
cy.get('.euiTableRow').should('have.length.greaterThan', 0);
});
after(() => clearAll());
});

describe('Query Insights Dashboard - Dynamic Columns with Stubbed Top Queries', () => {
beforeEach(() => {
cy.fixture('stub_top_queries.json').then((stubResponse) => {
cy.intercept('GET', '**/api/top_queries/*', {
statusCode: 200,
body: stubResponse,
}).as('getTopQueries');
});

cy.navigateToOverview();
cy.wait(1000);
cy.wait('@getTopQueries');
});

const testMetricSorting = (columnLabel, columnIndex) => {
cy.get('.euiTableHeaderCell').contains(columnLabel).click();
cy.wait(1000);

cy.get('.euiTableRow').then(($rows) => {
const values = [...$rows].map(($row) => {
const rawText = Cypress.$($row).find('td').eq(columnIndex).text().trim();
return parseFloat(rawText.replace(/[^\d.]/g, '')); // remove 'ms'/'B'
});
const sortedAsc = [...values].sort((a, b) => a - b);
expect(values).to.deep.equal(sortedAsc);
});

cy.get('.euiTableHeaderCell').contains(columnLabel).click();
cy.wait(1000);

cy.get('.euiTableRow').then(($rows) => {
const values = [...$rows].map(($row) => {
const rawText = Cypress.$($row).find('td').eq(columnIndex).text().trim();
return parseFloat(rawText.replace(/[^\d.]/g, ''));
});
const sortedDesc = [...values].sort((a, b) => b - a);
expect(values).to.deep.equal(sortedDesc);
});
};

it('should render only individual query-related headers when NONE filter is applied', () => {
cy.wait(1000);
cy.get('.euiFilterButton').contains('Type').click();
cy.get('.euiFilterSelectItem').contains('query').click();
cy.wait(1000);

const expectedHeaders = [
'Id',
'Type',
'Timestamp',
'Latency',
'CPU Time',
'Memory Usage',
'Indices',
'Search Type',
'Coordinator Node ID',
'Total Shards',
];

cy.get('.euiTableHeaderCell').should('have.length', expectedHeaders.length);

cy.get('.euiTableHeaderCell').should(($headers) => {
const actualHeaders = $headers.map((index, el) => Cypress.$(el).text().trim()).get();
expect(actualHeaders).to.deep.equal(expectedHeaders);
});
testMetricSorting('Timestamp', 2);
testMetricSorting('Latency', 3);
testMetricSorting('CPU Time', 4);
testMetricSorting('Memory Usage', 5);
});

it('should render only group-related headers in the correct order when SIMILARITY filter is applied', () => {
cy.get('.euiFilterButton').contains('Type').click();
cy.get('.euiFilterSelectItem').contains('group').click();
cy.wait(1000);

const expectedHeaders = [
'Id',
'Type',
'Query Count',
'Average Latency',
'Average CPU Time',
'Average Memory Usage',
];

cy.get('.euiTableHeaderCell').should(($headers) => {
const actualHeaders = $headers.map((index, el) => Cypress.$(el).text().trim()).get();
expect(actualHeaders).to.deep.equal(expectedHeaders);
});
testMetricSorting('Query Count', 2);
testMetricSorting('Average Latency', 3);
testMetricSorting('Average CPU Time', 4);
testMetricSorting('Average Memory Usage', 5);
});
it('should display both query and group data with proper headers when both are selected', () => {
cy.get('.euiFilterButton').contains('Type').click();
cy.get('.euiFilterSelectItem').contains('query').click();
cy.get('.euiFilterSelectItem').contains('group').click();
cy.wait(1000);

const expectedGroupHeaders = [
'Id',
'Type',
'Query Count',
'Timestamp',
'Avg Latency / Latency',
'Avg CPU Time / CPU Time',
'Avg Memory Usage / Memory Usage',
'Indices',
'Search Type',
'Coordinator Node ID',
'Total Shards',
];
cy.get('.euiTableHeaderCell').should(($headers) => {
const actualHeaders = $headers.map((index, el) => Cypress.$(el).text().trim()).get();
expect(actualHeaders).to.deep.equal(expectedGroupHeaders);
});
testMetricSorting('Query Count', 2);
testMetricSorting('Timestamp', 3);
testMetricSorting('Avg Latency / Latency', 4);
testMetricSorting('Avg CPU Time / CPU Time', 5);
testMetricSorting('Avg Memory Usage / Memory Usage', 6);
});
});
140 changes: 140 additions & 0 deletions cypress/fixtures/stub_top_queries.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
{
"ok": true,
"response": {
"top_queries": [
{
"timestamp": 1713934974000,
"id": "a2e1c822-3e3c-4d1b-adb2-9f73af094b43",
"search_type": "query_then_fetch",
"indices": ["my-index"],
"node_id": "UYKFun8PSAeJvkkt9cWf0w",
"group_by": "NONE",
"total_shards": 1,
"labels": {
"X-Opaque-Id": "90eb5c3b-8448-4af3-84ce-a941eee9ed5f"
},
"measurements": {
"cpu": { "number": 1340000, "count": 1, "aggregationType": "NONE" },
"latency": { "number": 22, "count": 1, "aggregationType": "NONE" },
"memory": { "number": 204800, "count": 1, "aggregationType": "NONE" }
}
},
{
"timestamp": 1713934989000,
"id": "130a5d36-615e-43e8-ad99-e1b90d527f44",
"search_type": "query_then_fetch",
"indices": [".kibana"],
"node_id": "UYKFun8PSAeJvkkt9cWf0w",
"group_by": "SIMILARITY",
"total_shards": 1,
"labels": {},
"query_group_hashcode": "2219a4bb71bd0d262e6d0f5504b88537",
"measurements": {
"memory": { "number": 58000, "count": 1, "aggregationType": "AVERAGE" },
"cpu": { "number": 1780000, "count": 1, "aggregationType": "AVERAGE" },
"latency": { "number": 10, "count": 1, "aggregationType": "AVERAGE" }
}
},
{
"timestamp": 1713935004000,
"id": "a2e1c822-3e3c-4d1b-adb2-9f73af094b43",
"search_type": "query_then_fetch",
"indices": ["my-index"],
"node_id": "UYKFun8PSAeJvkkt9cWf0w",
"group_by": "NONE",
"total_shards": 1,
"labels": {
"X-Opaque-Id": "90eb5c3b-8448-4af3-84ce-a941eee9ed5f"
},
"measurements": {
"memory": { "number": 170320, "count": 1, "aggregationType": "NONE" },
"cpu": { "number": 2100000, "count": 1, "aggregationType": "NONE" },
"latency": { "number": 13, "count": 1, "aggregationType": "NONE" }
}
},
{
"timestamp": 1713935019000,
"id": "7cd4c7f1-3803-4c5e-a41c-258e04f96f78",
"search_type": "query_then_fetch",
"indices": ["my-index"],
"node_id": "UYKFun8PSAeJvkkt9cWf0w",
"group_by": "NONE",
"total_shards": 1,
"labels": {
"X-Opaque-Id": "8a936346-8d19-409c-9fe6-8b890eca1f7c"
},
"measurements": {
"cpu": { "number": 990000, "count": 1, "aggregationType": "NONE" },
"latency": { "number": 18, "count": 1, "aggregationType": "NONE" },
"memory": { "number": 81200, "count": 1, "aggregationType": "NONE" }
}
},
{
"timestamp": 1713935033000,
"id": "76f5e51f-33f6-480c-8b20-8003abb93d19",
"search_type": "query_then_fetch",
"indices": [".kibana"],
"node_id": "UYKFun8PSAeJvkkt9cWf0w",
"group_by": "SIMILARITY",
"total_shards": 1,
"labels": {
"X-Opaque-Id": "660baeb1-077b-4884-8bae-890cfe30e776"
},
"query_group_hashcode": "a336f9580d5d980f7403f6d179f454eb",
"measurements": {
"memory": { "number": 133600, "count": 1, "aggregationType": "AVERAGE" },
"cpu": { "number": 990000, "count": 1, "aggregationType": "AVERAGE" },
"latency": { "number": 9, "count": 1, "aggregationType": "AVERAGE" }
}
},
{
"timestamp": 1713935048000,
"id": "37d633a7-20e6-41a1-96e9-cd4806511dbf",
"search_type": "query_then_fetch",
"indices": [".kibana"],
"node_id": "UYKFun8PSAeJvkkt9cWf0w",
"group_by": "SIMILARITY",
"total_shards": 1,
"labels": {},
"query_group_hashcode": "7cef9a399c117a0278025a89e943eebc",
"measurements": {
"memory": { "number": 640320, "count": 3, "aggregationType": "AVERAGE" },
"cpu": { "number": 3190000, "count": 3, "aggregationType": "AVERAGE" },
"latency": { "number": 6, "count": 1, "aggregationType": "NONE" }
}
},
{
"timestamp": 1713935062000,
"id": "9982b7fc-0339-47d8-b77f-8de1bda76b72",
"search_type": "query_then_fetch",
"indices": [".kibana"],
"node_id": "UYKFun8PSAeJvkkt9cWf0w",
"group_by": "SIMILARITY",
"total_shards": 1,
"labels": {},
"query_group_hashcode": "7cef9a399c117a0278025a89e943eebc",
"measurements": {
"memory": { "number": 812400, "count": 4, "aggregationType": "AVERAGE" },
"cpu": { "number": 2280000, "count": 4, "aggregationType": "AVERAGE" },
"latency": { "number": 4, "count": 4, "aggregationType": "AVERAGE" }
}
},
{
"timestamp": 1713935078000,
"id": "d8dccf54-8dcb-4411-9fd6-977844be8fb3",
"search_type": "query_then_fetch",
"indices": [".kibana"],
"node_id": "UYKFun8PSAeJvkkt9cWf0w",
"group_by": "SIMILARITY",
"total_shards": 1,
"labels": {},
"query_group_hashcode": "2219a4bb71bd0d262e6d0f5504b88537",
"measurements": {
"memory": { "number": 59000, "count": 1, "aggregationType": "AVERAGE" },
"cpu": { "number": 1720000, "count": 1, "aggregationType": "AVERAGE" },
"latency": { "number": 11, "count": 1, "aggregationType": "NONE" }
}
}
]
}
}
14 changes: 14 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ Cypress.Commands.add('disableGrouping', () => {
failOnStatusCode: true,
});
});
Cypress.Commands.add('setWindowSize', (size = '1m') => {
cy.request({
method: 'PUT',
url: `${Cypress.env('openSearchUrl')}/_cluster/settings`,
body: {
persistent: {
'search.insights.top_queries.latency.window_size': size,
'search.insights.top_queries.cpu.window_size': size,
'search.insights.top_queries.memory.window_size': size,
},
},
failOnStatusCode: true,
});
});

Cypress.Commands.add('createIndexByName', (indexName, body = {}) => {
cy.request('POST', `${Cypress.env('openSearchUrl')}/${indexName}/_doc`, body);
Expand Down
8 changes: 4 additions & 4 deletions public/components/__snapshots__/app.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ exports[`<QueryInsightsDashboardsApp /> spec renders the component 1`] = `
aria-live="polite"
aria-sort="none"
class="euiTableHeaderCell"
data-test-subj="tableHeaderCell_measurements_2"
data-test-subj="tableHeaderCell_Query Count_2"
role="columnheader"
scope="col"
>
Expand Down Expand Up @@ -570,7 +570,7 @@ exports[`<QueryInsightsDashboardsApp /> spec renders the component 1`] = `
aria-live="polite"
aria-sort="none"
class="euiTableHeaderCell"
data-test-subj="tableHeaderCell_measurements.latency_4"
data-test-subj="tableHeaderCell_Latency_4"
role="columnheader"
scope="col"
>
Expand All @@ -595,7 +595,7 @@ exports[`<QueryInsightsDashboardsApp /> spec renders the component 1`] = `
aria-live="polite"
aria-sort="none"
class="euiTableHeaderCell"
data-test-subj="tableHeaderCell_measurements.cpu_5"
data-test-subj="tableHeaderCell_CPU Time_5"
role="columnheader"
scope="col"
>
Expand All @@ -620,7 +620,7 @@ exports[`<QueryInsightsDashboardsApp /> spec renders the component 1`] = `
aria-live="polite"
aria-sort="none"
class="euiTableHeaderCell"
data-test-subj="tableHeaderCell_measurements.memory_6"
data-test-subj="tableHeaderCell_Memory Usage_6"
role="columnheader"
scope="col"
>
Expand Down
Loading
Loading