-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathAlerts.tsx
More file actions
179 lines (165 loc) · 3.91 KB
/
Alerts.tsx
File metadata and controls
179 lines (165 loc) · 3.91 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { useMemo } from 'react';
import { Control, useController } from 'react-hook-form';
import { Select, SelectProps } from 'react-hook-form-mantine';
import { Label, ReferenceArea, ReferenceLine } from 'recharts';
import {
type AlertChannelType,
WebhookService,
} from '@hyperdx/common-utils/dist/types';
import { Button, ComboboxData, Group, Modal } from '@mantine/core';
import { useDisclosure } from '@mantine/hooks';
import api from '@/api';
import { WebhookForm } from '../components/TeamSettings/WebhookForm';
type Webhook = {
_id: string;
name: string;
};
const WebhookChannelForm = <T extends object>(
props: Partial<SelectProps<T>>,
) => {
const { data: webhooks, refetch: refetchWebhooks } = api.useWebhooks([
WebhookService.Slack,
WebhookService.Generic,
WebhookService.IncidentIO,
]);
const [opened, { open, close }] = useDisclosure(false);
const hasWebhooks = Array.isArray(webhooks?.data) && webhooks.data.length > 0;
const options = useMemo<ComboboxData>(() => {
const webhookOptions =
webhooks?.data.map((sw: Webhook) => ({
value: sw._id,
label: sw.name,
})) || [];
return [
{
value: '',
label: 'Select a Webhook',
disabled: true,
},
...webhookOptions,
];
}, [webhooks]);
const { field } = useController({
control: props.control,
name: props.name!,
});
const handleWebhookCreated = async (webhookId?: string) => {
await refetchWebhooks();
if (webhookId) {
field.onChange(webhookId);
field.onBlur();
}
close();
};
return (
<div>
<Group gap="md" justify="space-between" align="flex-start">
<Select
data-testid="select-webhook"
comboboxProps={{
withinPortal: false,
}}
required
size="xs"
flex={1}
placeholder={
hasWebhooks ? 'Select a Webhook' : 'No Webhooks available'
}
data={options}
name={props.name!}
control={props.control}
{...props}
/>
<Button
data-testid="add-new-webhook-button"
size="xs"
variant="subtle"
color="gray"
onClick={open}
>
Add New Incoming Webhook
</Button>
</Group>
<Modal
data-testid="alert-modal"
opened={opened}
onClose={close}
title="Add New Webhook"
centered
zIndex={9999}
size="lg"
>
<WebhookForm onClose={close} onSuccess={handleWebhookCreated} />
</Modal>
</div>
);
};
export const AlertChannelForm = ({
control,
type,
namePrefix = '',
}: {
control: Control<any>; // TODO: properly type this
type: AlertChannelType;
namePrefix?: string;
}) => {
if (type === 'webhook') {
return (
<WebhookChannelForm
control={control}
name={`${namePrefix}channel.webhookId`}
/>
);
}
return null;
};
export const getAlertReferenceLines = ({
thresholdType,
threshold,
// TODO: zScore
}: {
thresholdType: 'above' | 'below';
threshold: number;
}) => {
if (threshold != null && thresholdType === 'below') {
return (
<ReferenceArea
y1={0}
y2={threshold}
ifOverflow="extendDomain"
fill="red"
strokeWidth={0}
fillOpacity={0.05}
/>
);
}
if (threshold != null && thresholdType === 'above') {
return (
<ReferenceArea
y1={threshold}
ifOverflow="extendDomain"
fill="red"
strokeWidth={0}
fillOpacity={0.05}
/>
);
}
if (threshold != null) {
return (
<ReferenceLine
y={threshold}
label={
<Label
value="Alert Threshold"
fill={'white'}
fontSize={11}
opacity={0.7}
/>
}
stroke="red"
strokeDasharray="3 3"
/>
);
}
return null;
};