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

[Backport 2.10] Separated fields for every index in correlation rule creation/update #808

Merged
merged 2 commits into from
Dec 6, 2023
Merged
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
45 changes: 35 additions & 10 deletions public/pages/Correlations/containers/CreateCorrelationRule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ export const CreateCorrelationRule: React.FC<CreateCorrelationRuleProps> = (
) => {
const correlationStore = DataStore.correlations;
const [indices, setIndices] = useState<CorrelationOptions[]>([]);
const [logFields, setLogFields] = useState<CorrelationOptions[]>([]);
const [logFieldsByIndex, setLogFieldsByIndex] = useState<{
[index: string]: CorrelationOptions[];
}>({});
const validateCorrelationRule = useCallback((rule: CorrelationRuleModel) => {
if (!rule.name) {
return 'Invalid rule name';
Expand Down Expand Up @@ -121,6 +123,12 @@ export const CreateCorrelationRule: React.FC<CreateCorrelationRuleProps> = (
}
}, []);

useEffect(() => {
initialValues.queries.forEach(({ index }) => {
updateLogFieldsForIndex(index);
});
}, [initialValues]);

const submit = async (values: any) => {
let error;
if ((error = validateCorrelationRule(values))) {
Expand Down Expand Up @@ -165,24 +173,40 @@ export const CreateCorrelationRule: React.FC<CreateCorrelationRuleProps> = (

const getLogFields = useCallback(
async (indexName: string) => {
let fields: {
label: string;
value: string;
}[] = [];

if (indexName) {
const result = await props.indexService.getIndexFields(indexName);
if (result?.ok) {
let fields: {
label: string;
value: string;
}[] = result.response?.map((field) => ({
fields = result.response?.map((field) => ({
label: field,
value: field,
}));

setLogFields(fields);
}

return fields;
}

return fields;
},
[props.fieldMappingService?.getMappingsView]
[props.indexService.getIndexFields]
);

const updateLogFieldsForIndex = (index: string) => {
if (!index) {
return;
}
getLogFields(index).then((fields) => {
setLogFieldsByIndex((prevState) => ({
...prevState,
[index]: fields,
}));
});
};

const createForm = (
correlationQueries: CorrelationRuleQuery[],
touchedInputs: FormikTouched<CorrelationRuleModel>,
Expand All @@ -192,6 +216,7 @@ export const CreateCorrelationRule: React.FC<CreateCorrelationRuleProps> = (
return (
<>
{correlationQueries.map((query, queryIdx) => {
const fieldOptions = logFieldsByIndex[query.index] || [];
const isInvalidInputForQuery = (field: 'logType' | 'index'): boolean => {
return (
!!touchedInputs.queries?.[queryIdx]?.[field] &&
Expand Down Expand Up @@ -252,7 +277,7 @@ export const CreateCorrelationRule: React.FC<CreateCorrelationRuleProps> = (
props.handleChange(`queries[${queryIdx}].index`)(
e[0]?.value ? e[0].value : ''
);
getLogFields(e[0]?.value ? e[0].value : '');
updateLogFieldsForIndex(e[0]?.value || '');
}}
onBlur={props.handleBlur(`queries[${queryIdx}].index`)}
selectedOptions={
Expand Down Expand Up @@ -316,7 +341,7 @@ export const CreateCorrelationRule: React.FC<CreateCorrelationRuleProps> = (
// isInvalid={isInvalidInputForQuery('logType')}
placeholder="Select a field"
data-test-subj={'field_dropdown'}
options={logFields}
options={fieldOptions}
singleSelection={{ asPlainText: true }}
onChange={(e) => {
props.handleChange(
Expand Down
Loading