Skip to content

Commit

Permalink
[Security Solution][DQD][Tech Debt] Refactor top level helpers (#191233)
Browse files Browse the repository at this point in the history
addresses #190964

Third in the series of PRs to address general DQD tech debt

This one builds on previous 2 PRs 

#190970
#190978 

Gist of changes:

- split top level helpers into series of utils/* files
- each utils/ file is named after common behavior it export or works
with.
- cleanup dead code
  • Loading branch information
kapral18 authored Aug 26, 2024
1 parent 110ec27 commit ad36040
Show file tree
Hide file tree
Showing 73 changed files with 2,644 additions and 2,562 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ export const ilmPhaseOptionsStatic: EuiComboBoxOptionOption[] = [
value: 'unmanaged',
},
];

export const EMPTY_STAT = '--';

export const INTERNAL_API_VERSION = '1';
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import numeral from '@elastic/numeral';
import { render, screen, waitFor } from '@testing-library/react';
import React from 'react';

import { EMPTY_STAT } from '../../helpers';
import { EMPTY_STAT } from '../../constants';
import { alertIndexWithAllResults } from '../../mock/pattern_rollup/mock_alerts_pattern_rollup';
import { auditbeatWithAllResults } from '../../mock/pattern_rollup/mock_auditbeat_pattern_rollup';
import { packetbeatNoResults } from '../../mock/pattern_rollup/mock_packetbeat_pattern_rollup';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import { mockDataQualityCheckResult } from '../../../mock/data_quality_check_res
import { auditbeatWithAllResults } from '../../../mock/pattern_rollup/mock_auditbeat_pattern_rollup';
import { mockStats } from '../../../mock/stats/mock_stats';
import { DataQualityCheckResult } from '../../../types';
import { getIndexNames, getTotalDocsCount } from '../../../helpers';
import { IndexSummaryTableItem } from './types';
import { getIndexNames, getPatternDocsCount } from './utils/stats';

const hot: IlmExplainLifecycleLifecycleExplainManaged = {
index: '.ds-packetbeat-8.6.1-2023.02.04-000001',
Expand Down Expand Up @@ -569,7 +569,7 @@ describe('helpers', () => {
ilmPhases: ['hot', 'unmanaged'],
isILMAvailable,
});
const newDocsCount = getTotalDocsCount({ indexNames: newIndexNames, stats: mockStats });
const newDocsCount = getPatternDocsCount({ indexNames: newIndexNames, stats: mockStats });
test('it returns false when the `patternRollup.docsCount` equals newDocsCount', () => {
expect(
shouldCreatePatternRollup({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import type {
SortConfig,
MeteringStatsIndex,
} from '../../../types';
import { getDocsCount, getSizeInBytes } from '../../../helpers';
import { IndexSummaryTableItem } from './types';
import { getDocsCount, getSizeInBytes } from '../../../utils/stats';

export const isManaged = (
ilmExplainRecord: IlmExplainLifecycleLifecycleExplain | undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { IlmExplainLifecycleLifecycleExplain } from '@elastic/elasticsearch
import { useEffect, useState } from 'react';

import { useDataQualityContext } from '../../../../../data_quality_context';
import { INTERNAL_API_VERSION } from '../../../../../helpers';
import { INTERNAL_API_VERSION } from '../../../../../constants';
import * as i18n from '../../../../../translations';
import { useIsMounted } from '../../../../../hooks/use_is_mounted';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { HttpFetchQuery } from '@kbn/core/public';

import { useDataQualityContext } from '../../../../../data_quality_context';
import * as i18n from '../../../../../translations';
import { INTERNAL_API_VERSION } from '../../../../../helpers';
import { INTERNAL_API_VERSION } from '../../../../../constants';
import { MeteringStatsIndex } from '../../../../../types';
import { useIsMounted } from '../../../../../hooks/use_is_mounted';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import numeral from '@elastic/numeral';
import { render, screen } from '@testing-library/react';
import React, { ComponentProps } from 'react';

import { EMPTY_STAT } from '../../../helpers';
import { EMPTY_STAT } from '../../../constants';
import {
TestDataQualityProviders,
TestExternalProviders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@ import {
shouldCreateIndexNames,
shouldCreatePatternRollup,
} from './helpers';
import {
getIndexNames,
getTotalDocsCount,
getTotalPatternIncompatible,
getTotalPatternIndicesChecked,
getTotalSizeInBytes,
} from '../../../helpers';
import { getTotalPatternIncompatible, getTotalPatternIndicesChecked } from '../../../utils/stats';
import { getIndexNames, getPatternDocsCount, getPatternSizeInBytes } from './utils/stats';
import { LoadingEmptyPrompt } from './loading_empty_prompt';
import { PatternSummary } from './pattern_summary';
import { RemoteClustersCallout } from './remote_clusters_callout';
Expand Down Expand Up @@ -152,7 +147,7 @@ const PatternComponent: React.FC<Props> = ({

useEffect(() => {
const newIndexNames = getIndexNames({ stats, ilmExplain, ilmPhases, isILMAvailable });
const newDocsCount = getTotalDocsCount({ indexNames: newIndexNames, stats });
const newDocsCount = getPatternDocsCount({ indexNames: newIndexNames, stats });

if (
shouldCreateIndexNames({
Expand Down Expand Up @@ -188,7 +183,7 @@ const PatternComponent: React.FC<Props> = ({
pattern,
results: undefined,
sizeInBytes: isILMAvailable
? getTotalSizeInBytes({
? getPatternSizeInBytes({
indexNames: getIndexNames({ stats, ilmExplain, ilmPhases, isILMAvailable }),
stats,
}) ?? 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import {
} from '@elastic/eui';
import React, { useCallback, useEffect } from 'react';
import moment from 'moment';
import { getDocsCount, getSizeInBytes } from '../../../../utils/stats';
import { useIndicesCheckContext } from '../../../../contexts/indices_check_context';

import { EMPTY_STAT, getDocsCount, getSizeInBytes } from '../../../../helpers';
import { EMPTY_STAT } from '../../../../constants';
import { MeteringStatsIndex, PatternRollup } from '../../../../types';
import { useDataQualityContext } from '../../../../data_quality_context';
import { IndexProperties } from './index_properties';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
* 2.0.
*/

import {
getMappingsProperties,
getSortedPartitionedFieldMetadata,
hasAllDataFetchingCompleted,
} from './helpers';
import { getMappingsProperties, getSortedPartitionedFieldMetadata } from './helpers';
import { mockIndicesGetMappingIndexMappingRecords } from '../../../../../mock/indices_get_mapping_index_mapping_record/mock_indices_get_mapping_index_mapping_record';
import { mockMappingsProperties } from '../../../../../mock/mappings_properties/mock_mappings_properties';
import { EcsFlatTyped } from '../../../../../constants';
Expand Down Expand Up @@ -250,42 +246,4 @@ describe('helpers', () => {
).toBeNull();
});
});

