Search in sources :

Example 1 with UserEventCache

use of com.serotonin.m2m2.rt.event.UserEventCache in project ma-modules-public by infiniteautomation.

the class EventsRestController method getActiveSummary.

@ApiOperation(value = "Get the active events summary", notes = "List of counts for all active events by type and the most recent active alarm for each.")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/active-summary")
public ResponseEntity<List<EventLevelSummaryModel>> getActiveSummary(HttpServletRequest request) {
    RestProcessResult<List<EventLevelSummaryModel>> result = new RestProcessResult<List<EventLevelSummaryModel>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        List<EventLevelSummaryModel> list = new ArrayList<EventLevelSummaryModel>();
        // This query is slow the first time as it must fill the UserEventCache
        List<EventInstance> events = Common.eventManager.getAllActiveUserEvents(user.getId());
        int lifeSafetyTotal = 0;
        EventInstance lifeSafetyEvent = null;
        int criticalTotal = 0;
        EventInstance criticalEvent = null;
        int urgentTotal = 0;
        EventInstance urgentEvent = null;
        int warningTotal = 0;
        EventInstance warningEvent = null;
        int importantTotal = 0;
        EventInstance importantEvent = null;
        int informationTotal = 0;
        EventInstance informationEvent = null;
        int noneTotal = 0;
        EventInstance noneEvent = null;
        int doNotLogTotal = 0;
        EventInstance doNotLogEvent = null;
        for (EventInstance event : events) {
            switch(event.getAlarmLevel()) {
                case AlarmLevels.LIFE_SAFETY:
                    lifeSafetyTotal++;
                    lifeSafetyEvent = event;
                    break;
                case AlarmLevels.CRITICAL:
                    criticalTotal++;
                    criticalEvent = event;
                    break;
                case AlarmLevels.URGENT:
                    urgentTotal++;
                    urgentEvent = event;
                    break;
                case AlarmLevels.WARNING:
                    warningTotal++;
                    warningEvent = event;
                    break;
                case AlarmLevels.IMPORTANT:
                    importantTotal++;
                    importantEvent = event;
                    break;
                case AlarmLevels.INFORMATION:
                    informationTotal++;
                    informationEvent = event;
                    break;
                case AlarmLevels.NONE:
                    noneTotal++;
                    noneEvent = event;
                    break;
                case AlarmLevels.DO_NOT_LOG:
                    doNotLogTotal++;
                    doNotLogEvent = event;
                    break;
            }
        }
        EventInstanceModel model;
        // Life Safety
        if (lifeSafetyEvent != null)
            model = new EventInstanceModel(lifeSafetyEvent);
        else
            model = null;
        list.add(new EventLevelSummaryModel(AlarmLevels.CODES.getCode(AlarmLevels.LIFE_SAFETY), lifeSafetyTotal, model));
        // Critical Events
        if (criticalEvent != null)
            model = new EventInstanceModel(criticalEvent);
        else
            model = null;
        list.add(new EventLevelSummaryModel(AlarmLevels.CODES.getCode(AlarmLevels.CRITICAL), criticalTotal, model));
        // Urgent Events
        if (urgentEvent != null)
            model = new EventInstanceModel(urgentEvent);
        else
            model = null;
        list.add(new EventLevelSummaryModel(AlarmLevels.CODES.getCode(AlarmLevels.URGENT), urgentTotal, model));
        // Warning Events
        if (warningEvent != null)
            model = new EventInstanceModel(warningEvent);
        else
            model = null;
        list.add(new EventLevelSummaryModel(AlarmLevels.CODES.getCode(AlarmLevels.WARNING), warningTotal, model));
        // Important Events
        if (importantEvent != null)
            model = new EventInstanceModel(importantEvent);
        else
            model = null;
        list.add(new EventLevelSummaryModel(AlarmLevels.CODES.getCode(AlarmLevels.IMPORTANT), importantTotal, model));
        // Information Events
        if (informationEvent != null)
            model = new EventInstanceModel(informationEvent);
        else
            model = null;
        list.add(new EventLevelSummaryModel(AlarmLevels.CODES.getCode(AlarmLevels.INFORMATION), informationTotal, model));
        // None Events
        if (noneEvent != null)
            model = new EventInstanceModel(noneEvent);
        else
            model = null;
        list.add(new EventLevelSummaryModel(AlarmLevels.CODES.getCode(AlarmLevels.NONE), noneTotal, model));
        // Do Not Log Events
        if (doNotLogEvent != null)
            model = new EventInstanceModel(doNotLogEvent);
        else
            model = null;
        list.add(new EventLevelSummaryModel(AlarmLevels.CODES.getCode(AlarmLevels.DO_NOT_LOG), doNotLogTotal, model));
        return result.createResponseEntity(list);
    }
    return result.createResponseEntity();
}
Also used : EventInstance(com.serotonin.m2m2.rt.event.EventInstance) EventInstanceModel(com.serotonin.m2m2.web.mvc.rest.v1.model.events.EventInstanceModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) EventLevelSummaryModel(com.serotonin.m2m2.web.mvc.rest.v1.model.events.EventLevelSummaryModel) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with UserEventCache

