Skip to content

Commit

Permalink
separated fields for every index (#801)
Browse files Browse the repository at this point in the history
Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>
  • Loading branch information
amsiglan committed Nov 30, 2023
1 parent 5bf43a6 commit 4479c68
Showing 1 changed file with 35 additions and 10 deletions.
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 @@ -128,6 +130,12 @@ export const CreateCorrelationRule: React.FC<CreateCorrelationRuleProps> = (
setupLogTypeOptions();
}, []);

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

const submit = async (values: any) => {
let error;
if ((error = validateCorrelationRule(values))) {
Expand Down Expand Up @@ -172,24 +180,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 @@ -199,6 +223,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 @@ -259,7 +284,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 @@ -320,7 +345,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

0 comments on commit 4479c68

Please sign in to comment.