You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brooklyn.apache.org by Graeme-Miller <gi...@git.apache.org> on 2016/06/24 18:07:07 UTC

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

GitHub user Graeme-Miller opened a pull request:

    https://github.com/apache/brooklyn-server/pull/220

    Added a percentage enricher

    

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/Graeme-Miller/brooklyn-server add_percent_enricher

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/brooklyn-server/pull/220.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #220
    
----
commit a2c4110a25eeaa50e7b5f36c7caa05a8777ead43
Author: graeme.miller <gr...@cloudsoftcorp.com>
Date:   2016-06-23T13:34:21Z

    Added a percentage enricher

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by grkvlt <gi...@git.apache.org>.
Github user grkvlt commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68439929
  
    --- Diff: core/src/main/java/org/apache/brooklyn/enricher/stock/PercentageEnricher.java ---
    @@ -0,0 +1,127 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.reflect.TypeToken;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.entity.EntityLocal;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.SensorEvent;
    +import org.apache.brooklyn.api.sensor.SensorEventListener;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.config.ConfigKeys;
    +import org.apache.brooklyn.core.enricher.AbstractEnricher;
    +import org.apache.brooklyn.util.collections.MutableMap;
    +import org.apache.brooklyn.util.javalang.JavaClassNames;
    +
    +public class PercentageEnricher extends AbstractEnricher implements SensorEventListener<Number> {
    --- End diff --
    
    Add some JavaDoc explaining what this does


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68456955
  
    --- Diff: core/src/test/java/org/apache/brooklyn/enricher/stock/PercentageEnricherTest.java ---
    @@ -0,0 +1,275 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import static org.testng.Assert.assertEquals;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.testng.annotations.BeforeMethod;
    +import org.testng.annotations.Test;
    +
    +import com.google.common.collect.ImmutableList;
    +
    +import org.apache.brooklyn.api.entity.EntitySpec;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.EnricherSpec;
    +import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
    +import org.apache.brooklyn.core.entity.EntityAsserts;
    +import org.apache.brooklyn.core.location.SimulatedLocation;
    +import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
    +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
    +import org.apache.brooklyn.core.test.entity.TestApplication;
    +
    +public class PercentageEnricherTest extends BrooklynAppUnitTestSupport {
    +
    +    public static final Logger log = LoggerFactory.getLogger(PercentageEnricherTest.class);
    +
    +    AttributeSensor<Double> currentSensor;
    +    AttributeSensor<Double> totalSensor;
    +    AttributeSensor<Double> targetSensor;
    +
    +    @BeforeMethod(alwaysRun = true)
    +    @Override
    +    public void setUp() throws Exception {
    +        super.setUp();
    +        currentSensor = new BasicAttributeSensor<Double>(Double.class, "current");
    +        totalSensor = new BasicAttributeSensor<Double>(Double.class, "total");
    +        targetSensor = new BasicAttributeSensor<Double>(Double.class, "target");
    +
    +        app.start(ImmutableList.of(new SimulatedLocation()));
    +    }
    +
    +    private void addEnricher() {
    +        app.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, currentSensor)
    +                .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, totalSensor)
    +                .configure(PercentageEnricher.TARGET_SENSOR, targetSensor));
    +    }
    +
    +    @Test
    +    public void vanillaTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 50d);
    +    }
    +
    +    @Test
    +    public void currentNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, null);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    --- End diff --
    
    Do you want to test that it set the value to null, or that it didn't change the pre-existing value? I think it will do the latter. If you want to test that, then you want to initialise it to something first. Same for other tests.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/brooklyn-server/pull/220


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by grkvlt <gi...@git.apache.org>.
Github user grkvlt commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68438477
  
    --- Diff: core/src/main/java/org/apache/brooklyn/enricher/stock/PercentageEnricher.java ---
    @@ -0,0 +1,127 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.reflect.TypeToken;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.entity.EntityLocal;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.SensorEvent;
    +import org.apache.brooklyn.api.sensor.SensorEventListener;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.config.ConfigKeys;
    +import org.apache.brooklyn.core.enricher.AbstractEnricher;
    +import org.apache.brooklyn.util.collections.MutableMap;
    +import org.apache.brooklyn.util.javalang.JavaClassNames;
    +
    +public class PercentageEnricher extends AbstractEnricher implements SensorEventListener<Number> {
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(PercentageEnricher.class);
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_CURRENT_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceCurrentSensor");
    --- End diff --
    
    Don't split the line for the `TypeToken` block, just do`new TypeToken() { }, "enricher.blah"` and also add a description to the key definition.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server issue #220: Added a percentage enricher

Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on the issue:

    https://github.com/apache/brooklyn-server/pull/220
  
    LGTM; merging.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by grkvlt <gi...@git.apache.org>.
Github user grkvlt commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68440119
  
    --- Diff: core/src/test/java/org/apache/brooklyn/enricher/stock/PercentageEnricherTest.java ---
    @@ -0,0 +1,275 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import static org.testng.Assert.assertEquals;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.testng.annotations.BeforeMethod;
    +import org.testng.annotations.Test;
    +
    +import com.google.common.collect.ImmutableList;
    +
    +import org.apache.brooklyn.api.entity.EntitySpec;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.EnricherSpec;
    +import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
    +import org.apache.brooklyn.core.entity.EntityAsserts;
    +import org.apache.brooklyn.core.location.SimulatedLocation;
    +import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
    +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
    +import org.apache.brooklyn.core.test.entity.TestApplication;
    +
    +public class PercentageEnricherTest extends BrooklynAppUnitTestSupport {
    +
    +    public static final Logger log = LoggerFactory.getLogger(PercentageEnricherTest.class);
    +
    +    AttributeSensor<Double> currentSensor;
    +    AttributeSensor<Double> totalSensor;
    +    AttributeSensor<Double> targetSensor;
    +
    +    @BeforeMethod(alwaysRun = true)
    +    @Override
    +    public void setUp() throws Exception {
    +        super.setUp();
    +        currentSensor = new BasicAttributeSensor<Double>(Double.class, "current");
    +        totalSensor = new BasicAttributeSensor<Double>(Double.class, "total");
    +        targetSensor = new BasicAttributeSensor<Double>(Double.class, "target");
    +
    +        app.start(ImmutableList.of(new SimulatedLocation()));
    +    }
    +
    +    private void addEnricher() {
    +        app.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, currentSensor)
    +                .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, totalSensor)
    +                .configure(PercentageEnricher.TARGET_SENSOR, targetSensor));
    +    }
    +
    +    @Test
    +    public void vanillaTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 50d);
    +    }
    +
    +    @Test
    +    public void currentNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, null);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, null);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void bothInputNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, null);
    +        app.sensors().set(totalSensor, null);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void currentZeroTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 0d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 0d);
    +    }
    +
    +    @Test
    +    public void totalZeroTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 0d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalLessThanCurrentTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 25d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    --- End diff --
    
    So, the result here should be 50/25 = 2.0d


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68457734
  
    --- Diff: software/base/src/main/java/org/apache/brooklyn/entity/machine/AddMachineMetrics.java ---
    @@ -75,6 +77,12 @@ public static void addMachineMetricsEnrichers(EntityLocal entity) {
             entity.enrichers().add(EnricherSpec.create(YamlRollingTimeWindowMeanEnricher.class)
                     .configure(YamlRollingTimeWindowMeanEnricher.SOURCE_SENSOR, MachineAttributes.USED_MEMORY_DELTA_PER_SECOND_LAST)
                     .configure(YamlRollingTimeWindowMeanEnricher.TARGET_SENSOR, MachineAttributes.USED_MEMORY_DELTA_PER_SECOND_IN_WINDOW));
    +
    +        entity.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, MachineAttributes.USED_MEMORY)
    +                .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, MachineAttributes.TOTAL_MEMORY)
    +                .configure(PercentageEnricher.TARGET_SENSOR, MachineAttributes.USED_MEMORY_PERCENT));
    --- End diff --
    
    I'd also do `.configure(PercentageEnricher.SUPPRESS_DUPLICATES, true)`.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server issue #220: Added a percentage enricher

