enrichers().add(Enrichers.builder() .aggregating(t) .publishing(total) .fromMembers() .computingSum() .build());
enrichers().add(Enrichers.builder() .aggregating(t) .publishing(total) .fromMembers() .computingSum() .build()); enrichers().add(Enrichers.builder() .aggregating(t) .publishing(average) .fromMembers() .computingAverage() .defaultValueForUnreportedSensors(0) .build());
@Test public void testSummingEnricherWhenNoSensorValuesYet() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(entity)) .defaultValueForUnreportedSensors(11) .valueToReportIfNoSensors(40) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, target, 11); }
.computingAverage() .fromMembers() .publishing(MachineAttributes.AVERAGE_CPU_USAGE) .valueToReportIfNoSensors(0d) .build()); hosts.enrichers().add(Enrichers.builder() .aggregating(DOCKER_CONTAINER_COUNT) .computingSum() .fromMembers() .publishing(DOCKER_CONTAINER_COUNT) .build());
@Test public void testAggregatingExcludingBlankString() { group.addMember(entity); group.addMember(entity2); group.enrichers().add(Enrichers.builder() .aggregating(STR1) .publishing(SET1) .fromMembers() .excludingBlank() .computing(new Function<Collection<?>, Set<Object>>() { @Override public Set<Object> apply(Collection<?> input) { // accept null values, so don't use ImmutableSet return (input == null) ? ImmutableSet.<Object>of() : MutableSet.<Object>copyOf(input); }}) .build()); entity.sensors().set(STR1, "1"); entity2.sensors().set(STR1, "2"); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of("1", "2")); entity.sensors().set(STR1, "3"); entity2.sensors().set(STR1, null); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of("3")); entity.sensors().set(STR1, ""); entity2.sensors().set(STR1, "4"); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of("4")); }
@Test public void testCustomAggregatingFunction() { TestEntity producer1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); Function<Collection<Integer>,Integer> aggregator = new Function<Collection<Integer>, Integer>() { @Override public Integer apply(Collection<Integer> input) { int result = 1; for (Integer in : input) result += in*in; return result; } }; entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computing(aggregator) .fromHardcodedProducers(ImmutableList.of(producer1)) .defaultValueForUnreportedSensors(0) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, target, 1); // Event by producer producer1.sensors().set(intSensor, 2); EntityAsserts.assertAttributeEqualsEventually(entity, target, 5); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override protected void initEnrichers() { super.initEnrichers(); propagateMasterAttribute(MySqlNode.HOSTNAME); propagateMasterAttribute(MySqlNode.ADDRESS); propagateMasterAttribute(MySqlNode.SUBNET_HOSTNAME); propagateMasterAttribute(MySqlNode.SUBNET_ADDRESS); propagateMasterAttribute(MySqlNode.MYSQL_PORT); propagateMasterAttribute(MySqlNode.DATASTORE_URL); enrichers().add(Enrichers.builder() .aggregating(MySqlNode.DATASTORE_URL) .publishing(SLAVE_DATASTORE_URL_LIST) .computing((Function<Collection<String>, List<String>>)(Function)Functions.identity()) .entityFilter(Predicates.not(MySqlClusterUtils.IS_MASTER)) .fromMembers() .build()); enrichers().add(Enrichers.builder() .aggregating(MySqlNode.QUERIES_PER_SECOND_FROM_MYSQL) .publishing(QUERIES_PER_SECOND_FROM_MYSQL_PER_NODE) .fromMembers() .computingAverage() .defaultValueForUnreportedSensors(0d) .build()); }
@Test public void testAggregatesNewMembersOfGroup() { BasicGroup group = app.createAndManageChild(EntitySpec.create(BasicGroup.class)); TestEntity p1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity p2 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); log.debug("created {} and the entities it will contain {} {}", new Object[] {group, p1, p2}); group.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromMembers() .defaultValueForUnreportedSensors(0) .valueToReportIfNoSensors(0) .build()); EntityAsserts.assertAttributeEqualsEventually(group, target, 0); group.addMember(p1); p1.sensors().set(intSensor, 1); EntityAsserts.assertAttributeEqualsEventually(group, target, 1); group.addMember(p2); p2.sensors().set(intSensor, 2); EntityAsserts.assertAttributeEqualsEventually(group, target, 3); group.removeMember(p2); EntityAsserts.assertAttributeEqualsEventually(group, target, 1); }
@Test public void testAggregatingExcludingNull() { group.addMember(entity); group.enrichers().add(Enrichers.builder() .aggregating(NUM1) .publishing(SET1) .fromMembers() .excludingBlank() .computing(new Function<Collection<?>, Set<Object>>() { @Override public Set<Object> apply(Collection<?> input) { // accept null values, so don't use ImmutableSet return (input == null) ? ImmutableSet.<Object>of() : MutableSet.<Object>copyOf(input); }}) .build()); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of()); entity.sensors().set(NUM1, 1); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of(1)); entity.sensors().set(NUM1, null); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of()); entity.sensors().set(NUM1, 2); EntityAsserts.assertAttributeEqualsEventually(group, SET1, ImmutableSet.<Object>of(2)); }
@Test public void testMultipleProducersAverage() { TestEntity producer1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity producer2 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity producer3 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(doubleSensor) .computingAverage() .fromHardcodedProducers(ImmutableList.of(producer1, producer2, producer3)) .build()); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, doubleSensor, null); producer1.sensors().set(intSensor, 3); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 3d); producer2.sensors().set(intSensor, 1); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 2d); producer3.sensors().set(intSensor, 5); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 3d); producer2.sensors().set(intSensor, 4); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 4d); }
@Test public void testAppliesFilterWhenAggregatingMembersOfGroup() { BasicGroup group = app.createAndManageChild(EntitySpec.create(BasicGroup.class)); TestEntity p1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity p2 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity p3 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); group.addMember(p1); group.addMember(p2); p1.sensors().set(intSensor, 1); p2.sensors().set(intSensor, 2); p3.sensors().set(intSensor, 4); group.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromMembers() .entityFilter(Predicates.equalTo((Entity)p1)) .build()); EntityAsserts.assertAttributeEqualsEventually(group, target, 1); group.addMember(p3); EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", SHORT_WAIT_MS), group, target, 1); }
@Test public void testAggregatesMembersOfProducer() { BasicGroup group = app.addChild(EntitySpec.create(BasicGroup.class)); TestEntity p1 = group.addChild(EntitySpec.create(TestEntity.class)); TestEntity p2 = group.addChild(EntitySpec.create(TestEntity.class)); group.addMember(p1); group.addMember(p2); p1.sensors().set(intSensor, 1); app.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .from(group) .fromMembers() .build()); EntityAsserts.assertAttributeEqualsEventually(app, target, 1); p2.sensors().set(intSensor, 2); EntityAsserts.assertAttributeEqualsEventually(app, target, 3); group.removeMember(p2); EntityAsserts.assertAttributeEqualsEventually(app, target, 1); }
@Test public void testAggregatingRespectsUnchanged() { group.addMember(entity); group.enrichers().add(Enrichers.builder() .aggregating(NUM1) .<Object>publishing(LONG1) .fromMembers() .computing(new Function<Iterable<Integer>, Object>() { @Override public Object apply(Iterable<Integer> input) { if (input != null && Iterables.contains(input, 123)) { return Enrichers.sum(input, 0, 0, new TypeToken<Integer>(){}); } else { return Entities.UNCHANGED; } }}) .build()); entity.sensors().set(NUM1, 123); EntityAsserts.assertAttributeEqualsEventually(group, LONG1, Long.valueOf(123)); entity.sensors().set(NUM1, 987654); EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", "100ms"), group, LONG1, Long.valueOf(123)); }
@Test public void testAggregatesExistingMembersOfGroup() { BasicGroup group = app.addChild(EntitySpec.create(BasicGroup.class)); TestEntity p1 = group.addChild(EntitySpec.create(TestEntity.class)); TestEntity p2 = group.addChild(EntitySpec.create(TestEntity.class)); group.addMember(p1); group.addMember(p2); p1.sensors().set(intSensor, 1); group.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromMembers() .build()); EntityAsserts.assertAttributeEqualsEventually(group, target, 1); p2.sensors().set(intSensor, 2); EntityAsserts.assertAttributeEqualsEventually(group, target, 3); group.removeMember(p2); EntityAsserts.assertAttributeEqualsEventually(group, target, 1); }
@Test public void testAggregatesNewChidren() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromChildren() .defaultValueForUnreportedSensors(0) .valueToReportIfNoSensors(0) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, target, 0); TestEntity p1 = entity.createAndManageChild(EntitySpec.create(TestEntity.class)); p1.sensors().set(intSensor, 1); EntityAsserts.assertAttributeEqualsEventually(entity, target, 1); TestEntity p2 = entity.createAndManageChild(EntitySpec.create(TestEntity.class)); p2.sensors().set(intSensor, 2); EntityAsserts.assertAttributeEqualsEventually(entity, target, 3); Entities.unmanage(p2); EntityAsserts.assertAttributeEqualsEventually(entity, target, 1); }
@Test public void testMultipleProducersAverageDefaultingZero() { TestEntity producer1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity producer2 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity producer3 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(doubleSensor) .computingAverage() .fromHardcodedProducers(ImmutableList.of(producer1, producer2, producer3)) .defaultValueForUnreportedSensors(0) .valueToReportIfNoSensors(0) .build()); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 0d); producer1.sensors().set(intSensor, 3); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 1d); producer2.sensors().set(intSensor, 3); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 2d); producer3.sensors().set(intSensor, 3); EntityAsserts.assertAttributeEqualsEventually(entity, doubleSensor, 3d); }
@Test public void testMultipleProducersSum() { TestEntity producer1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity producer2 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity producer3 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(producer1, producer2, producer3)) .build()); producer3.sensors().set(intSensor, 1); EntityAsserts.assertAttributeEqualsEventually(entity, target, 1); producer1.sensors().set(intSensor, 2); EntityAsserts.assertAttributeEqualsEventually(entity, target, 3); producer2.sensors().set(intSensor, 4); EntityAsserts.assertAttributeEqualsEventually(entity, target, 7); }
@Test public void testAggregatesExistingChildren() { TestEntity p1 = entity.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity p2 = entity.createAndManageChild(EntitySpec.create(TestEntity.class)); p1.sensors().set(intSensor, 1); entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromChildren() .build()); EntityAsserts.assertAttributeEqualsEventually(entity, target, 1); p2.sensors().set(intSensor, 2); EntityAsserts.assertAttributeEqualsEventually(entity, target, 3); Entities.unmanage(p2); EntityAsserts.assertAttributeEqualsEventually(entity, target, 1); }
@Test public void testAggregatingMap() { group.addMember(entity); group.enrichers().add(Enrichers.builder() .aggregating(STR1, STR2) .publishing(MAP1) .fromMembers() .excludingBlank() .build()); EntityAsserts.assertAttributeEqualsEventually(group, MAP1, ImmutableMap.<String, String>of()); entity.sensors().set(STR1, "a"); EntityAsserts.assertAttributeEqualsEventually(group, MAP1, ImmutableMap.<String, String>of()); entity.sensors().set(STR2, "b"); EntityAsserts.assertAttributeEqualsEventually(group, MAP1, ImmutableMap.<String, String>of("a", "b")); entity.sensors().set(STR2, "c"); EntityAsserts.assertAttributeEqualsEventually(group, MAP1, ImmutableMap.<String, String>of("a", "c")); }
@Override protected void initEnrichers() { super.initEnrichers(); EnricherSpec<?> zks = Enrichers.builder() .aggregating(ZooKeeperNode.ZOOKEEPER_ENDPOINT) .publishing(ZOOKEEPER_SERVERS) .fromMembers() .build(); EnricherSpec<?> zke = Enrichers.builder() .joining(ZOOKEEPER_SERVERS) .publishing(ZOOKEEPER_ENDPOINTS) .quote(false) .separator(",") .build(); enrichers().add(zks); enrichers().add(zke); }