All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graylog.events.notifications.NotificationGracePeriodService Maven / Gradle / Ivy

There is a newer version: 6.0.1
Show newest version
/**
 * This file is part of Graylog.
 *
 * Graylog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Graylog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Graylog.  If not, see .
 */
package org.graylog.events.notifications;

import com.google.auto.value.AutoValue;
import com.google.common.base.Throwables;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.graylog.events.event.Event;
import org.graylog.events.processor.EventDefinition;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.ExecutionException;

/**
 * Checks the grace period of events at an early stage, to prevent creating unnecessary JobTriggers.
 * This is an additional filter to {@link org.graylog.events.notifications.DBNotificationGracePeriodService}
 */
@Singleton
public class NotificationGracePeriodService {
    private static final Logger LOG = LoggerFactory.getLogger(NotificationGracePeriodService.class);

    // This is only used to limit the memory usage
    private static final Duration CACHE_EXPIRY = Duration.ofMinutes(5);

    @AutoValue
    public static abstract class CacheKey {
        public abstract String eventDefinitionId();

        public abstract String notificationId();

        public abstract String eventKey();

        public static CacheKey create(String eventDefinitionId, String notificationId, String eventKey) {
            return new AutoValue_NotificationGracePeriodService_CacheKey(eventDefinitionId, notificationId, eventKey);
        }
    }

    private final LoadingCache> seenEvents;

    @Inject
    public NotificationGracePeriodService() {
        seenEvents = CacheBuilder.newBuilder()
                .expireAfterAccess(CACHE_EXPIRY)
                .build(new CacheLoader>() {
                    @Override
                    public Optional load(CacheKey key) {
                        return Optional.ofNullable(null);
                    }
                });
    }

    public boolean inGracePeriod(EventDefinition definition, String notificationId, Event event) {
        final long gracePeriodMs = definition.notificationSettings().gracePeriodMs();
        if (gracePeriodMs <= 0) {
            return false;
        }
        final Optional lastEventTime = get(definition.id(), notificationId, event.toDto().key());
        final boolean isInGracePeriod = lastEventTime.map(dateTime -> dateTime.isAfter(event.getEventTimestamp().minus(gracePeriodMs))).orElse(false);
        // Only update the timestamp if we are not within the grace period
        if (!isInGracePeriod) {
            put(definition.id(), notificationId, event.toDto().key(), event.getEventTimestamp());
        }
        return isInGracePeriod;
    }

    private Optional get(String eventDefinitionId, String notificationId, String eventKey) {
        try {
            return seenEvents.get(CacheKey.create(eventDefinitionId, notificationId, eventKey));
        } catch (ExecutionException e) {
            final Throwable rootCause = Throwables.getRootCause(e);
            LOG.error("Unable to get seenEvent {}/{}/{} from cache", eventDefinitionId, notificationId, eventKey, rootCause);
            throw new RuntimeException(rootCause);
        }
    }

    private void put(String eventDefinitionId, String notificationId, String eventKey, DateTime time) {
        seenEvents.put(CacheKey.create(eventDefinitionId, notificationId, eventKey), Optional.of(time));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy