Send alarm state transitioned events
This commit is contained in:
parent
acdfefd630
commit
d13b8d8a48
|
@ -24,6 +24,7 @@ import com.hpcloud.mon.MonApiConfiguration;
|
|||
import com.hpcloud.mon.app.command.UpdateAlarmCommand;
|
||||
import com.hpcloud.mon.common.event.AlarmCreatedEvent;
|
||||
import com.hpcloud.mon.common.event.AlarmDeletedEvent;
|
||||
import com.hpcloud.mon.common.event.AlarmStateTransitionedEvent;
|
||||
import com.hpcloud.mon.common.event.AlarmUpdatedEvent;
|
||||
import com.hpcloud.mon.common.model.alarm.AlarmExpression;
|
||||
import com.hpcloud.mon.common.model.alarm.AlarmState;
|
||||
|
@ -139,11 +140,11 @@ public class AlarmService {
|
|||
*/
|
||||
public Alarm update(String tenantId, String alarmId, AlarmExpression alarmExpression,
|
||||
UpdateAlarmCommand command) {
|
||||
assertAlarmExists(tenantId, alarmId, command.alarmActions, command.okActions,
|
||||
Alarm alarm = assertAlarmExists(tenantId, alarmId, command.alarmActions, command.okActions,
|
||||
command.undeterminedActions);
|
||||
updateInternal(tenantId, alarmId, false, command.name, command.description, command.expression,
|
||||
alarmExpression, command.state, command.actionsEnabled, command.alarmActions,
|
||||
command.okActions, command.undeterminedActions);
|
||||
alarmExpression, alarm.getState(), command.state, command.actionsEnabled,
|
||||
command.alarmActions, command.okActions, command.undeterminedActions);
|
||||
return new Alarm(alarmId, command.name, command.description, command.expression, command.state,
|
||||
command.actionsEnabled, command.alarmActions, command.okActions,
|
||||
command.undeterminedActions);
|
||||
|
@ -167,8 +168,8 @@ public class AlarmService {
|
|||
state = state == null ? alarm.getState() : state;
|
||||
enabled = enabled == null ? alarm.isActionsEnabled() : enabled;
|
||||
|
||||
updateInternal(tenantId, alarmId, true, name, description, expression, alarmExpression, state,
|
||||
enabled, alarmActions, okActions, undeterminedActions);
|
||||
updateInternal(tenantId, alarmId, true, name, description, expression, alarmExpression,
|
||||
alarm.getState(), state, enabled, alarmActions, okActions, undeterminedActions);
|
||||
|
||||
return new Alarm(alarmId, name, description, expression, state, enabled,
|
||||
alarmActions == null ? alarm.getAlarmActions() : alarmActions,
|
||||
|
@ -177,22 +178,30 @@ public class AlarmService {
|
|||
}
|
||||
|
||||
private void updateInternal(String tenantId, String alarmId, boolean patch, String name,
|
||||
String description, String expression, AlarmExpression alarmExpression, AlarmState state,
|
||||
Boolean enabled, List<String> alarmActions, List<String> okActions,
|
||||
String description, String expression, AlarmExpression alarmExpression, AlarmState oldState,
|
||||
AlarmState newState, Boolean enabled, List<String> alarmActions, List<String> okActions,
|
||||
List<String> undeterminedActions) {
|
||||
SubExpressions subExpressions = subExpressionsFor(alarmId, alarmExpression);
|
||||
|
||||
try {
|
||||
LOG.debug("Updating alarm {} for tenant {}", name, tenantId);
|
||||
repo.update(tenantId, alarmId, patch, name, description, expression, state, enabled,
|
||||
repo.update(tenantId, alarmId, patch, name, description, expression, newState, enabled,
|
||||
subExpressions.oldAlarmSubExpressions.keySet(), subExpressions.changedSubExpressions,
|
||||
subExpressions.newAlarmSubExpressions, alarmActions, okActions, undeterminedActions);
|
||||
|
||||
// Notify interested parties of updated alarm
|
||||
String event = Serialization.toJson(new AlarmUpdatedEvent(tenantId, alarmId, name,
|
||||
description, expression, state, enabled, subExpressions.oldAlarmSubExpressions,
|
||||
description, expression, newState, enabled, subExpressions.oldAlarmSubExpressions,
|
||||
subExpressions.changedSubExpressions, subExpressions.newAlarmSubExpressions));
|
||||
producer.send(new KeyedMessage<>(config.eventsTopic, tenantId, event));
|
||||
|
||||
// Notify interested parties of transitioned alarm state
|
||||
if (!oldState.equals(newState)) {
|
||||
event = Serialization.toJson(new AlarmStateTransitionedEvent(tenantId, alarmId, name,
|
||||
description, oldState, newState, enabled, stateChangeReasonFor(oldState, newState),
|
||||
System.currentTimeMillis()));
|
||||
producer.send(new KeyedMessage<>(config.eventsTopic, tenantId, event));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw Exceptions.uncheck(e, "Error updating alarm for project / tenant %s", tenantId);
|
||||
}
|
||||
|
@ -242,6 +251,10 @@ public class AlarmService {
|
|||
return subExpressions;
|
||||
}
|
||||
|
||||
private String stateChangeReasonFor(AlarmState oldState, AlarmState newState) {
|
||||
return "State changed via API";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether all of the fields of {@code a} and {@code b} are the same except the operator
|
||||
* and threshold.
|
||||
|
|
|
@ -4,6 +4,7 @@ import static org.mockito.Matchers.any;
|
|||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
@ -116,7 +117,7 @@ public class AlarmServiceTest {
|
|||
Alarm expected = new Alarm(alarm.getId(), "foo bar baz", "foo bar baz", newExprStr,
|
||||
AlarmState.ALARM, false, newAlarmActions, newOkActions, newUndeterminedActions);
|
||||
assertEquals(expected, alarm);
|
||||
verify(producer).send(any(KeyedMessage.class));
|
||||
verify(producer, times(2)).send(any(KeyedMessage.class));
|
||||
}
|
||||
|
||||
public void testOldAndNewSubExpressionsFor() {
|
||||
|
|
Loading…
Reference in New Issue