/**
 * Operation 2.8
 */

angular.module(PKG.name+'.feature.dashboard')
/* ------------------------------------------------------ */

  .controller('Op28CdapCtrl',
  function ($scope, $state, op28helper, MyDataSource) {

    var dataSrc = new MyDataSource($scope);

    $scope.panels = [
      ['Collect', 'EPS',   'collect.events'],
      ['Process', '%',     'process.busyness'],
      ['Store',   '/S',    'dataset.store.bytes', true],
      ['Query',   'QPS',   'query.requests']
    ].map(op28helper.panelMap);

    angular.forEach($scope.panels, function (panel) {
      var c = panel.chart;
      dataSrc.poll({
          _cdapPathV2: '/metrics/system/' +
              c.metric + '?start=now-60s&end=now',
          method: 'GET'
        },
        op28helper.pollCb.bind(c)
      );
    });

  })


/* ------------------------------------------------------ */


  .controller('Op28SystemCtrl',
  function ($scope, op28helper, MyDataSource) {

    var dataSrc = new MyDataSource($scope);

    $scope.panels = [
      ['AppFabric',  'Containers', 'containers'],
      ['Processors', 'Cores',      'vcores'],
      ['Memory',     '',           'memory', true],
      ['DataFabric', '',           'storage', true]
    ].map(op28helper.panelMap);

    angular.forEach($scope.panels, function (panel) {
      var c = panel.chart;
      dataSrc.poll({
          _cdapPathV2: '/metrics/system/resources.used.' +
                        c.metric +
                        '?start=now-60s&end=now',
          method: 'GET'
        },
        op28helper.pollCb.bind(c)
      );
    });

  })


/* ------------------------------------------------------ */


  .controller('Op28AppsCtrl',
  function ($scope, $state, myHelpers, MyDataSource) {

    var dataSrc = new MyDataSource($scope);

    $scope.apps = [];

    dataSrc
      .request({
        _cdapNsPath: '/apps'
      },
      function (apps) {
        $scope.apps = apps;

        var m = ['vcores', 'containers', 'memory'];

        for (var i = 0; i < m.length; i++) {

          dataSrc
            .poll({
              _cdapPath: '/metrics/query' +
                '?context=namespace.system' +
                '&metric=system.resources.used.' +
                m[i] + '&groupBy=app',
              method: 'POST'
            }, setMetric);
        }

      });

    function setMetric(r) {

      angular.forEach($scope.apps, function (app) {
        angular.forEach(r.series, function (s) {
          if(app.id === s.grouping.app) {
            myHelpers.deepSet(
              app,
              'metric.' + s.metricName.split('.').pop(),
              s.data[0].value
            );
          }
        });
      });

    }

  })


/* ------------------------------------------------------ */


  .factory('op28helper', function () {

    function panelMap (d) {
      return {
        title: d[0],
        unit: d[1],
        useByteFilter: d[3],
        chart: {
          metric: d[2],
          context: 'system',
          history: null,
          stream: null,
          lastValue: 0
        }
      };
    }


    function pollCb (res) {
      var v = res.data.map(function (o) {
        return {
          time: o.time,
          y: o.value
        };
      });
      if(this.history) {
        this.stream = v.slice(-1); // because poll is once per second
      }
      this.history = [{
        label: this.metric,
        values: v
      }];
      this.lastValue = res.data.pop().value;
    }

    return {
      panelMap: panelMap,
      pollCb: pollCb
    };

  })

  ;

@@ -250,32 +250,106 @@
 Source($scope);%0A
+    // waiting for system.request.received to be changed to query requests
 %0A    $scope.pane
@@ -383,16 +383,23 @@
 PS',   '
+system.
 collect.
@@ -431,17 +431,22 @@
 ', '
-%25
+events
 ',     '
 proc
@@ -445,16 +445,23 @@
    '
+system.
 process.
 busy
@@ -460,16 +460,24 @@
 ess.
-busyn
+events.proc
 ess
+ed
 '%5D,%0A
@@ -504,16 +504,23 @@
 S',    '
+system.
 dataset.
@@ -572,21 +572,22 @@
    '
-query
+system
 .request
 s'%5D%0A
@@ -582,17 +582,25 @@
 .request
-s
+.received
 '%5D%0A    %5D
@@ -700,32 +700,66 @@
  = panel.chart;%0A
+      console.log('c', c.metric);%0A
       dataSrc.po
@@ -774,34 +774,32 @@
        _cdapPath
-V2
 : '/metrics/syst
@@ -794,23 +794,29 @@
 metrics/
-system/
+query?metric=
 ' +%0A    
@@ -837,17 +837,17 @@
 tric + '
-?
+&
 start=no
@@ -854,32 +854,58 @@
 w-60s&end=now',%0A
+          interval: 1000,%0A
           method
@@ -899,34 +899,35 @@
        method: '
-GE
+POS
 T'%0A        %7D,%0A  
@@ -1226,24 +1226,46 @@
 ntainers', '
+system.resources.used.
 containers'%5D
@@ -1302,16 +1302,38 @@
 ,      '
+system.resources.used.
 vcores'%5D
@@ -1370,16 +1370,38 @@
        '
+system.resources.used.
 memory',
@@ -1444,16 +1444,38 @@
        '
+system.resources.used.
 storage'
@@ -1640,10 +1640,8 @@
 Path
-V2
 : '/
@@ -1652,29 +1652,15 @@
 ics/
-system/resources.used
+metric?
 .' +
@@ -1720,17 +1720,17 @@
        '
-?
+&
 start=no
@@ -1737,32 +1737,63 @@
 w-60s&end=now',%0A
+          interval: 60 * 1000,%0A
           method
@@ -1799,10 +1799,11 @@
 d: '
-GE
+POS
 T'%0A 
@@ -3074,16 +3074,25 @@
 nction (
+myHelpers
 ) %7B%0A%0A   
@@ -3412,20 +3412,131 @@
 var 
-v = res.data
+result = myHelpers.objectQuery(res, 'series', 0, 'data');%0A      if (!result) %7B%0A        return;%0A      %7D%0A      var v = result
 .map
@@ -3844,16 +3844,9 @@
 e = 
-res.data
+v
 .pop

change metric API to v3