-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhookUtil.java
More file actions
105 lines (82 loc) · 3.85 KB
/
WebhookUtil.java
File metadata and controls
105 lines (82 loc) · 3.85 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
package dev.vality.alerting.tg.bot.util;
import dev.vality.alerting.tg.bot.model.Webhook;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@Slf4j
public final class WebhookUtil {
public static final String FIRING = "firing";
public static final String RESOLVED = "resolved";
public static String formatWebhook(Webhook webhook) {
Webhook.Annotation annotation = webhook.getCommonAnnotations();
if (annotation == null) {
log.error("Отсутствует описание алерта, webhook.getCommonAnnotations() is null. {}", webhook);
throw new IllegalStateException("Отсутствует описание алерта: webhook.getCommonAnnotations() is null");
}
if (FIRING.equals(webhook.getStatus())) {
return """
```
АЛЕРТ СРАБОТАЛ ❗
%s
```
""".formatted(annotation.getDescription());
} else if (RESOLVED.equals(webhook.getStatus())) {
return """
```
Ситуация пришла в норму ✅
%s
```
""".formatted(annotation.getSummary());
} else {
log.error("Отсутствует статус алерта, webhook.getStatus() is null. {}", webhook);
throw new IllegalStateException("Отсутствует статус алерта: webhook.getStatus() is null");
}
}
public static String formatWebhook(Webhook webhook, List<Webhook.Alert> alerts) {
if (alerts == null || alerts.isEmpty()) {
log.error("Список алертов пуст. {}", webhook);
throw new IllegalStateException("Список алертов пуст");
}
List<Webhook.Alert> firingAlerts = alerts.stream()
.filter(alert -> FIRING.equals(alert.getStatus()))
.toList();
List<Webhook.Alert> resolvedAlerts = alerts.stream()
.filter(alert -> RESOLVED.equals(alert.getStatus()))
.toList();
StringBuilder messageBuilder = new StringBuilder();
messageBuilder.append("```\n");
if (!firingAlerts.isEmpty()) {
messageBuilder.append("АЛЕРТЫ СРАБОТАЛИ ❗\n\n");
String firingText = firingAlerts.stream()
.map(Webhook.Alert::getAnnotations)
.filter(Objects::nonNull)
.map(Webhook.Annotation::getDescription)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.joining("\n\n"));
messageBuilder.append(firingText);
}
if (!firingAlerts.isEmpty() && !resolvedAlerts.isEmpty()) {
messageBuilder.append("\n\n---\n\n");
}
if (!resolvedAlerts.isEmpty()) {
messageBuilder.append("Ситуации пришли в норму ✅\n\n");
String resolvedText = resolvedAlerts.stream()
.map(Webhook.Alert::getAnnotations)
.filter(Objects::nonNull)
.map(Webhook.Annotation::getSummary)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.joining("\n\n"));
messageBuilder.append(resolvedText);
}
messageBuilder.append("\n```");
String result = messageBuilder.toString();
if (result.isBlank()) {
log.error("Не удалось сформировать текст алертов. {}", webhook);
throw new IllegalStateException("Не удалось сформировать текст алертов");
}
return result;
}
}