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

fix: facets values "dancing" and Only/All button is not working #2489

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
165 changes: 92 additions & 73 deletions keep-ui/app/alerts/alert-table-alert-facets.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useCallback, useEffect } from "react";
import {
AlertFacetsProps,
FacetValue,
Expand All @@ -12,11 +12,10 @@ import {
import { useLocalStorage } from "utils/hooks/useLocalStorage";
import { AlertDto } from "./models";
import {
DynamicFacet,
DynamicFacetWrapper,
AddFacetModal,
} from "./alert-table-facet-dynamic";
import { PlusIcon, TrashIcon } from "@heroicons/react/24/outline";
import { PlusIcon } from "@heroicons/react/24/outline";

export const AlertFacets: React.FC<AlertFacetsProps> = ({
alerts,
Expand Down Expand Up @@ -76,88 +75,92 @@ export const AlertFacets: React.FC<AlertFacetsProps> = ({
setFacetFilters(newFilters);
};

const getFacetValues = (key: keyof AlertDto | string): FacetValue[] => {
const filteredAlerts = getFilteredAlertsForFacet(
alerts,
facetFilters,
key,
timeRange
);
const valueMap = new Map<string, number>();
let nullCount = 0;
const getFacetValues = useCallback(
(key: keyof AlertDto | string): FacetValue[] => {
const filteredAlerts = getFilteredAlertsForFacet(
alerts,
facetFilters,
key,
timeRange
);
const valueMap = new Map<string, number>();
let nullCount = 0;

filteredAlerts.forEach((alert) => {
let value;
filteredAlerts.forEach((alert) => {
let value;

// Handle nested keys like "labels.host"
if (typeof key === "string" && key.includes(".")) {
const [parentKey, childKey] = key.split(".");
const parentValue = alert[parentKey as keyof AlertDto];
// Handle nested keys like "labels.host"
if (typeof key === "string" && key.includes(".")) {
const [parentKey, childKey] = key.split(".");
const parentValue = alert[parentKey as keyof AlertDto];

if (
typeof parentValue === "object" &&
parentValue !== null &&
!Array.isArray(parentValue) &&
!(parentValue instanceof Date)
) {
value = (parentValue as Record<string, unknown>)[childKey];
if (
typeof parentValue === "object" &&
parentValue !== null &&
!Array.isArray(parentValue) &&
!(parentValue instanceof Date)
) {
value = (parentValue as Record<string, unknown>)[childKey];
} else {
value = undefined;
}
} else {
value = undefined;
value = alert[key as keyof AlertDto];
}
} else {
value = alert[key as keyof AlertDto];
}

if (Array.isArray(value)) {
if (value.length === 0) {
nullCount++;
if (Array.isArray(value)) {
if (value.length === 0) {
nullCount++;
} else {
value.forEach((v) => {
valueMap.set(v, (valueMap.get(v) || 0) + 1);
});
}
} else if (value !== undefined && value !== null) {
const strValue = String(value);
valueMap.set(strValue, (valueMap.get(strValue) || 0) + 1);
} else {
value.forEach((v) => {
valueMap.set(v, (valueMap.get(v) || 0) + 1);
});
nullCount++;
}
} else if (value !== undefined && value !== null) {
const strValue = String(value);
valueMap.set(strValue, (valueMap.get(strValue) || 0) + 1);
} else {
nullCount++;
}
});

let values = Array.from(valueMap.entries()).map(([label, count]) => ({
label,
count,
isSelected:
facetFilters[key]?.includes(label) || !facetFilters[key]?.length,
}));
});

if (["assignee", "incident"].includes(key as string) && nullCount > 0) {
values.push({
label: "n/a",
count: nullCount,
let values = Array.from(valueMap.entries()).map(([label, count]) => ({
label,
count,
isSelected:
facetFilters[key]?.includes("n/a") || !facetFilters[key]?.length,
});
}
facetFilters[key]?.includes(label) || !facetFilters[key]?.length,
}));

if (key === "severity") {
values.sort((a, b) => {
if (a.label === "n/a") return 1;
if (b.label === "n/a") return -1;
const orderDiff = getSeverityOrder(a.label) - getSeverityOrder(b.label);
if (orderDiff !== 0) return orderDiff;
return b.count - a.count;
});
} else {
values.sort((a, b) => {
if (a.label === "n/a") return 1;
if (b.label === "n/a") return -1;
return b.count - a.count;
});
}
if (["assignee", "incident"].includes(key as string) && nullCount > 0) {
values.push({
label: "n/a",
count: nullCount,
isSelected:
facetFilters[key]?.includes("n/a") || !facetFilters[key]?.length,
});
}

return values;
};
if (key === "severity") {
values.sort((a, b) => {
if (a.label === "n/a") return 1;
if (b.label === "n/a") return -1;
const orderDiff =
getSeverityOrder(a.label) - getSeverityOrder(b.label);
if (orderDiff !== 0) return orderDiff;
return b.count - a.count;
});
} else {
values.sort((a, b) => {
if (a.label === "n/a") return 1;
if (b.label === "n/a") return -1;
return b.count - a.count;
});
}

return values;
},
[alerts, facetFilters, timeRange]
);

const staticFacets = [
"severity",
Expand All @@ -168,6 +171,22 @@ export const AlertFacets: React.FC<AlertFacetsProps> = ({
"incident",
];

// TODO: move to the alert-table.tsx
useEffect(
function setInitialFacetFilters() {
setFacetFilters((facetFilters) => {
return Object.keys(facetFilters).reduce((acc, key) => {
acc[key] =
facetFilters[key]?.length === 0
? getFacetValues(key as keyof AlertDto).map((v) => v.label)
: facetFilters[key];
return acc;
}, {} as FacetFilters);
});
},
[getFacetValues, setFacetFilters]
);

const handleAddFacet = (column: string) => {
setDynamicFacets([
...dynamicFacets,
Expand Down
8 changes: 6 additions & 2 deletions keep-ui/app/alerts/alert-table-facet-types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ export interface FacetProps {
export interface AlertFacetsProps {
alerts: AlertDto[];
facetFilters: FacetFilters;
setFacetFilters: (filters: FacetFilters) => void;
setFacetFilters: (
filters: FacetFilters | ((filters: FacetFilters) => FacetFilters)
) => void;
dynamicFacets: DynamicFacet[];
setDynamicFacets: (facets: DynamicFacet[]) => void;
setDynamicFacets: (
facets: DynamicFacet[] | ((facets: DynamicFacet[]) => DynamicFacet[])
) => void;
onDelete: (facetKey: string) => void;
className?: string;
table: Table<AlertDto>;
Expand Down
122 changes: 59 additions & 63 deletions keep-ui/app/alerts/alert-table-facet-value.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React from "react";
import { Icon } from "@tremor/react";
import Image from "next/image";
import { Text } from "@tremor/react";
Expand All @@ -22,8 +22,6 @@ export const FacetValue: React.FC<FacetValueProps> = ({
showIcon = false,
facetFilters,
}) => {
const [isHovered, setIsHovered] = useState(false);

const handleCheckboxClick = (e: React.MouseEvent) => {
e.stopPropagation();
onSelect(label, false, false);
Expand Down Expand Up @@ -51,8 +49,6 @@ export const FacetValue: React.FC<FacetValueProps> = ({
<div
className="flex items-center px-2 py-1 hover:bg-gray-100 rounded-sm cursor-pointer group"
onClick={handleCheckboxClick}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
<div className="flex items-center min-w-[24px]">
<input
Expand All @@ -65,67 +61,67 @@ export const FacetValue: React.FC<FacetValueProps> = ({
/>
</div>

{showIcon && (
<div className="flex items-center min-w-[24px] ml-2">
{facetKey === "source" && (
<Image
className="inline-block"
alt={label}
height={16}
width={16}
title={label}
src={
label.includes("@")
? "/icons/mailgun-icon.png"
: `/icons/${label}-icon.png`
}
/>
)}
{facetKey === "severity" && (
<AlertSeverity severity={label as Severity} />
)}
{facetKey === "assignee" && (
<Icon
icon={UserCircleIcon}
size="sm"
className="text-gray-600 !p-0"
/>
)}
{facetKey === "status" && (
<Icon
icon={getStatusIcon(label)}
size="sm"
color={getStatusColor(label)}
className="!p-0"
/>
)}
{facetKey === "dismissed" && (
<Icon
icon={label === "true" ? BellSlashIcon : BellIcon}
size="sm"
className="text-gray-600 !p-0"
/>
)}
{facetKey === "incident" && (
<Icon icon={FireIcon} size="sm" className="text-gray-600 !p-0" />
)}
</div>
)}

<div className="flex-1 min-w-0 mx-2" title={label}>
<div className="flex-1 flex items-center min-w-0" title={label}>
{showIcon && (
<div className="flex items-center min-w-[24px]">
{facetKey === "source" && (
<Image
className="inline-block"
alt={label}
height={16}
width={16}
title={label}
src={
label.includes("@")
? "/icons/mailgun-icon.png"
: `/icons/${label}-icon.png`
}
/>
)}
{facetKey === "severity" && (
<AlertSeverity severity={label as Severity} />
)}
{facetKey === "assignee" && (
<Icon
icon={UserCircleIcon}
size="sm"
className="text-gray-600 !p-0"
/>
)}
{facetKey === "status" && (
<Icon
icon={getStatusIcon(label)}
size="sm"
color={getStatusColor(label)}
className="!p-0"
/>
)}
{facetKey === "dismissed" && (
<Icon
icon={label === "true" ? BellSlashIcon : BellIcon}
size="sm"
className="text-gray-600 !p-0"
/>
)}
{facetKey === "incident" && (
<Icon icon={FireIcon} size="sm" className="text-gray-600 !p-0" />
)}
</div>
)}
<Text className="truncate">{label}</Text>
</div>

<div className="flex-shrink-0 w-8 text-right">
{isHovered ? (
<button
onClick={handleActionClick}
className="text-xs text-orange-600 hover:text-orange-800 w-full"
>
{isExclusivelySelected() ? "All" : "Only"}
</button>
) : (
count > 0 && <Text className="text-xs text-gray-500">{count}</Text>
<div className="flex-shrink-0 w-8 text-right flex justify-end">
<button
onClick={handleActionClick}
className="text-xs text-orange-600 hover:text-orange-800 hidden group-hover:block"
>
{isExclusivelySelected() ? "All" : "Only"}
</button>
{count > 0 && (
<Text className="text-xs text-gray-500 group-hover:hidden">
{count}
</Text>
)}
</div>
</div>
Expand Down
Loading
Loading