brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rich...@apache.org
Subject [43/50] [abbrv] git commit: add function for converting to string, and use that for the "cpu fraction" sensor
Date Fri, 23 May 2014 16:57:28 GMT
add function for converting to string, and use that for the "cpu fraction" sensor


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/17ac8986
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/17ac8986
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/17ac8986

Branch: refs/pull/1414/merge
Commit: 17ac89862c4fd89fb35c35a816b9967ab50b455e
Parents: 37bf67a
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Fri May 23 10:28:04 2014 -0600
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Fri May 23 10:28:04 2014 -0600

----------------------------------------------------------------------
 .../java/brooklyn/entity/java/JavaAppUtils.java |  5 ++--
 .../java/brooklyn/util/math/MathFunctions.java  | 24 +++++++++++++++++++-
 .../brooklyn/util/math/MathFunctionsTest.java   | 18 +++++++++++++++
 3 files changed, 43 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/17ac8986/software/base/src/main/java/brooklyn/entity/java/JavaAppUtils.java
----------------------------------------------------------------------
diff --git a/software/base/src/main/java/brooklyn/entity/java/JavaAppUtils.java b/software/base/src/main/java/brooklyn/entity/java/JavaAppUtils.java
index b5bfb31..eb6d2e5 100644
--- a/software/base/src/main/java/brooklyn/entity/java/JavaAppUtils.java
+++ b/software/base/src/main/java/brooklyn/entity/java/JavaAppUtils.java
@@ -216,7 +216,6 @@ public class JavaAppUtils {
     private static final AtomicBoolean initialized = new AtomicBoolean(false);
 
     /** Setup renderer hints for the MXBean attributes. */
-    @SuppressWarnings("rawtypes")
     public static void init() {
         if (initialized.get()) return;
         synchronized (initialized) {
@@ -233,8 +232,8 @@ public class JavaAppUtils {
             RendererHints.register(UsesJavaMXBeans.START_TIME, RendererHints.displayValue(Time.toDateString()));
             RendererHints.register(UsesJavaMXBeans.UP_TIME, RendererHints.displayValue(Duration.millisToStringRounded()));
             RendererHints.register(UsesJavaMXBeans.PROCESS_CPU_TIME, RendererHints.displayValue(Duration.millisToStringRounded()));
-            RendererHints.register(UsesJavaMXBeans.PROCESS_CPU_TIME_FRACTION_LAST, RendererHints.displayValue(Duration.millisToStringRounded()));
-            RendererHints.register(UsesJavaMXBeans.PROCESS_CPU_TIME_FRACTION_IN_WINDOW, RendererHints.displayValue(Duration.millisToStringRounded()));
+            RendererHints.register(UsesJavaMXBeans.PROCESS_CPU_TIME_FRACTION_LAST, RendererHints.displayValue(MathFunctions.percent(4)));
+            RendererHints.register(UsesJavaMXBeans.PROCESS_CPU_TIME_FRACTION_IN_WINDOW, RendererHints.displayValue(MathFunctions.percent(4)));
 
             initialized.set(true);
         }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/17ac8986/utils/common/src/main/java/brooklyn/util/math/MathFunctions.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/brooklyn/util/math/MathFunctions.java b/utils/common/src/main/java/brooklyn/util/math/MathFunctions.java
index c70cdfc..dbf5604 100644
--- a/utils/common/src/main/java/brooklyn/util/math/MathFunctions.java
+++ b/utils/common/src/main/java/brooklyn/util/math/MathFunctions.java
@@ -2,6 +2,8 @@ package brooklyn.util.math;
 
 import javax.annotation.Nullable;
 
+import brooklyn.util.text.Strings;
+
 import com.google.common.base.Function;
 
 public class MathFunctions {
@@ -61,5 +63,25 @@ public class MathFunctions {
             }
         };
     }
-    
+
+    /** returns a string of up to maxLen length (longer in extreme cases) also capped at
significantDigits significantDigits */
+    public static Function<Number, String> readableString(final int significantDigits,
final int maxLen) {
+        return new Function<Number, String>() {
+            public String apply(@Nullable Number input) {
+                if (input==null) return null;
+                return Strings.makeRealString(input.doubleValue(), maxLen, significantDigits,
0);
+            }
+        };
+    }
+
+    /** returns a string where the input number is expressed as percent, with given number
of significant digits */
+    public static Function<Number, String> percent(final int significantDigits) {
+        return new Function<Number, String>() {
+            public String apply(@Nullable Number input) {
+                if (input==null) return null;
+                return readableString(significantDigits, significantDigits+3).apply(input.doubleValue()
* 100d)+"%";
+            }
+        };
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/17ac8986/utils/common/src/test/java/brooklyn/util/math/MathFunctionsTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/brooklyn/util/math/MathFunctionsTest.java b/utils/common/src/test/java/brooklyn/util/math/MathFunctionsTest.java
new file mode 100644
index 0000000..6f0955d
--- /dev/null
+++ b/utils/common/src/test/java/brooklyn/util/math/MathFunctionsTest.java
@@ -0,0 +1,18 @@
+package brooklyn.util.math;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class MathFunctionsTest {
+
+    @Test
+    public void testAdd() {
+        Assert.assertEquals(MathFunctions.plus(3).apply(4), (Integer)7);
+        Assert.assertEquals(MathFunctions.plus(0.3).apply(0.4).doubleValue(), 0.7, 0.00000001);
+    }
+    
+    @Test
+    public void testPercent() {
+        Assert.assertEquals(MathFunctions.percent(3).apply(0.0123456), "1.23%");
+    }
+}


Mime
View raw message