all files / src/workflows/generic/components/steps/ JobSubmission.js

43.17% Statements 60/139
28.57% Branches 18/63
25% Functions 7/28
39.6% Lines 40/101
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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _react = require('react');
 
var _react2 = _interopRequireDefault(_react);
 
var _propTypes = require('prop-types');
 
var _propTypes2 = _interopRequireDefault(_propTypes);
 
var _queryString = require('query-string');
 
var _queryString2 = _interopRequireDefault(_queryString);
 
var _values = require('mout/src/object/values');
 
var _values2 = _interopRequireDefault(_values);
 
var _ButtonBar = require('../../../../panels/ButtonBar');
 
var _ButtonBar2 = _interopRequireDefault(_ButtonBar);
 
var _defaults = require('../../../../panels/run/defaults');
 
var _defaults2 = _interopRequireDefault(_defaults);
 
var _run = require('../../../../panels/run');
 
var _run2 = _interopRequireDefault(_run);
 
var _ClusterPayload = require('../../../../utils/ClusterPayload');
 
var _ClusterPayload2 = _interopRequireDefault(_ClusterPayload);
 
var _redux = require('../../../../redux');
 
var _taskflows = require('../../../../redux/actions/taskflows');
 
var Actions = _interopRequireWildcard(_taskflows);
 
var _network = require('../../../../redux/actions/network');
 
var NetActions = _interopRequireWildcard(_network);
 
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; }
 
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
 
