Search in sources :

Example 1 with AggrEventsTransactional

use of org.apereo.portal.jpa.BaseAggrEventsJpaDao.AggrEventsTransactional in project uPortal by Jasig.

the class BaseIntervalAwarePortalEventAggregator method aggregateEvent.

@AggrEventsTransactional
@Override
public final void aggregateEvent(E e, EventSession eventSession, EventAggregationContext eventAggregationContext, Map<AggregationInterval, AggregationIntervalInfo> currentIntervals) {
    final BaseAggregationPrivateDao<T, K> aggregationDao = this.getAggregationDao();
    for (Map.Entry<AggregationInterval, AggregationIntervalInfo> intervalInfoEntry : currentIntervals.entrySet()) {
        final AggregationIntervalInfo intervalInfo = intervalInfoEntry.getValue();
        //Map used to cache aggregations locally after loading
        Map<K, T> aggregationsCache = eventAggregationContext.getAttribute(this.aggregationsCacheKey);
        if (aggregationsCache == null) {
            aggregationsCache = new HashMap<K, T>();
            eventAggregationContext.setAttribute(this.aggregationsCacheKey, aggregationsCache);
        }
        //Groups this event is for
        final Set<AggregatedGroupMapping> groupMappings = eventSession.getGroupMappings();
        //For each group get/create then update the aggregation
        for (final AggregatedGroupMapping groupMapping : groupMappings) {
            final K key = this.createAggregationKey(e, eventAggregationContext, intervalInfo, groupMapping);
            //Load the aggregation, try from the cache first
            T aggregation = aggregationsCache.get(key);
            if (aggregation == null) {
                //Then try loading from the db
                aggregation = aggregationDao.getAggregation(key);
                if (aggregation == null) {
                    //Finally create the aggregation
                    aggregation = aggregationDao.createAggregation(key);
                }
                //Store the loaded/created aggregation in the local cache
                aggregationsCache.put(key, aggregation);
            }
            //Update the aggregation with the event
            updateAggregation(e, eventAggregationContext, intervalInfo, aggregation);
        }
    }
}
Also used : AggregatedGroupMapping(org.apereo.portal.events.aggr.groups.AggregatedGroupMapping) Map(java.util.Map) HashMap(java.util.HashMap) AggrEventsTransactional(org.apereo.portal.jpa.BaseAggrEventsJpaDao.AggrEventsTransactional)

Example 2 with AggrEventsTransactional

use of org.apereo.portal.jpa.BaseAggrEventsJpaDao.AggrEventsTransactional in project uPortal by Jasig.

the class PortalEventPurgerImpl method doPurgeRawEvents.

@AggrEventsTransactional
public EventProcessingResult doPurgeRawEvents() {
    if (!this.clusterLockService.isLockOwner(PURGE_RAW_EVENTS_LOCK_NAME)) {
        throw new IllegalStateException("The cluster lock " + PURGE_RAW_EVENTS_LOCK_NAME + " must be owned by the current thread and server");
    }
    final IEventAggregatorStatus eventPurgerStatus = eventAggregationManagementDao.getEventAggregatorStatus(IEventAggregatorStatus.ProcessingType.PURGING, true);
    //Update status with current server name
    final String serverName = this.portalInfoProvider.getUniqueServerName();
    eventPurgerStatus.setServerName(serverName);
    eventPurgerStatus.setLastStart(new DateTime());
    //Determine date of most recently aggregated data
    final IEventAggregatorStatus eventAggregatorStatus = eventAggregationManagementDao.getEventAggregatorStatus(IEventAggregatorStatus.ProcessingType.AGGREGATION, false);
    if (eventAggregatorStatus == null || eventAggregatorStatus.getLastEventDate() == null) {
        //Nothing has been aggregated, skip purging
        eventPurgerStatus.setLastEnd(new DateTime());
        eventAggregationManagementDao.updateEventAggregatorStatus(eventPurgerStatus);
        return new EventProcessingResult(0, null, null, true);
    }
    boolean complete = true;
    //Calculate purge end date from most recent aggregation minus the purge delay
    final DateTime lastAggregated = eventAggregatorStatus.getLastEventDate();
    DateTime purgeEnd = lastAggregated.minus(this.purgeDelay);
    //Determine the DateTime of the oldest event
    DateTime oldestEventDate = eventPurgerStatus.getLastEventDate();
    if (oldestEventDate == null) {
        oldestEventDate = this.portalEventDao.getOldestPortalEventTimestamp();
    }
    //Make sure purgeEnd is no more than 1 hour after the oldest event date to limit delete scope
    final DateTime purgeEndLimit = oldestEventDate.plusHours(1);
    if (purgeEndLimit.isBefore(purgeEnd)) {
        purgeEnd = purgeEndLimit;
        complete = false;
    }
    final Thread currentThread = Thread.currentThread();
    final String currentName = currentThread.getName();
    final int events;
    try {
        currentThread.setName(currentName + "-" + purgeEnd);
        //Purge events
        logger.debug("Starting purge of events before {}", purgeEnd);
        events = portalEventDao.deletePortalEventsBefore(purgeEnd);
    } finally {
        currentThread.setName(currentName);
    }
    //Update the status object and store it
    purgeEnd = purgeEnd.minusMillis(//decrement by 100ms since deletePortalEventsBefore uses lessThan and not lessThanEqualTo
    100);
    eventPurgerStatus.setLastEventDate(purgeEnd);
    eventPurgerStatus.setLastEnd(new DateTime());
    eventAggregationManagementDao.updateEventAggregatorStatus(eventPurgerStatus);
    return new EventProcessingResult(events, oldestEventDate, purgeEnd, complete);
}
Also used : DateTime(org.joda.time.DateTime) AggrEventsTransactional(org.apereo.portal.jpa.BaseAggrEventsJpaDao.AggrEventsTransactional)