use of com.serotonin.m2m2.rt.event.UserEventCache in project ma-core-public by infiniteautomation.

the class EventManagerImpl method raiseEvent.

// 
// 
// Basic event management.
// 
/**
 * Raise Event
 * @param type
 * @param time
 * @param rtnApplicable - does this event return to normal?
 * @param alarmLevel
 * @param message
 * @param context
 */
public void raiseEvent(EventType type, long time, boolean rtnApplicable, int alarmLevel, TranslatableMessage message, Map<String, Object> context) {
    if (state != RUNNING)
        return;
    if (alarmLevel == AlarmLevels.IGNORE)
        return;
    // Check if there is an event for this type already active.
    EventInstance dup = get(type);
    if (dup != null) {
        // Check the duplicate handling.
        boolean discard = canDiscard(type, message);
        if (discard)
            return;
    // Otherwise we just continue...
    } else if (!rtnApplicable) {
        // Check if we've already seen this type recently.
        boolean recent = isRecent(type, message);
        if (recent)
            return;
    }
    // Determine if the event should be suppressed.
    TranslatableMessage autoAckMessage = null;
    for (EventManagerListenerDefinition l : listeners) {
        autoAckMessage = l.autoAckEventWithMessage(type);
        if (autoAckMessage != null)
            break;
    }
    EventInstance evt = new EventInstance(type, time, rtnApplicable, alarmLevel, message, context);
    if (autoAckMessage == null)
        setHandlers(evt);
    // Check to see if we are Not Logging these
    if (alarmLevel != AlarmLevels.DO_NOT_LOG) {
        eventDao.saveEvent(evt);
    }
    // Create user alarm records for all applicable users
    List<Integer> eventUserIds = new ArrayList<Integer>();
    Set<String> emailUsers = new HashSet<String>();
    for (User user : userDao.getActiveUsers()) {
        // user should be skipped.
        if (type.excludeUser(user))
            continue;
        if (Permissions.hasEventTypePermission(user, type)) {
            eventUserIds.add(user.getId());
            if (user.getReceiveAlarmEmails() > AlarmLevels.IGNORE && alarmLevel >= user.getReceiveAlarmEmails() && !StringUtils.isEmpty(user.getEmail()))
                emailUsers.add(user.getEmail());
            // Notify All User Event Listeners of the new event
            if ((alarmLevel != AlarmLevels.DO_NOT_LOG) && (!evt.getEventType().getEventType().equals(EventType.EventTypeNames.AUDIT))) {
                for (UserEventListener l : this.userEventListeners) {
                    if (l.getUserId() == user.getId()) {
                        Common.backgroundProcessing.addWorkItem(new EventNotifyWorkItem(user, l, evt, true, false, false, false));
                    }
                }
                // Add to the UserEventCache if the user has recently accessed their events
                this.userEventCache.addEvent(user.getId(), evt);
            }
        }
    }
    DateTime now = new DateTime(Common.timer.currentTimeMillis());
    for (MailingList ml : MailingListDao.instance.getAlarmMailingLists(alarmLevel)) {
        ml.appendAddresses(emailUsers, now);
    }
    // No Audit or Do Not Log events are User Events
    if ((eventUserIds.size() > 0) && (alarmLevel != AlarmLevels.DO_NOT_LOG) && (!evt.getEventType().getEventType().equals(EventType.EventTypeNames.AUDIT))) {
        eventDao.insertUserEvents(evt.getId(), eventUserIds, true);
        if (autoAckMessage == null)
            lastAlarmTimestamp = Common.timer.currentTimeMillis();
    }
    if (evt.isRtnApplicable()) {
        activeEventsLock.writeLock().lock();
        try {
            activeEvents.add(evt);
        } finally {
            activeEventsLock.writeLock().unlock();
        }
    } else if (evt.getEventType().isRateLimited()) {
        recentEventsLock.writeLock().lock();
        try {
            recentEvents.add(evt);
        } finally {
            recentEventsLock.writeLock().unlock();
        }
    }
    if ((autoAckMessage != null) && (alarmLevel != AlarmLevels.DO_NOT_LOG) && (!evt.getEventType().getEventType().equals(EventType.EventTypeNames.AUDIT)))
        this.acknowledgeEvent(evt, time, null, autoAckMessage);
    else {
        if (evt.isRtnApplicable()) {
            if (alarmLevel > highestActiveAlarmLevel) {
                int oldValue = highestActiveAlarmLevel;
                highestActiveAlarmLevel = alarmLevel;
                SystemEventType.raiseEvent(new SystemEventType(SystemEventType.TYPE_MAX_ALARM_LEVEL_CHANGED), time, false, getAlarmLevelChangeMessage("event.alarmMaxIncreased", oldValue));
            }
        }
        // Call raiseEvent handlers.
        handleRaiseEvent(evt, emailUsers);
        if (log.isDebugEnabled())
            log.trace("Event raised: type=" + type + ", message=" + message.translate(Common.getTranslations()));
    }
}
Also used : EventInstance(com.serotonin.m2m2.rt.event.EventInstance) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) DateTime(org.joda.time.DateTime) UserEventListener(com.serotonin.m2m2.rt.event.UserEventListener) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) EventManagerListenerDefinition(com.serotonin.m2m2.module.EventManagerListenerDefinition) HashSet(java.util.HashSet)