function _inherits(subClass, superClass) { Iif (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
 
// ----------------------------------------------------------------------------
 
function getServerProfiles(state) {
  var profiles = {};
  ['EC2', 'Traditional'].forEach(function (name) {
    profiles[name] = state[name];
  });
  return profiles;
}
 
// ----------------------------------------------------------------------------
 
function onJobSubmition(taskflowName, primaryJob, payload, simulationStep, location) {
  (0, _redux.dispatch)(Actions.createTaskflow(taskflowName, primaryJob, payload, simulationStep, location));
}
 
// ----------------------------------------------------------------------------
 
function onError(message) {
  (0, _redux.dispatch)(NetActions.errorNetworkCall('create_taskflow', { data: { message: message } }, 'form'));
}
 
// ----------------------------------------------------------------------------
 
var JobSubmission = function (_React$Component) {
  _inherits(JobSubmission, _React$Component);
 
  function JobSubmission(props) {
    _classCallCheck(this, JobSubmission);
 
    // Manually bind this method to the component instance...
    var _this = _possibleConstructorReturn(this, (JobSubmission.__proto__ || Object.getPrototypeOf(JobSubmission)).call(this, props));
 
    _this.dataChange = _this.dataChange.bind(_this);
    _this.buttonAction = _this.buttonAction.bind(_this);
    _this.updateServerType = _this.updateServerType.bind(_this);
    _this.prepareJob = _this.prepareJob.bind(_this);
 
    // Manage internal state
    _this.state = Object.assign({ serverType: 'Traditional' }, _defaults2.default);
    return _this;
  }
 
  _createClass(JobSubmission, [{
    key: 'dataChange',
    value: function dataChange(key, value, which) {
      var profile = this.state[which];
      profile[key] = value;
      this.setState(_defineProperty({}, which, profile));
    }
  }, {
    key: 'buttonAction',
    value: function buttonAction(action) {
      var fn = this.props.actionFunctions[action] || this[action];
      if (fn) {
        fn(this.props);
      } else {
        console.error('Could not find action', action, 'from', this.props.actionFunctions, 'or', this);
      }
    }
  }, {
    key: 'updateServerType',
    value: function updateServerType(e) {
      this.setState({ serverType: e.target.value });
    }
  }, {
    key: 'prepareJob',
    value: function prepareJob() {
      var payload = this.props.getPayload(this.props, this.state);
      var simulationStep = this.props.getSimulationStep(this.props, this.state);
 
      // Generic cluster management
      var clusterSettings = this.state[this.state.serverType];
      Object.assign(payload, clusterSettings.runtime);
      var clusterNames = (0, _values2.default)(this.props.clusters).filter(function (el) {
        return el.type === 'ec2';
      }).map(function (el) {
        return el.name;
      });
      try {
        var cluster = (0, _ClusterPayload2.default)(this.state.serverType, clusterSettings, clusterNames);
        if (!cluster) {
          throw Error('Unrecognized serverType: ' + this.state.serverType);
        }
        Object.assign(payload, clusterSettings.runtime, { cluster: cluster });
      } catch (error) {
        this.props.onError(error.message);
        return;
      }
 
      if (this.state.serverType === 'EC2') {
        var volumeNames = (0, _values2.default)(this.props.volumes).map(function (el) {
          return el.name;
        });
        if (this.state.EC2.volume) {
          payload.volume = { _id: this.state.EC2.volume };
        } else if (this.state.EC2.volumeName && this.state.EC2.volumeSize) {
          var volumeSize = this.state.EC2.volumeSize;
          var volumeName = this.state.EC2.volumeName.trim();
          try {
            if (volumeNames.indexOf(volumeName) !== -1) {
              throw Error('A volume with the name \'' + volumeName + '\' already exists');
            } else if (volumeSize <= 0) {
              throw Error('Volume size must be greater than zero');
            }
            payload.volume = {
              name: volumeName,
              size: volumeSize
            };
          } catch (error) {
            this.props.onError(error.message);
            return;
          }
        }
      }
 
      this.props.onJobSubmition(this.props.taskFlowName, this.props.primaryJob, payload, simulationStep, {
        // new location
        pathname: this.props.location.pathname,
        search: _queryString2.default.stringify(Object.assign({}, _queryString2.default.parse(this.props.location.search), {
          view: this.props.nextView
        })),
        state: this.props.location.state
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;
 
      // Add add-on UI if provided
      var workflowAddOn = this.props.addOn ? _react2.default.createElement(this.props.addOn, {
        owner: function owner() {
          return _this2;
        },
        parentProps: this.props,
        parentState: this.state
      }) : null;
 
      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(_run2.default, {
          serverType: this.state.serverType,
          serverTypeChange: this.updateServerType,
          profiles: getServerProfiles(this.state),
          dataChange: this.dataChange,
          clusterFilter: this.props.clusterFilter
        }),
        workflowAddOn,
        _react2.default.createElement(_ButtonBar2.default, {
          visible: this.state[this.state.serverType].profile !== '',
          onAction: this.buttonAction,
          actions: this.props.actionList,
          error: this.props.error
        })
      );
    }
  }]);
 
  return JobSubmission;
}(_react2.default.Component);
 
/* eslint-disable react/no-unused-prop-types */
 
 
exports.default = JobSubmission;
JobSubmission.propTypes = {
  actionList: _propTypes2.default.array,
  actionFunctions: _propTypes2.default.object,
  clusterFilter: _propTypes2.default.func,
  getPayload: _propTypes2.default.func,
  getTaskflowMetaData: _propTypes2.default.func,
  getSimulationStep: _propTypes2.default.func,
  nextView: _propTypes2.default.string,
 
  addOn: _propTypes2.default.func,
  primaryJob: _propTypes2.default.string,
  location: _propTypes2.default.object,
  project: _propTypes2.default.object,
  simulation: _propTypes2.default.object,
  step: _propTypes2.default.string,
  taskFlowName: _propTypes2.default.string,
  view: _propTypes2.default.string,
  error: _propTypes2.default.string,
  clusters: _propTypes2.default.object,
  volumes: _propTypes2.default.object,
 
  onJobSubmition: _propTypes2.default.func,
  onError: _propTypes2.default.func
};
/* eslint-enable react/no-unused-prop-types */
 
JobSubmission.defaultProps = {
  actionList: [{ name: 'prepareJob', label: 'Run Job', icon: '' }],
  actionFunctions: {},
  clusterFilter: function clusterFilter() {
    return true;
  },
  nextView: 'run',
 
  getPayload: function getPayload(props, state) {
    return {
      input: {
        folder: {
          id: props.simulation.metadata.inputFolder._id
        }
      },
      output: {
        folder: {
          id: props.simulation.metadata.outputFolder._id
        }
      }
    };
  },
  getTaskflowMetaData: function getTaskflowMetaData(props, state) {
    return {};
  },
  getSimulationStep: function getSimulationStep(props, state) {
    return {
      id: props.simulation._id,
      name: props.simulation.name,
      step: props.step,
      data: {
        view: props.nextView,
        metadata: props.getTaskflowMetaData(props, state)
      }
    };
  },
  onJobSubmition: onJobSubmition,
  onError: onError,
 
  addOn: undefined,
  primaryJob: undefined,
  location: undefined,
  project: undefined,
  simulation: undefined,
  step: undefined,
  taskFlowName: undefined,
  view: undefined,
  error: undefined,
  clusters: undefined,
  volumes: undefined
};