Skip to content

Commit

Permalink
[8.16] [Security Solution][Data Quality Dashboard] fix pattern state …
Browse files Browse the repository at this point in the history
…reset on ilm phase filter change (#198549) (#200618)

# Backport

This will backport the following commits from `main` to `8.16`:
- [[Security Solution][Data Quality Dashboard] fix pattern state reset
on ilm phase filter change
(#198549)](#198549)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Karen
Grigoryan","email":"karen.grigoryan@elastic.co"},"sourceCommit":{"committedDate":"2024-11-04T15:09:12Z","message":"[Security
Solution][Data Quality Dashboard] fix pattern state reset on ilm phase
filter change (#198549)\n\naddresses #196523\r\n\r\n- Fixes ilm phase
change propagation on patterns.\r\n- Adds missing tests for
useResultsRollup functionality\r\n\r\n## UI changes\r\n\r\n###
Before\r\n\r\nhttps://github.com/user-attachments/assets/78a1d809-6a9a-4bfc-88a9-079f829a2017\r\n\r\n###
After\r\n\r\nhttps://github.com/user-attachments/assets/f689fcc9-e1c6-4ccf-a7ca-8f13e9507ba4","sha":"ddf55ea3a79ad7439dc02cb1f93291b1bc95c3b9","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:Threat
Hunting","Team:Threat
Hunting:Explore","backport:prev-minor","ci:cloud-deploy","ci:project-deploy-security","v8.17.0"],"number":198549,"url":"https://github.com/elastic/kibana/pull/198549","mergeCommit":{"message":"[Security
Solution][Data Quality Dashboard] fix pattern state reset on ilm phase
filter change (#198549)\n\naddresses #196523\r\n\r\n- Fixes ilm phase
change propagation on patterns.\r\n- Adds missing tests for
useResultsRollup functionality\r\n\r\n## UI changes\r\n\r\n###
Before\r\n\r\nhttps://github.com/user-attachments/assets/78a1d809-6a9a-4bfc-88a9-079f829a2017\r\n\r\n###
After\r\n\r\nhttps://github.com/user-attachments/assets/f689fcc9-e1c6-4ccf-a7ca-8f13e9507ba4","sha":"ddf55ea3a79ad7439dc02cb1f93291b1bc95c3b9"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","labelRegex":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/198549","number":198549,"mergeCommit":{"message":"[Security
Solution][Data Quality Dashboard] fix pattern state reset on ilm phase
filter change (#198549)\n\naddresses #196523\r\n\r\n- Fixes ilm phase
change propagation on patterns.\r\n- Adds missing tests for
useResultsRollup functionality\r\n\r\n## UI changes\r\n\r\n###
Before\r\n\r\nhttps://github.com/user-attachments/assets/78a1d809-6a9a-4bfc-88a9-079f829a2017\r\n\r\n###
After\r\n\r\nhttps://github.com/user-attachments/assets/f689fcc9-e1c6-4ccf-a7ca-8f13e9507ba4","sha":"ddf55ea3a79ad7439dc02cb1f93291b1bc95c3b9"}},{"branch":"8.x","label":"v8.17.0","labelRegex":"^v8.17.0$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/198806","number":198806,"state":"MERGED","mergeCommit":{"sha":"fa0bddaac166c1bd17989a54b183677203f1812b","message":"[8.x]
[Security Solution][Data Quality Dashboard] fix pattern state reset on
ilm phase filter change (#198549) (#198806)\n\n# Backport\n\nThis will
backport the following commits from `main` to `8.x`:\n- [[Security
Solution][Data Quality Dashboard] fix pattern state reset\non ilm phase
filter
change\n(#198549)](https://github.com/elastic/kibana/pull/198549)\n\n<!---
Backport version: 9.4.3 -->\n\n### Questions ?\nPlease refer to the
[Backport
tool\ndocumentation](https://github.com/sqren/backport)\n\n<!--BACKPORT
[{\"author\":{\"name\":\"Karen\nGrigoryan\",\"email\":\"karen.grigoryan@elastic.co\"},\"sourceCommit\":{\"committedDate\":\"2024-11-04T15:09:12Z\",\"message\":\"[Security\nSolution][Data
Quality Dashboard] fix pattern state reset on ilm phase\nfilter change
(#198549)\\n\\naddresses #196523\\r\\n\\r\\n- Fixes ilm phase\nchange
propagation on patterns.\\r\\n- Adds missing tests for\nuseResultsRollup
functionality\\r\\n\\r\\n## UI
changes\\r\\n\\r\\n###\nBefore\\r\\n\\r\\nhttps://github.com/user-attachments/assets/78a1d809-6a9a-4bfc-88a9-079f829a2017\\r\\n\\r\\n###\nAfter\\r\\n\\r\\nhttps://github.com/user-attachments/assets/f689fcc9-e1c6-4ccf-a7ca-8f13e9507ba4\",\"sha\":\"ddf55ea3a79ad7439dc02cb1f93291b1bc95c3b9\",\"branchLabelMapping\":{\"^v9.0.0$\":\"main\",\"^v8.17.0$\":\"8.x\",\"^v(\\\\d+).(\\\\d+).\\\\d+$\":\"$1.$2\"}},\"sourcePullRequest\":{\"labels\":[\"release_note:skip\",\"v9.0.0\",\"Team:Threat\nHunting\",\"Team:Threat\nHunting:Explore\",\"backport:prev-minor\",\"ci:cloud-deploy\",\"ci:project-deploy-security\"],\"title\":\"[Security\nSolution][Data
Quality Dashboard] fix pattern state reset on ilm
phase\nfilter\nchange\",\"number\":198549,\"url\":\"https://github.com/elastic/kibana/pull/198549\",\"mergeCommit\":{\"message\":\"[Security\nSolution][Data
Quality Dashboard] fix pattern state reset on ilm phase\nfilter change
(#198549)\\n\\naddresses #196523\\r\\n\\r\\n- Fixes ilm phase\nchange
propagation on patterns.\\r\\n- Adds missing tests for\nuseResultsRollup
functionality\\r\\n\\r\\n## UI
changes\\r\\n\\r\\n###\nBefore\\r\\n\\r\\nhttps://github.com/user-attachments/assets/78a1d809-6a9a-4bfc-88a9-079f829a2017\\r\\n\\r\\n###\nAfter\\r\\n\\r\\nhttps://github.com/user-attachments/assets/f689fcc9-e1c6-4ccf-a7ca-8f13e9507ba4\",\"sha\":\"ddf55ea3a79ad7439dc02cb1f93291b1bc95c3b9\"}},\"sourceBranch\":\"main\",\"suggestedTargetBranches\":[],\"targetPullRequestStates\":[{\"branch\":\"main\",\"label\":\"v9.0.0\",\"branchLabelMappingKey\":\"^v9.0.0$\",\"isSourceBranch\":true,\"state\":\"MERGED\",\"url\":\"https://github.com/elastic/kibana/pull/198549\",\"number\":198549,\"mergeCommit\":{\"message\":\"[Security\nSolution][Data
Quality Dashboard] fix pattern state reset on ilm phase\nfilter change
(#198549)\\n\\naddresses #196523\\r\\n\\r\\n- Fixes ilm phase\nchange
propagation on patterns.\\r\\n- Adds missing tests for\nuseResultsRollup
functionality\\r\\n\\r\\n## UI
changes\\r\\n\\r\\n###\nBefore\\r\\n\\r\\nhttps://github.com/user-attachments/assets/78a1d809-6a9a-4bfc-88a9-079f829a2017\\r\\n\\r\\n###\nAfter\\r\\n\\r\\nhttps://github.com/user-attachments/assets/f689fcc9-e1c6-4ccf-a7ca-8f13e9507ba4\",\"sha\":\"ddf55ea3a79ad7439dc02cb1f93291b1bc95c3b9\"}}]}]\nBACKPORT-->\n\nCo-authored-by:
Karen Grigoryan <karen.grigoryan@elastic.co>"}}]}] BACKPORT-->
  • Loading branch information
kapral18 authored Nov 18, 2024
1 parent a639df8 commit 8869d3d
Show file tree
Hide file tree
Showing 8 changed files with 986 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { renderHook } from '@testing-library/react-hooks';
import { notificationServiceMock } from '@kbn/core-notifications-browser-mocks';

import { getHistoricalResultStub } from '../../../../stub/get_historical_result_stub';
import { useStoredPatternResults } from '.';

describe('useStoredPatternResults', () => {
const httpFetch = jest.fn();
const mockToasts = notificationServiceMock.createStartContract().toasts;

beforeEach(() => {
jest.clearAllMocks();
});

describe('when patterns are empty', () => {
it('should return an empty array and not call getStorageResults', () => {
const { result } = renderHook(() => useStoredPatternResults([], mockToasts, httpFetch));

expect(result.current).toEqual([]);
expect(httpFetch).not.toHaveBeenCalled();
});
});

describe('when patterns are provided', () => {
it('should fetch and return stored pattern results correctly', async () => {
const patterns = ['pattern1-*', 'pattern2-*'];

httpFetch.mockImplementation((path: string) => {
if (path === '/internal/ecs_data_quality_dashboard/results_latest/pattern1-*') {
return Promise.resolve([getHistoricalResultStub('pattern1-index1')]);
}

if (path === '/internal/ecs_data_quality_dashboard/results_latest/pattern2-*') {
return Promise.resolve([getHistoricalResultStub('pattern2-index1')]);
}

return Promise.reject(new Error('Invalid path'));
});

const { result, waitFor } = renderHook(() =>
useStoredPatternResults(patterns, mockToasts, httpFetch)
);

await waitFor(() => result.current.length > 0);

expect(httpFetch).toHaveBeenCalledTimes(2);

expect(httpFetch).toHaveBeenCalledWith(
'/internal/ecs_data_quality_dashboard/results_latest/pattern1-*',
{
method: 'GET',
signal: expect.any(AbortSignal),
version: '1',
}
);
expect(httpFetch).toHaveBeenCalledWith(
'/internal/ecs_data_quality_dashboard/results_latest/pattern2-*',
{
method: 'GET',
signal: expect.any(AbortSignal),
version: '1',
}
);

expect(result.current).toEqual([
{
pattern: 'pattern1-*',
results: {
'pattern1-index1': {
docsCount: expect.any(Number),
error: null,
ilmPhase: expect.any(String),
incompatible: expect.any(Number),
indexName: 'pattern1-index1',
pattern: 'pattern1-*',
markdownComments: expect.any(Array),
sameFamily: expect.any(Number),
checkedAt: expect.any(Number),
},
},
},
{
pattern: 'pattern2-*',
results: {
'pattern2-index1': {
docsCount: expect.any(Number),
error: null,
ilmPhase: expect.any(String),
incompatible: expect.any(Number),
indexName: 'pattern2-index1',
pattern: 'pattern2-*',
markdownComments: expect.any(Array),
sameFamily: expect.any(Number),
checkedAt: expect.any(Number),
},
},
},
]);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { useEffect, useState } from 'react';
import { IToasts } from '@kbn/core-notifications-browser';
import { HttpHandler } from '@kbn/core-http-browser';
import { isEmpty } from 'lodash/fp';

import { DataQualityCheckResult } from '../../../../types';
import { formatResultFromStorage, getStorageResults } from '../../utils/storage';

export const useStoredPatternResults = (
patterns: string[],
toasts: IToasts,
httpFetch: HttpHandler
) => {
const [storedPatternResults, setStoredPatternResults] = useState<
Array<{ pattern: string; results: Record<string, DataQualityCheckResult> }>
>([]);

useEffect(() => {
if (isEmpty(patterns)) {
return;
}

const abortController = new AbortController();
const fetchStoredPatternResults = async () => {
const requests = patterns.map((pattern) =>
getStorageResults({ pattern, httpFetch, abortController, toasts }).then((results = []) => ({
pattern,
results: Object.fromEntries(
results.map((storageResult) => [
storageResult.indexName,
formatResultFromStorage({ storageResult, pattern }),
])
),
}))
);

const patternResults = await Promise.all(requests);
if (patternResults?.length) {
setStoredPatternResults(patternResults);
}
};

fetchStoredPatternResults();
}, [httpFetch, patterns, toasts]);

return storedPatternResults;
};
Loading

0 comments on commit 8869d3d

Please sign in to comment.