all files / src/redux/reducers/ clusters.js

56.95% Statements 86/151
41.67% Branches 35/84
53.33% Functions 8/15
60.58% Lines 83/137
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320                                                                                                                                                          76× 76×   76×                                                                                                                                                                                                                                                                                                                         67×      
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.clusterTemplate = exports.initialState = undefined;
exports.default = clustersReducer;
 
var _deepClone = require('mout/src/lang/deepClone');
 
var _deepClone2 = _interopRequireDefault(_deepClone);
 
var _set = require('mout/src/object/set');
 
var _set2 = _interopRequireDefault(_set);
 
var _PageWithMenu = require('HPCCloudStyle/PageWithMenu.mcss');
 
var _PageWithMenu2 = _interopRequireDefault(_PageWithMenu);
 
var _clusters = require('../actions/clusters');
 
var Actions = _interopRequireWildcard(_clusters);
 
function _interopRequireWildcard(obj) { Eif (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
 
var initialState = exports.initialState = {
  list: [],
  active: 0,
  pending: false,
  presets: {},
  mapById: {}
};
 
var clusterTemplate = exports.clusterTemplate = {
  name: 'new cluster',
  type: 'trad',
  classPrefix: _PageWithMenu2.default.statusCreatingIcon,
  log: [],
  config: {
    host: 'localhost',
    ssh: {
      user: 'Your_Login'
    },
    scheduler: {
      type: 'sge'
    },
    parallelEnvironment: '',
    numberOfSlots: 1,
    jobOutputDir: '/tmp',
    paraview: {
      installDir: '/opt/paraview'
    },
    hydra: {
      executablePath: '/some/path/fake'
    }
  }
};
 
function applyPreset(obj) {
  var preset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
 
  if (preset) {
    Object.keys(preset).forEach(function (key) {
      (0, _set2.default)(obj, key, preset[key]);
    });
  }
 
  return obj;
}
 
var STATUS_TO_ICON = {
  undefined: _PageWithMenu2.default.statusNeedSave,
  error: _PageWithMenu2.default.statusErrorIcon,
  creating: _PageWithMenu2.default.statusCreatingIcon,
  created: _PageWithMenu2.default.statusCreatedIcon,
  launching: _PageWithMenu2.default.statusLaunchingIcon,
  running: _PageWithMenu2.default.statusRunningIcon,
  terminated: _PageWithMenu2.default.statusTerminatedIcon
};
 
var tradFilter = function tradFilter(el) {
  return el.type === 'trad';
};
 
function updateIcon(clusters) {
  clusters.forEach(function (cluster) {
    // Debug
    Iif (!STATUS_TO_ICON[cluster.status]) {
      console.log('missing icon for:', cluster.status);
    }
 
    cluster.classPrefix = STATUS_TO_ICON[cluster.status];
  });
}
 
function clustersReducer() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
  var action = arguments[1];
 
  switch (action.type) {
    case Actions.ADD_CLUSTER:
      {
        return Object.assign({}, state, {
          list: [].concat(state.list, (0, _deepClone2.default)(clusterTemplate)),
          active: state.list.filter(tradFilter).length
        });
      }
 
    case Actions.ADD_EXISTING_CLUSTER:
      {
        var newCluster = action.cluster;
        var list = [].concat(state.list);
        var mapById = Object.assign({}, state.mapById);
 
        mapById[newCluster._id] = newCluster;
        mapById[newCluster._id].log = [];
 
        Iif (newCluster.type === 'trad' && list.some(function (el) {
          return el._id === newCluster._id;
        })) {
          for (var i = 0; i < list.length; i++) {
            if (list[i]._id === newCluster._id) {
              list[i] = newCluster;
              updateIcon(list);
              break;
            }
          }
        } else Eif (newCluster.type === 'trad') {
          list.push(newCluster);
        }
        return Object.assign({}, state, { list: list, mapById: mapById });
      }
 
    case Actions.REMOVE_CLUSTER:
      {
        var _list = state.list.filter(function (item, idx) {
          return idx !== action.index;
        });
        var cluster = state.list.filter(function (item, idx) {
          return idx === action.index;
        })[0];
        var active = state.active < _list.length ? state.active : _list.length - 1;
        var newState = Object.assign({}, state, { list: _list, active: active });
 
        if (cluster && cluster._id && state.mapById[cluster._id]) {
          var _mapById = Object.assign({}, state.mapById);
          delete _mapById[cluster._id];
          return Object.assign(newState, { mapById: _mapById });
        }
        return newState;
      }
 
    case Actions.REMOVE_CLUSTER_BY_ID:
      {
        var id = action.id;
        var _list2 = state.list.filter(function (item) {
          return item._id !== id;
        });
        var _active = state.active < _list2.length ? state.active : _list2.length - 1;
        var _mapById2 = Object.assign({}, state.mapById);
        delete _mapById2[id];
        return Object.assign({}, state, { mapById: _mapById2, list: _list2, active: _active });
      }
 
    case Actions.UPDATE_ACTIVE_CLUSTER:
      {
        return Object.assign({}, state, { active: action.index });
      }
 
    case Actions.UPDATE_EXISTING_CLUSTER:
      {
        var _mapById3 = Object.assign({}, state.mapById);
        Iif (!action.cluster.log) {
          action.cluster.log = [].concat(_mapById3[action.cluster._id].log);
        }
        _mapById3[action.cluster._id] = action.cluster;
        return Object.assign({}, state, { mapById: _mapById3 });
      }
 
    case Actions.UPDATE_CLUSTERS:
      {
        // do not save ec2 clusters in the list, it's only used for trad clusters
        var _list3 = action.clusters.filter(function (cluster) {
          return cluster.type === 'trad';
        });
        var _active2 = state.active < _list3.length ? state.active : _list3.length - 1;
        var _mapById4 = Object.assign({}, state.mapById);
        updateIcon(_list3);
        action.clusters.forEach(function (cluster) {
          Eif (cluster._id && !_mapById4[cluster._id]) {
            _mapById4[cluster._id] = cluster;
          }
        });
        return Object.assign({}, state, { list: _list3, active: _active2, mapById: _mapById4 });
      }
 
    case Actions.UPDATE_CLUSTER_PRESETS:
      {
        return Object.assign({}, state, { presets: action.presets });
      }
 
    case Actions.SAVE_CLUSTER:
      {
        var index = action.index,
            _cluster = action.cluster;
 
        updateIcon([_cluster]);
 
        var _list4 = [].concat(state.list.slice(0, index), _cluster, state.list.slice(index + 1));
        var _active3 = state.active < _list4.length ? state.active : _list4.length - 1;
 
        if (_cluster._id) {
          var _mapById5 = Object.assign({}, state.mapById, _defineProperty({}, _cluster._id, _cluster));
          return Object.assign({}, state, { list: _list4, active: _active3, mapById: _mapById5 });
        }
 
        return Object.assign({}, state, { list: _list4, active: _active3 });
      }
 
    case Actions.UPDATE_CLUSTER_STATUS:
      {
        var _list5 = [].concat(state.list);
        var _mapById6 = Object.assign({}, state.mapById);
        var _cluster2 = Object.assign({}, state.mapById[action.id]);
        _cluster2.status = action.status;
        _mapById6[action.id] = _cluster2;
        Eif (_cluster2.type === 'trad') {
          for (var _i = 0; _i < _list5.length; _i++) {
            Eif (_list5[_i]._id === action.id) {
              _list5[_i] = _cluster2;
              updateIcon(_list5);
              break;
            }
          }
        }
        return Object.assign({}, state, { list: _list5, mapById: _mapById6 });
      }
 
    case Actions.CLUSTER_APPLY_PRESET:
      {
        var _index = action.index,
            name = action.name;
 
        var _cluster3 = applyPreset(Object.assign({}, state.list[_index]), state.presets[name]);
        var _list6 = [].concat(state.list.slice(0, _index), _cluster3, state.list.slice(_index + 1));
        var _active4 = state.active < _list6.length ? state.active : _list6.length - 1;
 
        if (_cluster3._id) {
          var _mapById7 = Object.assign({}, state.mapById, _defineProperty({}, _cluster3._id, _cluster3));
          return Object.assign({}, state, { list: _list6, active: _active4, mapById: _mapById7 });
        }
 
        return Object.assign({}, state, { list: _list6, active: _active4 });
      }
 
    case Actions.PENDING_CLUSTER_NETWORK:
      {
        return Object.assign({}, state, { pending: action.pending });
      }
 
    case Actions.TESTING_CLUSTER:
      {
        var _index2 = action.index;
        var _cluster4 = Object.assign({}, state.list[_index2], {
          classPrefix: _PageWithMenu2.default.statusTestingIcon
        });
        var _list7 = [].concat(state.list.slice(0, _index2), _cluster4, state.list.slice(_index2 + 1));
 
        return Object.assign({}, state, { list: _list7 });
      }
 
    case Actions.APPEND_TO_CLUSTER_LOG:
      {
        var _mapById8 = Object.assign({}, state.mapById);
        var _cluster5 = Object.assign({}, state.mapById[action.id]);
        Iif (!_cluster5.log) {
          _cluster5.log = [];
        }
        Iif (Array.isArray(action.logEntry)) {
          _cluster5.log = _cluster5.log.concat(action.logEntry);
        } else {
          _cluster5.log.push(action.logEntry);
        }
        _mapById8[action.id] = _cluster5;
        return Object.assign({}, state, { mapById: _mapById8 });
      }
 
    case Actions.UPDATE_CLUSTER_LOG:
      {
        var _mapById9 = Object.assign({}, state.mapById);
        var _cluster6 = Object.assign({}, state.mapById[action.id]);
        _cluster6.log = action.log;
        _mapById9[action.id] = _cluster6;
        return Object.assign({}, state, { mapById: _mapById9 });
      }
 
    case Actions.RESTRICTED_CLUSTER_LOG:
      {
        var _mapById10 = Object.assign({}, state.mapById);
        var _cluster7 = Object.assign({}, state.mapById[action.id]);
        _cluster7.log = [{
          created: Date.now() / 1000,
          levelname: 'INFO',
          status: 'not permitted',
          msg: 'Cluster log access not permitted'
        }];
        _mapById10[action.id] = _cluster7;
        return Object.assign({}, state, { mapById: _mapById10 });
      }
 
    default:
      return state;
  }
}