Browse Source

AMBARI-15768. Ambari Widget should display metrics for available metrics even if some of the metrics it expects are not returned by API. (atkach via yusaku)

Yusaku Sako 9 years ago
parent
commit
e3edcf574c

+ 24 - 10
ambari-web/app/mixins/common/widgets/widget_mixin.js

@@ -287,6 +287,8 @@ App.WidgetMixin = Ember.Mixin.create({
    */
   getMetricsSuccessCallback: function (data) {
     var metrics = [];
+    var atLeastOneMetricPresent = false;
+
     if (this.get('content.metrics')) {
       this.get('content.metrics').forEach(function (_metric) {
         var metric_path = _metric.metric_path;
@@ -310,20 +312,32 @@ App.WidgetMixin = Ember.Mixin.create({
           }, this);
         }
         if (!Em.isNone(metric_data)) {
+          atLeastOneMetricPresent = true;
           _metric.data = metric_data;
           this.get('metrics').pushObject(_metric);
-        } else if (this.get('graphView')) {
-          var graph = this.get('childViews') && this.get('childViews').findProperty('_showMessage');
-          if (graph) {
-            graph.set('hasData', false);
-            this.set('isExportButtonHidden', true);
-            graph._showMessage('info', this.t('graphs.noData.title'), this.t('graphs.noDataAtTime.message'));
-            this.set('metrics', this.get('metrics').reject(function (item) {
-              return this.get('content.metrics').someProperty('name', item.name);
-            }, this));
-          }
         }
       }, this);
+
+      if (!atLeastOneMetricPresent) {
+        this.disableGraph();
+      }
+    }
+  },
+
+  /**
+   * if no metrics were received from server then disable graph
+   */
+  disableGraph: function() {
+    if (this.get('graphView')) {
+      var graph = this.get('childViews') && this.get('childViews').findProperty('_showMessage');
+      if (graph) {
+        graph.set('hasData', false);
+        this.set('isExportButtonHidden', true);
+        graph._showMessage('info', this.t('graphs.noData.title'), this.t('graphs.noDataAtTime.message'));
+        this.set('metrics', this.get('metrics').reject(function (item) {
+          return this.get('content.metrics').someProperty('name', item.name);
+        }, this));
+      }
     }
   },
 

+ 41 - 0
ambari-web/test/mixins/common/widget_mixin_test.js

@@ -243,6 +243,47 @@ describe('App.WidgetMixin', function () {
     });
   });
 
+  describe("#disableGraph", function () {
+    var graph = Em.Object.create({
+        _showMessage: Em.K
+      });
+
+    beforeEach(function() {
+      mixinObject.setProperties({
+        childViews: [
+          graph
+        ],
+        graphView: {},
+        metrics: [{name: 'm1'}, {name: 'm2'}],
+        content: {
+          metrics: [{name: 'm2'}]
+        }
+      });
+      sinon.stub(graph, '_showMessage');
+      mixinObject.disableGraph();
+    });
+
+    afterEach(function() {
+      graph._showMessage.restore();
+    });
+
+    it("hasData should be false", function() {
+      expect(graph.get('hasData')).to.be.false;
+    });
+
+    it("isExportButtonHidden should be true", function() {
+      expect(mixinObject.get('isExportButtonHidden')).to.be.true;
+    });
+
+    it("_showMessage should be called", function() {
+      expect(graph._showMessage.calledWith('info', mixinObject.t('graphs.noData.title'), mixinObject.t('graphs.noDataAtTime.message'))).to.be.true;
+    });
+
+    it("metrics should be filtered", function() {
+      expect(mixinObject.get('metrics').mapProperty('name')).to.be.eql(['m1']);
+    });
+  });
+
   describe("#getHostComponentMetrics()", function () {
     beforeEach(function () {
       sinon.stub(mixinObject, 'computeHostComponentCriteria').returns('criteria')