-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathuseAlertsData.ts
101 lines (88 loc) · 3.03 KB
/
useAlertsData.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { useQuery } from "@tanstack/react-query";
import {
AlertConversation,
v1GetWorkspaceAlerts,
V1GetWorkspaceAlertsData,
} from "@/api/generated";
import { getMaliciousPackage } from "@/lib/utils";
import { MaliciousPkgType, TriggerType } from "@/types";
import { useAlertSearch } from "./useAlertSearch";
import { v1GetWorkspaceAlertsQueryKey } from "@/api/generated/@tanstack/react-query.gen";
import { useActiveWorkspaceName } from "@/features/workspace/hooks/use-active-workspace-name";
const fetchAlerts = async (
options: V1GetWorkspaceAlertsData,
): Promise<AlertConversation[]> => {
const { data } = await v1GetWorkspaceAlerts(options);
const results = (data ?? [])
.filter((alert): alert is AlertConversation => alert !== null)
.filter((alert) => alert.trigger_category === "critical")
.filter((alert) =>
alert?.conversation.question_answers.every(
(item) => item.answer && item.question,
),
)
.sort(
(a, b) =>
new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime(),
);
return results;
};
export const useAlertsData = ({ ...args } = {}) => {
const { data: activeWorkspaceName } = useActiveWorkspaceName();
const options: V1GetWorkspaceAlertsData = {
path: {
workspace_name: activeWorkspaceName ?? "default",
},
};
return useQuery({
queryKey: v1GetWorkspaceAlertsQueryKey(options),
queryFn: () => fetchAlerts(options),
...args,
});
};
export const useFilteredAlerts = () => {
const { isMaliciousFilterActive, search } = useAlertSearch();
return useAlertsData({
select: (
data: Exclude<ReturnType<typeof useAlertsData>["data"], undefined>,
) => {
return data
.filter((alert) => {
const maliciousPkg = getMaliciousPackage(alert.trigger_string);
const maliciousPkgName =
typeof maliciousPkg === "object"
? maliciousPkg?.type
: maliciousPkg;
const maliciousPkgType =
typeof maliciousPkg === "object"
? maliciousPkg?.name
: maliciousPkg;
return (
maliciousPkgName?.toLowerCase().includes(search) ||
maliciousPkgType?.toLowerCase().includes(search) ||
alert.trigger_type?.toLowerCase().includes(search)
);
})
.filter((alert) => {
if (!isMaliciousFilterActive) {
return true;
}
return (
typeof alert.trigger_string === "object" &&
(alert.trigger_type as TriggerType) === "codegate-context-retriever"
);
})
.sort(
(a, b) =>
new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime(),
);
},
});
};
export function useMaliciousPackagesChartData() {
const { data: alerts = [] } = useAlertsData();
return alerts
.filter((item) => typeof item.trigger_string === "object")
.filter((item) => item.trigger_type === "codegate-context-retriever")
.map((item) => item.trigger_string as MaliciousPkgType);
}