use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.
the class BehaviorGroupRepository method findBehaviorGroupsByEndpointId.
public List<BehaviorGroup> findBehaviorGroupsByEndpointId(String accountId, UUID endpointId) {
Endpoint endpoint = entityManager.find(Endpoint.class, endpointId);
if (endpoint == null) {
throw new NotFoundException("Endpoint not found");
}
String query = "SELECT bg FROM BehaviorGroup bg LEFT JOIN FETCH bg.bundle JOIN bg.actions a WHERE bg.accountId = :accountId AND a.endpoint.id = :endpointId";
List<BehaviorGroup> behaviorGroups = entityManager.createQuery(query, BehaviorGroup.class).setParameter("accountId", accountId).setParameter("endpointId", endpointId).getResultList();
for (BehaviorGroup behaviorGroup : behaviorGroups) {
behaviorGroup.filterOutActions();
}
return behaviorGroups;
}
use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.
the class EndpointRepository method getEndpoint.
public Endpoint getEndpoint(String tenant, UUID id) {
String query = "SELECT e FROM Endpoint e WHERE e.accountId = :accountId AND e.id = :id";
try {
Endpoint endpoint = entityManager.createQuery(query, Endpoint.class).setParameter("id", id).setParameter("accountId", tenant).getSingleResult();
loadProperties(endpoint);
return endpoint;
} catch (NoResultException e) {
return null;
}
}
use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.
the class EndpointRepository method getOrCreateEmailSubscriptionEndpoint.
@Transactional
public Endpoint getOrCreateEmailSubscriptionEndpoint(String accountId, EmailSubscriptionProperties properties) {
List<Endpoint> emailEndpoints = getEndpointsPerCompositeType(accountId, null, Set.of(new CompositeEndpointType(EndpointType.EMAIL_SUBSCRIPTION)), null, null);
loadProperties(emailEndpoints);
Optional<Endpoint> endpointOptional = emailEndpoints.stream().filter(endpoint -> properties.hasSameProperties(endpoint.getProperties(EmailSubscriptionProperties.class))).findFirst();
if (endpointOptional.isPresent()) {
return endpointOptional.get();
}
Endpoint endpoint = new Endpoint();
endpoint.setProperties(properties);
endpoint.setAccountId(accountId);
endpoint.setEnabled(true);
endpoint.setDescription("System email endpoint");
endpoint.setName("Email endpoint");
endpoint.setType(EndpointType.EMAIL_SUBSCRIPTION);
return createEndpoint(endpoint);
}
use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.
the class EndpointProcessor method process.
public void process(Event event) {
processedItems.increment();
List<Endpoint> endpoints = endpointRepository.getTargetEndpoints(event.getAccountId(), event.getEventType());
// Target endpoints are grouped by endpoint type.
endpointTargeted.increment(endpoints.size());
Map<EndpointType, List<Endpoint>> endpointsByType = endpoints.stream().collect(Collectors.groupingBy(Endpoint::getType));
for (Map.Entry<EndpointType, List<Endpoint>> entry : endpointsByType.entrySet()) {
/*
* For each endpoint type, the list of target endpoints is sent alongside with the action to the relevant processor.
* Each processor returns a list of history entries. All of the returned lists are flattened into a single list.
*/
EndpointTypeProcessor processor = endpointTypeToProcessor(entry.getKey());
List<NotificationHistory> historyEntries = processor.process(event, entry.getValue());
// Now each history entry is persisted.
for (NotificationHistory history : historyEntries) {
try {
notificationHistoryRepository.createNotificationHistory(history);
} catch (Exception e) {
LOGGER.errorf("Notification history creation failed for %s", history.getEndpoint());
}
}
}
}
use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.
the class FromCamelHistoryFiller method reinjectIfNeeded.
private void reinjectIfNeeded(Map<String, Object> payloadMap) {
if (!enableReInject || (payloadMap.containsKey("successful") && ((Boolean) payloadMap.get("successful")))) {
return;
}
String historyId = (String) payloadMap.get("historyId");
log.infof("Notification with id %s was not successful, resubmitting for further processing", historyId);
Endpoint ep = notificationHistoryRepository.getEndpointForHistoryId(historyId);
Event event = new Event();
Payload.PayloadBuilder payloadBuilder = new Payload.PayloadBuilder();
payloadMap.forEach(payloadBuilder::withAdditionalProperty);
// TODO augment with details from Endpoint and original event
event.setPayload(payloadBuilder.build());
// Save the original id, as we may need it in the future.
Context.ContextBuilder contextBuilder = new Context.ContextBuilder();
contextBuilder.withAdditionalProperty("original-id", historyId);
if (ep != null) {
// TODO For the current tests. EP should not be null in real life
contextBuilder.withAdditionalProperty("failed-integration", ep.getName());
}
Action action = new Action.ActionBuilder().withId(UUID.randomUUID()).withBundle("console").withApplication("notifications").withEventType("integration-failed").withAccountId(ep != null ? ep.getAccountId() : "").withContext(contextBuilder.build()).withTimestamp(LocalDateTime.now(ZoneOffset.UTC)).withEvents(Collections.singletonList(event)).withRecipients(Collections.singletonList(new Recipient.RecipientBuilder().withOnlyAdmins(true).withIgnoreUserPreferences(true).build())).build();
String ser = Parser.encode(action);
// Add the message id in Kafka header for the de-duplicator
Message<String> message = buildMessageWithId(ser);
emitter.send(message);
}
Aggregations