Search in sources :

Example 1 with NotificationSettings

use of com.epam.pipeline.entity.notification.NotificationSettings in project cloud-pipeline by epam.

the class NotificationManager method notifyIdleRuns.

/**
 * Issues a notification of an idle Pipeline Run for multiple runs.
 *
 * @param pipelineCpuRatePairs a list of pairs of PipelineRun and Double cpu usage rate value
 * @param notificationType a type of notification to be issued. Supported types are IDLE_RUN, IDLE_RUN_PAUSED,
 *                         IDLE_RUN_STOPPED
 * @throws IllegalArgumentException if notificationType is not from IDLE_RUN group
 */
@Transactional(propagation = Propagation.REQUIRED)
public void notifyIdleRuns(List<Pair<PipelineRun, Double>> pipelineCpuRatePairs, NotificationType notificationType) {
    if (CollectionUtils.isEmpty(pipelineCpuRatePairs)) {
        return;
    }
    Assert.isTrue(NotificationSettings.NotificationGroup.IDLE_RUN == notificationType.getGroup(), "Only IDLE_RUN group notification types are allowed");
    NotificationSettings idleRunSettings = notificationSettingsManager.load(notificationType);
    if (idleRunSettings == null || !idleRunSettings.isEnabled()) {
        LOGGER.info("No template configured for idle pipeline run notifications or it was disabled!");
        return;
    }
    List<Long> ccUserIds = getKeepInformedUserIds(idleRunSettings);
    if (idleRunSettings.isKeepInformedAdmins()) {
        ExtendedRole extendedRole = roleManager.loadRoleWithUsers(DefaultRoles.ROLE_ADMIN.getId());
        ccUserIds.addAll(extendedRole.getUsers().stream().map(PipelineUser::getId).collect(Collectors.toList()));
    }
    double idleCpuLevel = preferenceManager.getPreference(SystemPreferences.SYSTEM_IDLE_CPU_THRESHOLD_PERCENT);
    Map<String, PipelineUser> pipelineOwners = userManager.loadUsersByNames(pipelineCpuRatePairs.stream().map(p -> p.getLeft().getOwner()).collect(Collectors.toList())).stream().collect(Collectors.toMap(PipelineUser::getUserName, user -> user));
    List<NotificationMessage> messages = pipelineCpuRatePairs.stream().map(pair -> {
        NotificationMessage message = new NotificationMessage();
        message.setTemplate(new NotificationTemplate(idleRunSettings.getTemplateId()));
        message.setTemplateParameters(PipelineRunMapper.map(pair.getLeft(), null));
        message.getTemplateParameters().put("idleCpuLevel", idleCpuLevel);
        message.getTemplateParameters().put("cpuRate", pair.getRight() * PERCENT);
        message.setToUserId(pipelineOwners.getOrDefault(pair.getLeft().getOwner(), new PipelineUser()).getId());
        message.setCopyUserIds(ccUserIds);
        return message;
    }).collect(Collectors.toList());
    monitoringNotificationDao.createMonitoringNotifications(messages);
}
Also used : PipelineRunMapper(com.epam.pipeline.mapper.PipelineRunMapper) Arrays(java.util.Arrays) MessageConstants(com.epam.pipeline.common.MessageConstants) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) ArrayList(java.util.ArrayList) PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) Matcher(java.util.regex.Matcher) Pair(org.apache.commons.lang3.tuple.Pair) MessageHelper(com.epam.pipeline.common.MessageHelper) Propagation(org.springframework.transaction.annotation.Propagation) ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) Service(org.springframework.stereotype.Service) Map(java.util.Map) NotificationType(com.epam.pipeline.entity.notification.NotificationSettings.NotificationType) IssueComment(com.epam.pipeline.entity.issue.IssueComment) TypeReference(com.fasterxml.jackson.core.type.TypeReference) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) EntityManager(com.epam.pipeline.manager.EntityManager) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) Logger(org.slf4j.Logger) AbstractSecuredEntity(com.epam.pipeline.entity.AbstractSecuredEntity) PreferenceManager(com.epam.pipeline.manager.preference.PreferenceManager) RoleManager(com.epam.pipeline.manager.user.RoleManager) DefaultRoles(com.epam.pipeline.entity.user.DefaultRoles) JsonMapper(com.epam.pipeline.config.JsonMapper) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) NotificationMessageVO(com.epam.pipeline.controller.vo.notification.NotificationMessageVO) Collectors(java.util.stream.Collectors) List(java.util.List) UserManager(com.epam.pipeline.manager.user.UserManager) MonitoringNotificationDao(com.epam.pipeline.dao.notification.MonitoringNotificationDao) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) CollectionUtils(org.springframework.util.CollectionUtils) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Issue(com.epam.pipeline.entity.issue.Issue) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with NotificationSettings

use of com.epam.pipeline.entity.notification.NotificationSettings in project cloud-pipeline by epam.

the class NotificationSettingsManager method loadAll.

