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

Fix various bugs #504

Merged
merged 8 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ import {
ORIGIN_PLUGIN_VIS_LAYER,
OVERLAY_ANOMALIES,
VIS_LAYER_PLUGIN_TYPE,
PLUGIN_AUGMENTATION_ENABLE_SETTING,
PLUGIN_AUGMENTATION_MAX_OBJECTS_SETTING,
} from '../../../../public/expressions/constants';
import { formikToDetectorName, visFeatureListToFormik } from './helpers';
import { AssociateExisting } from './AssociateExisting';
Expand Down Expand Up @@ -160,14 +162,47 @@ function AddAnomalyDetector({
const handleValidationAndSubmit = (formikProps) => {
if (formikProps.values.featureList.length !== 0) {
amitgalitz marked this conversation as resolved.
Show resolved Hide resolved
formikProps.setFieldTouched('featureList', true);
formikProps.validateForm().then((errors) => {
formikProps.validateForm().then(async (errors) => {
if (!isEmpty(errors)) {
focusOnFirstWrongFeature(errors, formikProps.setFieldTouched);
notifications.toasts.addDanger(
'One or more input fields is invalid.'
);
} else {
handleSubmit(formikProps);
const isAugmentationEnabled = uiSettings.get(
PLUGIN_AUGMENTATION_ENABLE_SETTING
);
if (!isAugmentationEnabled) {
notifications.toasts.addDanger(
'Visualization augmentation is disabled, please enable visualization:enablePluginAugmentation.'
);
} else {
const maxAssociatedCount = uiSettings.get(
PLUGIN_AUGMENTATION_MAX_OBJECTS_SETTING
);
await savedObjectLoader.findAll().then(async (resp) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why not use getAugmentVisSavedObjs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the return type of this function is not ideal, wanted to show warning toast message instead of that not well formatted error message

if (resp !== undefined) {
const savedAugmentObjects = get(resp, 'hits', []);
// gets all the saved object for this visualization
const savedObjectsForThisVisualization =
savedAugmentObjects.filter(
(savedObj) =>
get(savedObj, 'visId', '') === embeddable.vis.id
);
if (
maxAssociatedCount <= savedObjectsForThisVisualization.length
) {
notifications.toasts.addDanger(
`Cannot create the detector and associate it to the visualization due to the limit of the max
amount of associated plugin resources (${maxAssociatedCount}) with
${savedObjectsForThisVisualization.length} associated to the visualization`
);
} else {
handleSubmit(formikProps);
}
}
});
}
}
});
} else {
Expand Down Expand Up @@ -217,7 +252,7 @@ function AddAnomalyDetector({
const detectorToCreate = formikToDetector(formikProps.values);
await dispatch(createDetector(detectorToCreate))
.then(async (response) => {
await dispatch(startDetector(response.response.id))
dispatch(startDetector(response.response.id))
.then((startDetectorResponse) => {})
.catch((err: any) => {
notifications.toasts.addDanger(
Expand Down Expand Up @@ -420,7 +455,7 @@ function AddAnomalyDetector({
windowDelay: delayValue,
shingleSize: 8,
filterQuery: { match_all: {} },
description: '',
description: 'Created based on ' + embeddable.vis.title,
resultIndex: undefined,
filters: [],
featureList: visFeatureListToFormik(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this can be removed I think

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed


exports[`AddAnomalyDetector renders 1`] = `ShallowWrapper {}`;
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

import { FEATURE_TYPE } from '../../../../public/models/interfaces';
import { FeaturesFormikValues } from '../../../../public/pages/ConfigureModel/models/interfaces';
import { find, snakeCase } from 'lodash';
import { find, get, snakeCase } from 'lodash';
import { AGGREGATION_TYPES } from '../../../../public/pages/ConfigureModel/utils/constants';
import { getSavedFeatureAnywhereLoader } from '../../../../public/services';

export function visFeatureListToFormik(
featureList,
Expand Down Expand Up @@ -36,6 +37,24 @@ export function formikToDetectorName(title) {
return detectorName;
}

export async function isExceededMaxAssociatedCount(visId, savedObjLoader) {
ohltyler marked this conversation as resolved.
Show resolved Hide resolved
const loader =
savedObjLoader !== undefined
? savedObjLoader
: getSavedFeatureAnywhereLoader();
ohltyler marked this conversation as resolved.
Show resolved Hide resolved

await loader.findAll().then(async (resp) => {
if (resp !== undefined) {
const savedAugmentObjects = get(resp, 'hits', []);
// gets all the saved object for this visualization
const savedObjectsForThisVisualization = savedAugmentObjects.filter(
(savedObj) => get(savedObj, 'visId', '') === visId
);
return savedObjectsForThisVisualization.length;
}
});
}

const getFeatureNameFromVisParams = (id, seriesParams) => {
let name = find(seriesParams, function (param) {
if (param.data.id === id) {
Expand Down
6 changes: 6 additions & 0 deletions public/expressions/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ export const TYPE_OF_EXPR_VIS_LAYERS = 'vis_layers';
export const OVERLAY_ANOMALIES = 'overlay_anomalies';

export const PLUGIN_EVENT_TYPE = 'Anomalies';

export const PLUGIN_AUGMENTATION_ENABLE_SETTING =
'visualization:enablePluginAugmentation';

export const PLUGIN_AUGMENTATION_MAX_OBJECTS_SETTING =
'visualization:enablePluginAugmentation.maxPluginObjects';