Skip to content

Commit a13b910

Browse files
committed
Fixed util methods and webhook model
1 parent 181c204 commit a13b910

File tree

4 files changed

+60
-135
lines changed

4 files changed

+60
-135
lines changed

src/main/java/dev/vality/alerting/tg/bot/model/Webhook.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package dev.vality.alerting.tg.bot.model;
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4-
import com.fasterxml.jackson.annotation.JsonProperty;
54
import lombok.Data;
65
import lombok.NoArgsConstructor;
76

87
import java.util.List;
9-
import java.util.Map;
108

119
/**
1210
* Alertmanager webhook body
@@ -31,7 +29,6 @@ public class Webhook {
3129
@NoArgsConstructor
3230
@JsonIgnoreProperties(ignoreUnknown = true)
3331
public static class Alert {
34-
3532
private String status;
3633
private Label labels;
3734
private Annotation annotations;
@@ -45,14 +42,7 @@ public static class Alert {
4542
@NoArgsConstructor
4643
@JsonIgnoreProperties(ignoreUnknown = true)
4744
public static class Label {
48-
4945
private String alertname;
50-
@JsonProperty("api_type")
51-
private String apiType;
52-
private String code;
53-
private String prometheus;
54-
private String team;
55-
private String url;
5646
}
5747

5848
@Data

src/main/java/dev/vality/alerting/tg/bot/service/AlertBot.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
import java.util.*;
2020

21-
import static dev.vality.alerting.tg.bot.util.WebhookFormatter.formatWebhook;
21+
import static dev.vality.alerting.tg.bot.util.WebhookUtil.extractAlertname;
22+
import static dev.vality.alerting.tg.bot.util.WebhookUtil.formatWebhook;
2223

2324
@Slf4j
2425
@Service
@@ -34,12 +35,7 @@ public class AlertBot implements SpringLongPollingBot, LongPollingSingleThreadUp
3435

3536
@PostConstruct
3637
public void init() {
37-
alertTopics = Map.of(
38-
"APIErrorHttpCodeIncrease", properties.getTopics().getErrors5xx(),
39-
"AltPayConversion", properties.getTopics().getAltpayConversion(),
40-
"FailedMachines", properties.getTopics().getFailedMachines(),
41-
"PendingPayments", properties.getTopics().getPendingPayments()
42-
);
38+
alertTopics = buildAlertTopicMap();
4339
}
4440

4541
@Override
@@ -91,16 +87,7 @@ public void sendAlertMessage(Webhook webhook) {
9187
return;
9288
}
9389

94-
String alertName;
95-
if (webhook.getCommonLabels() != null) {
96-
alertName = webhook.getCommonLabels().getAlertname();
97-
} else if (webhook.getGroupLabels() != null) {
98-
alertName = webhook.getGroupLabels().getAlertname();
99-
} else if (webhook.getAlerts().getFirst().getLabels() != null) {
100-
alertName = webhook.getAlerts().getFirst().getLabels().getAlertname();
101-
} else {
102-
return;
103-
}
90+
String alertName = extractAlertname(webhook);
10491

10592
sendResponse(
10693
properties.getChatId(),

src/main/java/dev/vality/alerting/tg/bot/util/WebhookFormatter.java

Lines changed: 0 additions & 108 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package dev.vality.alerting.tg.bot.util;
2+
3+
import dev.vality.alerting.tg.bot.model.Webhook;
4+
5+
public final class WebhookUtil {
6+
public static final String FIRING = "firing";
7+
public static final String RESOLVED = "resolved";
8+
9+
public static String extractAlertname(Webhook webhook) {
10+
if (webhook.getCommonLabels() != null) {
11+
return webhook.getCommonLabels().getAlertname();
12+
} else if (webhook.getGroupLabels() != null) {
13+
return webhook.getGroupLabels().getAlertname();
14+
} else if (webhook.getAlerts().getFirst().getLabels() != null) {
15+
return webhook.getAlerts().getFirst().getLabels().getAlertname();
16+
} else {
17+
return null;
18+
}
19+
}
20+
21+
public static String formatWebhook(Webhook webhook) {
22+
StringBuilder sb = new StringBuilder();
23+
24+
sb.append("```").append("\n");
25+
26+
Webhook.Annotation annotation = webhook.getCommonAnnotations();
27+
28+
if (webhook.getStatus().equals(FIRING)) {
29+
sb.append("АЛЕРТ СРАБОТАЛ ❗").append("\n");
30+
if (annotation != null) {
31+
appendLine(sb, "", annotation.getDescription());
32+
}
33+
} else if (webhook.getStatus().equals(RESOLVED)) {
34+
sb.append("Ситуация пришла в норму ✅").append("\n");
35+
if (annotation != null) {
36+
appendLine(sb, "", annotation.getSummary());
37+
}
38+
}
39+
40+
sb.append("```");
41+
42+
return sb.toString();
43+
}
44+
45+
private static boolean isNotBlank(String s) {
46+
return s != null && !s.isBlank();
47+
}
48+
49+
private static void appendLine(StringBuilder sb, String indent, String value) {
50+
if (isNotBlank(value)) {
51+
sb.append(indent)
52+
.append(value)
53+
.append("\n");
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)