public List<NotificationSettings> loadAll() {
    List<NotificationSettings> settings = notificationSettingsDao.loadAllNotificationsSettings();
    Set<NotificationType> existingTemplateTypes = settings.stream().map(NotificationSettings::getType).collect(Collectors.toSet());
    for (NotificationType type : NotificationType.values()) {
        if (!existingTemplateTypes.contains(type)) {
            NotificationSettings defaultSetting = NotificationSettings.getDefault(type);
            settings.add(defaultSetting);
        }
    }
    return settings;
}
Also used : NotificationType(com.epam.pipeline.entity.notification.NotificationSettings.NotificationType) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings)

Example 3 with NotificationSettings

use of com.epam.pipeline.entity.notification.NotificationSettings in project cloud-pipeline by epam.

the class PodMonitor method notifyIfExceedsThreshold.

private void notifyIfExceedsThreshold(PipelineRun run, Pod pod, NotificationType type) {
    NotificationSettings settings = notificationSettingsManager.load(type);
    if (settings == null || !settings.isEnabled()) {
        LOGGER.warn(messageHelper.getMessage(MessageConstants.ERROR_NOTIFICATION_SETTINGS_NOT_FOUND, type));
        return;
    }
    // get the diff measured in seconds
    long threshold = settings.getThreshold();
    long resendDelay = settings.getResendDelay();
    boolean isClusterNode = pod.getMetadata() != null && pod.getMetadata().getLabels() != null && pod.getMetadata().getLabels().containsKey(CLUSTER_ID_LABEL);
    if (threshold > 0 && !isClusterNode) {
        long duration = Duration.between(run.getStartDate().toInstant(), DateUtils.now().toInstant()).abs().getSeconds();
        if (duration >= threshold) {
            Date lastNotificationDate = run.getLastNotificationTime();
            if (checkNeedOfNotificationResend(lastNotificationDate, resendDelay)) {
                notificationManager.notifyLongRunningTask(run, settings);
                run.setLastNotificationTime(DateUtils.now());
                pipelineRunManager.updatePipelineRunLastNotification(run);
            }
        }
    }
}
Also used : NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) Date(java.util.Date)

Example 4 with NotificationSettings

use of com.epam.pipeline.entity.notification.NotificationSettings in project cloud-pipeline by epam.

the class NotificationSettingsManagerTest method testCreateAndUpdate.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
public void testCreateAndUpdate() {
    NotificationTemplate template = createTemplate(1L, "template");
    NotificationSettings settings = createSettings(NotificationType.LONG_RUNNING, template.getId(), 1L, 1L);
    notificationSettingsManager.createOrUpdate(settings);
    NotificationSettings loaded = notificationSettingsManager.load(NotificationType.LONG_RUNNING);
    Assert.assertNotNull(loaded);
    Assert.assertEquals(NotificationType.LONG_RUNNING, loaded.getType());
    Assert.assertEquals(1L, loaded.getThreshold().longValue());
    Assert.assertEquals(settings.isKeepInformedOwner(), loaded.isKeepInformedOwner());
    settings.setThreshold(2L);
    notificationSettingsManager.createOrUpdate(settings);
    loaded = notificationSettingsManager.load(NotificationType.LONG_RUNNING);
    Assert.assertNotNull(loaded);
    Assert.assertEquals(2L, loaded.getThreshold().longValue());
}
Also used : NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Test(org.junit.Test) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with NotificationSettings

use of com.epam.pipeline.entity.notification.NotificationSettings in project cloud-pipeline by epam.

the class NotificationManagerTest method createSettings.

private NotificationSettings createSettings(NotificationType type, long templateId, long threshold, long delay) {
    NotificationSettings settings = new NotificationSettings();
    settings.setType(type);
    settings.setKeepInformedAdmins(true);
    settings.setInformedUserIds(Collections.emptyList());
    settings.setTemplateId(templateId);
    settings.setThreshold(threshold);
    settings.setResendDelay(delay);
    settings.setEnabled(true);
    settings.setKeepInformedOwner(true);
    notificationSettingsDao.createNotificationSettings(settings);
    return settings;
}
Also used : NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings)

Aggregations

NotificationSettings (com.epam.pipeline.entity.notification.NotificationSettings)13 NotificationTemplate (com.epam.pipeline.entity.notification.NotificationTemplate)8 Transactional (org.springframework.transaction.annotation.Transactional)8 NotificationMessage (com.epam.pipeline.entity.notification.NotificationMessage)5 PipelineUser (com.epam.pipeline.entity.user.PipelineUser)5 Test (org.junit.Test)4 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 AbstractSecuredEntity (com.epam.pipeline.entity.AbstractSecuredEntity)2 Issue (com.epam.pipeline.entity.issue.Issue)2 IssueComment (com.epam.pipeline.entity.issue.IssueComment)2 NotificationType (com.epam.pipeline.entity.notification.NotificationSettings.NotificationType)2 ExtendedRole (com.epam.pipeline.entity.user.ExtendedRole)2 Map (java.util.Map)2 MessageConstants (com.epam.pipeline.common.MessageConstants)1 MessageHelper (com.epam.pipeline.common.MessageHelper)1 JsonMapper (com.epam.pipeline.config.JsonMapper)1 NotificationMessageVO (com.epam.pipeline.controller.vo.notification.NotificationMessageVO)1 MonitoringNotificationDao (com.epam.pipeline.dao.notification.MonitoringNotificationDao)1 Pipeline (com.epam.pipeline.entity.pipeline.Pipeline)1