ReplicaExecutions.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React, { Component, PropTypes } from 'react';
  15. import withStyles from 'isomorphic-style-loader/lib/withStyles';
  16. import s from './ReplicaExecutions.scss';
  17. import Dropdown from '../NewDropdown';
  18. import LoadingIcon from '../LoadingIcon';
  19. import moment from 'moment';
  20. import MigrationActions from '../../actions/MigrationActions';
  21. import Tasks from '../Tasks';
  22. import ExecutionsTimeline from '../ExecutionsTimeline';
  23. import {tasksPollTimeout} from '../../config'
  24. import ConfirmationDialog from '../ConfirmationDialog'
  25. const title = 'Replica Executions';
  26. class ReplicaExecutions extends Component {
  27. static contextTypes = {
  28. onSetTitle: PropTypes.func.isRequired,
  29. };
  30. static propTypes = {
  31. replica: PropTypes.object
  32. }
  33. constructor(props) {
  34. super(props)
  35. this.changeExecution = this.changeExecution.bind(this)
  36. this.state = {
  37. executionRef: null,
  38. tasks: null,
  39. confirmationDialog: {
  40. visible: false,
  41. message: "Are you sure?",
  42. onConfirm: null,
  43. onCancel: null
  44. }
  45. }
  46. }
  47. componentWillMount() {
  48. this.context.onSetTitle(title);
  49. this.componentWillReceiveProps(this.props)
  50. this.timeout = setInterval((e) => this.pollTasks(e), tasksPollTimeout)
  51. }
  52. componentDidMount() {
  53. this.pollTasks()
  54. }
  55. componentWillUnmount() {
  56. clearInterval(this.timeout)
  57. }
  58. componentWillReceiveProps(newProps) {
  59. if (newProps.replica && newProps.replica.executions.length) {
  60. let execution = newProps.replica.executions[newProps.replica.executions.length - 1]
  61. this.setState({
  62. executionRef: execution,
  63. tasks: execution.tasks
  64. })
  65. } else if (newProps.replica.executions.length == 0) {
  66. this.setState({
  67. executionRef: null,
  68. tasks: null
  69. })
  70. }
  71. }
  72. executeNow() {
  73. MigrationActions.executeReplica(this.props.replica)
  74. clearInterval(this.timeout)
  75. this.timeout = setInterval((e) => this.pollTasks(e), tasksPollTimeout)
  76. }
  77. cancelExecution() {
  78. MigrationActions.cancelMigration(this.props.replica, (replica, response) => {
  79. this.refreshExecution()
  80. })
  81. }
  82. deleteExecution() {
  83. this.setState({
  84. confirmationDialog: {
  85. visible: true,
  86. onConfirm: () => {
  87. this.setState({ confirmationDialog: { visible: false }})
  88. let index = this.props.replica.executions.indexOf(this.state.executionRef)
  89. MigrationActions.deleteReplicaExecution(this.props.replica, this.state.executionRef.id, () => {
  90. if (this.props.replica.executions[index - 1]) {
  91. this.changeExecution(this.props.replica.executions[index - 1])
  92. } else if (this.props.replica.executions[index + 1]) {
  93. this.changeExecution(this.props.replica.executions[index + 1])
  94. } else {
  95. this.changeExecution(null)
  96. }
  97. })
  98. },
  99. onCancel: () => {
  100. this.setState({ confirmationDialog: { visible: false }})
  101. }
  102. }
  103. })
  104. }
  105. refreshExecution() {
  106. MigrationActions.getReplicaExecutionDetail(this.props.replica, this.state.executionRef.id,
  107. (replica, executionId, response) => {
  108. let props = this.props
  109. props.migration.tasks = response.data.execution.tasks
  110. MigrationActions.getReplicaExecutions(replica)
  111. let state = this.processProps({ migration: {tasks: response.data.execution.tasks } }, null)
  112. this.setState(state)
  113. })
  114. }
  115. pollTasks() {
  116. if (this.props && this.props.replica && this.props.replica.executions.length) {
  117. if (this.props.replica.executions[this.props.replica.executions.length - 1].status == "RUNNING") {
  118. MigrationActions.getReplicaExecutionDetail(this.props.replica, this.state.executionRef.id,
  119. (replica, executionId, response) => {
  120. this.setState({
  121. tasks: response.data.execution.tasks
  122. })
  123. })
  124. }
  125. }
  126. }
  127. changeExecution(execution) {
  128. if (execution == null) {
  129. this.setState({
  130. executionRef: null,
  131. tasks: null
  132. })
  133. } else {
  134. this.setState({
  135. executionRef: execution,
  136. tasks: execution.tasks
  137. })
  138. }
  139. }
  140. render() {
  141. if (this.props.replica) {
  142. if (this.props.replica.executions.length && this.state.executionRef) {
  143. let executionBtn = <button className="wire" onClick={(e) => this.deleteExecution(e)}>Delete</button>
  144. if (this.props.replica.executions && this.props.replica.executions[this.props.replica.executions.length - 1] &&
  145. this.props.replica.executions[this.props.replica.executions.length - 1].status == "RUNNING") {
  146. executionBtn =
  147. <button className="gray wire" onClick={(e) => this.cancelExecution(e)}>Cancel execution</button>
  148. }
  149. let executionsSorted = this.props.replica.executions
  150. executionsSorted.sort((a, b) => a.number - b.number)
  151. return (
  152. <div className={s.root}>
  153. <div className={s.container}>
  154. <ExecutionsTimeline
  155. executions={this.props.replica.executions}
  156. currentExecution={this.state.executionRef}
  157. handleChangeExecution={this.changeExecution}
  158. />
  159. <div className={s.executionsWrapper}>
  160. <div className={s.leftSide}>
  161. <h4>Execution #{this.state.executionRef && this.state.executionRef.number}</h4>
  162. <span className={s.date}>
  163. {this.state.executionRef && moment(this.state.executionRef.created_at).format("MMM Do YYYY HH:mm")}
  164. </span>
  165. <span
  166. className={"status-pill " + this.state.executionRef.status}>{this.state.executionRef.status}</span>
  167. </div>
  168. <div className={s.rightSide}>
  169. {executionBtn}
  170. </div>
  171. </div>
  172. <Tasks tasks={this.state.tasks}/>
  173. </div>
  174. <ConfirmationDialog
  175. visible={this.state.confirmationDialog.visible}
  176. message={this.state.confirmationDialog.message}
  177. onConfirm={(e) => this.state.confirmationDialog.onConfirm(e)}
  178. onCancel={(e) => this.state.confirmationDialog.onCancel(e)}
  179. />
  180. </div>
  181. );
  182. } else {
  183. return (
  184. <div className={s.root}>
  185. <div className={s.container}>
  186. <div className="no-results">No executions for this replica <br /> <br />
  187. <button onClick={(e) => this.executeNow(e)}>Execute Now</button>
  188. </div>
  189. </div>
  190. </div>
  191. )
  192. }
  193. } else {
  194. return (
  195. <div className={s.root}>
  196. <div className={s.container}>
  197. <LoadingIcon />
  198. </div>
  199. </div>
  200. )
  201. }
  202. }
  203. }
  204. export default withStyles(ReplicaExecutions, s);