From 62b3a575ca1f9019159bbab3b3762240f6b28c06 Mon Sep 17 00:00:00 2001 From: James Gowdy Date: Fri, 17 Jan 2020 15:10:39 +0000 Subject: [PATCH] function move and rename --- .../new_job/common/job_creator/job_creator.ts | 24 ++----------------- .../common/job_creator/util/general.ts | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/x-pack/legacy/plugins/ml/public/application/jobs/new_job/common/job_creator/job_creator.ts b/x-pack/legacy/plugins/ml/public/application/jobs/new_job/common/job_creator/job_creator.ts index 6d7e9e6d473d908..90c189c9d61971c 100644 --- a/x-pack/legacy/plugins/ml/public/application/jobs/new_job/common/job_creator/job_creator.ts +++ b/x-pack/legacy/plugins/ml/public/application/jobs/new_job/common/job_creator/job_creator.ts @@ -19,7 +19,7 @@ import { CREATED_BY_LABEL, SHARED_RESULTS_INDEX_NAME, } from '../../../../../../common/constants/new_job'; -import { isSparseDataJob } from './util/general'; +import { isSparseDataJob, collectAggs } from './util/general'; import { parseInterval } from '../../../../../../common/util/parse_interval'; import { Calendar } from '../../../../../../common/types/calendars'; import { mlCalendarService } from '../../../../services/calendar_service'; @@ -624,27 +624,7 @@ export class JobCreator { this._aggregationFields = []; if (this._datafeed_config.aggregations?.buckets !== undefined) { - traverseAggs(this._datafeed_config.aggregations.buckets, this._aggregationFields); - } - } -} - -function traverseAggs(o: any, aggFields: Field[]) { - for (const i in o) { - if (o[i] !== null && typeof o[i] === 'object') { - if (i === 'aggregations' || i === 'aggs') { - Object.keys(o[i]).forEach(k => { - if (k !== 'aggregations' && i !== 'aggs') { - aggFields.push({ - id: k, - name: k, - type: ES_FIELD_TYPES.KEYWORD, - aggregatable: true, - }); - } - }); - } - traverseAggs(o[i], aggFields); + collectAggs(this._datafeed_config.aggregations.buckets, this._aggregationFields); } } } diff --git a/x-pack/legacy/plugins/ml/public/application/jobs/new_job/common/job_creator/util/general.ts b/x-pack/legacy/plugins/ml/public/application/jobs/new_job/common/job_creator/util/general.ts index e4419689d8927da..e5b6212a4326e50 100644 --- a/x-pack/legacy/plugins/ml/public/application/jobs/new_job/common/job_creator/util/general.ts +++ b/x-pack/legacy/plugins/ml/public/application/jobs/new_job/common/job_creator/util/general.ts @@ -12,6 +12,7 @@ import { SPARSE_DATA_AGGREGATIONS, } from '../../../../../../../common/constants/aggregation_types'; import { MLCATEGORY, DOC_COUNT } from '../../../../../../../common/constants/field_types'; +import { ES_FIELD_TYPES } from '../../../../../../../../../../../src/plugins/data/public'; import { EVENT_RATE_FIELD_ID, Field, @@ -315,3 +316,26 @@ export function getJobCreatorTitle(jobCreator: JobCreatorType) { return ''; } } + +// recurse through a datafeed aggregation object, +// adding top level keys from each nested agg to an array +// of fields +export function collectAggs(o: any, aggFields: Field[]) { + for (const i in o) { + if (o[i] !== null && typeof o[i] === 'object') { + if (i === 'aggregations' || i === 'aggs') { + Object.keys(o[i]).forEach(k => { + if (k !== 'aggregations' && i !== 'aggs') { + aggFields.push({ + id: k, + name: k, + type: ES_FIELD_TYPES.KEYWORD, + aggregatable: true, + }); + } + }); + } + collectAggs(o[i], aggFields); + } + } +}