describe('hasAllDataFetchingCompleted', () => {
test('it returns false when both the mappings and unallowed values are loading', () => {
expect(
hasAllDataFetchingCompleted({
loadingMappings: true,
loadingUnallowedValues: true,
})
).toBe(false);
});

test('it returns false when mappings are loading, and unallowed values are NOT loading', () => {
expect(
hasAllDataFetchingCompleted({
loadingMappings: true,
loadingUnallowedValues: false,
})
).toBe(false);
});

test('it returns false when mappings are NOT loading, and unallowed values are loading', () => {
expect(
hasAllDataFetchingCompleted({
loadingMappings: false,
loadingUnallowedValues: true,
})
).toBe(false);
});

test('it returns true when both the mappings and unallowed values have finished loading', () => {
expect(
hasAllDataFetchingCompleted({
loadingMappings: false,
loadingUnallowedValues: false,
})
).toBe(true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import type {
import { sortBy } from 'lodash/fp';

import { EcsFlatTyped } from '../../../../../constants';
import type { PartitionedFieldMetadata, UnallowedValueCount } from '../../../../../types';
import {
getEnrichedFieldMetadata,
getFieldTypes,
getMissingTimestampFieldMetadata,
getPartitionedFieldMetadata,
} from '../../../../../helpers';
import type { PartitionedFieldMetadata, UnallowedValueCount } from '../../../../../types';
} from './utils/metadata';

export const ALL_TAB_ID = 'allTab';
export const ECS_COMPLIANT_TAB_ID = 'ecsCompliantTab';
Expand Down Expand Up @@ -90,11 +90,3 @@ export const getMappingsProperties = ({

return null;
};

export const hasAllDataFetchingCompleted = ({
loadingMappings,
loadingUnallowedValues,
}: {
loadingMappings: boolean;
loadingUnallowedValues: boolean;
}): boolean => loadingMappings === false && loadingUnallowedValues === false;
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import numeral from '@elastic/numeral';
import { render, screen, waitFor } from '@testing-library/react';
import React from 'react';

import { EMPTY_STAT } from '../../../../../helpers';
import { EMPTY_STAT } from '../../../../../constants';
import { auditbeatWithAllResults } from '../../../../../mock/pattern_rollup/mock_auditbeat_pattern_rollup';
import {
TestDataQualityProviders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import React from 'react';

import { SameFamily } from '../same_family';
import { EcsAllowedValues } from '../ecs_allowed_values';
import { getIsInSameFamily } from '../../../../../../../../../helpers';
import { IndexInvalidValues } from '../index_invalid_values';
import { CodeDanger, CodeSuccess } from '../../../../../../../../../styles';
import * as i18n from '../translations';
Expand All @@ -20,6 +19,7 @@ import type {
EnrichedFieldMetadata,
UnallowedValueCount,
} from '../../../../../../../../../types';
import { getIsInSameFamily } from '../../../../utils/get_is_in_same_family';

export const EMPTY_PLACEHOLDER = '--';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
someField,
} from '../../../../../../../../mock/enriched_field_metadata/mock_enriched_field_metadata';
import { mockPartitionedFieldMetadata } from '../../../../../../../../mock/partitioned_field_metadata/mock_partitioned_field_metadata';
import { EMPTY_STAT } from '../../../../../../../../helpers';
import { EMPTY_STAT } from '../../../../../../../../constants';

const defaultBytesFormat = '0,0.[0]b';
const formatBytes = (value: number | undefined) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import { EuiBadge } from '@elastic/eui';
import React from 'react';
import styled from 'styled-components';

import { getSizeInBytes } from '../../../../../../../utils/stats';
import { getIncompatibleStatBadgeColor } from '../../../../../../../utils/get_incompatible_stat_badge_color';
import { AllTab } from './all_tab';
import { CustomTab } from './custom_tab';
import { EcsCompliantTab } from './ecs_compliant_tab';
import { getIncompatibleStatBadgeColor, getSizeInBytes } from '../../../../../../../helpers';
import { IncompatibleTab } from './incompatible_tab';
import {
ALL_TAB_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
getIncompatibleValuesFields,
showInvalidCallout,
} from './helpers';
import { EMPTY_STAT } from '../../../../../../../../helpers';
import { EMPTY_STAT } from '../../../../../../../../constants';
import {
DETECTION_ENGINE_RULES_MAY_NOT_MATCH,
MAPPINGS_THAT_CONFLICT_WITH_ECS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
DOCUMENT_VALUES_ACTUAL,
ECS_VALUES_EXPECTED,
} from '../compare_fields_table/translations';
import { getIsInSameFamily } from '../../../../../../../../helpers';
import { getIsInSameFamily } from '../../../utils/get_is_in_same_family';

export const getIncompatibleFieldsMarkdownComment = (incompatible: number): string =>
getMarkdownComment({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
getSameFamilyMarkdownComment,
getSameFamilyMarkdownTablesComment,
} from './helpers';
import { EMPTY_STAT } from '../../../../../../../../helpers';
import { EMPTY_STAT } from '../../../../../../../../constants';
import { mockPartitionedFieldMetadata } from '../../../../../../../../mock/partitioned_field_metadata/mock_partitioned_field_metadata';
import { mockPartitionedFieldMetadataWithSameFamily } from '../../../../../../../../mock/partitioned_field_metadata/mock_partitioned_field_metadata_with_same_family';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { DOCS } from '../translations';
import { ILM_PHASE } from '../../../../../../translations';
import { SIZE } from '../../../summary_table/translations';
import { Stat } from '../../../../../../stat';
import { getIlmPhaseDescription } from '../../../../../../helpers';
import { getIlmPhaseDescription } from '../../../../../../utils/get_ilm_phase_description';

const StyledFlexItem = styled(EuiFlexItem)`
border-right: 1px solid ${({ theme }) => theme.eui.euiBorderColor};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
getSummaryTableMarkdownRow,
getTabCountsMarkdownComment,
} from './helpers';
import { EMPTY_STAT } from '../../../../../../helpers';
import { EMPTY_STAT } from '../../../../../../constants';
import { mockAllowedValues } from '../../../../../../mock/allowed_values/mock_allowed_values';
import {
eventCategory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* 2.0.
*/

import {
getTotalPatternIncompatible,
getTotalPatternIndicesChecked,
} from '../../../../../../utils/stats';
import {
ERRORS_MAY_OCCUR,
ERRORS_CALLOUT_SUMMARY,
Expand All @@ -15,11 +19,7 @@ import {
THE_FOLLOWING_PRIVILEGES_ARE_REQUIRED,
VIEW_INDEX_METADATA,
} from '../../../../../../data_quality_summary/summary_actions/check_status/errors_popover/translations';
import {
EMPTY_STAT,
getTotalPatternIncompatible,
getTotalPatternIndicesChecked,
} from '../../../../../../helpers';
import { EMPTY_STAT } from '../../../../../../constants';
import { SAME_FAMILY } from '../index_check_fields/tabs/compare_fields_table/same_family/translations';
import {
HOT,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 { getIsInSameFamily } from './get_is_in_same_family';

describe('getIsInSameFamily', () => {
test('it returns false when ecsExpectedType is undefined', () => {
expect(getIsInSameFamily({ ecsExpectedType: undefined, type: 'keyword' })).toBe(false);
});

const expectedFamilyMembers: {
[key: string]: string[];
} = {
constant_keyword: ['keyword', 'wildcard'], // `keyword` and `wildcard` in the same family as `constant_keyword`
keyword: ['constant_keyword', 'wildcard'],
match_only_text: ['text'],
text: ['match_only_text'],
wildcard: ['keyword', 'constant_keyword'],
};

const ecsExpectedTypes = Object.keys(expectedFamilyMembers);

ecsExpectedTypes.forEach((ecsExpectedType) => {
const otherMembersOfSameFamily = expectedFamilyMembers[ecsExpectedType];

otherMembersOfSameFamily.forEach((type) =>
test(`it returns true for ecsExpectedType '${ecsExpectedType}' when given '${type}', a type in the same family`, () => {
expect(getIsInSameFamily({ ecsExpectedType, type })).toBe(true);
})
);

test(`it returns false for ecsExpectedType '${ecsExpectedType}' when given 'date', a type NOT in the same family`, () => {
expect(getIsInSameFamily({ ecsExpectedType, type: 'date' })).toBe(false);
});
});
});
Loading

0 comments on commit ad36040

Please sign in to comment.