Skip to content

Commit

Permalink
20282 refactor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
palina-krukovich committed Nov 29, 2024
1 parent ae0f255 commit 3db6f3e
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ public class EmailNotificationService extends NotificationService {
private final LayersApiService layersApiService;
private final GeometryTransformer geometryTransformer;

// ConcurrentHashMap is used because the implementation of Guava and Caffeine cache has bugs,
// which cause race conditions and can lead to incorrect behavior.
// For our simple case, the map with a manual cleaning function should be enough.
private final ConcurrentHashMap<UUID, Long> notificationTimestamps = new ConcurrentHashMap<>();

@Value("${notifications.emailNotificationsFrequencyMs}")
private long emailNotificationsFrequencyMs;


@Value("${notifications.relevantLocationsLayer}")
private String relevantLocationsLayer;

Expand All @@ -62,7 +64,7 @@ public void process(EventApiEventDto event, Map<String, Object> urbanPopulationP
LOG.info("Found new event, sending email notification. Event ID = '{}', name = '{}'", event.getEventId(), event.getName());

try {
if (!canSendNotification(event.getEventId())) {
if (exceedsNotificationFrequencyLimit(event.getEventId())) {
LOG.info("Skipped email notification. Event ID = '{}', name = '{}'", event.getEventId(), event.getName());
return;
}
Expand Down Expand Up @@ -102,13 +104,13 @@ private List<Feature> getRelevantLocations(FeatureCollection fc) {
return layersApiService.getFeatures(geometry, relevantLocationsLayer, UUID.fromString(relevantLocationsLayerAppId));
}

private boolean canSendNotification(UUID eventId) {
private boolean exceedsNotificationFrequencyLimit(UUID eventId) {
long now = System.currentTimeMillis();
Long lastNotificationTime = notificationTimestamps.get(eventId);
if (lastNotificationTime != null && (now - lastNotificationTime) < emailNotificationsFrequencyMs)
return false;
return true;
notificationTimestamps.put(eventId, now);
return true;
return false;
}

/**
Expand Down

0 comments on commit 3db6f3e

Please sign in to comment.