Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Single Metric Viewer: Enable cross-filtering for 'by', 'over' and 'partition' field values #193255

4 changes: 4 additions & 0 deletions x-pack/plugins/ml/common/types/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export type PartitionFieldConfig =
by: 'anomaly_score' | 'name';
order: 'asc' | 'desc';
};
filterBy?: {
field: `${MlEntityFieldType}_value`;
query: string;
};
}
| undefined;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type {
} from '../../../../../common/types/anomaly_detection_jobs';
import { useMlKibana } from '../../../contexts/kibana';
import { APP_STATE_ACTION } from '../../timeseriesexplorer_constants';
import type { ComboBoxOption, EntityControlProps } from '../entity_control/entity_control';
import type { ComboBoxOption, Entity, EntityControlProps } from '../entity_control/entity_control';
peteharverson marked this conversation as resolved.
Show resolved Hide resolved
import { EMPTY_FIELD_VALUE_LABEL } from '../entity_control/entity_control';
import { getControlsForDetector } from '../../get_controls_for_detector';
import {
Expand Down Expand Up @@ -53,19 +53,36 @@ export type UiPartitionFieldsConfig = Exclude<PartitionFieldsConfig, undefined>;

export type UiPartitionFieldConfig = Exclude<PartitionFieldConfig, undefined>;

/**
* Returns a filterBy object based on the partition_field value.
*/
const getFilterBy = (entities: Entity[]): Pick<UiPartitionFieldConfig, 'filterBy'> | null => {
rbrtj marked this conversation as resolved.
Show resolved Hide resolved
const query = entities.find((e) => e.fieldType === 'partition_field')?.fieldValue;

if (!query) return null;

return {
filterBy: {
field: 'partition_field_value',
query,
},
};
};

/**
* Provides default fields configuration.
*/
const getDefaultFieldConfig = (
fieldTypes: MlEntityFieldType[],
entities: Entity[],
isAnomalousOnly: boolean,
applyTimeRange: boolean
): UiPartitionFieldsConfig => {
return fieldTypes.reduce((acc, f) => {
acc[f] = {
return entities.reduce((acc, f) => {
acc[f.fieldType] = {
applyTimeRange,
anomalousOnly: isAnomalousOnly,
sort: { by: 'anomaly_score', order: 'desc' },
...(f.fieldType === 'by_field' ? getFilterBy(entities) : {}),
};
return acc;
}, {} as UiPartitionFieldsConfig);
Expand Down Expand Up @@ -143,7 +160,7 @@ export const SeriesControls: FC<PropsWithChildren<SeriesControlsProps>> = ({
const resultFieldsConfig = useMemo(() => {
return {
...getDefaultFieldConfig(
entityControls.map((v) => v.fieldType),
entityControls,
!storageFieldsConfig
? true
: Object.values(storageFieldsConfig).some((v) => !!v?.anomalousOnly),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ function getFieldAgg(
},
]
: []),
...(fieldConfig?.filterBy
rbrtj marked this conversation as resolved.
Show resolved Hide resolved
? [
{
term: {
[fieldConfig.filterBy.field]: {
value: fieldConfig.filterBy.query,
},
},
},
]
: []),
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ const fieldConfig = schema.maybe(
by: schema.string(),
order: schema.maybe(schema.string()),
}),
filterBy: schema.maybe(
schema.object({
field: schema.string(),
query: schema.string(),
})
),
})
);

Expand Down