core/src/nl/tudelft/watchdog/core/logic/event/TrackingEventManager.java
package nl.tudelft.watchdog.core.logic.event;
import nl.tudelft.watchdog.core.logic.event.eventtypes.EventBase;
import nl.tudelft.watchdog.core.logic.storage.PersisterBase;
import nl.tudelft.watchdog.core.util.WatchDogLogger;
import java.util.stream.Stream;
/**
* Base class for managing events generated by the IDE. Contains functionality
* for adding events.
*/
public class TrackingEventManager {
/**
* The session seed, a random number generated on each instantiation of the
* {@link nl.tudelft.watchdog.core.logic.interval.IntervalManagerBase } to be able to tell running Eclipse instances
* apart. So, the session seed used by the TrackingEventManager equals the one from
* the IntervalManager to be able to connect events and intervals of the
* same session.
*/
private String sessionSeed;
private PersisterBase eventsToTransferPersister;
private PersisterBase eventsStatisticsPersister;
/** Constructor. */
public TrackingEventManager(PersisterBase eventsToTransferPersister,
PersisterBase eventsStatisticsPersister) {
this.eventsToTransferPersister = eventsToTransferPersister;
this.eventsStatisticsPersister = eventsStatisticsPersister;
}
/** Sets the session seed used by this TrackingEventManager. */
public void setSessionSeed(String sessionSeed) {
this.sessionSeed = sessionSeed;
}
/**
* Saves the supplied event to persistent storage. New events must use this
* method to be registered properly.
*/
public void addEvent(EventBase event) {
if (event != null) {
event.setSessionSeed(sessionSeed);
eventsToTransferPersister.save(event);
eventsStatisticsPersister.save(event);
WatchDogLogger.getInstance().logInfo("Created event " + event + " " + event.getType());
}
}
/**
* Saves a stream of events to persistent storage. Use this in favor of {@link #addEvent(EventBase)}
* if you have multiple events to store. This method is faster, as it will only serialize to disc
* once rather than for every event.
*
* @param events The collection of events you want to serialize efficiently
*/
public void addEvents(Stream<EventBase> events) {
eventsToTransferPersister.startBatch();
eventsStatisticsPersister.startBatch();
events.forEach(event -> {
if (event != null) {
event.setSessionSeed(sessionSeed);
eventsToTransferPersister.batchedSave(event);
eventsStatisticsPersister.batchedSave(event);
WatchDogLogger.getInstance().logInfo("Created event " + event + " " + event.getType());
}
});
eventsToTransferPersister.commitBatch();
eventsStatisticsPersister.commitBatch();
}
/**
* @return the events statistics persister
*/
public PersisterBase getEventStatisticsPersister() {
return eventsStatisticsPersister;
}
}