You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by al...@apache.org on 2015/03/24 23:25:51 UTC

ambari git commit: AMBARI-10194. Better Client Side Validation Of Alert Definition Threshold Data (alexantonenko)

Repository: ambari
Updated Branches:
  refs/heads/trunk 8f2232c04 -> c42db2ebe


AMBARI-10194. Better Client Side Validation Of Alert Definition Threshold Data (alexantonenko)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/c42db2eb
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/c42db2eb
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/c42db2eb

Branch: refs/heads/trunk
Commit: c42db2ebe37b4658c8b5a52024520c9ea7875d93
Parents: 8f2232c
Author: Alex Antonenko <hi...@gmail.com>
Authored: Tue Mar 24 20:58:31 2015 +0200
Committer: Alex Antonenko <hi...@gmail.com>
Committed: Wed Mar 25 00:25:46 2015 +0200

----------------------------------------------------------------------
 ambari-web/app/models/alert_config.js       | 50 +++++++++++++--
 ambari-web/app/utils/number_utils.js        | 17 +++++-
 ambari-web/test/models/alert_config_test.js | 78 +++++++++++++++++++++++-
 3 files changed, 134 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/c42db2eb/ambari-web/app/models/alert_config.js
----------------------------------------------------------------------
diff --git a/ambari-web/app/models/alert_config.js b/ambari-web/app/models/alert_config.js
index 2a78d4c..e4b72e6 100644
--- a/ambari-web/app/models/alert_config.js
+++ b/ambari-web/app/models/alert_config.js
@@ -18,6 +18,7 @@
 
 var App = require('app');
 var validator = require('utils/validator');
