Skip to content

Commit

Permalink
function move and rename
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdyelastic committed Jan 20, 2020
1 parent ef56b0e commit 6facb00
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 6facb00

Please sign in to comment.