2021-11-22 11:49:09 +05:30
|
|
|
import { SaveOutlined } from '@ant-design/icons';
|
|
|
|
|
import { Button, notification } from 'antd';
|
|
|
|
|
import createAlertsApi from 'api/alerts/create';
|
|
|
|
|
import Editor from 'components/Editor';
|
|
|
|
|
import ROUTES from 'constants/routes';
|
|
|
|
|
import { State } from 'hooks/useFetch';
|
|
|
|
|
import history from 'lib/history';
|
2022-04-25 22:41:46 +05:30
|
|
|
import React, { useCallback, useState } from 'react';
|
2021-11-22 11:49:09 +05:30
|
|
|
import { PayloadProps as CreateAlertPayloadProps } from 'types/api/alerts/create';
|
|
|
|
|
|
|
|
|
|
import { ButtonContainer, Title } from './styles';
|
|
|
|
|
|
2022-03-22 12:10:31 +05:30
|
|
|
function CreateAlert(): JSX.Element {
|
2022-04-25 22:41:46 +05:30
|
|
|
const [value, setEditorValue] = useState<string>(
|
2021-12-11 11:26:02 +05:30
|
|
|
`\n alert: High RPS\n expr: sum(rate(signoz_latency_count{span_kind="SPAN_KIND_SERVER"}[2m])) by (service_name) > 100\n for: 0m\n labels:\n severity: warning\n annotations:\n summary: High RPS of Applications\n description: "RPS is > 100\n\t\t\t VALUE = {{ $value }}\n\t\t\t LABELS = {{ $labels }}"\n `,
|
2021-11-22 11:49:09 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const [newAlertState, setNewAlertState] = useState<
|
|
|
|
|
State<CreateAlertPayloadProps>
|
|
|
|
|
>({
|
|
|
|
|
error: false,
|
|
|
|
|
errorMessage: '',
|
|
|
|
|
loading: false,
|
|
|
|
|
payload: undefined,
|
|
|
|
|
success: false,
|
|
|
|
|
});
|
|
|
|
|
const [notifications, Element] = notification.useNotification();
|
|
|
|
|
|
2022-03-14 20:12:42 +05:30
|
|
|
const defaultError =
|
|
|
|
|
'Oops! Some issue occured in saving the alert please try again or contact support@signoz.io';
|
|
|
|
|
|
2021-11-22 11:49:09 +05:30
|
|
|
const onSaveHandler = useCallback(async () => {
|
|
|
|
|
try {
|
|
|
|
|
setNewAlertState((state) => ({
|
|
|
|
|
...state,
|
|
|
|
|
loading: true,
|
|
|
|
|
}));
|
|
|
|
|
|
2022-04-25 22:41:46 +05:30
|
|
|
if (value.length === 0) {
|
2021-11-22 11:49:09 +05:30
|
|
|
setNewAlertState((state) => ({
|
|
|
|
|
...state,
|
|
|
|
|
loading: false,
|
|
|
|
|
}));
|
|
|
|
|
notifications.error({
|
|
|
|
|
description: `Oops! We didn't catch that. Please make sure the alert settings are not empty or try again`,
|
|
|
|
|
message: 'Error',
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const response = await createAlertsApi({
|
2022-04-25 22:41:46 +05:30
|
|
|
query: value,
|
2021-11-22 11:49:09 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (response.statusCode === 200) {
|
|
|
|
|
setNewAlertState((state) => ({
|
|
|
|
|
...state,
|
|
|
|
|
loading: false,
|
|
|
|
|
payload: response.payload,
|
|
|
|
|
}));
|
|
|
|
|
notifications.success({
|
|
|
|
|
message: 'Success',
|
|
|
|
|
description: 'Congrats. The alert was saved correctly.',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
history.push(ROUTES.LIST_ALL_ALERT);
|
|
|
|
|
}, 3000);
|
|
|
|
|
} else {
|
|
|
|
|
notifications.error({
|
2022-03-14 20:12:42 +05:30
|
|
|
description: response.error || defaultError,
|
2021-11-22 11:49:09 +05:30
|
|
|
message: 'Error',
|
|
|
|
|
});
|
|
|
|
|
setNewAlertState((state) => ({
|
|
|
|
|
...state,
|
|
|
|
|
loading: false,
|
|
|
|
|
error: true,
|
2022-03-14 20:12:42 +05:30
|
|
|
errorMessage: response.error || defaultError,
|
2021-11-22 11:49:09 +05:30
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
notifications.error({
|
2022-03-14 20:12:42 +05:30
|
|
|
message: defaultError,
|
2021-11-22 11:49:09 +05:30
|
|
|
});
|
|
|
|
|
}
|
2022-04-25 22:41:46 +05:30
|
|
|
}, [notifications, value]);
|
2021-11-22 11:49:09 +05:30
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{Element}
|
|
|
|
|
|
|
|
|
|
<Title>Create New Alert</Title>
|
2022-04-25 22:41:46 +05:30
|
|
|
<Editor onChange={(value): void => setEditorValue(value)} value={value} />
|
2021-11-22 11:49:09 +05:30
|
|
|
|
|
|
|
|
<ButtonContainer>
|
|
|
|
|
<Button
|
|
|
|
|
loading={newAlertState.loading || false}
|
|
|
|
|
type="primary"
|
|
|
|
|
onClick={onSaveHandler}
|
|
|
|
|
icon={<SaveOutlined />}
|
|
|
|
|
>
|
|
|
|
|
Save
|
|
|
|
|
</Button>
|
|
|
|
|
</ButtonContainer>
|
|
|
|
|
</>
|
|
|
|
|
);
|
2022-03-22 12:10:31 +05:30
|
|
|
}
|
2021-11-22 11:49:09 +05:30
|
|
|
|
|
|
|
|
export default CreateAlert;
|