Search in sources :

Example 6 with AlertReceiver

use of io.hops.hopsworks.persistence.entity.alertmanager.AlertReceiver in project hopsworks by logicalclocks.

the class TestAlertManagerConfigTimer method setUp.

@Before
public void setUp() throws ServiceDiscoveryException, AlertManagerConfigFileNotFoundException, AlertManagerConfigReadException, JsonProcessingException {
    String alertmanagerConfigPath = TestAlertManagerConfigTimer.class.getResource("/alertmanager.yml").getPath();
    alertManagerConfigDBPath = TestAlertManagerConfigTimer.class.getResource("/alertmanagerDB.yml").getPath();
    client = Mockito.mock(AlertManagerClient.class);
    alertManagerConfigController = new AlertManagerConfigController.Builder().withConfigPath(alertmanagerConfigPath).withClient(client).build();
    alertManagerConfigFacade = Mockito.mock(AlertManagerConfigFacade.class);
    Mockito.doAnswer((Answer<Void>) invocation -> {
        Object[] args = invocation.getArguments();
        LOGGER.log(Level.INFO, "Save to database: {0}.", args);
        return null;
    }).when(alertManagerConfigFacade).save(Mockito.any());
    Mockito.doAnswer((Answer<Void>) invocation -> {
        Object[] args = invocation.getArguments();
        LOGGER.log(Level.INFO, "Update database: {0}.", args);
        return null;
    }).when(alertManagerConfigFacade).update(Mockito.any());
    AlertManagerConfig alertManagerConfig = read(new File(alertManagerConfigDBPath));
    AlertManagerConfigEntity alertManagerConfigEntity = new AlertManagerConfigEntity();
    alertManagerConfigEntity.setId(1);
    alertManagerConfigEntity.setCreated(new Date());
    alertManagerConfigEntity.setContent(toJson(alertManagerConfig));
    Mockito.when(alertManagerConfigFacade.getLatest()).thenReturn(Optional.of(alertManagerConfigEntity));
    alertReceiverFacade = Mockito.mock(AlertReceiverFacade.class);
    List<AlertReceiver> receivers = createReceivers();
    Mockito.when(alertReceiverFacade.findAll()).thenReturn(receivers);
    Mockito.doAnswer((Answer<Optional<AlertReceiver>>) invocation -> {
        Object[] args = invocation.getArguments();
        Optional<AlertReceiver> alertReceiver = receivers.stream().filter(receiver -> receiver.getName().equals(args[0])).findFirst();
        return alertReceiver;
    }).when(alertReceiverFacade).findByName(Mockito.any());
    alertManagerConfiguration = new AlertManagerConfiguration(client, alertManagerConfigController, alertManagerConfigFacade, alertReceiverFacade);
    alertManagerConfigBackup = alertManagerConfigController.read();
    alertManagerConfigBackupDB = read(new File(alertManagerConfigDBPath));
}
Also used : YAMLGenerator(com.fasterxml.jackson.dataformat.yaml.YAMLGenerator) AlertType(io.hops.hopsworks.persistence.entity.alertmanager.AlertType) FeatureGroupAlert(io.hops.hopsworks.persistence.entity.featurestore.featuregroup.datavalidation.alert.FeatureGroupAlert) Date(java.util.Date) Random(java.util.Random) AlertSeverity(io.hops.hopsworks.persistence.entity.alertmanager.AlertSeverity) AlertManagerConfigController(io.hops.hopsworks.alerting.config.AlertManagerConfigController) AlertManagerClient(io.hops.hopsworks.alerting.api.AlertManagerClient) JSONObject(org.json.JSONObject) AlertManagerConfiguration(io.hops.hopsworks.alert.AlertManagerConfiguration) After(org.junit.After) JobAlert(io.hops.hopsworks.persistence.entity.jobs.description.JobAlert) AlertManagerConfigFacade(io.hops.hopsworks.alert.dao.AlertManagerConfigFacade) AlertManagerUnreachableException(io.hops.hopsworks.alert.exception.AlertManagerUnreachableException) ValidationRuleAlertStatus(io.hops.hopsworks.persistence.entity.featurestore.featuregroup.datavalidation.alert.ValidationRuleAlertStatus) SlackConfig(io.hops.hopsworks.alerting.config.dto.SlackConfig) AlertManagerConfigReadException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigReadException) Jobs(io.hops.hopsworks.persistence.entity.jobs.description.Jobs) Featuregroup(io.hops.hopsworks.persistence.entity.featurestore.featuregroup.Featuregroup) Logger(java.util.logging.Logger) ServiceDiscoveryException(com.logicalclocks.servicediscoverclient.exceptions.ServiceDiscoveryException) ProjectServiceAlert(io.hops.hopsworks.persistence.entity.project.alert.ProjectServiceAlert) List(java.util.List) AlertManagerConfigCtrlCreateException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigCtrlCreateException) JobAlertStatus(io.hops.hopsworks.persistence.entity.jobs.description.JobAlertStatus) Optional(java.util.Optional) AlertReceiver(io.hops.hopsworks.persistence.entity.alertmanager.AlertReceiver) EmailConfig(io.hops.hopsworks.alerting.config.dto.EmailConfig) ProjectServiceEnum(io.hops.hopsworks.persistence.entity.project.service.ProjectServiceEnum) Receiver(io.hops.hopsworks.alerting.config.dto.Receiver) Project(io.hops.hopsworks.persistence.entity.project.Project) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Answer(org.mockito.stubbing.Answer) AlertManagerConfigFileNotFoundException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigFileNotFoundException) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) AlertManagerConfigUpdateException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException) PagerdutyConfig(io.hops.hopsworks.alerting.config.dto.PagerdutyConfig) Before(org.junit.Before) AlertManagerConfigEntity(io.hops.hopsworks.persistence.entity.alertmanager.AlertManagerConfigEntity) AlertManagerConfig(io.hops.hopsworks.alerting.config.dto.AlertManagerConfig) AlertManagerClientCreateException(io.hops.hopsworks.alerting.exceptions.AlertManagerClientCreateException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) IOException(java.io.IOException) Featurestore(io.hops.hopsworks.persistence.entity.featurestore.Featurestore) ProjectServiceAlertStatus(io.hops.hopsworks.persistence.entity.project.alert.ProjectServiceAlertStatus) File(java.io.File) Mockito(org.mockito.Mockito) AlertReceiverFacade(io.hops.hopsworks.alert.dao.AlertReceiverFacade) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) AlertManagerConfig(io.hops.hopsworks.alerting.config.dto.AlertManagerConfig) Optional(java.util.Optional) AlertReceiver(io.hops.hopsworks.persistence.entity.alertmanager.AlertReceiver) AlertManagerConfigFacade(io.hops.hopsworks.alert.dao.AlertManagerConfigFacade) Date(java.util.Date) AlertManagerClient(io.hops.hopsworks.alerting.api.AlertManagerClient) AlertReceiverFacade(io.hops.hopsworks.alert.dao.AlertReceiverFacade) File(java.io.File) AlertManagerConfigEntity(io.hops.hopsworks.persistence.entity.alertmanager.AlertManagerConfigEntity) AlertManagerConfiguration(io.hops.hopsworks.alert.AlertManagerConfiguration) Before(org.junit.Before)

