Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<dependency>
<groupId>dev.vality</groupId>
<artifactId>mayday-proto</artifactId>
<version>1.8-d79c25b</version>
<version>1.3-bd21fa1</version>
</dependency>
<dependency>
<groupId>dev.vality</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,57 +27,43 @@ public class AlertingService implements AlertingServiceSrv.Iface {

private final TemplateHelper templateHelper;

private final Converter<AlertTemplate, Alert> alertTemplateAlertConverter;
private final Converter<List<AlertTemplate.AlertConfigurationParameter>, AlertConfiguration>
alertParamsToAlertConfiguration;

@Override
public void deleteAllAlerts(String userId) {
log.info("Removing all alerts for user '{}'", userId);
alertmanagerService.deleteUserRoutes(userId);
prometheusService.deleteAllUserAlerts(userId);
log.info("Removed all alerts for user '{}'", userId);
public void deleteAlert(DeleteAlertRequest deleteAlertRequest) throws AlertNotFound {
String userAlertId = deleteAlertRequest.getUserAlertId();
String userId = deleteAlertRequest.getUserId();
log.info("Removing alert '{}' for user '{}'", userAlertId, userId);
alertmanagerService.deleteUserRoute(userAlertId);
prometheusService.deleteUserAlert(userId, userAlertId);
log.info("Removed alert '{}' for user '{}'", userAlertId, userId);
}

@Override
public void deleteAlert(String userId, String alertId) {
log.info("Removing alert '{}' for user '{}'", alertId, userId);
alertmanagerService.deleteUserRoute(alertId);
prometheusService.deleteUserAlert(userId, alertId);
log.info("Removed alert '{}' for user '{}'", alertId, userId);
}

@Override
public List<UserAlert> getUserAlerts(String userId) {
public List<UserAlert> getUserAlerts(GetUserAlertsRequest getUserAlertsRequest) throws UserNotFound {
String userId = getUserAlertsRequest.getUserId();
log.info("Retrieving all alerts for user '{}'", userId);
List<UserAlert> userAlerts = prometheusService.getUserAlerts(userId);
log.info("Retrieved {} alerts for user '{}'", userAlerts.size(), userId);
return userAlerts;
}

@Override
public List<Alert> getSupportedAlerts() {
log.info("Retrieving all supported alerts");
List<AlertTemplate> metricTemplates =
templateService.getAlertTemplates();
List<Alert> supportedAlerts =
metricTemplates.stream()
.map(alertTemplateAlertConverter::convert)
.sorted(Comparator.comparing(a -> a.getName()))
.collect(Collectors.toList());
log.info("Retrieved {} supported alerts", supportedAlerts.size());
return supportedAlerts;
}

@Override
public AlertConfiguration getAlertConfiguration(String alertTemplateId) {
log.info("Retrieving configuration for alert '{}'", alertTemplateId);
List<AlertTemplate.AlertConfigurationParameter> metricParams =
templateService.getAlertTemplateParams(alertTemplateId);
AlertConfiguration alertConfiguration = alertParamsToAlertConfiguration.convert(metricParams);
alertConfiguration.setId(alertTemplateId);
log.info("Successfully retrieved configuration for alert '{}': {}", alertTemplateId, alertConfiguration);
return alertConfiguration;
public List<AlertConfiguration> getAlertConfigurationsList() {
log.info("Retrieving all alert configurations");
List<AlertConfiguration> alertConfigurations = templateService.getAlertTemplates().stream()
.sorted(Comparator.comparing(AlertTemplate::getReadableName))
.map(alertTemplate -> {
AlertConfiguration alertConfiguration =
alertParamsToAlertConfiguration.convert(alertTemplate.getParameters());
alertConfiguration.setId(alertTemplate.getId());
alertConfiguration.setName(alertTemplate.getReadableName());
return alertConfiguration;
})
.collect(Collectors.toList());
log.info("Retrieved {} alert configurations", alertConfigurations.size());
return alertConfigurations;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package dev.vality.alerting.mayday.integration;

import dev.vality.alerting.mayday.Alert;
import dev.vality.alerting.mayday.AlertConfiguration;
import dev.vality.alerting.mayday.AlertingServiceSrv;
import dev.vality.alerting.mayday.UserAlert;
import dev.vality.alerting.mayday.*;
import dev.vality.alerting.mayday.alertmanager.client.k8s.AlertmanagerClient;
import dev.vality.alerting.mayday.alertmanager.client.k8s.model.AlertmanagerConfig;
import dev.vality.alerting.mayday.alertmanager.service.AlertmanagerService;
Expand Down Expand Up @@ -75,6 +72,9 @@ void createAlert() throws TException {
when(dawayDao.getPaymentTerminals()).thenReturn(DawayObjectUtil.getTestTerminals());
when(dawayDao.getShops()).thenReturn(DawayObjectUtil.getTestShops());
when(dawayDao.getCurrencies()).thenReturn(DawayObjectUtil.getTestCurrencies());
when(dawayDao.getPayoutProviders()).thenReturn(List.of());
when(dawayDao.getPayoutTerminals()).thenReturn(List.of());
when(dawayDao.getWallets()).thenReturn(List.of());

var createAlertRequest =
ThriftObjectUtil.testCreatePaymentConversionAlertRequest(getPaymentConversionAlertConfiguration());
Expand All @@ -88,10 +88,13 @@ void createAlert() throws TException {
verify(alertmanagerClient, times(1))
.addRouteIfNotExists(eq(alertmanagerService.getAlertmanagerConfigName()), any());

verify(dawayDao, times(2)).getPaymentProviders();
verify(dawayDao, times(2)).getPaymentTerminals();
verify(dawayDao, times(2)).getShops();
verify(dawayDao, times(2)).getCurrencies();
verify(dawayDao, times(6)).getPaymentProviders();
verify(dawayDao, times(6)).getPaymentTerminals();
verify(dawayDao, times(6)).getShops();
verify(dawayDao, times(6)).getCurrencies();
verify(dawayDao, times(4)).getPayoutProviders();
verify(dawayDao, times(4)).getPayoutTerminals();
verify(dawayDao, times(5)).getWallets();
}

@Test
Expand All @@ -101,7 +104,7 @@ void getUserAlertsEmpty() throws TException {
.thenReturn(Optional.of(new PrometheusRule()));
when(prometheusClient.getPrometheusRuleGroupAlerts(prometheusService.getPrometheusRuleName(), userName))
.thenReturn(Set.of());
List<UserAlert> userAlerts = thriftEndpoint.getUserAlerts(userName);
List<UserAlert> userAlerts = thriftEndpoint.getUserAlerts(new GetUserAlertsRequest().setUserId(userName));
assertNotNull(userAlerts);
assertTrue(userAlerts.isEmpty());
verify(prometheusClient, times(1))
Expand All @@ -118,7 +121,7 @@ void getUserAlerts() throws TException {
.thenReturn(Optional.of(new PrometheusRule()));
when(prometheusClient.getPrometheusRuleGroupAlerts(prometheusService.getPrometheusRuleName(), userName))
.thenReturn(Set.of(testRule));
List<UserAlert> userAlerts = thriftEndpoint.getUserAlerts(userName);
List<UserAlert> userAlerts = thriftEndpoint.getUserAlerts(new GetUserAlertsRequest().setUserId(userName));
assertNotNull(userAlerts);
assertEquals(1, userAlerts.size());

Expand All @@ -132,28 +135,17 @@ void getUserAlerts() throws TException {
}

AlertConfiguration getPaymentConversionAlertConfiguration() throws TException {
List<Alert> alertList = getSupportedAlerts();
AlertConfiguration alertConfiguration =
thriftEndpoint.getAlertConfiguration(alertList.stream()
.filter(alert -> alert.getId().equals("payment_conversion"))
.findFirst()
.orElseThrow().getId());
assertNotNull(alertConfiguration);
List<AlertConfiguration> alertConfigurations = thriftEndpoint.getAlertConfigurationsList();
assertNotNull(alertConfigurations);
assertFalse(alertConfigurations.isEmpty());
AlertConfiguration alertConfiguration = alertConfigurations.stream()
.filter(alert -> alert.getId().equals("payment_conversion"))
.findFirst()
.orElseThrow();
assertNotNull(alertConfiguration.getId());
assertNotNull(alertConfiguration.getName());
assertNotNull(alertConfiguration.getParameters());
assertFalse(alertConfiguration.getParameters().isEmpty());
return alertConfiguration;
}

private List<Alert> getSupportedAlerts() throws TException {
List<Alert> alertList = thriftEndpoint.getSupportedAlerts();
assertNotNull(alertList);
assertFalse(alertList.isEmpty());

for (Alert alert : alertList) {
assertNotNull(alert.getId());
assertNotNull(alert.getName());
}
return alertList;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.vality.alerting.mayday.unit;

import dev.vality.alerting.mayday.Alert;
import dev.vality.alerting.mayday.AlertConfiguration;
import dev.vality.alerting.mayday.AlertingServiceSrv;
import dev.vality.alerting.mayday.alertmanager.client.k8s.AlertmanagerClient;
Expand All @@ -17,7 +16,6 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.bean.override.mockito.MockitoBean;

import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -78,11 +76,9 @@ void preparePrometheusRuleDataTest() throws TException {


AlertConfiguration getPaymentConversionAlertConfiguration() throws TException {
List<Alert> alertList = thriftEndpoint.getSupportedAlerts();
return
thriftEndpoint.getAlertConfiguration(alertList.stream()
.filter(alert -> alert.getId().equals("payment_conversion"))
.findFirst()
.orElseThrow().getId());
return thriftEndpoint.getAlertConfigurationsList().stream()
.filter(alert -> alert.getId().equals("payment_conversion"))
.findFirst()
.orElseThrow();
}
}
Loading