| 5519 |
amar.kumar |
1 |
package in.shop2020.alert.handler;
|
|
|
2 |
|
|
|
3 |
import java.util.ArrayList;
|
|
|
4 |
import java.util.List;
|
|
|
5 |
|
|
|
6 |
import in.shop2020.alert.AlertMapper;
|
|
|
7 |
import in.shop2020.alert.AlertServiceException;
|
|
|
8 |
import in.shop2020.alert.AlertType;
|
|
|
9 |
import in.shop2020.alert.AlertedEntity;
|
| 5536 |
amar.kumar |
10 |
import in.shop2020.alert.EntityMonitoringStatus;
|
| 5519 |
amar.kumar |
11 |
import in.shop2020.alert.EntityProcessedState;
|
|
|
12 |
import in.shop2020.alert.EntityType;
|
|
|
13 |
import in.shop2020.alert.MonitoredEntity;
|
|
|
14 |
import in.shop2020.alert.SearchFilter;
|
|
|
15 |
import in.shop2020.alert.persistence.EntityMapper;
|
|
|
16 |
import in.shop2020.alert.util.Converter;
|
|
|
17 |
|
|
|
18 |
import org.apache.commons.logging.Log;
|
|
|
19 |
import org.apache.commons.logging.LogFactory;
|
|
|
20 |
import org.apache.thrift.TException;
|
|
|
21 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
22 |
import org.springframework.stereotype.Service;
|
|
|
23 |
|
|
|
24 |
@Service
|
|
|
25 |
public class EntityHandler {
|
|
|
26 |
|
|
|
27 |
@Autowired
|
|
|
28 |
private EntityMapper entityMapper;
|
|
|
29 |
|
|
|
30 |
private static final Log log = LogFactory.getLog(EntityHandler.class);
|
|
|
31 |
|
|
|
32 |
public List<MonitoredEntity> getEntities(SearchFilter searchFilter) {
|
|
|
33 |
List<in.shop2020.alert.domain.MonitoredEntity> entities =
|
|
|
34 |
entityMapper.getEntities(Converter.toDomainSearchFilter
|
|
|
35 |
(searchFilter));
|
|
|
36 |
if(entities!=null) {
|
|
|
37 |
List<MonitoredEntity> tEntities = new ArrayList<MonitoredEntity>();
|
|
|
38 |
for(in.shop2020.alert.domain.MonitoredEntity entity : entities) {
|
|
|
39 |
tEntities.add(Converter.toThriftMonitoredEntity(entity));
|
|
|
40 |
}
|
|
|
41 |
}
|
|
|
42 |
return null;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
public List<AlertedEntity> getAlertedEntities(SearchFilter searchFilter) {
|
|
|
46 |
List<in.shop2020.alert.domain.AlertedEntity> alertedEntities =
|
|
|
47 |
entityMapper.getAlertedEntities(Converter.toDomainSearchFilter
|
|
|
48 |
(searchFilter));
|
|
|
49 |
if(alertedEntities!=null) {
|
|
|
50 |
List<AlertedEntity> tAlertedEntities = new ArrayList<AlertedEntity>();
|
|
|
51 |
for(in.shop2020.alert.domain.AlertedEntity alertedEntity : alertedEntities) {
|
|
|
52 |
tAlertedEntities.add(Converter.toThriftAlertedEntity(alertedEntity));
|
|
|
53 |
}
|
|
|
54 |
}
|
|
|
55 |
return null;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
public void endMonitoring(EntityType entityType, String key) {
|
|
|
59 |
in.shop2020.alert.domain.MonitoredEntity entity = entityMapper.getEntity(entityType, key);
|
|
|
60 |
if(entity.getEntityProcessedState().getValue() > EntityProcessedState.UNEXPIRED.getValue()) {
|
| 5563 |
amar.kumar |
61 |
in.shop2020.alert.domain.AlertedEntity alertedEntity = new in.shop2020.alert.domain.AlertedEntity();
|
|
|
62 |
alertedEntity.setEntityType(entity.getEntityType());
|
|
|
63 |
alertedEntity.setEventType(entity.getEventType());
|
|
|
64 |
alertedEntity.setEntityIdentifier(entity.getEntityIdentifier());
|
|
|
65 |
alertedEntity.setEntityProperties(entity.getEntityProperties());
|
| 5519 |
amar.kumar |
66 |
if(entity.getEntityProcessedState().getValue() == EntityProcessedState.WARNING_SENT.getValue()) {
|
| 5563 |
amar.kumar |
67 |
alertedEntity.setLastAlertedTime(entity.getWarnExpiryTime());
|
|
|
68 |
alertedEntity.setEntityProcessedState(EntityProcessedState.WARNING_SENT);
|
|
|
69 |
entityMapper.insertAlertedEntity(alertedEntity);
|
| 5519 |
amar.kumar |
70 |
}
|
|
|
71 |
else {
|
| 5563 |
amar.kumar |
72 |
alertedEntity.setLastAlertedTime(entity.getCriticalExpiryTime());
|
|
|
73 |
alertedEntity.setEntityProcessedState(EntityProcessedState.CRITICAL_SENT);
|
|
|
74 |
entityMapper.insertAlertedEntity(alertedEntity);
|
| 5519 |
amar.kumar |
75 |
}
|
|
|
76 |
}
|
|
|
77 |
entityMapper.removeMonitoredEntity(entityType, key);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
public void updateMonitoredObject(MonitoredEntity monitoredEntity) {
|
|
|
81 |
in.shop2020.alert.domain.MonitoredEntity entity = entityMapper.getEntity(monitoredEntity.getEntityType(), monitoredEntity.getEntityIdentifier());
|
|
|
82 |
if(entity.getEntityProcessedState().getValue() > EntityProcessedState.UNEXPIRED.getValue()) {
|
| 5563 |
amar.kumar |
83 |
in.shop2020.alert.domain.AlertedEntity alertedEntity = new in.shop2020.alert.domain.AlertedEntity();
|
|
|
84 |
alertedEntity.setEntityType(entity.getEntityType());
|
|
|
85 |
alertedEntity.setEventType(entity.getEventType());
|
|
|
86 |
alertedEntity.setEntityIdentifier(entity.getEntityIdentifier());
|
|
|
87 |
alertedEntity.setEntityProperties(entity.getEntityProperties());
|
| 5519 |
amar.kumar |
88 |
if(entity.getEntityProcessedState().getValue() == EntityProcessedState.WARNING_SENT.getValue()) {
|
| 5563 |
amar.kumar |
89 |
alertedEntity.setLastAlertedTime(entity.getWarnExpiryTime());
|
|
|
90 |
alertedEntity.setEntityProcessedState(EntityProcessedState.WARNING_SENT);
|
|
|
91 |
entityMapper.insertAlertedEntity(alertedEntity);
|
| 5519 |
amar.kumar |
92 |
}
|
|
|
93 |
else {
|
| 5563 |
amar.kumar |
94 |
alertedEntity.setLastAlertedTime(entity.getCriticalExpiryTime());
|
|
|
95 |
alertedEntity.setEntityProcessedState(EntityProcessedState.CRITICAL_SENT);
|
|
|
96 |
entityMapper.insertAlertedEntity(alertedEntity);
|
| 5519 |
amar.kumar |
97 |
}
|
|
|
98 |
}
|
|
|
99 |
monitoredEntity.setId(entity.getId());
|
|
|
100 |
entityMapper.updateEntity(Converter.toDbEntity(monitoredEntity));
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
public void scheduleAlert(MonitoredEntity monitoredEntity)
|
|
|
104 |
throws AlertServiceException {
|
|
|
105 |
if(isEntityMonitorable(monitoredEntity.getEntityType())) {
|
|
|
106 |
entityMapper.insertEntity(Converter.toDbEntity(monitoredEntity));
|
|
|
107 |
} else {
|
|
|
108 |
log.error("EntityType : " + monitoredEntity.getEntityType() +
|
|
|
109 |
"is not monitorable currently");
|
|
|
110 |
throw new AlertServiceException("EntityType : " + monitoredEntity.getEntityType() +
|
|
|
111 |
"is not monitorable currently", 103);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
public List<MonitoredEntity> getEntitiesToBeAlerted() {
|
|
|
117 |
List<in.shop2020.alert.domain.MonitoredEntity> entitiesTobeAlerted =
|
|
|
118 |
entityMapper.getEntitiesToBeAlerted();
|
|
|
119 |
if(entitiesTobeAlerted != null) {
|
|
|
120 |
List<MonitoredEntity> tEntitiesTobeAlerted = new ArrayList<MonitoredEntity>();
|
|
|
121 |
for(in.shop2020.alert.domain.MonitoredEntity entity : entitiesTobeAlerted) {
|
|
|
122 |
tEntitiesTobeAlerted.add(Converter.toThriftMonitoredEntity(entity));
|
|
|
123 |
}
|
|
|
124 |
return tEntitiesTobeAlerted;
|
|
|
125 |
}
|
|
|
126 |
return null;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
public void updateEntityProcessedState(long id, EntityProcessedState state) {
|
|
|
130 |
entityMapper.updateEntityProcessedState(id, state);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
private boolean isEntityMonitorable(EntityType entityType) {
|
|
|
135 |
return entityMapper.isEntityMonitorable(entityType.getValue());
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
public void registerEventType(AlertMapper alertMap) {
|
|
|
139 |
if(entityMapper.getAlertMapper(alertMap.getEntityType(), alertMap.getEventType())==null){
|
|
|
140 |
entityMapper.registerEventType(Converter.toDbAlertMap(alertMap));
|
|
|
141 |
} {
|
|
|
142 |
//TODO update existing alertMap
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
public AlertMapper getAlertMapper(EntityType entityType, int eventType) {
|
|
|
147 |
return Converter.toThriftAlertMapper(entityMapper.getAlertMapper(entityType, eventType));
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
public void activateEntityMonitoring(EntityType entityType, String userIds) throws TException {
|
|
|
151 |
if(userIds == null) {
|
|
|
152 |
throw new TException("Default Ids for alert cannot be null while " +
|
|
|
153 |
"activating Entity Monitoring");
|
|
|
154 |
}
|
| 5536 |
amar.kumar |
155 |
if(entityMapper.getEntityMonitoringStatus(entityType.getValue())!=null) {
|
|
|
156 |
entityMapper.activateEntityMonitoring(entityType, userIds);
|
|
|
157 |
} else {
|
|
|
158 |
in.shop2020.alert.domain.EntityMonitoringStatus entityMonStatus =
|
|
|
159 |
new in.shop2020.alert.domain.EntityMonitoringStatus();
|
|
|
160 |
entityMonStatus.setEntityType(entityType);
|
|
|
161 |
entityMonStatus.setActive(true);
|
|
|
162 |
entityMonStatus.setDefaultAlertGroup(userIds);
|
|
|
163 |
entityMapper.addEntityType(entityMonStatus);
|
|
|
164 |
}
|
| 5519 |
amar.kumar |
165 |
}
|
|
|
166 |
|
|
|
167 |
public void deActivateEntityMonitoring(EntityType entityType) {
|
|
|
168 |
entityMapper.deActivateEntityMonitoring(entityType);
|
|
|
169 |
}
|
| 5536 |
amar.kumar |
170 |
|
|
|
171 |
public EntityMonitoringStatus getEntityMonitoringStatus(
|
|
|
172 |
EntityType entityType) {
|
|
|
173 |
in.shop2020.alert.domain.EntityMonitoringStatus entityMonStatus =
|
|
|
174 |
entityMapper.getEntityMonitoringStatus(entityType.getValue());
|
|
|
175 |
EntityMonitoringStatus tEntityMonitoringStatus = new EntityMonitoringStatus();
|
|
|
176 |
tEntityMonitoringStatus.setEntityType(entityMonStatus.getEntityType());
|
|
|
177 |
tEntityMonitoringStatus.setIsActive(entityMonStatus.isActive());
|
|
|
178 |
tEntityMonitoringStatus.setUserIds(entityMonStatus.getDefaultAlertGroup());
|
|
|
179 |
return tEntityMonitoringStatus;
|
|
|
180 |
}
|
| 5519 |
amar.kumar |
181 |
|
|
|
182 |
}
|