Ver Fonte

AMBARI-8414. Alerts UI: thresholds property should be taken from API. (akovalenko)

Aleksandr Kovalenko há 10 anos atrás
pai
commit
2b2accd9ca

+ 62 - 28
ambari-web/app/controllers/main/alerts/definition_configs_controller.js

@@ -50,6 +50,32 @@ App.MainAlertDefinitionConfigsController = Em.Controller.extend({
     return App.AggregateAlertDefinition.find().mapProperty('name');
   }.property(),
 
+  /**
+   * Compute thresholds from value using <code>content.reporting</code>
+   * @type {Number|Null}
+   */
+  thresholdsFrom: function () {
+    var warning = this.get('content.reporting').findProperty('type', 'warning');
+    if (warning && warning.get('value')) {
+      return warning.get('value');
+    } else {
+      return null;
+    }
+  }.property('content.reporting.@each.value'),
+
+  /**
+   * Compute thresholds to value using <code>content.reporting</code>
+   * @type {Number|Null}
+   */
+  thresholdsTo: function () {
+    var critical = this.get('content.reporting').findProperty('type', 'critical');
+    if (critical && critical.get('value')) {
+      return critical.get('value');
+    } else {
+      return null;
+    }
+  }.property('content.reporting.@each.value'),
+
   /**
    * Change options of "Component", after changing value of "Service" config
    */
@@ -120,9 +146,9 @@ App.MainAlertDefinitionConfigsController = Em.Controller.extend({
         value: alertDefinition.get('interval')
       }),
       App.AlertConfigProperties.Thresholds.create({
-        value: alertDefinition.get('thresholds'),
-        from: alertDefinition.get('thresholds').split('-')[0],
-        to: alertDefinition.get('thresholds').split('-')[1]
+        value: this.get('thresholdsFrom') + '-' + this.get('thresholdsTo'),
+        from: this.get('thresholdsFrom'),
+        to: this.get('thresholdsTo')
       }),
       App.AlertConfigProperties.URI.create({
         value: alertDefinition.get('uri')
@@ -161,9 +187,9 @@ App.MainAlertDefinitionConfigsController = Em.Controller.extend({
         value: alertDefinition.get('interval')
       }),
       App.AlertConfigProperties.Thresholds.create({
-        value: alertDefinition.get('thresholds'),
-        from: alertDefinition.get('thresholds').split('-')[0],
-        to: alertDefinition.get('thresholds').split('-')[1]
+        value: this.get('thresholdsFrom') + '-' + this.get('thresholdsTo'),
+        from: this.get('thresholdsFrom'),
+        to: this.get('thresholdsTo')
       }),
       App.AlertConfigProperties.URIExtended.create({
         value: JSON.stringify({
@@ -212,9 +238,9 @@ App.MainAlertDefinitionConfigsController = Em.Controller.extend({
         value: alertDefinition.get('interval')
       }),
       App.AlertConfigProperties.Thresholds.create({
-        value: alertDefinition.get('thresholds'),
-        from: alertDefinition.get('thresholds').split('-')[0],
-        to: alertDefinition.get('thresholds').split('-')[1]
+        value: this.get('thresholdsFrom') + '-' + this.get('thresholdsTo'),
+        from: this.get('thresholdsFrom'),
+        to: this.get('thresholdsTo')
       }),
       App.AlertConfigProperties.URIExtended.create({
         value: JSON.stringify({
@@ -255,9 +281,9 @@ App.MainAlertDefinitionConfigsController = Em.Controller.extend({
         value: alertDefinition.get('interval')
       }),
       App.AlertConfigProperties.Thresholds.create({
-        value: alertDefinition.get('thresholds'),
-        from: alertDefinition.get('thresholds').split('-')[0],
-        to: alertDefinition.get('thresholds').split('-')[1]
+        value: this.get('thresholdsFrom') + '-' + this.get('thresholdsTo'),
+        from: this.get('thresholdsFrom'),
+        to: this.get('thresholdsTo')
       }),
       App.AlertConfigProperties.Path.create({
         value: alertDefinition.get('location')
@@ -330,24 +356,32 @@ App.MainAlertDefinitionConfigsController = Em.Controller.extend({
   getPropertiesToUpdate: function () {
     var propertiesToUpdate = {};
     this.get('configs').filterProperty('wasChanged').forEach(function (property) {
-      if (property.get('apiProperty').contains('source.')) {
-        if (!propertiesToUpdate['AlertDefinition/source']) {
-          propertiesToUpdate['AlertDefinition/source'] = this.get('content.rawSourceData');
-        }
-
-        var sourcePath = propertiesToUpdate['AlertDefinition/source'];
-        property.get('apiProperty').replace('source.', '').split('.').forEach(function (path, index, array) {
-          // check if it is last path
-          if (array.length - index === 1) {
-            sourcePath[path] = property.get('apiFormattedValue');
-          } else {
-            sourcePath = sourcePath[path];
+      var apiProperties = property.get('apiProperty');
+      var apiFormattedValues = property.get('apiFormattedValue');
+      if (!Em.isArray(property.get('apiProperty'))) {
+        apiProperties = [property.get('apiProperty')];
+        apiFormattedValues = [property.get('apiFormattedValue')];
+      }
+      apiProperties.forEach(function (apiProperty, i) {
+        if (apiProperty.contains('source.')) {
+          if (!propertiesToUpdate['AlertDefinition/source']) {
+            propertiesToUpdate['AlertDefinition/source'] = this.get('content.rawSourceData');
           }
-        });
 
-      } else {
-        propertiesToUpdate['AlertDefinition/' + property.get('apiProperty')] = property.get('apiFormattedValue');
-      }
+          var sourcePath = propertiesToUpdate['AlertDefinition/source'];
+          apiProperty.replace('source.', '').split('.').forEach(function (path, index, array) {
+            // check if it is last path
+            if (array.length - index === 1) {
+              sourcePath[path] = apiFormattedValues[i];
+            } else {
+              sourcePath = sourcePath[path];
+            }
+          });
+
+        } else {
+          propertiesToUpdate['AlertDefinition/' + apiProperty] = apiFormattedValues[i];
+        }
+      }, this);
     }, this);
 
     return propertiesToUpdate;

+ 17 - 5
ambari-web/app/models/alert_config.js

@@ -70,8 +70,11 @@ App.AlertConfigProperty = Ember.Object.extend({
   classNames: '',
 
   /**
-   * name of property related to config
-   * @type {String}
+   * name or names of properties related to config
+   * may be either string for one property or array of strings for multiple properties
+   * if this property is array, then <code>apiFormattedValue</code> should also be an array
+   * example: <code>apiProperty[0]</code> relates to <code>apiFormattedValue[0]</code>
+   * @type {String|Array}
    */
   apiProperty: '',
 
@@ -180,8 +183,6 @@ App.AlertConfigProperties = {
     from: '',
     to: '',
     value: '',
-    // todo: check value after API will be provided
-    apiProperty: 'thresholds',
 
     setFromTo: function () {
       this.set('doNotChangeValue', true);
@@ -197,7 +198,18 @@ App.AlertConfigProperties = {
     }.observes('from', 'to'),
 
     // flag for providing correct from, to and value recomputing
-    doNotChangeValue: false
+    doNotChangeValue: false,
+
+    apiProperty: [
+      'source.reporting.warning.value',
+      'source.reporting.critical.value'
+    ],
+    apiFormattedValue: function () {
+      return [
+          +this.get('from'),
+          +this.get('to')
+      ]
+    }.property('from', 'to')
   }),
   URI: App.AlertConfigProperty.extend({
     label: 'URI',

+ 70 - 4
ambari-web/test/controllers/main/alerts/definitions_configs_controller_test.js

@@ -102,7 +102,20 @@ describe('App.MainAlertDefinitionConfigsController', function () {
         scope: 'HOST',
         description: 'alertDefinitionDescription',
         interval: 60,
-        thresholds: '10-20',
+        reporting: [
+          Em.Object.create({
+            type: 'warning',
+            value: 10
+          }),
+          Em.Object.create({
+            type: 'critical',
+            value: 20
+          }),
+          Em.Object.create({
+            type: 'ok',
+            value: 30
+          })
+        ],
         uri: 'alertDefinitionUri',
         defaultPort: '777'
       }));
@@ -134,7 +147,20 @@ describe('App.MainAlertDefinitionConfigsController', function () {
         scope: 'HOST',
         description: 'alertDefinitionDescription',
         interval: 60,
-        thresholds: '10-20',
+        reporting: [
+          Em.Object.create({
+            type: 'warning',
+            value: 10
+          }),
+          Em.Object.create({
+            type: 'critical',
+            value: 20
+          }),
+          Em.Object.create({
+            type: 'ok',
+            value: 30
+          })
+        ],
         uri: {
           "http": "{{mapred-site/mapreduce.jobhistory.webapp.address}}",
           "https": "{{mapred-site/mapreduce.jobhistory.webapp.https.address}}",
@@ -180,7 +206,20 @@ describe('App.MainAlertDefinitionConfigsController', function () {
         scope: 'HOST',
         description: 'alertDefinitionDescription',
         interval: 60,
-        thresholds: '10-20',
+        reporting: [
+          Em.Object.create({
+            type: 'warning',
+            value: 10
+          }),
+          Em.Object.create({
+            type: 'critical',
+            value: 20
+          }),
+          Em.Object.create({
+            type: 'ok',
+            value: 30
+          })
+        ],
         uri: {
           "http": "{{mapred-site/mapreduce.jobhistory.webapp.address}}",
           "https": "{{mapred-site/mapreduce.jobhistory.webapp.https.address}}",
@@ -216,7 +255,20 @@ describe('App.MainAlertDefinitionConfigsController', function () {
         scope: 'HOST',
         description: 'alertDefinitionDescription',
         interval: 60,
-        thresholds: '10-20',
+        reporting: [
+          Em.Object.create({
+            type: 'warning',
+            value: 10
+          }),
+          Em.Object.create({
+            type: 'critical',
+            value: 20
+          }),
+          Em.Object.create({
+            type: 'ok',
+            value: 30
+          })
+        ],
         location: 'path to script'
       }));
 
@@ -389,6 +441,20 @@ describe('App.MainAlertDefinitionConfigsController', function () {
             }
           }
         }
+      },
+      {
+        m: 'should correctly multiple apiProperties',
+        configs: [
+          Em.Object.create({
+            wasChanged: true,
+            apiProperty: ['name1', 'name2'],
+            apiFormattedValue: ['value1', 'value2']
+          })
+        ],
+        result: {
+          'AlertDefinition/name1': 'value1',
+          'AlertDefinition/name2': 'value2'
+        }
       }
     ];