Example 3 with AggrEventsTransactional

use of org.apereo.portal.jpa.BaseAggrEventsJpaDao.AggrEventsTransactional in project uPortal by Jasig.

the class BaseIntervalAwarePortalEventAggregator method cleanUnclosedAggregations.

@AggrEventsTransactional
@Override
public int cleanUnclosedAggregations(DateTime start, DateTime end, AggregationInterval interval) {
    final BaseAggregationPrivateDao<T, K> aggregationDao = this.getAggregationDao();
    final Collection<T> unclosedAggregations = aggregationDao.getUnclosedAggregations(start, end, interval);
    for (final T aggregation : unclosedAggregations) {
        final DateTime eventDate = aggregation.getDateTime();
        final AggregationIntervalInfo unclosedIntervalInfo = this.aggregationIntervalHelper.getIntervalInfo(interval, eventDate);
        aggregation.intervalComplete(unclosedIntervalInfo.getTotalDuration());
    }
    aggregationDao.updateAggregations(unclosedAggregations, true);
    return unclosedAggregations.size();
}
Also used : DateTime(org.joda.time.DateTime) AggrEventsTransactional(org.apereo.portal.jpa.BaseAggrEventsJpaDao.AggrEventsTransactional)

Example 4 with AggrEventsTransactional

use of org.apereo.portal.jpa.BaseAggrEventsJpaDao.AggrEventsTransactional in project uPortal by Jasig.

the class PortalEventSessionPurgerImpl method doPurgeEventSessions.

@Override
@AggrEventsTransactional
public EventProcessingResult doPurgeEventSessions() {
    if (!this.clusterLockService.isLockOwner(PURGE_EVENT_SESSION_LOCK_NAME)) {
        throw new IllegalStateException("The cluster lock " + PURGE_EVENT_SESSION_LOCK_NAME + " must be owned by the current thread and server");
    }
    final IEventAggregatorStatus eventAggregatorStatus = eventAggregationManagementDao.getEventAggregatorStatus(IEventAggregatorStatus.ProcessingType.AGGREGATION, false);
    if (eventAggregatorStatus == null || eventAggregatorStatus.getLastEventDate() == null) {
        return new EventProcessingResult(0, null, null, true);
    }
    final DateTime lastEventDate = eventAggregatorStatus.getLastEventDate();
    final DateTime sessionPurgeDate = lastEventDate.minus(eventSessionDuration);
    final int purgeCount = eventSessionDao.purgeEventSessionsBefore(sessionPurgeDate);
    return new EventProcessingResult(purgeCount, null, sessionPurgeDate, true);
}
Also used : DateTime(org.joda.time.DateTime) AggrEventsTransactional(org.apereo.portal.jpa.BaseAggrEventsJpaDao.AggrEventsTransactional)

Aggregations

AggrEventsTransactional (org.apereo.portal.jpa.BaseAggrEventsJpaDao.AggrEventsTransactional)4 DateTime (org.joda.time.DateTime)3 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AggregatedGroupMapping (org.apereo.portal.events.aggr.groups.AggregatedGroupMapping)1