2021-11-22 11:49:09 +05:30
|
|
|
import { Form, notification } from 'antd';
|
|
|
|
|
import createSlackApi from 'api/channels/createSlack';
|
|
|
|
|
import ROUTES from 'constants/routes';
|
|
|
|
|
import FormAlertChannels from 'container/FormAlertChannels';
|
|
|
|
|
import history from 'lib/history';
|
|
|
|
|
import React, { useCallback, useState } from 'react';
|
|
|
|
|
|
|
|
|
|
import { ChannelType, SlackChannel } from './config';
|
|
|
|
|
|
2022-03-22 12:10:31 +05:30
|
|
|
function CreateAlertChannels({
|
2021-11-22 11:49:09 +05:30
|
|
|
preType = 'slack',
|
2022-03-22 12:10:31 +05:30
|
|
|
}: CreateAlertChannelsProps): JSX.Element {
|
2021-11-22 11:49:09 +05:30
|
|
|
const [formInstance] = Form.useForm();
|
|
|
|
|
const [selectedConfig, setSelectedConfig] = useState<Partial<SlackChannel>>({
|
|
|
|
|
text: ` {{ range .Alerts -}}
|
|
|
|
|
*Alert:* {{ .Annotations.title }}{{ if .Labels.severity }} - {{ .Labels.severity }}{{ end }}
|
|
|
|
|
|
|
|
|
|
*Description:* {{ .Annotations.description }}
|
|
|
|
|
|
|
|
|
|
*Details:*
|
|
|
|
|
{{ range .Labels.SortedPairs }} • *{{ .Name }}:* {{ .Value }}
|
|
|
|
|
{{ end }}
|
|
|
|
|
{{ end }}`,
|
|
|
|
|
title: `[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .CommonLabels.alertname }} for {{ .CommonLabels.job }}
|
|
|
|
|
{{- if gt (len .CommonLabels) (len .GroupLabels) -}}
|
|
|
|
|
{{" "}}(
|
|
|
|
|
{{- with .CommonLabels.Remove .GroupLabels.Names }}
|
|
|
|
|
{{- range $index, $label := .SortedPairs -}}
|
|
|
|
|
{{ if $index }}, {{ end }}
|
|
|
|
|
{{- $label.Name }}="{{ $label.Value -}}"
|
|
|
|
|
{{- end }}
|
|
|
|
|
{{- end -}}
|
|
|
|
|
)
|
|
|
|
|
{{- end }}`,
|
|
|
|
|
});
|
|
|
|
|
const [savingState, setSavingState] = useState<boolean>(false);
|
|
|
|
|
const [notifications, NotificationElement] = notification.useNotification();
|
|
|
|
|
|
|
|
|
|
const [type, setType] = useState<ChannelType>(preType);
|
|
|
|
|
const onTypeChangeHandler = useCallback((value: string) => {
|
|
|
|
|
setType(value as ChannelType);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const onTestHandler = useCallback(() => {
|
|
|
|
|
console.log('test');
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const onSlackHandler = useCallback(async () => {
|
|
|
|
|
try {
|
|
|
|
|
setSavingState(true);
|
|
|
|
|
const response = await createSlackApi({
|
|
|
|
|
api_url: selectedConfig?.api_url || '',
|
|
|
|
|
channel: selectedConfig?.channel || '',
|
|
|
|
|
name: selectedConfig?.name || '',
|
|
|
|
|
send_resolved: true,
|
|
|
|
|
text: selectedConfig?.text || '',
|
|
|
|
|
title: selectedConfig?.title || '',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (response.statusCode === 200) {
|
|
|
|
|
notifications.success({
|
|
|
|
|
message: 'Success',
|
|
|
|
|
description: 'Successfully created the channel',
|
|
|
|
|
});
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
history.replace(ROUTES.SETTINGS);
|
|
|
|
|
}, 2000);
|
|
|
|
|
} else {
|
|
|
|
|
notifications.error({
|
|
|
|
|
message: 'Error',
|
|
|
|
|
description: response.error || 'Error while creating the channel',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
setSavingState(false);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
setSavingState(false);
|
|
|
|
|
}
|
|
|
|
|
}, [notifications, selectedConfig]);
|
|
|
|
|
|
|
|
|
|
const onSaveHandler = useCallback(
|
|
|
|
|
async (value: ChannelType) => {
|
2022-03-24 12:06:57 +05:30
|
|
|
if (value === 'slack') {
|
2021-11-22 11:49:09 +05:30
|
|
|
onSlackHandler();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[onSlackHandler],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
2022-03-22 12:10:31 +05:30
|
|
|
<FormAlertChannels
|
|
|
|
|
{...{
|
|
|
|
|
formInstance,
|
|
|
|
|
onTypeChangeHandler,
|
|
|
|
|
setSelectedConfig,
|
|
|
|
|
type,
|
|
|
|
|
onTestHandler,
|
|
|
|
|
onSaveHandler,
|
|
|
|
|
savingState,
|
|
|
|
|
NotificationElement,
|
|
|
|
|
title: 'New Notification Channels',
|
|
|
|
|
initialValue: {
|
2021-11-22 11:49:09 +05:30
|
|
|
type,
|
2022-03-22 12:10:31 +05:30
|
|
|
...selectedConfig,
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2021-11-22 11:49:09 +05:30
|
|
|
);
|
2022-03-22 12:10:31 +05:30
|
|
|
}
|
2021-11-22 11:49:09 +05:30
|
|
|
|
|
|
|
|
interface CreateAlertChannelsProps {
|
|
|
|
|
preType?: ChannelType;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-24 12:06:57 +05:30
|
|
|
CreateAlertChannels.defaultProps = {
|
|
|
|
|
preType: undefined,
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-22 11:49:09 +05:30
|
|
|
export default CreateAlertChannels;
|