-
Notifications
You must be signed in to change notification settings - Fork 0
Refactoring #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Refactoring #10
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/main/java/dev/vality/alerting/tg/bot/handler/alert/AlertHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package dev.vality.alerting.tg.bot.handler.alert; | ||
|
|
||
| import dev.vality.alerting.tg.bot.model.Webhook; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface AlertHandler { | ||
| boolean filter(String alertName); | ||
|
|
||
| void handle(Webhook webhook, List<Webhook.Alert> alerts); | ||
| } |
66 changes: 66 additions & 0 deletions
66
src/main/java/dev/vality/alerting/tg/bot/handler/alert/DefaultAlertHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package dev.vality.alerting.tg.bot.handler.alert; | ||
|
|
||
| import dev.vality.alerting.tg.bot.config.properties.AlertBotProperties; | ||
| import dev.vality.alerting.tg.bot.model.Webhook; | ||
| import dev.vality.alerting.tg.bot.service.TelegramApiService; | ||
| import jakarta.annotation.PostConstruct; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static dev.vality.alerting.tg.bot.constant.AlertThreadName.*; | ||
| import static dev.vality.alerting.tg.bot.constant.AlertThreadName.PENDING_PAYMENTS; | ||
| import static dev.vality.alerting.tg.bot.util.WebhookUtil.formatWebhook; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class DefaultAlertHandler implements AlertHandler { | ||
| private final AlertBotProperties properties; | ||
| private final TelegramApiService telegramApiService; | ||
| private Map<String, Integer> alertThreads; | ||
|
|
||
| @PostConstruct | ||
| public void init() { | ||
| alertThreads = buildAlertThreadMap(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean filter(String alertName) { | ||
| return alertName != null && !FAILED_MACHINES.equals(alertName); | ||
echerniak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public void handle(Webhook webhook, List<Webhook.Alert> alerts) { | ||
| if (alerts == null || alerts.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| String alertName = alerts.get(0).getLabels() != null ? alerts.get(0).getLabels().getAlertname() : null; | ||
|
|
||
| Integer threadId = alertThreads.get(alertName); | ||
| if (threadId == null) { | ||
| log.error("Неизвестный тип алерта, alertName=" + alertName); | ||
| threadId = properties.getThreads().getCommands(); | ||
| } | ||
|
|
||
| telegramApiService.sendMessage( | ||
| properties.getChatId(), | ||
| threadId, | ||
| formatWebhook(webhook), | ||
| "MarkdownV2" | ||
| ); | ||
| } | ||
|
|
||
| private Map<String, Integer> buildAlertThreadMap() { | ||
| return Map.of( | ||
| API_ERROR_HTTP_CODE_INCREASE, properties.getThreads().getErrors5xx(), | ||
| ALT_PAY_CONVERSION, properties.getThreads().getAltpayConversion(), | ||
| FAILED_MACHINES, properties.getThreads().getFailedMachines(), | ||
| PENDING_PAYMENTS, properties.getThreads().getPendingPayments() | ||
| ); | ||
| } | ||
| } | ||
50 changes: 50 additions & 0 deletions
50
src/main/java/dev/vality/alerting/tg/bot/handler/alert/FailedMachinesAlertHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package dev.vality.alerting.tg.bot.handler.alert; | ||
|
|
||
| import dev.vality.alerting.tg.bot.config.properties.AlertBotProperties; | ||
| import dev.vality.alerting.tg.bot.model.Webhook; | ||
| import dev.vality.alerting.tg.bot.service.FailedMachinesAlertService; | ||
| import dev.vality.alerting.tg.bot.service.TelegramApiService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static dev.vality.alerting.tg.bot.constant.AlertThreadName.FAILED_MACHINES; | ||
| import static dev.vality.alerting.tg.bot.util.WebhookUtil.formatWebhook; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class FailedMachinesAlertHandler implements AlertHandler { | ||
| private final AlertBotProperties properties; | ||
| private final TelegramApiService telegramApiService; | ||
| private final FailedMachinesAlertService service; | ||
|
|
||
| @Override | ||
| public boolean filter(String alertName) { | ||
| return FAILED_MACHINES.equals(alertName); | ||
| } | ||
|
|
||
| @Override | ||
| public void handle(Webhook webhook, List<Webhook.Alert> alerts) { | ||
| Map<Integer, List<Webhook.Alert>> threadIds = new HashMap<>(); | ||
|
|
||
| for (Webhook.Alert alert : alerts) { | ||
| Integer threadId = service.getOrCreateTopicIdForFailedMachinesAlert(alert); | ||
| threadIds.computeIfAbsent(threadId, key -> new ArrayList<>()).add(alert); | ||
| } | ||
|
|
||
| threadIds.forEach((threadId, threadAlerts) -> { | ||
| telegramApiService.sendMessage( | ||
| properties.getChatId(), | ||
| threadId, | ||
| formatWebhook(webhook), | ||
| "MarkdownV2" | ||
| ); | ||
| }); | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
56
...main/java/dev/vality/alerting/tg/bot/handler/command/CreateAlertThreadCommandHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package dev.vality.alerting.tg.bot.handler.command; | ||
|
|
||
| import dev.vality.alerting.tg.bot.config.properties.AlertBotProperties; | ||
| import dev.vality.alerting.tg.bot.service.CommandStateService; | ||
| import dev.vality.alerting.tg.bot.service.TelegramApiService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.core.annotation.Order; | ||
| import org.springframework.stereotype.Component; | ||
| import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| @Order(20) | ||
| public class CreateAlertThreadCommandHandler implements TelegramCommandHandler { | ||
|
|
||
| private static final String COMMAND = "/create_alert_topic"; | ||
|
|
||
| private final AlertBotProperties properties; | ||
| private final CommandStateService commandStateService; | ||
| private final TelegramApiService telegramApiService; | ||
|
|
||
| @Override | ||
| public boolean filter(Update update) { | ||
| var message = update.getMessage(); | ||
|
|
||
| Integer threadId = message.getMessageThreadId(); | ||
| if (threadId == null || !threadId.equals(properties.getThreads().getCommands())) { | ||
| return false; | ||
| } | ||
|
|
||
| String text = message.getText(); | ||
| if (text == null) { | ||
| return false; | ||
| } | ||
|
|
||
| return text.trim().startsWith(COMMAND); | ||
| } | ||
|
|
||
| @Override | ||
| public void handle(Update update) { | ||
| var message = update.getMessage(); | ||
| Long chatId = message.getChatId(); | ||
|
|
||
| commandStateService.markWaitingForNewThreadName(chatId); | ||
|
|
||
| telegramApiService.sendMessage(chatId, properties.getThreads().getCommands(), | ||
| "Введите название для нового треда:", null); | ||
|
|
||
| log.info("Create alert thread command accepted. chatId={}, user=@{}", | ||
| chatId, | ||
| message.getFrom() != null ? message.getFrom().getUserName() : null | ||
| ); | ||
| } | ||
| } |
83 changes: 83 additions & 0 deletions
83
src/main/java/dev/vality/alerting/tg/bot/handler/command/EnterAlertThreadNameHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package dev.vality.alerting.tg.bot.handler.command; | ||
|
|
||
| import dev.vality.alerting.tg.bot.config.properties.AlertBotProperties; | ||
| import dev.vality.alerting.tg.bot.exception.TelegramThreadCreationException; | ||
| import dev.vality.alerting.tg.bot.service.CommandStateService; | ||
| import dev.vality.alerting.tg.bot.service.TelegramApiService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.core.annotation.Order; | ||
| import org.springframework.stereotype.Component; | ||
| import org.telegram.telegrambots.meta.api.methods.forum.CreateForumTopic; | ||
| import org.telegram.telegrambots.meta.api.objects.Update; | ||
| import org.telegram.telegrambots.meta.exceptions.TelegramApiException; | ||
| import org.telegram.telegrambots.meta.generics.TelegramClient; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| @Order(10) | ||
| public class EnterAlertThreadNameHandler implements TelegramCommandHandler { | ||
| private final AlertBotProperties properties; | ||
| private final CommandStateService commandStateService; | ||
| private final TelegramApiService telegramApiService; | ||
| private final TelegramClient telegramClient; | ||
|
|
||
| @Override | ||
| public boolean filter(Update update) { | ||
| var message = update.getMessage(); | ||
|
|
||
| Integer threadId = message.getMessageThreadId(); | ||
| if (threadId == null || !threadId.equals(properties.getThreads().getCommands())) { | ||
| return false; | ||
| } | ||
|
|
||
| Long chatId = message.getChatId(); | ||
| if (chatId == null) { | ||
| return false; | ||
| } | ||
|
|
||
| return commandStateService.isWaitingForNewThreadName(chatId); | ||
| } | ||
|
|
||
| @Override | ||
| public void handle(Update update) { | ||
| var message = update.getMessage(); | ||
| Long chatId = message.getChatId(); | ||
|
|
||
| commandStateService.clearWaitingForNewThreadName(chatId); | ||
|
|
||
| String threadName = message.getText() != null ? message.getText().trim() : ""; | ||
| if (threadName.isBlank()) { | ||
| telegramApiService.sendMessage(chatId, properties.getThreads().getCommands(), | ||
| "Название треда не может быть пустым. Повторите /create_alert_topic.", null); | ||
| return; | ||
| } | ||
|
|
||
| if (threadName.startsWith("/")) { | ||
| telegramApiService.sendMessage(chatId, properties.getThreads().getCommands(), | ||
| "Ожидалось название треда (обычный текст), а не команда. Повторите /create_alert_topic.", null); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| CreateForumTopic createForumTopic = CreateForumTopic.builder() | ||
| .chatId(chatId.toString()) | ||
| .name(threadName) | ||
| .build(); | ||
|
|
||
| Integer messageThreadId = telegramClient.execute(createForumTopic).getMessageThreadId(); | ||
| if (messageThreadId == null) { | ||
| throw new TelegramThreadCreationException("Telegram вернул null messageThreadId: threadName='" + | ||
| threadName + "'"); | ||
| } | ||
|
|
||
| telegramApiService.sendMessage(chatId, properties.getThreads().getCommands(), | ||
| "✅ Тред '" + threadName + "' создан.", null); | ||
| } catch (TelegramApiException e) { | ||
| log.error("Ошибка при создании треда", e); | ||
| telegramApiService.sendMessage(chatId, properties.getThreads().getCommands(), | ||
| "❌ Ошибка при создании треда.", null); | ||
| } | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/dev/vality/alerting/tg/bot/handler/command/TelegramCommandHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package dev.vality.alerting.tg.bot.handler.command; | ||
|
|
||
| import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
|
||
| public interface TelegramCommandHandler { | ||
|
|
||
| boolean filter(final Update update); | ||
|
|
||
| void handle(Update update); | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/main/java/dev/vality/alerting/tg/bot/handler/command/UnknownCommandHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package dev.vality.alerting.tg.bot.handler.command; | ||
|
|
||
| import dev.vality.alerting.tg.bot.config.properties.AlertBotProperties; | ||
| import dev.vality.alerting.tg.bot.service.TelegramApiService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.core.annotation.Order; | ||
| import org.springframework.stereotype.Component; | ||
| import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| @Order(1000) | ||
| public class UnknownCommandHandler implements TelegramCommandHandler { | ||
| private final AlertBotProperties properties; | ||
| private final TelegramApiService telegramApiService; | ||
|
|
||
|
|
||
| @Override | ||
| public boolean filter(Update update) { | ||
| var message = update.getMessage(); | ||
| Integer threadId = message.getMessageThreadId(); | ||
| if (threadId == null || !threadId.equals(properties.getThreads().getCommands())) { | ||
| return false; | ||
| } | ||
|
|
||
| String text = message.getText(); | ||
| return text != null && text.trim().startsWith("/"); | ||
| } | ||
|
|
||
| @Override | ||
| public void handle(Update update) { | ||
| var message = update.getMessage(); | ||
| Long chatId = message.getChatId(); | ||
|
|
||
| telegramApiService.sendMessage(chatId, properties.getThreads().getCommands(), "Неизвестная команда.", null); | ||
| log.info("Введена неизвестная команда message:" + message); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.