Posted by grkvlt <gi...@git.apache.org>.
Github user grkvlt commented on the issue:

    https://github.com/apache/brooklyn-server/pull/220
  
    @Graeme-Miller looks much better now, happy to merge


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by neykov <gi...@git.apache.org>.
Github user neykov commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68587543
  
    --- Diff: core/src/main/java/org/apache/brooklyn/enricher/stock/PercentageEnricher.java ---
    @@ -0,0 +1,127 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.reflect.TypeToken;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.entity.EntityLocal;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.SensorEvent;
    +import org.apache.brooklyn.api.sensor.SensorEventListener;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.config.ConfigKeys;
    +import org.apache.brooklyn.core.enricher.AbstractEnricher;
    +import org.apache.brooklyn.util.collections.MutableMap;
    +import org.apache.brooklyn.util.javalang.JavaClassNames;
    +
    +public class PercentageEnricher extends AbstractEnricher implements SensorEventListener<Number> {
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(PercentageEnricher.class);
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_CURRENT_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    --- End diff --
    
    Can you add `@SuppressWarnings("serial")` to all `new TypeToken` places.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68456564
  
    --- Diff: core/src/main/java/org/apache/brooklyn/enricher/stock/PercentageEnricher.java ---
    @@ -0,0 +1,127 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.reflect.TypeToken;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.entity.EntityLocal;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.SensorEvent;
    +import org.apache.brooklyn.api.sensor.SensorEventListener;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.config.ConfigKeys;
    +import org.apache.brooklyn.core.enricher.AbstractEnricher;
    +import org.apache.brooklyn.util.collections.MutableMap;
    +import org.apache.brooklyn.util.javalang.JavaClassNames;
    +
    +public class PercentageEnricher extends AbstractEnricher implements SensorEventListener<Number> {
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(PercentageEnricher.class);
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_CURRENT_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceCurrentSensor");
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_TOTAL_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceTotalSensor");
    +
    +    public static final ConfigKey<AttributeSensor<Double>> TARGET_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<Double>>() {
    +    }, "enricher.targetSensor");
    +
    +    public static final ConfigKey<Entity> PRODUCER = ConfigKeys.newConfigKey(Entity.class, "enricher.producer");
    +
    +    protected AttributeSensor<? extends Number> sourceCurrentSensor;
    +    protected AttributeSensor<? extends Number> sourceTotalSensor;
    +    protected AttributeSensor<Double> targetSensor;
    +    protected Entity producer;
    +
    +    @SuppressWarnings({"unchecked", "rawtypes"})
    +    @Override
    +    public void setEntity(EntityLocal entity) {
    +        super.setEntity(entity);
    +
    +        this.sourceCurrentSensor = getConfig(SOURCE_CURRENT_SENSOR);
    +        this.sourceTotalSensor = getConfig(SOURCE_TOTAL_SENSOR);
    +        this.targetSensor = getConfig(TARGET_SENSOR);
    +        this.producer = getConfig(PRODUCER) == null ? entity : getConfig(PRODUCER);
    +
    +        if (sourceCurrentSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + SOURCE_CURRENT_SENSOR.getName());
    +        }
    +        if (sourceTotalSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + SOURCE_TOTAL_SENSOR.getName());
    +        }
    +        if (targetSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + TARGET_SENSOR.getName());
    +        }
    +
    +        if (targetSensor.equals(sourceCurrentSensor)) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " detect cycle with " + SOURCE_CURRENT_SENSOR.getName());
    +        }
    +        if (targetSensor.equals(sourceTotalSensor)) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " detect cycle with " + SOURCE_TOTAL_SENSOR.getName());
    +        }
    +
    +        subscriptions().subscribe(MutableMap.of("notifyOfInitialValue", true), producer, sourceCurrentSensor, this);
    +        subscriptions().subscribe(MutableMap.of("notifyOfInitialValue", true), producer, sourceTotalSensor, this);
    +
    +    }
    +
    +    @Override
    +    public void onEvent(SensorEvent<Number> event) {
    +        Number current = producer.sensors().get(sourceCurrentSensor);
    +        Number total = producer.sensors().get(sourceTotalSensor);
    +        Double result = null;
    +
    +        if (current == null) {
    +            LOG.debug("Current is null, returning");
    +            return;
    +        }
    +
    +        if (total == null || total.equals(0)) {
    +            LOG.debug("total {" + total + "} is null or zero, returning");
    +            return;
    +        }
    +
    +        Double currentDouble = current.doubleValue();
    +        Double totalDouble = total.doubleValue();
    +
    +        if (currentDouble > totalDouble) {
    +            LOG.debug("Current is greater than total, returning");
    --- End diff --
    
    Agreed - percentages above 100% are reasonable in some situations (e.g. over-committing virtual cores, etc).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server issue #220: Added a percentage enricher

Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on the issue:

    https://github.com/apache/brooklyn-server/pull/220
  
    The jenkins failure is for the reason Andrew pointed out:
    ```
    org.apache.brooklyn.util.exceptions.PropagatedRuntimeException: failed succeeds-eventually, 69 attempts, 30001ms elapsed: AssertionError: entity=Application[m0gi2x3r]; attribute=Sensor: target (java.lang.Double) expected [100.0] but found [1.0]
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by grkvlt <gi...@git.apache.org>.
Github user grkvlt commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68441093
  
    --- Diff: core/src/main/java/org/apache/brooklyn/enricher/stock/PercentageEnricher.java ---
    @@ -0,0 +1,127 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.reflect.TypeToken;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.entity.EntityLocal;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.SensorEvent;
    +import org.apache.brooklyn.api.sensor.SensorEventListener;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.config.ConfigKeys;
    +import org.apache.brooklyn.core.enricher.AbstractEnricher;
    +import org.apache.brooklyn.util.collections.MutableMap;
    +import org.apache.brooklyn.util.javalang.JavaClassNames;
    +
    +public class PercentageEnricher extends AbstractEnricher implements SensorEventListener<Number> {
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(PercentageEnricher.class);
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_CURRENT_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceCurrentSensor");
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_TOTAL_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceTotalSensor");
    +
    +    public static final ConfigKey<AttributeSensor<Double>> TARGET_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<Double>>() {
    --- End diff --
    
    Add a `RendererHint` that will format this as a percentage in the `setEntity()` method, like this:
    ```Java
    RendererHints.register(config().get(TARGET_SENSOR), RendererHints.displayValue(MathFunctions.percent(2)));
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by grkvlt <gi...@git.apache.org>.
Github user grkvlt commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68439260
  
    --- Diff: core/src/main/java/org/apache/brooklyn/enricher/stock/PercentageEnricher.java ---
    @@ -0,0 +1,127 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.reflect.TypeToken;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.entity.EntityLocal;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.SensorEvent;
    +import org.apache.brooklyn.api.sensor.SensorEventListener;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.config.ConfigKeys;
    +import org.apache.brooklyn.core.enricher.AbstractEnricher;
    +import org.apache.brooklyn.util.collections.MutableMap;
    +import org.apache.brooklyn.util.javalang.JavaClassNames;
    +
    +public class PercentageEnricher extends AbstractEnricher implements SensorEventListener<Number> {
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(PercentageEnricher.class);
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_CURRENT_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceCurrentSensor");
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_TOTAL_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceTotalSensor");
    +
    +    public static final ConfigKey<AttributeSensor<Double>> TARGET_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<Double>>() {
    +    }, "enricher.targetSensor");
    +
    +    public static final ConfigKey<Entity> PRODUCER = ConfigKeys.newConfigKey(Entity.class, "enricher.producer");
    +
    +    protected AttributeSensor<? extends Number> sourceCurrentSensor;
    +    protected AttributeSensor<? extends Number> sourceTotalSensor;
    +    protected AttributeSensor<Double> targetSensor;
    +    protected Entity producer;
    +
    +    @SuppressWarnings({"unchecked", "rawtypes"})
    +    @Override
    +    public void setEntity(EntityLocal entity) {
    +        super.setEntity(entity);
    +
    +        this.sourceCurrentSensor = getConfig(SOURCE_CURRENT_SENSOR);
    +        this.sourceTotalSensor = getConfig(SOURCE_TOTAL_SENSOR);
    +        this.targetSensor = getConfig(TARGET_SENSOR);
    +        this.producer = getConfig(PRODUCER) == null ? entity : getConfig(PRODUCER);
    +
    +        if (sourceCurrentSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + SOURCE_CURRENT_SENSOR.getName());
    +        }
    +        if (sourceTotalSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + SOURCE_TOTAL_SENSOR.getName());
    +        }
    +        if (targetSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + TARGET_SENSOR.getName());
    +        }
    +
    +        if (targetSensor.equals(sourceCurrentSensor)) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " detect cycle with " + SOURCE_CURRENT_SENSOR.getName());
    +        }
    +        if (targetSensor.equals(sourceTotalSensor)) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " detect cycle with " + SOURCE_TOTAL_SENSOR.getName());
    +        }
    +
    +        subscriptions().subscribe(MutableMap.of("notifyOfInitialValue", true), producer, sourceCurrentSensor, this);
    +        subscriptions().subscribe(MutableMap.of("notifyOfInitialValue", true), producer, sourceTotalSensor, this);
    +
    +    }
    +
    +    @Override
    +    public void onEvent(SensorEvent<Number> event) {
    +        Number current = producer.sensors().get(sourceCurrentSensor);
    +        Number total = producer.sensors().get(sourceTotalSensor);
    +        Double result = null;
    +
    +        if (current == null) {
    +            LOG.debug("Current is null, returning");
    +            return;
    +        }
    +
    +        if (total == null || total.equals(0)) {
    +            LOG.debug("total {" + total + "} is null or zero, returning");
    +            return;
    +        }
    +
    +        Double currentDouble = current.doubleValue();
    +        Double totalDouble = total.doubleValue();
    +
    +        if (currentDouble > totalDouble) {
    +            LOG.debug("Current is greater than total, returning");
    +            return;
    +        }
    +
    +        if (currentDouble < 0 || totalDouble < 0) {
    +            LOG.debug("Current {"+currentDouble+"}  or total {"+totalDouble+"} is negative, returning");
    --- End diff --
    
    Prefer using formatting in log method, as in `LOG.debug("Current value ({}) or total ({}) is negative, returning", currentDouble, totalDouble)`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68456375
  
    --- Diff: core/src/main/java/org/apache/brooklyn/enricher/stock/PercentageEnricher.java ---
    @@ -0,0 +1,127 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.reflect.TypeToken;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.entity.EntityLocal;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.SensorEvent;
    +import org.apache.brooklyn.api.sensor.SensorEventListener;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.config.ConfigKeys;
    +import org.apache.brooklyn.core.enricher.AbstractEnricher;
    +import org.apache.brooklyn.util.collections.MutableMap;
    +import org.apache.brooklyn.util.javalang.JavaClassNames;
    +
    +public class PercentageEnricher extends AbstractEnricher implements SensorEventListener<Number> {
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(PercentageEnricher.class);
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_CURRENT_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceCurrentSensor");
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_TOTAL_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceTotalSensor");
    +
    +    public static final ConfigKey<AttributeSensor<Double>> TARGET_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<Double>>() {
    +    }, "enricher.targetSensor");
    +
    +    public static final ConfigKey<Entity> PRODUCER = ConfigKeys.newConfigKey(Entity.class, "enricher.producer");
    +
    +    protected AttributeSensor<? extends Number> sourceCurrentSensor;
    +    protected AttributeSensor<? extends Number> sourceTotalSensor;
    +    protected AttributeSensor<Double> targetSensor;
    +    protected Entity producer;
    +
    +    @SuppressWarnings({"unchecked", "rawtypes"})
    +    @Override
    +    public void setEntity(EntityLocal entity) {
    +        super.setEntity(entity);
    +
    +        this.sourceCurrentSensor = getConfig(SOURCE_CURRENT_SENSOR);
    +        this.sourceTotalSensor = getConfig(SOURCE_TOTAL_SENSOR);
    +        this.targetSensor = getConfig(TARGET_SENSOR);
    +        this.producer = getConfig(PRODUCER) == null ? entity : getConfig(PRODUCER);
    +
    +        if (sourceCurrentSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + SOURCE_CURRENT_SENSOR.getName());
    +        }
    +        if (sourceTotalSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + SOURCE_TOTAL_SENSOR.getName());
    +        }
    +        if (targetSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + TARGET_SENSOR.getName());
    +        }
    +
    +        if (targetSensor.equals(sourceCurrentSensor)) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " detect cycle with " + SOURCE_CURRENT_SENSOR.getName());
    +        }
    +        if (targetSensor.equals(sourceTotalSensor)) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " detect cycle with " + SOURCE_TOTAL_SENSOR.getName());
    +        }
    +
    +        subscriptions().subscribe(MutableMap.of("notifyOfInitialValue", true), producer, sourceCurrentSensor, this);
    +        subscriptions().subscribe(MutableMap.of("notifyOfInitialValue", true), producer, sourceTotalSensor, this);
    +
    +    }
    +
    +    @Override
    +    public void onEvent(SensorEvent<Number> event) {
    +        Number current = producer.sensors().get(sourceCurrentSensor);
    +        Number total = producer.sensors().get(sourceTotalSensor);
    +        Double result = null;
    +
    +        if (current == null) {
    +            LOG.debug("Current is null, returning");
    --- End diff --
    
    Give more context in the log message. Imagine that this is buried inside a log file, with a dozen entities that have this kind of enricher and you want to figure out from the customer's log what this refers to. I'd do something like:
    
    ```
    LOG.debug("{} not updating target sensor of {}, because current is null", this, entity);
    ```
    
    Same for your other log messages below.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by grkvlt <gi...@git.apache.org>.
Github user grkvlt commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68438586
  
    --- Diff: core/src/main/java/org/apache/brooklyn/enricher/stock/PercentageEnricher.java ---
    @@ -0,0 +1,127 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.reflect.TypeToken;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.entity.EntityLocal;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.SensorEvent;
    +import org.apache.brooklyn.api.sensor.SensorEventListener;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.config.ConfigKeys;
    +import org.apache.brooklyn.core.enricher.AbstractEnricher;
    +import org.apache.brooklyn.util.collections.MutableMap;
    +import org.apache.brooklyn.util.javalang.JavaClassNames;
    +
    +public class PercentageEnricher extends AbstractEnricher implements SensorEventListener<Number> {
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(PercentageEnricher.class);
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_CURRENT_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceCurrentSensor");
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_TOTAL_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceTotalSensor");
    +
    +    public static final ConfigKey<AttributeSensor<Double>> TARGET_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<Double>>() {
    +    }, "enricher.targetSensor");
    +
    +    public static final ConfigKey<Entity> PRODUCER = ConfigKeys.newConfigKey(Entity.class, "enricher.producer");
    +
    +    protected AttributeSensor<? extends Number> sourceCurrentSensor;
    +    protected AttributeSensor<? extends Number> sourceTotalSensor;
    +    protected AttributeSensor<Double> targetSensor;
    +    protected Entity producer;
    +
    +    @SuppressWarnings({"unchecked", "rawtypes"})
    +    @Override
    +    public void setEntity(EntityLocal entity) {
    +        super.setEntity(entity);
    +
    +        this.sourceCurrentSensor = getConfig(SOURCE_CURRENT_SENSOR);
    --- End diff --
    
    Don't need `this` here, field names are unambiguous


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68457563
  
    --- Diff: core/src/test/java/org/apache/brooklyn/enricher/stock/PercentageEnricherTest.java ---
    @@ -0,0 +1,275 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import static org.testng.Assert.assertEquals;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.testng.annotations.BeforeMethod;
    +import org.testng.annotations.Test;
    +
    +import com.google.common.collect.ImmutableList;
    +
    +import org.apache.brooklyn.api.entity.EntitySpec;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.EnricherSpec;
    +import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
    +import org.apache.brooklyn.core.entity.EntityAsserts;
    +import org.apache.brooklyn.core.location.SimulatedLocation;
    +import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
    +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
    +import org.apache.brooklyn.core.test.entity.TestApplication;
    +
    +public class PercentageEnricherTest extends BrooklynAppUnitTestSupport {
    +
    +    public static final Logger log = LoggerFactory.getLogger(PercentageEnricherTest.class);
    +
    +    AttributeSensor<Double> currentSensor;
    +    AttributeSensor<Double> totalSensor;
    +    AttributeSensor<Double> targetSensor;
    +
    +    @BeforeMethod(alwaysRun = true)
    +    @Override
    +    public void setUp() throws Exception {
    +        super.setUp();
    +        currentSensor = new BasicAttributeSensor<Double>(Double.class, "current");
    +        totalSensor = new BasicAttributeSensor<Double>(Double.class, "total");
    +        targetSensor = new BasicAttributeSensor<Double>(Double.class, "target");
    +
    +        app.start(ImmutableList.of(new SimulatedLocation()));
    +    }
    +
    +    private void addEnricher() {
    +        app.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, currentSensor)
    +                .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, totalSensor)
    +                .configure(PercentageEnricher.TARGET_SENSOR, targetSensor));
    +    }
    +
    +    @Test
    +    public void vanillaTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 50d);
    +    }
    +
    +    @Test
    +    public void currentNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, null);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, null);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void bothInputNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, null);
    +        app.sensors().set(totalSensor, null);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void currentZeroTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 0d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 0d);
    +    }
    +
    +    @Test
    +    public void totalZeroTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 0d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalLessThanCurrentTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 25d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void oneHundredPercentTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 50d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 100d);
    +    }
    +
    +    @Test
    +    public void negativeCurrent() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, -50d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void negativeTotal() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, -100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void bothSourceNegative() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, -50d);
    +        app.sensors().set(totalSensor, -100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalDoubleMaxValue() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, Double.MAX_VALUE);
    +        app.sensors().set(totalSensor, Double.MAX_VALUE);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 100d);
    +    }
    +
    +    //SETUP TESTS
    +    @Test(expectedExceptions = IllegalArgumentException.class)
    --- End diff --
    
    Also want to test that it throws an exception if entity == producer && targetSensor == sourceSensor.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server issue #220: Added a percentage enricher

Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on the issue:

    https://github.com/apache/brooklyn-server/pull/220
  
    Looks good - clean code and good test coverage. Only minor comments.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by Graeme-Miller <gi...@git.apache.org>.
