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

Add check for mappings view API call during create detector #862

Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -80,31 +80,45 @@ export default class ConfigureFieldMapping extends Component<
}

componentDidMount = async () => {
await this.getAllMappings();
this.setupTabs();
await this.getAllMappings(this.setupTabs);
};

componentDidUpdate(
prevProps: Readonly<ConfigureFieldMappingProps>,
prevState: Readonly<ConfigureFieldMappingState>,
snapshot?: any
prevState: Readonly<ConfigureFieldMappingState>
) {
if (prevProps.detector !== this.props.detector) {
if (this.shouldUpdateMappingsState(prevProps)) {
this.setState(
{
detector: this.props.detector,
},
async () => {
await this.getAllMappings();
this.setupTabs();
await this.getAllMappings(this.setupTabs);
}
);
} else if (prevState.createdMappings !== this.state.createdMappings) {
this.setupTabs();
}
}

setupTabs() {
private shouldUpdateMappingsState(prevProps: ConfigureFieldMappingProps) {
const prevDetector = prevProps.detector;
const newDetector = this.props.detector;

return (
!!newDetector.detector_type &&
newDetector.inputs[0]?.detector_input.indices.length > 0 &&
(prevDetector.detector_type !== newDetector.detector_type ||
prevDetector.inputs[0].detector_input.indices !==
newDetector.inputs[0].detector_input.indices ||
prevDetector.inputs[0].detector_input.pre_packaged_rules !==
newDetector.inputs[0].detector_input.pre_packaged_rules ||
prevDetector.inputs[0].detector_input.custom_rules !==
newDetector.inputs[0].detector_input.custom_rules)
);
}

setupTabs = () => {
const {
loading,
mappingsData,
Expand Down Expand Up @@ -213,7 +227,7 @@ export default class ConfigureFieldMapping extends Component<
selectedTabContent:
tabs[selectedTabId === FieldMappingTabId.AutomaticMappings ? 0 : 1].content,
});
}
};

private getRuleFieldsForEnabledRules(): Set<string> {
const ruleFieldsForEnabledRules = new Set<string>();
Expand All @@ -226,8 +240,11 @@ export default class ConfigureFieldMapping extends Component<
return ruleFieldsForEnabledRules;
}

getAllMappings = async () => {
if (this.state.detector.inputs[0]?.detector_input.indices[0]) {
getAllMappings = async (onMappingsUpdate: () => void) => {
if (
this.state.detector.inputs[0]?.detector_input.indices[0] &&
!!this.state.detector.detector_type
) {
this.setState({ loading: true });
const mappingsView = await this.props.fieldMappingService.getMappingsView(
this.state.detector.inputs[0].detector_input.indices[0],
Expand Down Expand Up @@ -262,17 +279,22 @@ export default class ConfigureFieldMapping extends Component<
}
});

this.setState({
createdMappings: existingMappings,
mappingsData: {
...mappingsView.response,
unmapped_field_aliases: Array.from(unmappedRuleFields),
this.setState(
{
createdMappings: existingMappings,
mappingsData: {
...mappingsView.response,
unmapped_field_aliases: Array.from(unmappedRuleFields),
},
fieldMappingIsOpen: !!unmappedRuleFields.size,
loading: false,
},
fieldMappingIsOpen: !!unmappedRuleFields.size,
});
onMappingsUpdate
);
this.updateMappingSharedState(existingMappings);
} else {
this.setState({ loading: false });
}
this.setState({ loading: false });
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,16 @@ export default class DetectorDataSource extends Component<
}
}

for (const indexName of allIndices) {
if (!this.indicesMappings[indexName]) {
const detectorType = this.props.detector_type.toLowerCase();
const result = await this.props.fieldMappingService?.getMappingsView(
indexName,
detectorType
);
result?.ok && (this.indicesMappings[indexName] = result.response.unmapped_field_aliases);
const detectorType = this.props.detector_type.toLowerCase();
if (detectorType) {
for (const indexName of allIndices) {
if (!this.indicesMappings[indexName]) {
const result = await this.props.fieldMappingService?.getMappingsView(
indexName,
detectorType
);
result?.ok && (this.indicesMappings[indexName] = result.response.unmapped_field_aliases);
}
}
}

Expand Down
Loading