Example 3 with UserEventCache

use of com.serotonin.m2m2.rt.event.UserEventCache in project ma-core-public by infiniteautomation.

the class UserEventCacheTest method benchmark.

/**
 * To simulate Mango we will have 1 thread generating events
 * and occasionally purging them while several other threads read their user's events out.
 */
// @Test(timeout = 30000)
public void benchmark() {
    this.cache = new UserEventCache(15 * 60000, 60000);
    // Setup EventThread
    EventGeneratorThread egt = new EventGeneratorThread(this);
    // Setup User Threads
    List<UserThread> userThreads = new ArrayList<UserThread>();
    for (int i = 0; i < USER_COUNT; i++) {
        userThreads.add(new UserThread(i, this));
    }
    LogStopWatch timer = new LogStopWatch();
    // Start User Threads
    for (UserThread ut : userThreads) ut.start();
    // Start Event Thread
    egt.start();
    try {
        Thread.sleep(50);
    } catch (InterruptedException e) {
        fail(e.getMessage());
    }
    while (runningThreads.intValue() > 0) {
        synchronized (monitor) {
            try {
                monitor.wait();
            } catch (InterruptedException e) {
                fail(e.getMessage());
            }
        }
    }
    timer.stop("");
}
Also used : ArrayList(java.util.ArrayList) UserEventCache(com.serotonin.m2m2.rt.event.UserEventCache) LogStopWatch(com.serotonin.log.LogStopWatch)

Aggregations

ArrayList (java.util.ArrayList)3 EventInstance (com.serotonin.m2m2.rt.event.EventInstance)2 User (com.serotonin.m2m2.vo.User)2 LogStopWatch (com.serotonin.log.LogStopWatch)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 EventManagerListenerDefinition (com.serotonin.m2m2.module.EventManagerListenerDefinition)1 UserEventCache (com.serotonin.m2m2.rt.event.UserEventCache)1 UserEventListener (com.serotonin.m2m2.rt.event.UserEventListener)1 SystemEventType (com.serotonin.m2m2.rt.event.type.SystemEventType)1 MailingList (com.serotonin.m2m2.vo.mailingList.MailingList)1 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)1 EventInstanceModel (com.serotonin.m2m2.web.mvc.rest.v1.model.events.EventInstanceModel)1 EventLevelSummaryModel (com.serotonin.m2m2.web.mvc.rest.v1.model.events.EventLevelSummaryModel)1 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)1 HashSet (java.util.HashSet)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 DateTime (org.joda.time.DateTime)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1