angular.module(PKG.name + '.feature.flows')
  .controller('FlowletDetailOutputController', function($state, $scope, MyDataSource, MyMetricsQueryHelper, myFlowsApi) {

    var dataSrc = new MyDataSource($scope);
    var flowletid = $scope.$parent.activeFlowlet.name;
    var runid = $scope.runs.selected.runid;
    $scope.outputs = [];

    var params = {
      namespace: $state.params.namespace,
      appId: $state.params.appId,
      flowId: $state.params.programId,
      scope: $scope
    };

    var flowletTags = {
      namespace: $state.params.namespace,
      app: $state.params.appId,
      flow: $state.params.programId,
      run: runid,
      flowlet: flowletid
    };

    myFlowsApi.get(params)
      .$promise
      .then(function (res) {

        // OUTPUTS
        angular.forEach(res.connections, function(v) {
          if (v.sourceName === flowletid) {
            $scope.outputs.push(v.targetName);
          }
        });

        if ($scope.outputs.length > 0) {
          // OUTPUT METRICS
          dataSrc
            .poll({
              _cdapPath: '/metrics/query?' + MyMetricsQueryHelper.tagsToParams(flowletTags)
                            + '&metric=system.process.events.out&start=now-60s&count=60',
              method: 'POST'
            }, function(res) {
              if (res.series[0]) {
                updateOutput(res.series[0].data);
              } else {
                var val = [];

                for (var i = 60; i > 0; i--) {
                  val.push({
                    time: Math.floor((new Date()).getTime()/1000 - (i)),
                    y: 0
                  });
                }

                if ($scope.outputHistory) {
                  $scope.outputStream = val.slice(-1);
                }

                $scope.outputHistory = [{
                  label: 'output',
                  values: val
                }];

              }
            });

          // Total
          dataSrc
            .poll({
              _cdapPath: '/metrics/query?' + MyMetricsQueryHelper.tagsToParams(flowletTags)
                            + '&metric=system.process.events.out',
              method: 'POST'
            }, function(res) {
              if (res.series[0]) {
                $scope.total = res.series[0].data[0].value;
              }
            });

        }

      });

    function updateOutput(newVal) {
      if(angular.isObject(newVal)) {
        var v = [];

        angular.forEach(newVal, function(val) {
          v.push({
            time: val.time,
            y: val.value
          });
        });

        if ($scope.outputHistory) {
          $scope.outputStream = v.slice(-1);
        }

        $scope.outputHistory = [
          {
            label: 'output',
            values: v
          }
        ];

      }
    }

  });

@@ -1307,609 +1307,26 @@
     
-if (res.series%5B0%5D) %7B%0A                updateOutput(res.series%5B0%5D.data);%0A              %7D else %7B%0A                var val = %5B%5D;%0A%0A                for (var i = 60; i %3E 0; i--) %7B%0A                  val.push(%7B%0A                    time: Math.floor((new Date()).getTime()/1000 - (i)),%0A                    y: 0%0A                  %7D);%0A                %7D%0A%0A                if ($scope.outputHistory) %7B%0A                  $scope.outputStream = val.slice(-1);%0A                %7D%0A%0A                $scope.outputHistory = %5B%7B%0A                  label: 'output',%0A                  values: val%0A                %7D%5D;%0A%0A              %7D
+updateOutput(res);
 %0A   
@@ -1791,22 +1791,19 @@
 eOutput(
-newVal
+res
 ) %7B%0A    
@@ -1808,59 +1808,47 @@
     
-if(angular.isObject(newVal)) %7B%0A        var v = %5B%5D;%0A
+var v = %5B%5D;%0A%0A      if (res.series%5B0%5D) %7B
 %0A   
@@ -1872,14 +1872,26 @@
 ach(
-newVal
+res.series%5B0%5D.data
 , fu
@@ -1994,35 +1994,220 @@
 %7D);%0A        %7D);%0A
-%0A
   
+    %7D else %7B%0A        for (var i = 60; i %3E 0; i--) %7B%0A          v.push(%7B%0A            time: Math.floor((new Date()).getTime()/1000 - (i)),%0A            y: 0%0A          %7D);%0A        %7D%0A      %7D%0A%0A
       if ($scope
@@ -2220,26 +2220,24 @@
 tHistory) %7B%0A
-  
         $sco
@@ -2267,18 +2267,16 @@
 ce(-1);%0A
-  
       %7D%0A
@@ -2278,26 +2278,24 @@
    %7D%0A%0A      
-  
 $scope.outpu
@@ -2306,27 +2306,16 @@
 tory = %5B
-%0A          
 %7B%0A      
@@ -2316,20 +2316,16 @@
         
- 
-   
 label: '
@@ -2329,28 +2329,24 @@
 : 'output',%0A
-    
         valu
@@ -2361,33 +2361,12 @@
     
-    %7D%0A        %5D;%0A%0A      %7D
+%7D%5D;%0A
 %0A   

refactor flowlet output code addressing comments