Github user Graeme-Miller commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68921829
  
    --- Diff: core/src/test/java/org/apache/brooklyn/enricher/stock/PercentageEnricherTest.java ---
    @@ -0,0 +1,275 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import static org.testng.Assert.assertEquals;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.testng.annotations.BeforeMethod;
    +import org.testng.annotations.Test;
    +
    +import com.google.common.collect.ImmutableList;
    +
    +import org.apache.brooklyn.api.entity.EntitySpec;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.EnricherSpec;
    +import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
    +import org.apache.brooklyn.core.entity.EntityAsserts;
    +import org.apache.brooklyn.core.location.SimulatedLocation;
    +import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
    +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
    +import org.apache.brooklyn.core.test.entity.TestApplication;
    +
    +public class PercentageEnricherTest extends BrooklynAppUnitTestSupport {
    +
    +    public static final Logger log = LoggerFactory.getLogger(PercentageEnricherTest.class);
    +
    +    AttributeSensor<Double> currentSensor;
    +    AttributeSensor<Double> totalSensor;
    +    AttributeSensor<Double> targetSensor;
    +
    +    @BeforeMethod(alwaysRun = true)
    +    @Override
    +    public void setUp() throws Exception {
    +        super.setUp();
    +        currentSensor = new BasicAttributeSensor<Double>(Double.class, "current");
    +        totalSensor = new BasicAttributeSensor<Double>(Double.class, "total");
    +        targetSensor = new BasicAttributeSensor<Double>(Double.class, "target");
    +
    +        app.start(ImmutableList.of(new SimulatedLocation()));
    +    }
    +
    +    private void addEnricher() {
    +        app.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, currentSensor)
    +                .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, totalSensor)
    +                .configure(PercentageEnricher.TARGET_SENSOR, targetSensor));
    +    }
    +
    +    @Test
    +    public void vanillaTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 50d);
    +    }
    +
    +    @Test
    +    public void currentNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, null);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    --- End diff --
    
    I want to test the situation where the currentSensor has not been set at all. 
    No exceptions should be thrown, the target should just be set to null.
    
    There is a test later, validThenInvalid, which checks that a sensor having it's value updated to something invalid will not overwrite the previous result.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by grkvlt <gi...@git.apache.org>.
