github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/public/static/js/hosts.js (about)

     1  mciModule.controller('HostsCtrl', function($scope, $filter, $window, $location) {
     2    $scope.selectedHeader = {};
     3    $scope.headerFields = [{
     4        name: 'Id',
     5        by: 'host',
     6        order: false,
     7      },
     8      {
     9        name: 'Distro',
    10        by: 'distro._id',
    11        order: false,
    12      },
    13      {
    14        name: 'Status',
    15        by: 'status',
    16        order: false,
    17      },
    18      {
    19        name: 'Current task',
    20        by: 'task',
    21        order: false,
    22      },
    23      {
    24        name: 'Elapsed',
    25        by: 'start_time',
    26        order: false,
    27      },
    28      {
    29        name: 'Uptime',
    30        by: 'creation_time',
    31        order: false,
    32      },
    33      {
    34        name: 'Owner',
    35        by: 'started_by',
    36        order: false,
    37      },
    38    ]
    39  
    40    $scope.toggleIncludeSpawnedHosts = function(includeSpawnedHosts) {
    41        $window.location.href = "/hosts?includeSpawnedHosts=" + includeSpawnedHosts;
    42    };
    43  
    44    $scope.selectedClass = function(headerField) {
    45      var newIcon = 'fa-sort';
    46      if (headerField.name == $scope.selectedHeader.name) {
    47        newIcon = 'fa-sort-up';
    48        if ($scope.selectedHeader.order) {
    49          newIcon = 'fa-sort-down';
    50        }
    51      }
    52      return newIcon;
    53    }
    54  
    55    $scope.setSelectedHeader = function(headerField) {
    56      if ($scope.selectedHeader.name == headerField.name) {
    57        $scope.selectedHeader.order = !$scope.selectedHeader.order;
    58      } else {
    59        $scope.selectedHeader = headerField;
    60        $scope.selectedHeader.order = false;
    61      }
    62    };
    63  
    64    // format displayed uptime and task elapsed time
    65    var allHosts = $window.hosts.Hosts;
    66    $scope.hosts = [];
    67    var filterOpts = $location.path().split('/');
    68    $scope.filter = {
    69      hosts : filterOpts[2] || ''
    70    };
    71    $scope.selectAll = false;
    72    $scope.filteredHosts = $scope.hosts;
    73  
    74    $scope.$watch('filter.hosts', function() {
    75      $location.path('filter/' + $scope.filter.hosts);
    76    });
    77  
    78    _.forEach(allHosts, function(hostObj) {
    79      var host = {};
    80      var hostDoc = hostObj.Host;
    81      host.host_type = hostDoc.host_type;
    82      host.distro = hostDoc.distro;
    83      host.status = hostDoc.status;
    84      host.id = hostDoc.id;
    85      host.host = hostDoc.host;
    86      host.creation_time = hostDoc.creation_time;
    87      host.started_by = hostDoc.started_by;
    88      host.task = "";
    89      host.running_task = hostObj.RunningTask;
    90  
    91      if (hostDoc.host_type !== "static") {
    92        var uptime = moment().diff(hostDoc.creation_time, 'seconds');
    93        host.uptime = moment.duration(uptime, 'seconds').humanize();
    94      } else {
    95        host.creation_time = "N/A";
    96        host.uptime = "N/A";
    97      }
    98      if (hostObj.RunningTask) {
    99        host.start_time = hostObj.RunningTask.start_time
   100        var dispatchTimeDiffedPrefix = "";
   101        // in case the task is dispatched but not yet marked as
   102        // started, use the task's 'dispatch_time' in lieu of
   103        // 'start_time'
   104        var epochTime = moment("Jan 1, 1970");
   105        var startTime = moment(hostObj.RunningTask.start_time);
   106        // 'start_time' is set to epochTime by default we use
   107        // the <= comparison to allow for conversion imprecision
   108        if (startTime <= epochTime) {
   109          host.start_time = hostObj.RunningTask.dispatch_time;
   110          startTime = hostObj.RunningTask.dispatch_time;
   111          dispatchTimeDiffedPrefix = "*";
   112        }
   113        var elapsedTime = moment().diff(startTime, 'seconds');
   114        host.task = hostObj.RunningTask.display_name;
   115        host.elapsed = dispatchTimeDiffedPrefix + moment.duration(elapsedTime, 'seconds').humanize();
   116      } else {
   117        host.start_time = "N/A";
   118        host.elapsed = "N/A";
   119      }
   120      $scope.hosts.push(host);
   121    });
   122  
   123    $scope.selectedHosts = function() {
   124      return $filter('filter')($scope.hosts, {checked: true});
   125    };
   126  
   127    $scope.toggleHostCheck = function(host) {
   128      host.checked = !host.checked;
   129    };
   130  
   131    $scope.toggleSelectAll = function() {
   132      $scope.selectAll = !$scope.selectAll;
   133      $scope.setCheckBoxes($scope.selectAll);
   134    };
   135      
   136    $scope.clearSelectAll = function() {
   137      $scope.selectAll = false;
   138      $scope.setCheckBoxes($scope.selectAll);
   139    };
   140  
   141    $scope.setCheckBoxes = function(val) {
   142      for (idx in $scope.filteredHosts) {
   143          $scope.filteredHosts[idx].checked = val;
   144      }
   145    };
   146  
   147    $scope.$watch('hosts', function(hosts) {
   148      $scope.hostCount = 0;
   149      _.forEach(hosts, function(host) {
   150        if(host.checked){
   151          $scope.hostCount += 1;
   152        }
   153      });
   154    }, true);
   155  
   156  });