Skip to content

Commit

Permalink
fix jest test
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Jul 14, 2022
1 parent 524027e commit b7a43ec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
51 changes: 25 additions & 26 deletions src/plugins/data_views/server/fetcher/lib/jobs_compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
* Side Public License, v 1.
*/

import {
RollupGetRollupIndexCapsRollupJobSummary as RollupJobSummary,
RollupGetRollupIndexCapsRollupJobSummaryField as RollupJobSummaryField,
} from '@elastic/elasticsearch/lib/api/types';
import { Dictionary, get, isEqual, set } from 'lodash';
import { RollupGetRollupIndexCapsRollupJobSummary as RollupJobSummary } from '@elastic/elasticsearch/lib/api/types';
import { isEqual } from 'lodash';
import { FieldDescriptor } from '../index_patterns_fetcher';
import { RollupIndexCapability } from './map_capabilities';

/**
* Checks if given job configs are compatible by attempting to merge them
Expand All @@ -37,16 +35,16 @@ export function areJobsCompatible(jobs = []) {
* by aggregation, then by field
*
* @param jobs
* @returns {{ aggs?: Dictionary<FieldDescriptor> }}
* @returns {{ aggs: Dictionary<unknown> }}
*/
export function mergeJobConfigurations(jobs: RollupJobSummary[] = []): {
aggs?: Dictionary<FieldDescriptor>;
} {
export function mergeJobConfigurations(
jobs: RollupJobSummary[] = []
): RollupIndexCapability[string] {
if (!jobs || !Array.isArray(jobs) || !jobs.length) {
throw new Error('No capabilities available');
}

const allAggs: Dictionary<FieldDescriptor> = {};
const allAggs: RollupIndexCapability[string]['aggs'] = {};

// For each job, look through all of its fields
jobs.forEach((job) => {
Expand All @@ -55,41 +53,42 @@ export function mergeJobConfigurations(jobs: RollupJobSummary[] = []): {

// Check each field
fieldNames.forEach((fieldName) => {
const typedFieldName = fieldName as keyof FieldDescriptor;
const fieldAggs = fields[fieldName];

// Look through each field's capabilities (aggregations)
fieldAggs.forEach((agg) => {
const aggName = agg.agg;
const aggregation = allAggs[aggName];
const aggDoesntExist = !aggregation;
const aggregationField = aggregation[typedFieldName];
const fieldDoesntExist = aggregation && !aggregationField;
const fieldDescriptor = allAggs[aggName];
const aggDoesntExist = !fieldDescriptor;
const fieldDoesntExist =
fieldDescriptor && !fieldDescriptor[fieldName as keyof FieldDescriptor];
const isDateHistogramAgg = aggName === 'date_histogram';

// If we currently don't have this aggregation, add it.
// Special case for date histogram, since there can only be one
// date histogram field.
if (aggDoesntExist || (fieldDoesntExist && !isDateHistogramAgg)) {
allAggs[aggName] = aggregation || {};
(aggregation[typedFieldName] as RollupJobSummaryField) = { ...agg };
allAggs[aggName] = fieldDescriptor || {};
allAggs[aggName][fieldName] = { ...agg };
}
// If aggregation already exists, attempt to merge it
else {
const fieldAgg = aggregationField as object | null;
const fieldAgg = fieldDescriptor[fieldName as keyof FieldDescriptor];

switch (aggName) {
// For histograms, calculate the least common multiple between the
// new interval and existing interval
case 'histogram':
if (fieldAgg) {
// FIXME the interface infers only that calendar_interval property is valid
const aggInterval = (agg as any).interval ?? agg.calendar_interval;
// FIXME the interface infers `calendar_interval` is valid, not `interval`
// @ts-expect-error
const aggInterval: number = agg.interval ?? agg.calendar_interval;
// @ts-expect-error
const fieldAggInterval: number = fieldAgg.interval ?? fieldAgg.calendar_interval;

// TODO: Fix this with LCD algorithm
const intervals = [get(fieldAgg, 'interval'), aggInterval].sort((a, b) => a - b);
const isMultiple = intervals[1] % intervals[0] === 0;
set(fieldAgg, 'interval', isMultiple ? intervals[1] : intervals[0] * intervals[1]);
}
// TODO: Fix this with LCD algorithm
const intervals = [fieldAggInterval, aggInterval].sort((a, b) => a - b);
const isMultiple = intervals[1] % intervals[0] === 0;
fieldAgg.interval = isMultiple ? intervals[1] : intervals[0] * intervals[1];
break;

// For date histograms, if it is on the same field, check that the configuration is identical,
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data_views/server/fetcher/lib/map_capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
*/

import { RollupGetRollupIndexCapsResponse } from '@elastic/elasticsearch/lib/api/types';
import { SerializableRecord } from '@kbn/utility-types';
import { Dictionary } from 'lodash';
import { FieldDescriptor } from '../index_patterns_fetcher';
import { mergeJobConfigurations } from './jobs_compatibility';

/**
* A record of capabilities (aggregations) for an index rollup job
*/
export interface RollupIndexCapability {
[key: string]: { aggs?: Dictionary<FieldDescriptor>; error?: string };
[index: string]: { aggs?: Dictionary<Record<string, SerializableRecord>>; error?: string };
}

/**
Expand Down

0 comments on commit b7a43ec

Please sign in to comment.