Github user grkvlt commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68440401
  
    --- Diff: core/src/test/java/org/apache/brooklyn/enricher/stock/PercentageEnricherTest.java ---
    @@ -0,0 +1,275 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import static org.testng.Assert.assertEquals;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.testng.annotations.BeforeMethod;
    +import org.testng.annotations.Test;
    +
    +import com.google.common.collect.ImmutableList;
    +
    +import org.apache.brooklyn.api.entity.EntitySpec;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.EnricherSpec;
    +import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
    +import org.apache.brooklyn.core.entity.EntityAsserts;
    +import org.apache.brooklyn.core.location.SimulatedLocation;
    +import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
    +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
    +import org.apache.brooklyn.core.test.entity.TestApplication;
    +
    +public class PercentageEnricherTest extends BrooklynAppUnitTestSupport {
    +
    +    public static final Logger log = LoggerFactory.getLogger(PercentageEnricherTest.class);
    +
    +    AttributeSensor<Double> currentSensor;
    +    AttributeSensor<Double> totalSensor;
    +    AttributeSensor<Double> targetSensor;
    +
    +    @BeforeMethod(alwaysRun = true)
    +    @Override
    +    public void setUp() throws Exception {
    +        super.setUp();
    +        currentSensor = new BasicAttributeSensor<Double>(Double.class, "current");
    +        totalSensor = new BasicAttributeSensor<Double>(Double.class, "total");
    +        targetSensor = new BasicAttributeSensor<Double>(Double.class, "target");
    +
    +        app.start(ImmutableList.of(new SimulatedLocation()));
    +    }
    +
    +    private void addEnricher() {
    +        app.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, currentSensor)
    +                .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, totalSensor)
    +                .configure(PercentageEnricher.TARGET_SENSOR, targetSensor));
    +    }
    +
    +    @Test
    +    public void vanillaTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 50d);
    +    }
    +
    +    @Test
    +    public void currentNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, null);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, null);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void bothInputNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, null);
    +        app.sensors().set(totalSensor, null);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void currentZeroTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 0d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 0d);
    +    }
    +
    +    @Test
    +    public void totalZeroTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 0d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalLessThanCurrentTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 25d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void oneHundredPercentTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 50d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 100d);
    +    }
    +
    +    @Test
    +    public void negativeCurrent() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, -50d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void negativeTotal() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, -100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void bothSourceNegative() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, -50d);
    +        app.sensors().set(totalSensor, -100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalDoubleMaxValue() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, Double.MAX_VALUE);
    +        app.sensors().set(totalSensor, Double.MAX_VALUE);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 100d);
    --- End diff --
    
    Again, result should be 1.0d


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by grkvlt <gi...@git.apache.org>.
Github user grkvlt commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68439762
  
    --- Diff: core/src/main/java/org/apache/brooklyn/enricher/stock/PercentageEnricher.java ---
    @@ -0,0 +1,127 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.reflect.TypeToken;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.entity.EntityLocal;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.SensorEvent;
    +import org.apache.brooklyn.api.sensor.SensorEventListener;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.config.ConfigKeys;
    +import org.apache.brooklyn.core.enricher.AbstractEnricher;
    +import org.apache.brooklyn.util.collections.MutableMap;
    +import org.apache.brooklyn.util.javalang.JavaClassNames;
    +
    +public class PercentageEnricher extends AbstractEnricher implements SensorEventListener<Number> {
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(PercentageEnricher.class);
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_CURRENT_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceCurrentSensor");
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_TOTAL_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceTotalSensor");
    +
    +    public static final ConfigKey<AttributeSensor<Double>> TARGET_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<Double>>() {
    +    }, "enricher.targetSensor");
    +
    +    public static final ConfigKey<Entity> PRODUCER = ConfigKeys.newConfigKey(Entity.class, "enricher.producer");
    +
    +    protected AttributeSensor<? extends Number> sourceCurrentSensor;
    +    protected AttributeSensor<? extends Number> sourceTotalSensor;
    +    protected AttributeSensor<Double> targetSensor;
    +    protected Entity producer;
    +
    +    @SuppressWarnings({"unchecked", "rawtypes"})
    +    @Override
    +    public void setEntity(EntityLocal entity) {
    +        super.setEntity(entity);
    +
    +        this.sourceCurrentSensor = getConfig(SOURCE_CURRENT_SENSOR);
    +        this.sourceTotalSensor = getConfig(SOURCE_TOTAL_SENSOR);
    +        this.targetSensor = getConfig(TARGET_SENSOR);
    +        this.producer = getConfig(PRODUCER) == null ? entity : getConfig(PRODUCER);
    +
    +        if (sourceCurrentSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + SOURCE_CURRENT_SENSOR.getName());
    +        }
    +        if (sourceTotalSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + SOURCE_TOTAL_SENSOR.getName());
    +        }
    +        if (targetSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + TARGET_SENSOR.getName());
    +        }
    +
    +        if (targetSensor.equals(sourceCurrentSensor)) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " detect cycle with " + SOURCE_CURRENT_SENSOR.getName());
    +        }
    +        if (targetSensor.equals(sourceTotalSensor)) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " detect cycle with " + SOURCE_TOTAL_SENSOR.getName());
    +        }
    +
    +        subscriptions().subscribe(MutableMap.of("notifyOfInitialValue", true), producer, sourceCurrentSensor, this);
    +        subscriptions().subscribe(MutableMap.of("notifyOfInitialValue", true), producer, sourceTotalSensor, this);
    +
    +    }
    +
    +    @Override
    +    public void onEvent(SensorEvent<Number> event) {
    +        Number current = producer.sensors().get(sourceCurrentSensor);
    +        Number total = producer.sensors().get(sourceTotalSensor);
    +        Double result = null;
    +
    +        if (current == null) {
    +            LOG.debug("Current is null, returning");
    +            return;
    +        }
    +
    +        if (total == null || total.equals(0)) {
    +            LOG.debug("total {" + total + "} is null or zero, returning");
    +            return;
    +        }
    +
    +        Double currentDouble = current.doubleValue();
    +        Double totalDouble = total.doubleValue();
    +
    +        if (currentDouble > totalDouble) {
    +            LOG.debug("Current is greater than total, returning");
    +            return;
    +        }
    +
    +        if (currentDouble < 0 || totalDouble < 0) {
    +            LOG.debug("Current {"+currentDouble+"}  or total {"+totalDouble+"} is negative, returning");
    +            return;
    +        }
    +
    +        if (current.equals(0)) {
    +            LOG.debug("current is zero, setting percent to zero");
    +            result = 0d;
    --- End diff --
    
    This gives the same result as `0d / totalDouble` i.e. `0d` so don't need the check, just always do the division.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68457446
  
    --- Diff: core/src/test/java/org/apache/brooklyn/enricher/stock/PercentageEnricherTest.java ---
    @@ -0,0 +1,275 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import static org.testng.Assert.assertEquals;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.testng.annotations.BeforeMethod;
    +import org.testng.annotations.Test;
    +
    +import com.google.common.collect.ImmutableList;
    +
    +import org.apache.brooklyn.api.entity.EntitySpec;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.EnricherSpec;
    +import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
    +import org.apache.brooklyn.core.entity.EntityAsserts;
    +import org.apache.brooklyn.core.location.SimulatedLocation;
    +import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
    +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
    +import org.apache.brooklyn.core.test.entity.TestApplication;
    +
    +public class PercentageEnricherTest extends BrooklynAppUnitTestSupport {
    +
    +    public static final Logger log = LoggerFactory.getLogger(PercentageEnricherTest.class);
    +
    +    AttributeSensor<Double> currentSensor;
    +    AttributeSensor<Double> totalSensor;
    +    AttributeSensor<Double> targetSensor;
    +
    +    @BeforeMethod(alwaysRun = true)
    +    @Override
    +    public void setUp() throws Exception {
    +        super.setUp();
    +        currentSensor = new BasicAttributeSensor<Double>(Double.class, "current");
    +        totalSensor = new BasicAttributeSensor<Double>(Double.class, "total");
    +        targetSensor = new BasicAttributeSensor<Double>(Double.class, "target");
    +
    +        app.start(ImmutableList.of(new SimulatedLocation()));
    +    }
    +
    +    private void addEnricher() {
    +        app.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, currentSensor)
    +                .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, totalSensor)
    +                .configure(PercentageEnricher.TARGET_SENSOR, targetSensor));
    +    }
    +
    +    @Test
    +    public void vanillaTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 50d);
    +    }
    +
    +    @Test
    +    public void currentNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, null);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, null);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void bothInputNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, null);
    +        app.sensors().set(totalSensor, null);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void currentZeroTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 0d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 0d);
    +    }
    +
    +    @Test
    +    public void totalZeroTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 0d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalLessThanCurrentTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 25d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void oneHundredPercentTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 50d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 100d);
    +    }
    +
    +    @Test
    +    public void negativeCurrent() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, -50d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void negativeTotal() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, -100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void bothSourceNegative() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, -50d);
    +        app.sensors().set(totalSensor, -100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalDoubleMaxValue() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, Double.MAX_VALUE);
    +        app.sensors().set(totalSensor, Double.MAX_VALUE);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 100d);
    +    }
    +
    +    //SETUP TESTS
    +    @Test(expectedExceptions = IllegalArgumentException.class)
    +    public void totalNoCurrentSensor() {
    +        app.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, totalSensor)
    +                .configure(PercentageEnricher.TARGET_SENSOR, targetSensor));
    +    }
    +
    +    @Test(expectedExceptions = IllegalArgumentException.class)
    +    public void totalNoTotalSensor() {
    +        app.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, currentSensor)
    +                .configure(PercentageEnricher.TARGET_SENSOR, targetSensor));
    +    }
    +
    +    @Test(expectedExceptions = IllegalArgumentException.class)
    +    public void totalNoTargetSensor() {
    +        app.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, currentSensor)
    +                .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, totalSensor));
    +    }
    +
    +    @Test
    +    public void testDifferentProducer() {
    +        EntitySpec<TestApplication> appSpec = EntitySpec.create(TestApplication.class)
    +                .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, shouldSkipOnBoxBaseDirResolution());
    +
    +        TestApplication producer = mgmt.getEntityManager().createEntity(appSpec);
    --- End diff --
    
    Personal preference to just create a child entity as the producer, rather than a new app - it's less code:
    
    ```
    Entity producer = app.addChild(EntitySpec.create(TestEntity.class));
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by geomacy <gi...@git.apache.org>.
Github user geomacy commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68441946
  
    --- Diff: core/src/main/java/org/apache/brooklyn/enricher/stock/PercentageEnricher.java ---
    @@ -0,0 +1,127 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.reflect.TypeToken;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.entity.EntityLocal;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.SensorEvent;
    +import org.apache.brooklyn.api.sensor.SensorEventListener;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.config.ConfigKeys;
    +import org.apache.brooklyn.core.enricher.AbstractEnricher;
    +import org.apache.brooklyn.util.collections.MutableMap;
    +import org.apache.brooklyn.util.javalang.JavaClassNames;
    +
    +public class PercentageEnricher extends AbstractEnricher implements SensorEventListener<Number> {
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(PercentageEnricher.class);
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_CURRENT_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceCurrentSensor");
    --- End diff --
    
    At the same time can you split it into two lines for readability, on Github if nowhere else!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by grkvlt <gi...@git.apache.org>.
Github user grkvlt commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68439495
  
    --- Diff: core/src/main/java/org/apache/brooklyn/enricher/stock/PercentageEnricher.java ---
    @@ -0,0 +1,127 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.reflect.TypeToken;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.entity.EntityLocal;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.SensorEvent;
    +import org.apache.brooklyn.api.sensor.SensorEventListener;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.config.ConfigKeys;
    +import org.apache.brooklyn.core.enricher.AbstractEnricher;
    +import org.apache.brooklyn.util.collections.MutableMap;
    +import org.apache.brooklyn.util.javalang.JavaClassNames;
    +
    +public class PercentageEnricher extends AbstractEnricher implements SensorEventListener<Number> {
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(PercentageEnricher.class);
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_CURRENT_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceCurrentSensor");
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_TOTAL_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceTotalSensor");
    +
    +    public static final ConfigKey<AttributeSensor<Double>> TARGET_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<Double>>() {
    +    }, "enricher.targetSensor");
    +
    +    public static final ConfigKey<Entity> PRODUCER = ConfigKeys.newConfigKey(Entity.class, "enricher.producer");
    +
    +    protected AttributeSensor<? extends Number> sourceCurrentSensor;
    +    protected AttributeSensor<? extends Number> sourceTotalSensor;
    +    protected AttributeSensor<Double> targetSensor;
    +    protected Entity producer;
    +
    +    @SuppressWarnings({"unchecked", "rawtypes"})
    +    @Override
    +    public void setEntity(EntityLocal entity) {
    +        super.setEntity(entity);
    +
    +        this.sourceCurrentSensor = getConfig(SOURCE_CURRENT_SENSOR);
    +        this.sourceTotalSensor = getConfig(SOURCE_TOTAL_SENSOR);
    +        this.targetSensor = getConfig(TARGET_SENSOR);
    +        this.producer = getConfig(PRODUCER) == null ? entity : getConfig(PRODUCER);
    +
    +        if (sourceCurrentSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + SOURCE_CURRENT_SENSOR.getName());
    +        }
    +        if (sourceTotalSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + SOURCE_TOTAL_SENSOR.getName());
    +        }
    +        if (targetSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + TARGET_SENSOR.getName());
    +        }
    +
    +        if (targetSensor.equals(sourceCurrentSensor)) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " detect cycle with " + SOURCE_CURRENT_SENSOR.getName());
    +        }
    +        if (targetSensor.equals(sourceTotalSensor)) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " detect cycle with " + SOURCE_TOTAL_SENSOR.getName());
    +        }
    +
    +        subscriptions().subscribe(MutableMap.of("notifyOfInitialValue", true), producer, sourceCurrentSensor, this);
    +        subscriptions().subscribe(MutableMap.of("notifyOfInitialValue", true), producer, sourceTotalSensor, this);
    +
    +    }
    +
    +    @Override
    +    public void onEvent(SensorEvent<Number> event) {
    +        Number current = producer.sensors().get(sourceCurrentSensor);
    +        Number total = producer.sensors().get(sourceTotalSensor);
    +        Double result = null;
    +
    +        if (current == null) {
    +            LOG.debug("Current is null, returning");
    +            return;
    +        }
    +
    +        if (total == null || total.equals(0)) {
    +            LOG.debug("total {" + total + "} is null or zero, returning");
    +            return;
    +        }
    +
    +        Double currentDouble = current.doubleValue();
    +        Double totalDouble = total.doubleValue();
    +
    +        if (currentDouble > totalDouble) {
    +            LOG.debug("Current is greater than total, returning");
    --- End diff --
    
    Not necessarily wrong. The ratio will just be greater than 1.0


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by grkvlt <gi...@git.apache.org>.
Github user grkvlt commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68440340
  
    --- Diff: core/src/test/java/org/apache/brooklyn/enricher/stock/PercentageEnricherTest.java ---
    @@ -0,0 +1,275 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import static org.testng.Assert.assertEquals;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.testng.annotations.BeforeMethod;
    +import org.testng.annotations.Test;
    +
    +import com.google.common.collect.ImmutableList;
    +
    +import org.apache.brooklyn.api.entity.EntitySpec;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.EnricherSpec;
    +import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
    +import org.apache.brooklyn.core.entity.EntityAsserts;
    +import org.apache.brooklyn.core.location.SimulatedLocation;
    +import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
    +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
    +import org.apache.brooklyn.core.test.entity.TestApplication;
    +
    +public class PercentageEnricherTest extends BrooklynAppUnitTestSupport {
    +
    +    public static final Logger log = LoggerFactory.getLogger(PercentageEnricherTest.class);
    +
    +    AttributeSensor<Double> currentSensor;
    +    AttributeSensor<Double> totalSensor;
    +    AttributeSensor<Double> targetSensor;
    +
    +    @BeforeMethod(alwaysRun = true)
    +    @Override
    +    public void setUp() throws Exception {
    +        super.setUp();
    +        currentSensor = new BasicAttributeSensor<Double>(Double.class, "current");
    +        totalSensor = new BasicAttributeSensor<Double>(Double.class, "total");
    +        targetSensor = new BasicAttributeSensor<Double>(Double.class, "target");
    +
    +        app.start(ImmutableList.of(new SimulatedLocation()));
    +    }
    +
    +    private void addEnricher() {
    +        app.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, currentSensor)
    +                .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, totalSensor)
    +                .configure(PercentageEnricher.TARGET_SENSOR, targetSensor));
    +    }
    +
    +    @Test
    +    public void vanillaTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 50d);
    +    }
    +
    +    @Test
    +    public void currentNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, null);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, null);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void bothInputNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, null);
    +        app.sensors().set(totalSensor, null);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void currentZeroTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 0d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 0d);
    +    }
    +
    +    @Test
    +    public void totalZeroTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 0d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalLessThanCurrentTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 25d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void oneHundredPercentTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 50d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 100d);
    --- End diff --
    
    This should be 50/50 = 1.0d


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by aledsage <gi...@git.apache.org>.
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68456092
  
    --- Diff: core/src/main/java/org/apache/brooklyn/enricher/stock/PercentageEnricher.java ---
    @@ -0,0 +1,127 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.reflect.TypeToken;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.entity.EntityLocal;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.SensorEvent;
    +import org.apache.brooklyn.api.sensor.SensorEventListener;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.config.ConfigKeys;
    +import org.apache.brooklyn.core.enricher.AbstractEnricher;
    +import org.apache.brooklyn.util.collections.MutableMap;
    +import org.apache.brooklyn.util.javalang.JavaClassNames;
    +
    +public class PercentageEnricher extends AbstractEnricher implements SensorEventListener<Number> {
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(PercentageEnricher.class);
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_CURRENT_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceCurrentSensor");
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_TOTAL_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceTotalSensor");
    +
    +    public static final ConfigKey<AttributeSensor<Double>> TARGET_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<Double>>() {
    +    }, "enricher.targetSensor");
    +
    +    public static final ConfigKey<Entity> PRODUCER = ConfigKeys.newConfigKey(Entity.class, "enricher.producer");
    +
    +    protected AttributeSensor<? extends Number> sourceCurrentSensor;
    +    protected AttributeSensor<? extends Number> sourceTotalSensor;
    +    protected AttributeSensor<Double> targetSensor;
    +    protected Entity producer;
    +
    +    @SuppressWarnings({"unchecked", "rawtypes"})
    +    @Override
    +    public void setEntity(EntityLocal entity) {
    +        super.setEntity(entity);
    +
    +        this.sourceCurrentSensor = getConfig(SOURCE_CURRENT_SENSOR);
    +        this.sourceTotalSensor = getConfig(SOURCE_TOTAL_SENSOR);
    +        this.targetSensor = getConfig(TARGET_SENSOR);
    +        this.producer = getConfig(PRODUCER) == null ? entity : getConfig(PRODUCER);
    +
    +        if (sourceCurrentSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + SOURCE_CURRENT_SENSOR.getName());
    +        }
    +        if (sourceTotalSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + SOURCE_TOTAL_SENSOR.getName());
    +        }
    +        if (targetSensor == null) {
    +            throw new IllegalArgumentException("Enricher " + JavaClassNames.simpleClassName(this) + " has no " + TARGET_SENSOR.getName());
    +        }
    +
    +        if (targetSensor.equals(sourceCurrentSensor)) {
    --- End diff --
    
    This is presumably only a cycle if `entity.equals(producer)`.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by grkvlt <gi...@git.apache.org>.
Github user grkvlt commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68440473
  
    --- Diff: core/src/test/java/org/apache/brooklyn/enricher/stock/PercentageEnricherTest.java ---
    @@ -0,0 +1,275 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import static org.testng.Assert.assertEquals;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +import org.testng.annotations.BeforeMethod;
    +import org.testng.annotations.Test;
    +
    +import com.google.common.collect.ImmutableList;
    +
    +import org.apache.brooklyn.api.entity.EntitySpec;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.EnricherSpec;
    +import org.apache.brooklyn.core.entity.BrooklynConfigKeys;
    +import org.apache.brooklyn.core.entity.EntityAsserts;
    +import org.apache.brooklyn.core.location.SimulatedLocation;
    +import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
    +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
    +import org.apache.brooklyn.core.test.entity.TestApplication;
    +
    +public class PercentageEnricherTest extends BrooklynAppUnitTestSupport {
    +
    +    public static final Logger log = LoggerFactory.getLogger(PercentageEnricherTest.class);
    +
    +    AttributeSensor<Double> currentSensor;
    +    AttributeSensor<Double> totalSensor;
    +    AttributeSensor<Double> targetSensor;
    +
    +    @BeforeMethod(alwaysRun = true)
    +    @Override
    +    public void setUp() throws Exception {
    +        super.setUp();
    +        currentSensor = new BasicAttributeSensor<Double>(Double.class, "current");
    +        totalSensor = new BasicAttributeSensor<Double>(Double.class, "total");
    +        targetSensor = new BasicAttributeSensor<Double>(Double.class, "target");
    +
    +        app.start(ImmutableList.of(new SimulatedLocation()));
    +    }
    +
    +    private void addEnricher() {
    +        app.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, currentSensor)
    +                .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, totalSensor)
    +                .configure(PercentageEnricher.TARGET_SENSOR, targetSensor));
    +    }
    +
    +    @Test
    +    public void vanillaTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 50d);
    +    }
    +
    +    @Test
    +    public void currentNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, null);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, null);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void bothInputNullTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, null);
    +        app.sensors().set(totalSensor, null);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void currentZeroTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 0d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 0d);
    +    }
    +
    +    @Test
    +    public void totalZeroTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 0d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalLessThanCurrentTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 25d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void oneHundredPercentTest() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, 50d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 100d);
    +    }
    +
    +    @Test
    +    public void negativeCurrent() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, -50d);
    +        app.sensors().set(totalSensor, 100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void negativeTotal() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, 50d);
    +        app.sensors().set(totalSensor, -100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void bothSourceNegative() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, -50d);
    +        app.sensors().set(totalSensor, -100d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, null);
    +    }
    +
    +    @Test
    +    public void totalDoubleMaxValue() {
    +        addEnricher();
    +
    +        app.sensors().set(currentSensor, Double.MAX_VALUE);
    +        app.sensors().set(totalSensor, Double.MAX_VALUE);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 100d);
    +    }
    +
    +    //SETUP TESTS
    +    @Test(expectedExceptions = IllegalArgumentException.class)
    +    public void totalNoCurrentSensor() {
    +        app.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, totalSensor)
    +                .configure(PercentageEnricher.TARGET_SENSOR, targetSensor));
    +    }
    +
    +    @Test(expectedExceptions = IllegalArgumentException.class)
    +    public void totalNoTotalSensor() {
    +        app.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, currentSensor)
    +                .configure(PercentageEnricher.TARGET_SENSOR, targetSensor));
    +    }
    +
    +    @Test(expectedExceptions = IllegalArgumentException.class)
    +    public void totalNoTargetSensor() {
    +        app.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, currentSensor)
    +                .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, totalSensor));
    +    }
    +
    +    @Test
    +    public void testDifferentProducer() {
    +        EntitySpec<TestApplication> appSpec = EntitySpec.create(TestApplication.class)
    +                .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, shouldSkipOnBoxBaseDirResolution());
    +
    +        TestApplication producer = mgmt.getEntityManager().createEntity(appSpec);
    +
    +        app.enrichers().add(EnricherSpec.create(PercentageEnricher.class)
    +                .configure(PercentageEnricher.SOURCE_CURRENT_SENSOR, currentSensor)
    +                .configure(PercentageEnricher.SOURCE_TOTAL_SENSOR, totalSensor)
    +                .configure(PercentageEnricher.TARGET_SENSOR, targetSensor)
    +                .configure(PercentageEnricher.PRODUCER, producer)
    +        );
    +
    +        producer.sensors().set(currentSensor, 25d);
    +        producer.sensors().set(totalSensor, 50d);
    +        EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, 50d);
    --- End diff --
    
    Should be 0.5d


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] brooklyn-server pull request #220: Added a percentage enricher

Posted by grkvlt <gi...@git.apache.org>.
Github user grkvlt commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/220#discussion_r68438910
  
    --- Diff: core/src/main/java/org/apache/brooklyn/enricher/stock/PercentageEnricher.java ---
    @@ -0,0 +1,127 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.enricher.stock;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.reflect.TypeToken;
    +
    +import org.apache.brooklyn.api.entity.Entity;
    +import org.apache.brooklyn.api.entity.EntityLocal;
    +import org.apache.brooklyn.api.sensor.AttributeSensor;
    +import org.apache.brooklyn.api.sensor.SensorEvent;
    +import org.apache.brooklyn.api.sensor.SensorEventListener;
    +import org.apache.brooklyn.config.ConfigKey;
    +import org.apache.brooklyn.core.config.ConfigKeys;
    +import org.apache.brooklyn.core.enricher.AbstractEnricher;
    +import org.apache.brooklyn.util.collections.MutableMap;
    +import org.apache.brooklyn.util.javalang.JavaClassNames;
    +
    +public class PercentageEnricher extends AbstractEnricher implements SensorEventListener<Number> {
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(PercentageEnricher.class);
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_CURRENT_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceCurrentSensor");
    +
    +    public static final ConfigKey<AttributeSensor<? extends Number>> SOURCE_TOTAL_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<? extends Number>>() {
    +    }, "enricher.sourceTotalSensor");
    +
    +    public static final ConfigKey<AttributeSensor<Double>> TARGET_SENSOR = ConfigKeys.newConfigKey(new TypeToken<AttributeSensor<Double>>() {
    +    }, "enricher.targetSensor");
    +
    +    public static final ConfigKey<Entity> PRODUCER = ConfigKeys.newConfigKey(Entity.class, "enricher.producer");
    +
    +    protected AttributeSensor<? extends Number> sourceCurrentSensor;
    +    protected AttributeSensor<? extends Number> sourceTotalSensor;
    +    protected AttributeSensor<Double> targetSensor;
    +    protected Entity producer;
    +
    +    @SuppressWarnings({"unchecked", "rawtypes"})
    +    @Override
    +    public void setEntity(EntityLocal entity) {
    +        super.setEntity(entity);
    +
    +        this.sourceCurrentSensor = getConfig(SOURCE_CURRENT_SENSOR);
    +        this.sourceTotalSensor = getConfig(SOURCE_TOTAL_SENSOR);
    +        this.targetSensor = getConfig(TARGET_SENSOR);
    +        this.producer = getConfig(PRODUCER) == null ? entity : getConfig(PRODUCER);
    +
    +        if (sourceCurrentSensor == null) {
    --- End diff --
    
    Better doing these at assignment as follows:
    ```Java
    sourceCurrentSensor = Preconditions.checkNotNull(getConfig(SOURCE_TOTAL_SENSOR), "Enricher %s has no %s", JavaClassNames.simpleClassName(this), SOURCE_CURRENT_SENSOR.getName());
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---