-
Notifications
You must be signed in to change notification settings - Fork 701
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
feat: manually enrich alert #2559
Open
35C4n0r
wants to merge
12
commits into
keephq:main
Choose a base branch
from
35C4n0r:feat-manual-enrichment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+283
β5
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a0dfdd8
feat: enrich alert modal
35C4n0r 5a8c432
feat: manual enrich field integration
35C4n0r 1a5c47a
Merge branch 'main' into feat-manual-enrichment
35C4n0r ff07523
chore: lint
35C4n0r 2c04cb6
feat: move modal to sidebar
35C4n0r 983f179
Merge branch 'feat-manual-enrichment' of https://github.com/35C4n0r/kβ¦
35C4n0r 40d3c07
chore: remove unused import
35C4n0r d9856e9
chore: lint
35C4n0r 0e3ea59
Merge branch 'refs/heads/main' into feat-manual-enrichment
35C4n0r 60de177
fix: fix imports
35C4n0r 7871030
chore: minor changes
35C4n0r 9a75ee2
Merge branch 'main' into feat-manual-enrichment
talboren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
import { AlertDto } from "./models"; | ||
import { Dialog, Transition } from "@headlessui/react"; | ||
import React, { Fragment, useEffect, useState } from "react"; | ||
import { Button, TextInput } from "@tremor/react"; | ||
import { useSession } from "next-auth/react"; | ||
import { useApiUrl } from "utils/hooks/useConfig"; | ||
import { toast } from "react-toastify"; | ||
|
||
interface EnrichAlertModalProps { | ||
alert: AlertDto | null | undefined; | ||
isOpen: boolean; | ||
handleClose: () => void; | ||
mutate: () => void; | ||
} | ||
|
||
const EnrichAlertSidePanel: React.FC<EnrichAlertModalProps> = ({ | ||
alert, | ||
isOpen, | ||
handleClose, | ||
mutate, | ||
}) => { | ||
const { data: session } = useSession(); | ||
const apiUrl = useApiUrl(); | ||
|
||
const [customFields, setCustomFields] = useState< | ||
{ key: string; value: string }[] | ||
>([]); | ||
|
||
const [preEnrichedFields, setPreEnrichedFields] = useState< | ||
{ key: string; value: string }[] | ||
>([]); | ||
|
||
const [finalData, setFinalData] = useState<Record<string, any>>({}); | ||
const [isDataValid, setIsDataValid] = useState<boolean>(false); | ||
|
||
const addCustomField = () => { | ||
setCustomFields((prev) => [...prev, { key: "", value: "" }]); | ||
}; | ||
|
||
const updateCustomField = ( | ||
index: number, | ||
field: "key" | "value", | ||
value: string, | ||
) => { | ||
setCustomFields((prev) => | ||
prev.map((item, i) => (i === index ? { ...item, [field]: value } : item)), | ||
); | ||
}; | ||
|
||
const removeCustomField = (index: number) => { | ||
setCustomFields((prev) => prev.filter((_, i) => i !== index)); | ||
}; | ||
|
||
useEffect(() => { | ||
const preEnrichedFields = | ||
alert?.enriched_fields?.map((key) => { | ||
return { key, value: alert[key as keyof AlertDto] as any }; | ||
}) || []; | ||
setCustomFields(preEnrichedFields); | ||
setPreEnrichedFields(preEnrichedFields); | ||
}, [alert]); | ||
|
||
useEffect(() => { | ||
const validateData = () => { | ||
const areFieldsIdentical = | ||
customFields.length === preEnrichedFields.length && | ||
customFields.every((field) => { | ||
const matchingField = preEnrichedFields.find( | ||
(preField) => preField.key === field.key, | ||
); | ||
return matchingField && matchingField.value === field.value; | ||
}); | ||
|
||
if (areFieldsIdentical) { | ||
setIsDataValid(false); | ||
return; | ||
} | ||
|
||
const keys = customFields.map((field) => field.key); | ||
const hasEmptyKeys = keys.some((key) => !key); | ||
const hasDuplicateKeys = new Set(keys).size !== keys.length; | ||
|
||
setIsDataValid(!hasEmptyKeys && !hasDuplicateKeys); | ||
}; | ||
|
||
const calculateFinalData = () => { | ||
return customFields.reduce( | ||
(acc, field) => { | ||
if (field.key) { | ||
acc[field.key] = field.value; | ||
} | ||
return acc; | ||
}, | ||
{} as Record<string, string>, | ||
); | ||
}; | ||
setFinalData(calculateFinalData()); | ||
validateData(); | ||
}, [customFields, preEnrichedFields]); | ||
|
||
useEffect(() => { | ||
if (!isOpen) { | ||
setFinalData({}); | ||
setIsDataValid(false); | ||
} | ||
}, [isOpen]); | ||
|
||
const handleSave = async () => { | ||
const requestData = { | ||
enrichments: finalData, | ||
fingerprint: alert?.fingerprint, | ||
}; | ||
|
||
const enrichedFieldKeys = customFields.map((field) => field.key); | ||
const preEnrichedFieldKeys = preEnrichedFields.map((field) => field.key); | ||
|
||
const unEnrichedFields = preEnrichedFieldKeys.filter((key) => { | ||
if (!enrichedFieldKeys.includes(key)) { | ||
return key; | ||
} | ||
}); | ||
|
||
let fieldsUnEnrichedSuccessfully = true; | ||
|
||
if (unEnrichedFields.length != 0) { | ||
const unEnrichmentResponse = await fetch(`${apiUrl}/alerts/unenrich`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${session?.accessToken}`, | ||
}, | ||
body: JSON.stringify({ | ||
fingerprint: alert?.fingerprint, | ||
enrichments: unEnrichedFields, | ||
}), | ||
}); | ||
fieldsUnEnrichedSuccessfully = unEnrichmentResponse.ok; | ||
} | ||
|
||
const response = await fetch(`${apiUrl}/alerts/enrich`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${session?.accessToken}`, | ||
}, | ||
body: JSON.stringify(requestData), | ||
}); | ||
|
||
if (response.ok && fieldsUnEnrichedSuccessfully) { | ||
toast.success("Alert enriched successfully"); | ||
await mutate(); | ||
handleClose(); | ||
} else { | ||
toast.error("Failed to enrich alert"); | ||
} | ||
}; | ||
|
||
const renderCustomFields = () => | ||
customFields.map((field, index) => ( | ||
<div key={index} className="mb-4 flex items-center gap-2"> | ||
<TextInput | ||
placeholder="Field Name" | ||
value={field.key} | ||
onChange={(e) => updateCustomField(index, "key", e.target.value)} | ||
required | ||
className="w-1/3" | ||
/> | ||
<TextInput | ||
placeholder="Field Value" | ||
value={field.value} | ||
onChange={(e) => updateCustomField(index, "value", e.target.value)} | ||
className="w-full" | ||
/> | ||
<Button color="red" onClick={() => removeCustomField(index)}> | ||
β | ||
</Button> | ||
</div> | ||
)); | ||
|
||
return ( | ||
<Transition appear show={isOpen} as={Fragment}> | ||
<Dialog onClose={handleClose}> | ||
<Transition.Child | ||
as={Fragment} | ||
enter="ease-out duration-300" | ||
enterFrom="opacity-0" | ||
enterTo="opacity-100" | ||
leave="ease-in duration-200" | ||
leaveFrom="opacity-100" | ||
leaveTo="opacity-0" | ||
> | ||
<div className="fixed inset-0 bg-black/30 z-20" aria-hidden="true" /> | ||
</Transition.Child> | ||
<Transition.Child | ||
as={Fragment} | ||
enter="transition ease-in-out duration-300 transform" | ||
enterFrom="translate-x-full" | ||
enterTo="translate-x-0" | ||
leave="transition ease-in-out duration-300 transform" | ||
leaveFrom="translate-x-0" | ||
leaveTo="translate-x-full" | ||
> | ||
<Dialog.Panel className="fixed right-0 inset-y-0 w-1/3 bg-white z-30 flex flex-col"> | ||
<div className="flex justify-between items-center min-w-full p-6"> | ||
<h2 className="text-lg font-semibold">Enrich Alert</h2> | ||
</div> | ||
|
||
<div className="flex-1 overflow-auto pb-6 px-6 mt-2"> | ||
{renderCustomFields()} | ||
</div> | ||
|
||
<div className="sticky bottom-0 p-4 border-t border-gray-200 bg-white flex justify-end gap-2"> | ||
<Button | ||
onClick={addCustomField} | ||
className="bg-orange-500" | ||
variant="primary" | ||
> | ||
+ Add Field | ||
</Button> | ||
<Button | ||
onClick={handleSave} | ||
color="orange" | ||
variant="primary" | ||
disabled={!isDataValid} | ||
> | ||
Save | ||
</Button> | ||
<Button onClick={handleClose} color="orange" variant="secondary"> | ||
Close | ||
</Button> | ||
</div> | ||
</Dialog.Panel> | ||
</Transition.Child> | ||
</Dialog> | ||
</Transition> | ||
); | ||
}; | ||
|
||
export default EnrichAlertSidePanel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use the side panel component you created, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'll add that once #2581 is merged.