Skip to content

Commit

Permalink
Edited Severity Facet Filter in DrawerInterior.tsx
Browse files Browse the repository at this point in the history
- Added data validation logic to group irregular severity values into appropriate categories.
- Null, empty string, N/A, and undefined values are grouped into the "N/A" category.
- Special characters, numbers, and Other are grouped into the "Other" category.
- All categories are case insensitive.
- Edited vulnerability.severity agg in buildRequest.ts to inclues "null" values.
- Increased aggregation size to 50 to include all severity values.
  • Loading branch information
hawkishpolicy committed Dec 12, 2024
1 parent 938777f commit 48a5907
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
4 changes: 3 additions & 1 deletion backend/src/api/search/buildRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ export function buildRequest(
aggs: {
severity: {
terms: {
field: 'vulnerabilities.severity.keyword'
field: 'vulnerabilities.severity.keyword',
missing: 'null',
size: 50
}
},
cve: {
Expand Down
84 changes: 75 additions & 9 deletions frontend/src/components/DrawerInterior.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ interface Props {
initialFilters: any[];
}

interface SeverityData {
value: string;
count: number;
}

interface GroupedData {
[key: string]: number;
}

const FiltersApplied: React.FC = () => {
return (
<div className={classes.applied}>
Expand Down Expand Up @@ -167,15 +176,72 @@ export const DrawerInterior: React.FC<Props> = (props) => {
)
: [];

const severityLevels = ['N/A', 'Low', 'Medium', 'High', 'Critical', 'Other'];
const severityFacet: any[] = facets['vulnerabilities.severity']
? facets['vulnerabilities.severity'][0].data.sort(
(a: { value: string }, b: { value: string }) =>
severityLevels.indexOf(a.value) - severityLevels.indexOf(b.value)
// const severityFacet: any[] = facets['vulnerabilities.severity']
// ? facets['vulnerabilities.severity'][0].data.sort(
// (a: { value: string }, b: { value: string }) =>
// severityLevels.indexOf(a.value) - severityLevels.indexOf(b.value)
// )
// : [];

console.log('facets', facets['vulnerabilities.severity']);
const titleCaseSeverityFacet = facets['vulnerabilities.severity']
? facets['vulnerabilities.severity'][0].data.map(
(d: { value: string; count: number }) => {
if (
d.value === 'null' ||
d.value === null ||
d.value === '' ||
d.value === 'undefined' ||
d.value === 'N/A'
) {
return { value: 'N/A', count: d.count };
} else {
return {
value:
d.value[0]?.toUpperCase() + d.value.slice(1)?.toLowerCase(),
count: d.count
};
}
}
)
: [];
console.log(facets);
console.log('severityFacet', severityFacet);
console.log('titleCaseSeverityFacet', titleCaseSeverityFacet);

const groupedData: GroupedData = titleCaseSeverityFacet
.map((d: SeverityData) => {
const severityLevels = [
'N/A',
'Low',
'Medium',
'High',
'Critical',
'Other'
];
if (severityLevels.includes(d.value)) {
return d;
} else {
return { value: 'Other', count: d.count };
}
})
.reduce((acc: GroupedData, curr: SeverityData) => {
if (acc[curr.value]) {
acc[curr.value] += curr.count;
} else {
acc[curr.value] = curr.count;
}
return acc;
}, {});

console.log('groupedData', groupedData);

const sortedSeverityFacets = Object.entries(groupedData)
.map(([value, count]) => ({ value, count }))
.sort((a, b) => {
const order = ['N/A', 'Low', 'Medium', 'High', 'Critical', 'Other'];
return order.indexOf(a.value) - order.indexOf(b.value);
});

console.log('sortedSeverityFacets', sortedSeverityFacets);

return (
<StyledWrapper style={{ overflowY: 'auto' }}>
Expand Down Expand Up @@ -365,7 +431,7 @@ export const DrawerInterior: React.FC<Props> = (props) => {
</AccordionDetails>
</Accordion>
)}
{severityFacet.length > 0 && (
{sortedSeverityFacets.length > 0 && (
<Accordion
elevation={0}
square
Expand All @@ -391,7 +457,7 @@ export const DrawerInterior: React.FC<Props> = (props) => {
</AccordionSummary>
<AccordionDetails classes={{ root: classes.details }}>
<FacetFilter
options={severityFacet}
options={sortedSeverityFacets}
selected={filtersByColumn['vulnerabilities.severity'] ?? []}
onSelect={(value) =>
addFilter('vulnerabilities.severity', value, 'any')
Expand Down

0 comments on commit 48a5907

Please sign in to comment.