Skip to content

Commit

Permalink
feat: incident link from alert table
Browse files Browse the repository at this point in the history
  • Loading branch information
talboren committed Mar 6, 2025
1 parent 195b7c4 commit 2134f39
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
22 changes: 21 additions & 1 deletion keep-ui/app/(keep)/alerts/alert-table-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
TableSeverityCell,
UISeverity,
} from "@/shared/ui";
import { DynamicImageProviderIcon } from "@/components/ui";
import { DynamicImageProviderIcon, Link } from "@/components/ui";
import clsx from "clsx";
import {
RowStyle,
Expand All @@ -44,6 +44,7 @@ import {
isDateTimeColumn,
} from "./alert-table-time-format";
import { format } from "path";
import { useIncidents } from "@/utils/hooks/useIncidents";

export const DEFAULT_COLS = [
"severity",
Expand Down Expand Up @@ -197,6 +198,7 @@ export const useAlertTableCols = (
`column-time-formats-${presetName}`,
{}
);
const { data: incidents } = useIncidents();
const [columnListFormats, setColumnListFormats] = useLocalStorage<
Record<string, ListFormatOption>
>(`column-list-formats-${presetName}`, {});
Expand Down Expand Up @@ -281,6 +283,24 @@ export const useAlertTableCols = (
);
}

if (context.column.id === "incident") {
const incidentString = String(value || "");
const incidentSplit = incidentString.split(",");
return incidentSplit.map((incidentId, index) => {
const incidentName = incidents?.items.find(
(incident) => incident.id === incidentId
)?.user_generated_name;
return (
<>
<Link href={`/incidents/${incidentId}`}>
{incidentName || incidentId}
</Link>
{index < incidentSplit.length - 1 && ", "}
</>
);
});
}

let isList = isListColumn(context.column);
let listFormatOption =
columnListFormats[context.column.id] || "badges";
Expand Down
5 changes: 4 additions & 1 deletion keep/api/routes/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ def query_alerts(
status_code=400, detail=f"Error parsing CEL expression: {query.cel}"
) from e

enriched_alerts_dto = convert_db_alerts_to_dto_alerts(db_alerts)
db_alerts = enrich_alerts_with_incidents(tenant_id, db_alerts)
enriched_alerts_dto = convert_db_alerts_to_dto_alerts(
db_alerts, with_incidents=True
)
logger.info(
"Fetched alerts from DB",
extra={
Expand Down
14 changes: 11 additions & 3 deletions keep/api/utils/enrichment_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
from sqlmodel import Session

from keep.api.core.db import existed_or_new_session
from keep.api.models.alert import AlertDto, AlertStatus, AlertWithIncidentLinkMetadataDto
from keep.api.models.alert import (
AlertDto,
AlertStatus,
AlertWithIncidentLinkMetadataDto,
)
from keep.api.models.db.alert import Alert, LastAlertToIncident

tracer = trace.get_tracer(__name__)
Expand Down Expand Up @@ -117,10 +121,14 @@ def convert_db_alerts_to_dto_alerts(

if with_incidents:
if alert._incidents:
alert.event["incident"] = ",".join(str(incident.id) for incident in alert._incidents)
alert.event["incident"] = ",".join(
str(incident.id) for incident in alert._incidents
)
try:
if alert_to_incident is not None:
alert_dto = AlertWithIncidentLinkMetadataDto.from_db_instance(alert, alert_to_incident)
alert_dto = AlertWithIncidentLinkMetadataDto.from_db_instance(
alert, alert_to_incident
)
else:
alert_dto = AlertDto(**alert.event)

Expand Down

0 comments on commit 2134f39

Please sign in to comment.