From 81d8f64577fbd1fd79384437c50d62474a0dd3bb Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Wed, 22 May 2024 16:53:20 +0200 Subject: [PATCH] make logger/emitError optional --- .../queries/fetch_categories.ts | 25 +++++---- .../queries/fetch_category_counts.ts | 46 +++++++++------- .../queries/fetch_significant_categories.ts | 4 +- .../fetch_significant_term_p_values.ts | 25 +++++---- .../queries/fetch_simple_log_rate_analysis.ts | 54 ++++++++----------- 5 files changed, 82 insertions(+), 72 deletions(-) diff --git a/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_categories.ts b/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_categories.ts index 99300b2bef2e6c8..56f2b57f2fee8e5 100644 --- a/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_categories.ts +++ b/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_categories.ts @@ -97,10 +97,10 @@ export const fetchCategories = async ( esClient: ElasticsearchClient, params: AiopsLogRateAnalysisSchema, fieldNames: string[], - logger: Logger, + logger?: Logger, // The default value of 1 means no sampling will be used sampleProbability: number = 1, - emitError: (m: string) => void, + emitError?: (m: string) => void, abortSignal?: AbortSignal ): Promise => { const randomSamplerWrapper = createRandomSamplerWrapper({ @@ -122,14 +122,19 @@ export const fetchCategories = async ( function reportError(fieldName: string, error: unknown) { if (!isRequestAbortedError(error)) { - logger.error( - `Failed to fetch category aggregation for fieldName "${fieldName}", got: \n${JSON.stringify( - error, - null, - 2 - )}` - ); - emitError(`Failed to fetch category aggregation for fieldName "${fieldName}".`); + if (logger) { + logger.error( + `Failed to fetch category aggregation for fieldName "${fieldName}", got: \n${JSON.stringify( + error, + null, + 2 + )}` + ); + } + + if (emitError) { + emitError(`Failed to fetch category aggregation for fieldName "${fieldName}".`); + } } } diff --git a/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_category_counts.ts b/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_category_counts.ts index 93018370a54ffe9..0879e5de7aeff57 100644 --- a/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_category_counts.ts +++ b/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_category_counts.ts @@ -75,8 +75,8 @@ export const fetchCategoryCounts = async ( categories: FetchCategoriesResponse, from: number | undefined, to: number | undefined, - logger: Logger, - emitError: (m: string) => void, + logger?: Logger, + emitError?: (m: string) => void, abortSignal?: AbortSignal ): Promise => { const updatedCategories = cloneDeep(categories); @@ -101,14 +101,19 @@ export const fetchCategoryCounts = async ( ); } catch (error) { if (!isRequestAbortedError(error)) { - logger.error( - `Failed to fetch category counts for field name "${fieldName}", got: \n${JSON.stringify( - error, - null, - 2 - )}` - ); - emitError(`Failed to fetch category counts for field name "${fieldName}".`); + if (logger) { + logger.error( + `Failed to fetch category counts for field name "${fieldName}", got: \n${JSON.stringify( + error, + null, + 2 + )}` + ); + } + + if (emitError) { + emitError(`Failed to fetch category counts for field name "${fieldName}".`); + } } return updatedCategories; } @@ -118,14 +123,19 @@ export const fetchCategoryCounts = async ( updatedCategories.categories[index].count = (resp.hits.total as estypes.SearchTotalHits).value ?? 0; } else { - logger.error( - `Failed to fetch category count for category "${ - updatedCategories.categories[index].key - }", got: \n${JSON.stringify(resp, null, 2)}` - ); - emitError( - `Failed to fetch category count for category "${updatedCategories.categories[index].key}".` - ); + if (logger) { + logger.error( + `Failed to fetch category count for category "${ + updatedCategories.categories[index].key + }", got: \n${JSON.stringify(resp, null, 2)}` + ); + } + + if (emitError) { + emitError( + `Failed to fetch category count for category "${updatedCategories.categories[index].key}".` + ); + } } } diff --git a/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_significant_categories.ts b/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_significant_categories.ts index e15918d0896e793..a728fc2548c2d12 100644 --- a/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_significant_categories.ts +++ b/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_significant_categories.ts @@ -36,10 +36,10 @@ export const fetchSignificantCategories = async ( esClient: ElasticsearchClient, params: AiopsLogRateAnalysisSchema, fieldNames: string[], - logger: Logger, + logger?: Logger, // The default value of 1 means no sampling will be used sampleProbability: number = 1, - emitError: (m: string) => void, + emitError?: (m: string) => void, abortSignal?: AbortSignal ) => { const categoriesOverall = await fetchCategories( diff --git a/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_significant_term_p_values.ts b/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_significant_term_p_values.ts index 98dbd11398174a1..ec35cdeb1744845 100644 --- a/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_significant_term_p_values.ts +++ b/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_significant_term_p_values.ts @@ -115,10 +115,10 @@ export const fetchSignificantTermPValues = async ( esClient: ElasticsearchClient, params: AiopsLogRateAnalysisSchema, fieldNames: string[], - logger: Logger, + logger?: Logger, // The default value of 1 means no sampling will be used sampleProbability: number = 1, - emitError: (m: string) => void, + emitError?: (m: string) => void, abortSignal?: AbortSignal ): Promise => { const randomSamplerWrapper = createRandomSamplerWrapper({ @@ -139,14 +139,19 @@ export const fetchSignificantTermPValues = async ( function reportError(fieldName: string, error: unknown) { if (!isRequestAbortedError(error)) { - logger.error( - `Failed to fetch p-value aggregation for fieldName "${fieldName}", got: \n${JSON.stringify( - error, - null, - 2 - )}` - ); - emitError(`Failed to fetch p-value aggregation for fieldName "${fieldName}".`); + if (logger) { + logger.error( + `Failed to fetch p-value aggregation for fieldName "${fieldName}", got: \n${JSON.stringify( + error, + null, + 2 + )}` + ); + } + + if (emitError) { + emitError(`Failed to fetch p-value aggregation for fieldName "${fieldName}".`); + } } } diff --git a/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_simple_log_rate_analysis.ts b/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_simple_log_rate_analysis.ts index b0d9fe41a370520..2a1443af4d6358f 100644 --- a/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_simple_log_rate_analysis.ts +++ b/x-pack/packages/ml/aiops_log_rate_analysis/queries/fetch_simple_log_rate_analysis.ts @@ -26,19 +26,19 @@ import { fetchSignificantTermPValues } from './fetch_significant_term_p_values'; // regarding a limit of abort signal listeners of more than 10. const MAX_CONCURRENT_QUERIES = 5; -interface FieldCandidate { - fieldCandidate: string; +interface KeywordFieldCandidate { + keywordFieldCandidate: string; } -const isFieldCandidate = (d: unknown): d is FieldCandidate => - isPopulatedObject(d, ['fieldCandidate']); +const isKeywordFieldCandidate = (d: unknown): d is KeywordFieldCandidate => + isPopulatedObject(d, ['keywordFieldCandidate']); interface TextFieldCandidate { textFieldCandidate: string; } -const isTextFieldCandidate = (d: unknown): d is FieldCandidate => +const isTextFieldCandidate = (d: unknown): d is TextFieldCandidate => isPopulatedObject(d, ['textFieldCandidate']); -type Candidate = FieldCandidate | TextFieldCandidate; +type QueueFieldCandidate = KeywordFieldCandidate | TextFieldCandidate; export interface LogRateChange { type: string; @@ -118,7 +118,7 @@ export const fetchSimpleLogRateAnalysis = async ( // FIELD CANDIDATES - const fieldCandidates: string[] = []; + const keywordFieldCandidates: string[] = []; const textFieldCandidates: string[] = []; const indexInfoParams: AiopsLogRateAnalysisSchema = { @@ -162,7 +162,7 @@ export const fetchSimpleLogRateAnalysis = async ( ...analysisWindowParameters, }; - fieldCandidates.push(...indexInfo.fieldCandidates); + keywordFieldCandidates.push(...indexInfo.fieldCandidates); textFieldCandidates.push(...indexInfo.textFieldCandidates); const sampleProbability = getSampleProbability( indexInfo.deviationTotalDocCount + indexInfo.baselineTotalDocCount @@ -177,27 +177,20 @@ export const fetchSimpleLogRateAnalysis = async ( const significantTerms: SignificantItem[] = []; const fieldsToSample = new Set(); - const pValuesQueue = queue(async function (payload: Candidate) { - if (isFieldCandidate(payload)) { - const { fieldCandidate } = payload; + const pValuesQueue = queue(async function (payload: QueueFieldCandidate) { + if (isKeywordFieldCandidate(payload)) { + const { keywordFieldCandidate } = payload; let pValues: Awaited> = []; - try { - pValues = await fetchSignificantTermPValues( - esClient, - params, - [fieldCandidate], - undefined, - sampleProbability, - (e) => { - console.log('fetchSignificantTermPValues ERROR', e); - }, - abortSignal - ); - } catch (e) { - console.log('catch fetchSignificantTermPValues ERROR', e); - return; - } + pValues = await fetchSignificantTermPValues( + esClient, + params, + [keywordFieldCandidate], + undefined, + sampleProbability, + undefined, + abortSignal + ); if (pValues.length > 0) { pValues.forEach((d) => { @@ -214,9 +207,7 @@ export const fetchSimpleLogRateAnalysis = async ( [textFieldCandidate], undefined, sampleProbability, - (e) => { - console.log('fetchSignificantCategories ERROR', e); - }, + undefined, abortSignal ); @@ -229,11 +220,10 @@ export const fetchSimpleLogRateAnalysis = async ( pValuesQueue.push( [ ...textFieldCandidates.map((d) => ({ textFieldCandidate: d })), - ...fieldCandidates.map((d) => ({ fieldCandidate: d })), + ...keywordFieldCandidates.map((d) => ({ keywordFieldCandidate: d })), ], (err) => { if (err) { - console.error('queue push error', err); pValuesQueue.kill(); } }