+var numericUtils = require('utils/number_utils');
 
 App.AlertConfigProperty = Ember.Object.extend({
 
@@ -389,10 +390,23 @@ App.AlertConfigProperties = {
      * @return {boolean}
      */
     isValid: function () {
-      if (!this.get('showInputForValue')) return true;
+      if (!this.get('showInputForValue')) {
+        return true;
+      }
+
       var value = this.get('displayValue');
-      if (Em.isNone(value)) return false;
+
+      if (Em.isNone(value)) {
+        return false;
+      }
+
       value = ('' + value).trim();
+
+      //only allow 1/10th of a second
+      if (numericUtils.getFloatDecimals(value) > 1) {
+        return false;
+      }
+
       return validator.isValidFloat(value);
     }.property('displayValue', 'showInputForValue')
 
@@ -532,9 +546,23 @@ App.AlertConfigProperties.Thresholds = {
 
     isValid: function () {
       var value = this.get('displayValue');
-      if (!value) return false;
+
+      if (!value) {
+        return false;
+      }
+
       value = ('' + value).trim();
+      if (numericUtils.getFloatDecimals(value)) {
+        return false;
+      }
+
       value = parseFloat(value);
+
+      //do not allow float values
+      if (parseInt(value, 10) !== value) {
+        return false;
+      }
+
       return this.get('showInputForValue') ? !isNaN(value) && value > 0 && value <= 100 : true;
     }.property('displayValue', 'showInputForValue'),
 
@@ -566,11 +594,23 @@ App.AlertConfigProperties.Thresholds = {
   PositiveMixin: Em.Mixin.create({
 
     isValid: function () {
-      if (!this.get('showInputForValue')) return true;
+      if (!this.get('showInputForValue')) {
+        return true;
+      }
       var value = this.get('displayValue');
-      if (!value) return false;
+
+      if (!value) {
+        return false;
+      }
+
+      //only allow 1/10th of a second
+      if (numericUtils.getFloatDecimals(value) > 1) {
+        return false;
+      }
+
       value = ('' + value).trim();
       value = parseFloat(value);
+
       return !isNaN(value) && value > 0;
     }.property('displayValue', 'showInputForValue')
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/c42db2eb/ambari-web/app/utils/number_utils.js
----------------------------------------------------------------------
diff --git a/ambari-web/app/utils/number_utils.js b/ambari-web/app/utils/number_utils.js
index 098d2a5..85c38f6 100644
--- a/ambari-web/app/utils/number_utils.js
+++ b/ambari-web/app/utils/number_utils.js
@@ -5,9 +5,9 @@
  * 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
@@ -18,7 +18,7 @@ module.exports = {
 
   /**
    * Convert byte size to other metrics.
-   * 
+   *
    * @param {Number} bytes to convert to string
    * @param {Number} precision Number to adjust precision of return value. Default is 0.
    * @param {String} parseType
@@ -103,5 +103,16 @@ module.exports = {
     } else {
       return 0;
     }
+  },
+
+  getFloatDecimals: function (number, separator) {
+    separator = separator || '.';
+
+    var value = '' + number;
+    var decimals = value.split(separator);
+
+    decimals = decimals[1] ? decimals[1].length : 0;
+
+    return decimals;
   }
 };

http://git-wip-us.apache.org/repos/asf/ambari/blob/c42db2eb/ambari-web/test/models/alert_config_test.js
----------------------------------------------------------------------
diff --git a/ambari-web/test/models/alert_config_test.js b/ambari-web/test/models/alert_config_test.js
index 933a228..31f2ce9 100644
--- a/ambari-web/test/models/alert_config_test.js
+++ b/ambari-web/test/models/alert_config_test.js
@@ -156,14 +156,86 @@ describe('App.AlertConfigProperties', function () {
         expect(model.get('isValid')).to.be.false;
       });
 
-      it('should be true if displayValue is valid float', function () {
-        model.set('displayValue', '123.456');
+      it('should be false if METRIC displayValue is not valid float', function () {
+        model.set('displayValue', '$1234.444');
+        expect(model.get('isValid')).to.be.false;
+
+        model.set('displayValue', 'hello-world!');
+        expect(model.get('isValid')).to.be.false;
+      });
+
+      it('should be true if METRIC displayValue is valid float with at most one decimal', function () {
+        model.set('displayValue', '123.4');
         expect(model.get('isValid')).to.be.true;
 
-        model.set('displayValue', '$1234.444');
+        model.set('displayValue', '123.0');
+        expect(model.get('isValid')).to.be.true;
+
+        model.set('displayValue', '666');
+        expect(model.get('isValid')).to.be.true;
+      });
+
+      it('should be false if METRIC displayValue is valid float with more than one decimal', function () {
+        model.set('displayValue', '123.48');
         expect(model.get('isValid')).to.be.false;
       });
 
+      it('should be true for AGGREGATE percentage with precision of 1', function () {
+        model = Em.Object.create(App.AlertConfigProperties.Thresholds.PercentageMixin, {
+          displayValue: '1',
+          showInputForValue: true
+        });
+
+        expect(model.get('isValid')).to.be.true;
+
+        model.set('displayValue', '88');
+        expect(model.get('isValid')).to.be.true;
+      });
+
+      it('should be false for AGGREGATE percentage values with precision smaller than 1', function () {
+        model = Em.Object.create(App.AlertConfigProperties.Thresholds.PercentageMixin, {
+          displayValue: '70.01',
+          showInputForValue: true
+        });
+
+        expect(model.get('isValid')).to.be.false;
+
+        model.set('displayValue', '70.0');
+        expect(model.get('isValid')).to.be.false;
+
+        model.set('displayValue', '80.000');
+        expect(model.get('isValid')).to.be.false;
+      });
+
+      it('should be true for PORT percentage values with precision of 1/10th', function () {
+        model = App.AlertConfigProperties.Threshold.create({
+          value: '0.4',
+          showInputForValue: true
+        })
+
+        expect(model.get('isValid')).to.be.true;
+
+        model.set('value', '3');
+        expect(model.get('isValid')).to.be.true;
+
+        model.set('value', '33.0');
+        expect(model.get('isValid')).to.be.true;
+      });
+
+      it('should be false for PORT percentage values with precision greater than 1/10th', function() {
+        model = App.AlertConfigProperties.Threshold.create({
+          value: '4.234',
+          showInputForValue: true
+        });
+
+        expect(model.get('isValid')).to.be.false;
+
+        model.set('value', '44.001');
+        expect(model.get('isValid')).to.be.false;
+
+        model.set('value', '112.01');
+        expect(model.get('isValid')).to.be.false;
+      });
 
     });