Example 7 with AlertReceiver

use of io.hops.hopsworks.persistence.entity.alertmanager.AlertReceiver in project hopsworks by logicalclocks.

the class AlertManagerConfiguration method saveReceiverToDatabase.

private void saveReceiverToDatabase(String name, Receiver receiver) throws AlertManagerConfigUpdateException {
    try {
        ObjectMapper objectMapper = new ObjectMapper();
        JSONObject jsonObject = new JSONObject(objectMapper.writeValueAsString(receiver));
        AlertReceiver alertReceiver;
        Optional<AlertReceiver> optionalAlertReceiver = alertReceiverFacade.findByName(name);
        if (optionalAlertReceiver.isPresent()) {
            alertReceiver = optionalAlertReceiver.get();
            alertReceiver.setName(receiver.getName());
            alertReceiver.setConfig(jsonObject);
            alertReceiverFacade.update(alertReceiver);
        } else {
            alertReceiver = new AlertReceiver(receiver.getName(), jsonObject);
            alertReceiverFacade.save(alertReceiver);
        }
    } catch (JsonProcessingException e) {
        throw new AlertManagerConfigUpdateException("Failed to save receiver to database. Failed to parse receiver to json. " + e.getMessage(), e);
    }
}
Also used : JSONObject(org.json.JSONObject) AlertReceiver(io.hops.hopsworks.persistence.entity.alertmanager.AlertReceiver) AlertManagerConfigUpdateException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

AlertReceiver (io.hops.hopsworks.persistence.entity.alertmanager.AlertReceiver)7 ArrayList (java.util.ArrayList)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Receiver (io.hops.hopsworks.alerting.config.dto.Receiver)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 AlertManagerConfig (io.hops.hopsworks.alerting.config.dto.AlertManagerConfig)2 EmailConfig (io.hops.hopsworks.alerting.config.dto.EmailConfig)2 PagerdutyConfig (io.hops.hopsworks.alerting.config.dto.PagerdutyConfig)2 Route (io.hops.hopsworks.alerting.config.dto.Route)2 SlackConfig (io.hops.hopsworks.alerting.config.dto.SlackConfig)2 AlertManagerConfigUpdateException (io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException)2 Featurestore (io.hops.hopsworks.persistence.entity.featurestore.Featurestore)2 Featuregroup (io.hops.hopsworks.persistence.entity.featurestore.featuregroup.Featuregroup)2 FeatureGroupAlert (io.hops.hopsworks.persistence.entity.featurestore.featuregroup.datavalidation.alert.FeatureGroupAlert)2 JobAlert (io.hops.hopsworks.persistence.entity.jobs.description.JobAlert)2 Jobs (io.hops.hopsworks.persistence.entity.jobs.description.Jobs)2 Project (io.hops.hopsworks.persistence.entity.project.Project)2 ProjectServiceAlert (io.hops.hopsworks.persistence.entity.project.alert.ProjectServiceAlert)2 Date (java.util.Date)2 JSONObject (org.json.JSONObject)2