MigrationList.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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, { PropTypes } from 'react';
  15. import Reflux from 'reflux';
  16. import withStyles from 'isomorphic-style-loader/lib/withStyles';
  17. import Location from '../../core/Location';
  18. import Dropdown from '../NewDropdown';
  19. import UserIcon from '../UserIcon';
  20. import NotificationIcon from '../NotificationIcon';
  21. import SearchBox from '../SearchBox';
  22. import Moment from 'react-moment';
  23. import s from './MigrationList.scss';
  24. import MigrationStore from '../../stores/MigrationStore';
  25. import ProjectStore from '../../stores/MigrationStore';
  26. import MigrationActions from '../../actions/MigrationActions';
  27. import FilteredTable from '../FilteredTable';
  28. import TextTruncate from 'react-text-truncate';
  29. import LoadingIcon from "../LoadingIcon/LoadingIcon";
  30. import ConfirmationDialog from '../ConfirmationDialog'
  31. import ProjectsDropdown from '../ProjectsDropdown';
  32. const title = 'Migrations';
  33. const migrationTypes = [
  34. { label: "Replicas", type: "replica" },
  35. { label: "Migrations", type: "migration" },
  36. { label: "All", type: "all" }
  37. ]
  38. const statusTypes = [
  39. { label: "Running", type: "RUNNING" },
  40. { label: "Error", type: "ERROR" },
  41. { label: "Completed", type: "COMPLETED" },
  42. { label: "All", type: "all" }
  43. ]
  44. const migrationActions = [
  45. { label: "Execute", value: "execute" },
  46. { label: "Cancel", value: "cancel" },
  47. { label: "Delete", value: "delete" }
  48. ]
  49. class MigrationList extends Reflux.Component {
  50. constructor(props) {
  51. super(props)
  52. this.store = MigrationStore;
  53. this.state = {
  54. title: props.type == "migrations" ? "Migrations" : "Replicas",
  55. queryText: '',
  56. filterType: props.type == "migrations" ? "migration" : "replica",
  57. filterStatus: "all",
  58. currentProject: "My Project",
  59. searchMin: true,
  60. filteredData: [],
  61. selectedAll: {
  62. migration: false,
  63. replica: false
  64. },
  65. confirmationDialog: {
  66. visible: false,
  67. message: "Are you sure?",
  68. onConfirm: null,
  69. onCancel: null
  70. }
  71. }
  72. }
  73. static contextTypes = {
  74. onSetTitle: PropTypes.func.isRequired
  75. };
  76. componentWillReceiveProps(newProps, oldProps) {
  77. this.setState({
  78. title: newProps.type == "migrations" ? "Migrations" : "Replicas",
  79. filterType: newProps.type == "migrations" ? "migration" : "replica"
  80. })
  81. }
  82. componentWillMount() {
  83. super.componentWillMount.call(this)
  84. this.context.onSetTitle(this.state.title);
  85. MigrationActions.loadMigrations()
  86. this.projects = [
  87. { label: "My Project", value: "Project1" },
  88. { label: "Project 2", value: "Project2" }
  89. ]
  90. }
  91. componentDidMount() {
  92. this.setState({ filteredData: this.state.migrations }) // eslint-disable-line react/no-did-mount-set-state
  93. }
  94. newMigration() {
  95. Location.push('/migrations/new')
  96. }
  97. migrationsSelected() {
  98. let count = 0
  99. let total = 0
  100. if (this.state.migrations) {
  101. count = this.migrationsSelectedCount()
  102. if (this.state.filterType == "all") {
  103. total = this.state.migrations.length
  104. } else {
  105. this.state.migrations.forEach(item => {
  106. if (item.type == this.state.filterType) {
  107. total++
  108. }
  109. })
  110. }
  111. }
  112. let term = "migration"
  113. if (this.state.filterType == "replica") term = "replica"
  114. return `${count} of ${total} ${term}(s) selected`;
  115. }
  116. migrationsSelectedCount() {
  117. let count = 0
  118. if (this.state.migrations) {
  119. this.state.migrations.forEach((item) => {
  120. if (item.selected) {
  121. if (this.state.filterType == "all") {
  122. count++
  123. } else {
  124. if (item.type == this.state.filterType && item.selected) {
  125. count++
  126. }
  127. }
  128. }
  129. })
  130. }
  131. return count
  132. }
  133. migrationDetail(e, item) {
  134. if (item.type == "migration") {
  135. Location.push('/migration/' + item.id + "/")
  136. } else {
  137. Location.push('/replica/' + item.id + "/")
  138. }
  139. }
  140. checkItem(e, itemRef) {
  141. let items = this.state.migrations
  142. items.forEach((item) => {
  143. if (item == itemRef) {
  144. item.selected = !item.selected
  145. }
  146. })
  147. let selectedAll = this.state.selectedAll
  148. selectedAll[this.state.filterType] = false
  149. this.setState({ migrations: items, selectedAll: selectedAll })
  150. }
  151. checkAll(e) {
  152. let items = this.state.migrations
  153. let selectedAll = this.state.selectedAll
  154. items.forEach((item) => {
  155. if (item.type == this.state.filterType) {
  156. item.selected = !selectedAll[this.state.filterType]
  157. }
  158. })
  159. selectedAll[this.state.filterType] = !selectedAll[this.state.filterType]
  160. this.setState({ migrations: items, selectedAll: selectedAll })
  161. }
  162. searchItem(queryText) {
  163. if (queryText.target) {
  164. this.setState({queryText: queryText.target.value })
  165. } else {
  166. this.setState({queryText: queryText })
  167. }
  168. }
  169. filterType(e, type) {
  170. this.setState({ filterType: type }, () => {
  171. this.searchItem({ target: { value: this.state.queryText } })
  172. })
  173. }
  174. filterStatus(e, status) {
  175. this.setState({ filterStatus: status }, () => {
  176. this.searchItem({ target: { value: this.state.queryText } })
  177. })
  178. }
  179. filterFn(item, queryText, filterType, filterStatus) {
  180. return (
  181. item.name.toLowerCase().indexOf(queryText.toLowerCase()) != -1 &&
  182. (filterType == "all" || filterType == item.type) &&
  183. (filterStatus == "all" || filterStatus == item.status)
  184. )
  185. }
  186. renderSearch(items) {
  187. if (items) {
  188. let output = items.map((item, index) => {
  189. let count = 0
  190. if (item.type == 'replica' && item.executions.length) {
  191. item.tasks = item.executions[item.executions.length - 1].tasks
  192. }
  193. if (!item.tasks) {
  194. item.tasks = []
  195. }
  196. item.tasks.forEach((task) => {
  197. if (task.status != "COMPLETED") count++
  198. })
  199. let tasksRemaining = count + " out of " + item.tasks.length
  200. return (
  201. <div className={"item " + (item.selected ? "selected" : "")} key={"vm_" + index}>
  202. <div className="checkbox-container">
  203. <input
  204. id={"vm_check_" + index}
  205. type="checkbox"
  206. checked={item.selected}
  207. onChange={(e) => this.checkItem(e, item)}
  208. className="checkbox-normal"
  209. />
  210. <label htmlFor={"vm_check_" + index}></label>
  211. </div>
  212. <span className="cell cell-icon" onClick={(e) => this.migrationDetail(e, item)}>
  213. <div className={"icon " + item.type}></div>
  214. <span className="details">
  215. {/*{item.name ? item.name : "N/A"}*/}
  216. <TextTruncate line={1} truncateText="..." text={item.name} />
  217. <span className={s.migrationStatus + " status-pill " + item.status}>{item.status}</span>
  218. </span>
  219. </span>
  220. <span className="cell" onClick={(e) => this.migrationDetail(e, item)}>
  221. <div className={s.cloudImage + " icon small-cloud " + item.origin_endpoint_type}></div>
  222. <span className={s.chevronRight}></span>
  223. <div className={s.cloudImage + " icon small-cloud " + item.destination_endpoint_type}></div>
  224. </span>
  225. <span className={"cell " + s.composite} onClick={(e) => this.migrationDetail(e, item)}>
  226. <span className={s.label}>Created</span>
  227. <span className={s.value}>
  228. <Moment format="MMM Do YYYY HH:ss" date={item.created_at} />
  229. </span>
  230. </span>
  231. {/*<span className={"cell " + s.composite} onClick={(e) => this.migrationDetail(e, item)}>
  232. <span className={s.label}>Notes</span>
  233. <TextTruncate line={2} truncateText="..." text={item.notes} />
  234. </span>*/}
  235. <span className={"cell " + s.composite} onClick={(e) => this.migrationDetail(e, item)}>
  236. <span className={s.label}>Tasks remaining</span>
  237. <span className={s.value}>{tasksRemaining}</span>
  238. </span>
  239. {/*<span className={"cell " + s.composite}>
  240. <span className={s.label}>Current instance</span>
  241. <span className={s.value}>{this.currentInstance(item)}</span>
  242. </span>*/}
  243. </div>
  244. )
  245. })
  246. return output
  247. } else {
  248. return (<div className="no-results">Your search returned no results</div>)
  249. }
  250. }
  251. onProjectChange(project) {
  252. // TODO: Move setstate from here
  253. //this.setState({ currentProject: project.value })
  254. }
  255. onMigrationActionChange(option) {
  256. switch (option.value) {
  257. case "delete":
  258. let deletedItems = [] // we put here the items for deletion
  259. this.state.migrations.forEach((item) => {
  260. if (item.selected) {
  261. if (this.state.filterType == "all") {
  262. deletedItems.push(item)
  263. } else {
  264. if (item.type == this.state.filterType) {
  265. deletedItems.push(item)
  266. }
  267. }
  268. }
  269. })
  270. this.setState({
  271. confirmationDialog: {
  272. visible: true,
  273. onConfirm: () => {
  274. this.setState({ confirmationDialog: { visible: false }})
  275. deletedItems.forEach(item => {
  276. MigrationActions.deleteMigration(item)
  277. })
  278. },
  279. onCancel: () => {
  280. this.setState({ confirmationDialog: { visible: false }})
  281. }
  282. }
  283. })
  284. break
  285. case "execute":
  286. this.state.migrations.forEach((item) => {
  287. if (item.selected) {
  288. if (this.state.filterType == "all") {
  289. MigrationActions.executeReplica(item)
  290. } else {
  291. if (item.type == this.state.filterType) {
  292. MigrationActions.executeReplica(item)
  293. }
  294. }
  295. }
  296. })
  297. break
  298. case "cancel":
  299. this.state.migrations.forEach((item) => {
  300. if (item.selected) {
  301. if (this.state.filterType == "all") {
  302. MigrationActions.cancelMigration(item)
  303. } else {
  304. if (item.type == this.state.filterType) {
  305. MigrationActions.cancelMigration(item)
  306. }
  307. }
  308. }
  309. })
  310. break
  311. default:
  312. break
  313. }
  314. }
  315. currentInstance(migration) {
  316. let instance = "N/A"
  317. /*migration.vms.forEach((item) => {
  318. if (item.selected) {
  319. instance = item.name
  320. }
  321. })*/
  322. return instance
  323. }
  324. refreshList() {
  325. MigrationActions.loadMigrations()
  326. }
  327. render() {
  328. let _this = this
  329. let itemStates = statusTypes.map((state, index) => (
  330. <a
  331. className={_this.state.filterStatus == state.type || (_this.state.filterStatus == null && state.type == "all") ?
  332. "selected" : ""}
  333. onClick={(e) => _this.filterStatus(e, state.type)} key={"status_" + index}
  334. >{state.label}</a>
  335. )
  336. )
  337. return (
  338. <div className={s.root}>
  339. <div className={s.container}>
  340. <div className={s.pageHeader}>
  341. <div className={s.top}>
  342. <h1>Coriolis {this.state.title}</h1>
  343. <div className={s.topActions}>
  344. {/*<Dropdown
  345. options={this.projects}
  346. onChange={(e) => this.onProjectChange(e)}
  347. placeholder="Select"
  348. value={this.state.currentProject}
  349. />*/}
  350. <ProjectsDropdown />
  351. <button onClick={this.newMigration}>New</button>
  352. <UserIcon />
  353. <NotificationIcon />
  354. </div>
  355. </div>
  356. <div className="filters">
  357. <div className="checkbox-container">
  358. <input
  359. id={"vm_check_all"}
  360. type="checkbox"
  361. checked={this.state.selectedAll[this.state.filterType]}
  362. onChange={(e) => this.checkAll()}
  363. className="checkbox-normal"
  364. />
  365. <label htmlFor={"vm_check_all"}></label>
  366. </div>
  367. <div className="category-filter">
  368. {itemStates}
  369. </div>
  370. <div className={s.refreshBtn}>
  371. <div className="icon refresh" onClick={this.refreshList}></div>
  372. </div>
  373. <div className="name-filter">
  374. <SearchBox
  375. placeholder="Search"
  376. value={this.state.queryText}
  377. onChange={(e) => this.searchItem(e)}
  378. minimize={true} // eslint-disable-line react/jsx-boolean-value
  379. onClick={(e) => this.toggleSearch(e)}
  380. className={"searchBox " + (this.state.searchMin ? "minimize" : "")}
  381. />
  382. </div>
  383. <div className={s.bulkActions + (this.migrationsSelectedCount() === 0 ? " invisible": "")}>
  384. <div className={s.migrationsCount}>
  385. {this.migrationsSelected()}
  386. </div>
  387. <Dropdown
  388. options={migrationActions}
  389. onChange={(e) => this.onMigrationActionChange(e)}
  390. placeholder="More Actions"
  391. />
  392. </div>
  393. </div>
  394. </div>
  395. <div className={s.pageContent}>
  396. <FilteredTable
  397. items={this.state.migrations}
  398. filterFn={this.filterFn}
  399. queryText={this.state.queryText}
  400. filterType={this.state.filterType}
  401. filterStatus={this.state.filterStatus}
  402. renderSearch={(e) => this.renderSearch(e)}
  403. ></FilteredTable>
  404. </div>
  405. </div>
  406. <ConfirmationDialog
  407. visible={this.state.confirmationDialog.visible}
  408. message={this.state.confirmationDialog.message}
  409. onConfirm={(e) => this.state.confirmationDialog.onConfirm(e)}
  410. onCancel={(e) => this.state.confirmationDialog.onCancel(e)}
  411. />
  412. </div>
  413. );
  414. }
  415. }
  416. export default withStyles(MigrationList, s);