Subversion Repositories SmartDukaan

Rev

Rev 5680 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.alert.handler;

import in.shop2020.alert.AlertMapper;
import in.shop2020.alert.AlertServiceException;
import in.shop2020.alert.AlertedEntity;
import in.shop2020.alert.EntityMonitoringStatus;
import in.shop2020.alert.EntityProcessedState;
import in.shop2020.alert.EntityType;
import in.shop2020.alert.MonitoredEntity;
import in.shop2020.alert.SearchFilter;
import in.shop2020.alert.persistence.EntityMapper;
import in.shop2020.alert.util.Converter;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EntityHandler {

        @Autowired
        private EntityMapper entityMapper;
        
        private static final Log log = LogFactory.getLog(EntityHandler.class);
        private static final Logger alertLogger= Logger.getLogger("AppLogging");

        public List<MonitoredEntity> getEntities(SearchFilter searchFilter) {
                List<in.shop2020.alert.domain.MonitoredEntity> entities = 
                        entityMapper.getEntities(Converter.toDomainSearchFilter
                                        (searchFilter));
                if(entities!=null) {
                        List<MonitoredEntity> tEntities = new ArrayList<MonitoredEntity>();
                        for(in.shop2020.alert.domain.MonitoredEntity entity : entities) {
                                tEntities.add(Converter.toThriftMonitoredEntity(entity));
                        }
                }
                return null;
        }
        
        public List<AlertedEntity> getAlertedEntities(SearchFilter searchFilter) {
                List<in.shop2020.alert.domain.AlertedEntity> alertedEntities = 
                        entityMapper.getAlertedEntities(Converter.toDomainSearchFilter
                                        (searchFilter));
                if(alertedEntities!=null) {
                        List<AlertedEntity> tAlertedEntities = new ArrayList<AlertedEntity>();
                        for(in.shop2020.alert.domain.AlertedEntity alertedEntity : alertedEntities) {
                                tAlertedEntities.add(Converter.toThriftAlertedEntity(alertedEntity));
                        }
                }
                return null;
        }

        public void endMonitoring(EntityType entityType, String key) {
                in.shop2020.alert.domain.MonitoredEntity entity = entityMapper.getEntity(entityType, key);
                        if(entity!=null){
                        if(entity.getEntityProcessedState().getValue() > EntityProcessedState.UNEXPIRED.getValue()) {
                                in.shop2020.alert.domain.AlertedEntity alertedEntity = new in.shop2020.alert.domain.AlertedEntity();
                                alertedEntity.setEntityType(entity.getEntityType());
                                alertedEntity.setEventType(entity.getEventType());
                                alertedEntity.setEntityIdentifier(entity.getEntityIdentifier());
                                alertedEntity.setEntityProperties(entity.getEntityProperties());
                                if(entity.getEntityProcessedState().getValue() == EntityProcessedState.WARNING_SENT.getValue()) {
                                        alertedEntity.setLastAlertedTime(entity.getWarnExpiryTime());
                                        alertedEntity.setEntityProcessedState(EntityProcessedState.WARNING_SENT);
                                        entityMapper.insertAlertedEntity(alertedEntity);
        
                                        AlertMapper alertMap;
                                String  delayedEventDescription = "UNKNOWN";
                                alertMap = getAlertMapper(entity.getEntityType(), entity.getEventType());
                                if(alertMap!=null) {
                                        delayedEventDescription = alertMap.getEventdesciption();
                                }
                                alertLogger.warn("\tEntityType=" + 
                                                        entity.getEntityType() + " ; " + "\t" + entity.getEntityIdentifier() +
                                                        "\tEventType=" + delayedEventDescription + " ; " + "AlertSeverity=WARN ; " + 
                                                        "ExpiredTime=" + entity.getWarnExpiryTime());
                                }
                                else {
                                        alertedEntity.setLastAlertedTime(entity.getCriticalExpiryTime());
                                        alertedEntity.setEntityProcessedState(EntityProcessedState.CRITICAL_SENT);
                                        entityMapper.insertAlertedEntity(alertedEntity);
        
                                        AlertMapper alertMap;
                                String  delayedEventDescription = "UNKNOWN";
                                alertMap = getAlertMapper(entity.getEntityType(), entity.getEventType());
                                if(alertMap!=null) {
                                        delayedEventDescription = alertMap.getEventdesciption();
                                }
                                alertLogger.error("\tEntityType=" + 
                                                        entity.getEntityType() + " ; " + "\t" + entity.getEntityIdentifier() +
                                                        "\tEventType=" + delayedEventDescription + " ; " + "AlertSeverity=CRITICAL ; " +
                                                        "ExpiredTime=" + entity.getCriticalExpiryTime());
                                }
                        }
                        entityMapper.removeMonitoredEntity(entityType, key);
                }
        }

        public void updateMonitoredObject(MonitoredEntity monitoredEntity) 
                throws AlertServiceException{
                in.shop2020.alert.domain.MonitoredEntity entity = entityMapper.getEntity(monitoredEntity.getEntityType(), 
                                monitoredEntity.getEntityIdentifier());
                if(entity!=null) {
                        if(entity.getEntityProcessedState().getValue() > EntityProcessedState.UNEXPIRED.getValue()) {
                                in.shop2020.alert.domain.AlertedEntity alertedEntity = new in.shop2020.alert.domain.AlertedEntity();
                                alertedEntity.setEntityType(entity.getEntityType());
                                alertedEntity.setEventType(entity.getEventType());
                                alertedEntity.setEntityIdentifier(entity.getEntityIdentifier());
                                alertedEntity.setEntityProperties(entity.getEntityProperties());
                                if(entity.getEntityProcessedState().getValue() == EntityProcessedState.WARNING_SENT.getValue()) {
                                        alertedEntity.setLastAlertedTime(entity.getWarnExpiryTime());
                                        alertedEntity.setEntityProcessedState(EntityProcessedState.WARNING_SENT);
                                        entityMapper.insertAlertedEntity(alertedEntity);
        
                                        AlertMapper alertMap;
                                String  delayedEventDescription = "UNKNOWN";
                                alertMap = getAlertMapper(entity.getEntityType(), entity.getEventType());
                                if(alertMap!=null) {
                                        delayedEventDescription = alertMap.getEventdesciption();
                                }
                                alertLogger.warn("\tEntityType=" + 
                                                        entity.getEntityType() + " ; " + "\t" + entity.getEntityIdentifier() +
                                                        "\tEventType=" + delayedEventDescription + " ; " + "AlertSeverity=WARN ; " +
                                                        "ExpiredTime=" + entity.getWarnExpiryTime());
                                }
                                else {
                                        alertedEntity.setLastAlertedTime(entity.getCriticalExpiryTime());
                                        alertedEntity.setEntityProcessedState(EntityProcessedState.CRITICAL_SENT);
                                        entityMapper.insertAlertedEntity(alertedEntity);
                                        
                                        AlertMapper alertMap;
                                String  delayedEventDescription = "UNKNOWN";
                                alertMap = getAlertMapper(entity.getEntityType(), entity.getEventType());
                                if(alertMap!=null) {
                                        delayedEventDescription = alertMap.getEventdesciption();
                                }
                                alertLogger.error("\tEntityType=" + 
                                                        entity.getEntityType() + " ; " + "\t" + entity.getEntityIdentifier() +
                                                        "\tEventType=" + delayedEventDescription + " ; " + "AlertSeverity=CRITICAL ; " +
                                                        "ExpiredTime=" + entity.getCriticalExpiryTime());
                                }
                        }
                                monitoredEntity.setId(entity.getId());
                                entityMapper.updateEntity(Converter.toDbEntity(monitoredEntity));
                } else {
                        scheduleAlert(monitoredEntity);
                }
        }

        public void scheduleAlert(MonitoredEntity monitoredEntity) 
                throws AlertServiceException {
                if(isEntityMonitorable(monitoredEntity.getEntityType())) {
                        entityMapper.insertEntity(Converter.toDbEntity(monitoredEntity));
                } else {
                        log.error("EntityType : " + monitoredEntity.getEntityType() + 
                                        "is not monitorable currently");
                        throw new AlertServiceException("EntityType : " + monitoredEntity.getEntityType() + 
                                        "is not monitorable currently", 103);
                }
                
        }
        
        public List<MonitoredEntity> getEntitiesToBeAlerted() {
                List<in.shop2020.alert.domain.MonitoredEntity> entitiesTobeAlerted = 
                        entityMapper.getEntitiesToBeAlerted();
                if(entitiesTobeAlerted != null) {
                        List<MonitoredEntity> tEntitiesTobeAlerted = new ArrayList<MonitoredEntity>();
                        for(in.shop2020.alert.domain.MonitoredEntity entity : entitiesTobeAlerted) {
                                tEntitiesTobeAlerted.add(Converter.toThriftMonitoredEntity(entity));
                        }
                        return tEntitiesTobeAlerted;
                }
                return null;
        }
        
        public List<MonitoredEntity> getActiveAlertEntities() {
                List<in.shop2020.alert.domain.MonitoredEntity> activeAlertEntities = 
                        entityMapper.getActiveAlertEntities();
                if(activeAlertEntities != null) {
                        List<MonitoredEntity> tEntitiesTobeAlerted = new ArrayList<MonitoredEntity>();
                        for(in.shop2020.alert.domain.MonitoredEntity entity : activeAlertEntities) {
                                tEntitiesTobeAlerted.add(Converter.toThriftMonitoredEntity(entity));
                        }
                        return tEntitiesTobeAlerted;
                }
                return null;
        }

        public void updateEntityProcessedState(long id, EntityProcessedState state) {
                entityMapper.updateEntityProcessedState(id, state);
        }


        private boolean isEntityMonitorable(EntityType entityType) {
                return entityMapper.isEntityMonitorable(entityType.getValue());
        }

        public void registerEventType(AlertMapper alertMap) {
                if(entityMapper.getAlertMapper(alertMap.getEntityType(), alertMap.getEventType())==null){
                        entityMapper.registerEventType(Converter.toDbAlertMap(alertMap));
                } {
                //TODO update existing alertMap
                }
        }

        public AlertMapper getAlertMapper(EntityType entityType, int eventType) {
                return Converter.toThriftAlertMapper(entityMapper.getAlertMapper(entityType, eventType));
        }

        public void activateEntityMonitoring(EntityType entityType, String userIds) throws TException {
                if(userIds == null) {
                        throw new TException("Default Ids for alert cannot be null while " + 
                                        "activating Entity Monitoring");
                }
                if(entityMapper.getEntityMonitoringStatus(entityType.getValue())!=null) {
                        entityMapper.activateEntityMonitoring(entityType, userIds);
                } else {
                        in.shop2020.alert.domain.EntityMonitoringStatus entityMonStatus = 
                                new in.shop2020.alert.domain.EntityMonitoringStatus();
                        entityMonStatus.setEntityType(entityType);
                        entityMonStatus.setActive(true);
                        entityMonStatus.setDefaultAlertGroup(userIds);
                        entityMapper.addEntityType(entityMonStatus);
                }
        }

        public void deActivateEntityMonitoring(EntityType entityType) {
                entityMapper.deActivateEntityMonitoring(entityType);
        }

        public EntityMonitoringStatus getEntityMonitoringStatus(
                        EntityType entityType) {
                in.shop2020.alert.domain.EntityMonitoringStatus entityMonStatus = 
                        entityMapper.getEntityMonitoringStatus(entityType.getValue());
                EntityMonitoringStatus tEntityMonitoringStatus = new EntityMonitoringStatus();
                tEntityMonitoringStatus.setEntityType(entityMonStatus.getEntityType());
                tEntityMonitoringStatus.setIsActive(entityMonStatus.isActive());
                tEntityMonitoringStatus.setUserIds(entityMonStatus.getDefaultAlertGroup());
                return tEntityMonitoringStatus;
        }
        
}