Skip to content

Commit 6facb00

Browse files
committed
function move and rename
1 parent ef56b0e commit 6facb00

2 files changed

Lines changed: 26 additions & 22 deletions

File tree

x-pack/legacy/plugins/ml/public/application/jobs/new_job/common/job_creator/job_creator.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
CREATED_BY_LABEL,
2020
SHARED_RESULTS_INDEX_NAME,
2121
} from '../../../../../../common/constants/new_job';
22-
import { isSparseDataJob } from './util/general';
22+
import { isSparseDataJob, collectAggs } from './util/general';
2323
import { parseInterval } from '../../../../../../common/util/parse_interval';
2424
import { Calendar } from '../../../../../../common/types/calendars';
2525
import { mlCalendarService } from '../../../../services/calendar_service';
@@ -624,27 +624,7 @@ export class JobCreator {
624624

625625
this._aggregationFields = [];
626626
if (this._datafeed_config.aggregations?.buckets !== undefined) {
627-
traverseAggs(this._datafeed_config.aggregations.buckets, this._aggregationFields);
628-
}
629-
}
630-
}
631-
632-
function traverseAggs(o: any, aggFields: Field[]) {
633-
for (const i in o) {
634-
if (o[i] !== null && typeof o[i] === 'object') {
635-
if (i === 'aggregations' || i === 'aggs') {
636-
Object.keys(o[i]).forEach(k => {
637-
if (k !== 'aggregations' && i !== 'aggs') {
638-
aggFields.push({
639-
id: k,
640-
name: k,
641-
type: ES_FIELD_TYPES.KEYWORD,
642-
aggregatable: true,
643-
});
644-
}
645-
});
646-
}
647-
traverseAggs(o[i], aggFields);
627+
collectAggs(this._datafeed_config.aggregations.buckets, this._aggregationFields);
648628
}
649629
}
650630
}

x-pack/legacy/plugins/ml/public/application/jobs/new_job/common/job_creator/util/general.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
SPARSE_DATA_AGGREGATIONS,
1313
} from '../../../../../../../common/constants/aggregation_types';
1414
import { MLCATEGORY, DOC_COUNT } from '../../../../../../../common/constants/field_types';
15+
import { ES_FIELD_TYPES } from '../../../../../../../../../../../src/plugins/data/public';
1516
import {
1617
EVENT_RATE_FIELD_ID,
1718
Field,
@@ -315,3 +316,26 @@ export function getJobCreatorTitle(jobCreator: JobCreatorType) {
315316
return '';
316317
}
317318
}
319+
320+
// recurse through a datafeed aggregation object,
321+
// adding top level keys from each nested agg to an array
322+
// of fields
323+
export function collectAggs(o: any, aggFields: Field[]) {
324+
for (const i in o) {
325+
if (o[i] !== null && typeof o[i] === 'object') {
326+
if (i === 'aggregations' || i === 'aggs') {
327+
Object.keys(o[i]).forEach(k => {
328+
if (k !== 'aggregations' && i !== 'aggs') {
329+
aggFields.push({
330+
id: k,
331+
name: k,
332+
type: ES_FIELD_TYPES.KEYWORD,
333+
aggregatable: true,
334+
});
335+
}
336+
});
337+
}
338+
collectAggs(o[i], aggFields);
339+
}
340+
}
341+
}

0 commit comments

Comments
 (0)