Skip to content

Commit

Permalink
change to check alias
Browse files Browse the repository at this point in the history
Signed-off-by: Kaituo Li <[email protected]>
  • Loading branch information
kaituo committed Jun 8, 2024
1 parent f924b45 commit 4cf545f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function CustomResultIndex(props: CustomResultIndexProps) {
<EuiFlexItem>
<EuiCallOut
data-test-subj="cannotEditResultIndexCallout"
title="You can't change the custom result index after creating the detector. You can manage the result index using the following three settings inside Anomaly Detection plugin or with the Index Management plugin."
title="You can't change the custom result index after creating the detector. You can manage the result index using the following three settings."
color="warning"
iconType="alert"
size="s"
Expand Down
16 changes: 9 additions & 7 deletions public/pages/DetectorDetail/containers/DetectorDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
getDetector,
stopHistoricalDetector,
} from '../../../redux/reducers/ad';
import { getIndices } from '../../../redux/reducers/opensearch';
import { getAliases, getIndices } from '../../../redux/reducers/opensearch';
import { getErrorMessage, Listener } from '../../../utils/utils';
import { darkModeEnabled } from '../../../utils/opensearchDashboardsUtils';
import { BREADCRUMBS, MDS_BREADCRUMBS } from '../../../utils/constants';
Expand Down Expand Up @@ -145,7 +145,7 @@ export const DetectorDetail = (props: DetectorDetailProps) => {
- If the detector is still loading, the result index is not missing.
- If the result index retrieved from the detector is empty, it is not missing.
- If cat indices are being requested, the result index is not missing.
- If visible indices are empty, it is likely there is an issue retrieving existing indices.
- If visible indices/aliaes are empty, it is likely there is an issue retrieving existing indices.
To be safe, we'd rather not show the error message and consider the result index not missing.
- If the result index is not found in the visible indices, then it is missing.
*/
Expand All @@ -156,13 +156,14 @@ export const DetectorDetail = (props: DetectorDetailProps) => {
? false
: isCatIndicesRequesting
? false
: isEmpty(visibleIndices)
: isEmpty(visibleIndices) || isEmpty(visibleAliases)
? false
: !containsIndex(resultIndexOrAlias, visibleIndices) && !containsAlias(resultIndexOrAlias, visibleAliases);

// debug message: prints visibleIndices if isResultIndexMissing is true
if (isResultIndexMissing) {
console.log(`isResultIndexMissing is true, visibleIndices: ${visibleIndices}, visibleAliases: ${visibleAliases}, detector result index: ${resultIndexOrAlias}`);
// The JSON.stringify method converts a JavaScript object or value to a JSON string. The optional null parameter is for the replacer function (not used here), and 2 specifies the indentation level for pretty-printing the JSON.
console.log(`isResultIndexMissing is true, visibleIndices: ${JSON.stringify(visibleIndices, null, 2)}, visibleAliases: ${JSON.stringify(visibleAliases, null, 2)}, detector result index: ${resultIndexOrAlias}`);
}

// String to set in the modal if the realtime detector and/or historical analysis
Expand Down Expand Up @@ -201,20 +202,21 @@ export const DetectorDetail = (props: DetectorDetailProps) => {
scroll(0, 0);
}, []);

// Getting all visible indices. Will re-fetch if changes to the detector (e.g.,
// Getting all visible indices & aliases. Will re-fetch if changes to the detector (e.g.,
// detector starts, result index recreated or user switches tabs to re-fetch detector)
useEffect(() => {
const getInitialIndices = async () => {
const getInitialIndicesAliases = async () => {
try {
await dispatch(getIndices('', dataSourceId));
await dispatch(getAliases('', dataSourceId));
} catch (error) {
console.error(error);
core.notifications.toasts.addDanger('Error getting all indices');
}
};
// only need to check if indices exist after detector finishes loading
if (!isLoadingDetector) {
getInitialIndices();
getInitialIndicesAliases();
}
}, [detector]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ describe('detector detail', () => {
expect(element).toBeNull();
});

test('the result index is not found in the visible indices', () => {
test('the result index is not found in the visible indices but alias is empty', () => {
const detectorInfo = {
detector: getRandomDetector(true, resultIndex),
hasError: false,
Expand All @@ -236,7 +236,7 @@ describe('detector detail', () => {
const element = screen.queryByTestId('missingResultIndexCallOut');

// Assert that the element is in the document
expect(element).not.toBeNull();
expect(element).toBeNull();
});

test('the result index is found in the visible indices', () => {
Expand Down Expand Up @@ -273,8 +273,9 @@ describe('detector detail', () => {
expect(element).toBeNull();
});

test('the result index prefix is found in the visible indices', () => {
test('the result index prefix is found in the visible aliaes', () => {
const detector = getRandomDetector(true, resultIndex);
const resultIndexFull = resultIndex + '-history-2024.06.05-1';

// Set up the mock implementation for useFetchDetectorInfo
(useFetchDetectorInfo as jest.Mock).mockImplementation(() => ({
Expand All @@ -288,7 +289,12 @@ describe('detector detail', () => {
opensearch: {
indices: [
{ health: 'green', index: '.kibana_-962704462_v992471_1' },
{ health: 'green', index: resultIndex + '-history-2024.06.05-1' },
{ health: 'green', index: resultIndexFull},
],
aliases : [
{index: '.opendistro-anomaly-results-history-2024.06.08-1', alias: '.opendistro-anomaly-results'},
{index: resultIndexFull, alias: resultIndex},
{index: '.kibana_1', alias: '.kibana'},
],
requesting: false,
},
Expand All @@ -306,4 +312,41 @@ describe('detector detail', () => {
// Assert that the element is not in the document
expect(element).toBeNull();
});

test('the result index prefix is not found in both visible aliaes and indices', () => {
const detector = getRandomDetector(true, resultIndex);

// Set up the mock implementation for useFetchDetectorInfo
(useFetchDetectorInfo as jest.Mock).mockImplementation(() => ({
detector: detector,
hasError: false,
isLoadingDetector: false,
errorMessage: undefined,
}));

const initialState = {
opensearch: {
indices: [
{ health: 'green', index: '.kibana_-962704462_v992471_1' },
],
aliases : [
{index: '.opendistro-anomaly-results-history-2024.06.08-1', alias: '.opendistro-anomaly-results'},
{index: '.kibana_1', alias: '.kibana'},
],
requesting: false,
},
ad: {
detectors: {},
},
alerting: {
monitors: {},
},
};

renderWithRouter(detectorId, initialState);
const element = screen.queryByTestId('missingResultIndexCallOut');

// Assert that the element is not in the document
expect(element).not.toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ export const DetectorDefinitionFields = (
}
};

const minAge = get(props, 'detector.resultIndexMinAge', '-');
const minSize = get(props, 'detector.resultIndexMinSize', '-');
const ttl = get(props, 'detector.resultIndexTtl', '-');

return (
<ContentPanel
title="Detector settings"
Expand Down Expand Up @@ -220,19 +224,19 @@ export const DetectorDefinitionFields = (
<EuiFlexItem>
<ConfigCell
title="Custom result index min age"
description={get(props, 'detector.resultIndexMinAge', '-') + ' Days'}
description={minAge === '-' ? minAge : `${minAge} Days`}
/>
</EuiFlexItem>
<EuiFlexItem>
<ConfigCell
title="Custom result index min size"
description={get(props, 'detector.resultIndexMinSize', '-') + ' MB'}
description={minSize == '-' ? minSize : `${minSize} MB`}
/>
</EuiFlexItem>
<EuiFlexItem>
<ConfigCell
title="Custom result index TTL"
description={get(props, 'detector.resultIndexTtl', '-') + ' Days'}
description={ttl == '-' ? ttl : `${ttl} Days`}
/>
</EuiFlexItem>
</EuiFlexGrid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ exports[`<ReviewAndCreate /> spec renders the component, validation loading 1`]
<p
class="enabled"
>
- Days
-
</p>
</div>
</div>
Expand Down Expand Up @@ -458,7 +458,7 @@ exports[`<ReviewAndCreate /> spec renders the component, validation loading 1`]
<p
class="enabled"
>
- MB
-
</p>
</div>
</div>
Expand Down Expand Up @@ -492,7 +492,7 @@ exports[`<ReviewAndCreate /> spec renders the component, validation loading 1`]
<p
class="enabled"
>
- Days
-
</p>
</div>
</div>
Expand Down Expand Up @@ -1518,7 +1518,7 @@ exports[`issue in detector validation issues in feature query 1`] = `
<p
class="enabled"
>
- Days
-
</p>
</div>
</div>
Expand Down Expand Up @@ -1552,7 +1552,7 @@ exports[`issue in detector validation issues in feature query 1`] = `
<p
class="enabled"
>
- MB
-
</p>
</div>
</div>
Expand Down Expand Up @@ -1586,7 +1586,7 @@ exports[`issue in detector validation issues in feature query 1`] = `
<p
class="enabled"
>
- Days
-
</p>
</div>
</div>
Expand Down

0 comments on commit 4cf545f

Please sign in to comment.