MigrationsPage.jsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. // @flow
  15. import React from 'react'
  16. import styled from 'styled-components'
  17. import { observer } from 'mobx-react'
  18. import MainTemplate from '../../templates/MainTemplate'
  19. import Navigation from '../../organisms/Navigation'
  20. import FilterList from '../../organisms/FilterList'
  21. import PageHeader from '../../organisms/PageHeader'
  22. import AlertModal from '../../organisms/AlertModal'
  23. import MainListItem from '../../molecules/MainListItem'
  24. import type { MainItem } from '../../../types/MainItem'
  25. import migrationItemImage from './images/migration.svg'
  26. import migrationLargeImage from './images/migration-large.svg'
  27. import projectStore from '../../../stores/ProjectStore'
  28. import migrationStore from '../../../stores/MigrationStore'
  29. import endpointStore from '../../../stores/EndpointStore'
  30. import notificationStore from '../../../stores/NotificationStore'
  31. import configLoader from '../../../utils/Config'
  32. import Palette from '../../styleUtils/Palette'
  33. const Wrapper = styled.div``
  34. type State = {
  35. selectedMigrations: MainItem[],
  36. modalIsOpen: boolean,
  37. showDeleteMigrationModal: boolean,
  38. showCancelMigrationModal: boolean,
  39. }
  40. @observer
  41. class MigrationsPage extends React.Component<{ history: any }, State> {
  42. state = {
  43. showDeleteMigrationModal: false,
  44. showCancelMigrationModal: false,
  45. selectedMigrations: [],
  46. modalIsOpen: false,
  47. }
  48. pollTimeout: TimeoutID
  49. stopPolling: boolean
  50. componentDidMount() {
  51. document.title = 'Coriolis Migrations'
  52. projectStore.getProjects()
  53. endpointStore.getEndpoints({ showLoading: true })
  54. this.stopPolling = false
  55. this.pollData()
  56. }
  57. componentWillUnmount() {
  58. clearTimeout(this.pollTimeout)
  59. this.stopPolling = true
  60. }
  61. getEndpoint(endpointId: string) {
  62. return endpointStore.endpoints.find(endpoint => endpoint.id === endpointId)
  63. }
  64. getFilterItems() {
  65. return [
  66. { label: 'All', value: 'all' },
  67. { label: 'Running', value: 'RUNNING' },
  68. { label: 'Error', value: 'ERROR' },
  69. { label: 'Completed', value: 'COMPLETED' },
  70. ]
  71. }
  72. handleProjectChange() {
  73. endpointStore.getEndpoints({ showLoading: true })
  74. migrationStore.getMigrations({ showLoading: true })
  75. }
  76. handleReloadButtonClick() {
  77. projectStore.getProjects()
  78. endpointStore.getEndpoints({ showLoading: true })
  79. migrationStore.getMigrations({ showLoading: true })
  80. }
  81. handleItemClick(item: MainItem) {
  82. if (item.status === 'RUNNING') {
  83. this.props.history.push(`/migration/tasks/${item.id}`)
  84. } else {
  85. this.props.history.push(`/migration/${item.id}`)
  86. }
  87. }
  88. getStatus(migrationId: string): string {
  89. let migration = migrationStore.migrations.find(m => m.id === migrationId)
  90. return migration ? migration.status : ''
  91. }
  92. deleteSelectedMigrations() {
  93. this.state.selectedMigrations.forEach(migration => {
  94. migrationStore.delete(migration.id)
  95. })
  96. this.setState({ showDeleteMigrationModal: false })
  97. }
  98. cancelSelectedMigrations() {
  99. this.state.selectedMigrations.forEach(migration => {
  100. if (this.getStatus(migration.id) === 'RUNNING') {
  101. migrationStore.cancel(migration.id)
  102. }
  103. })
  104. notificationStore.alert('Canceling migrations')
  105. this.setState({ showCancelMigrationModal: false })
  106. }
  107. handleEmptyListButtonClick() {
  108. this.props.history.push('/wizard/migration')
  109. }
  110. handleModalOpen() {
  111. this.setState({ modalIsOpen: true })
  112. }
  113. handleModalClose() {
  114. this.setState({ modalIsOpen: false }, () => {
  115. this.pollData()
  116. })
  117. }
  118. searchText(item: MainItem, text?: string) {
  119. let result = false
  120. if (item.instances[0].toLowerCase().indexOf(text || '') > -1) {
  121. return true
  122. }
  123. if (item.destination_environment) {
  124. Object.keys(item.destination_environment).forEach(prop => {
  125. if (item.destination_environment[prop] && item.destination_environment[prop].toLowerCase
  126. // $FlowIssue
  127. && item.destination_environment[prop].toLowerCase().indexOf(text) > -1) {
  128. result = true
  129. }
  130. })
  131. }
  132. return result
  133. }
  134. itemFilterFunction(item: MainItem, filterStatus?: ?string, filterText?: string) {
  135. if ((filterStatus !== 'all' && (item.status !== filterStatus)) ||
  136. !this.searchText(item, filterText)
  137. ) {
  138. return false
  139. }
  140. return true
  141. }
  142. async pollData() {
  143. if (this.state.modalIsOpen || this.stopPolling) {
  144. return
  145. }
  146. await Promise.all([migrationStore.getMigrations({ skipLog: true }), endpointStore.getEndpoints({ skipLog: true })])
  147. this.pollTimeout = setTimeout(() => { this.pollData() }, configLoader.config.requestPollTimeout)
  148. }
  149. render() {
  150. let atLeaseOneIsRunning = false
  151. this.state.selectedMigrations.forEach(migration => {
  152. atLeaseOneIsRunning = atLeaseOneIsRunning || this.getStatus(migration.id) === 'RUNNING'
  153. })
  154. const BulkActions = [{
  155. label: 'Cancel',
  156. disabled: !atLeaseOneIsRunning,
  157. action: () => { this.setState({ showCancelMigrationModal: true }) },
  158. }, {
  159. label: 'Delete Migration',
  160. color: Palette.alert,
  161. action: () => { this.setState({ showDeleteMigrationModal: true }) },
  162. }]
  163. return (
  164. <Wrapper>
  165. <MainTemplate
  166. navigationComponent={<Navigation currentPage="migrations" />}
  167. listComponent={
  168. <FilterList
  169. filterItems={this.getFilterItems()}
  170. selectionLabel="migration"
  171. loading={migrationStore.loading}
  172. items={migrationStore.migrations}
  173. onItemClick={item => { this.handleItemClick(item) }}
  174. onReloadButtonClick={() => { this.handleReloadButtonClick() }}
  175. itemFilterFunction={(...args) => this.itemFilterFunction(...args)}
  176. onSelectedItemsChange={selectedMigrations => { this.setState({ selectedMigrations }) }}
  177. dropdownActions={BulkActions}
  178. renderItemComponent={options =>
  179. (<MainListItem
  180. {...options}
  181. image={migrationItemImage}
  182. endpointType={id => {
  183. let endpoint = this.getEndpoint(id)
  184. if (endpoint) {
  185. return endpoint.type
  186. }
  187. if (endpointStore.loading) {
  188. return 'Loading...'
  189. }
  190. return 'Not Found'
  191. }}
  192. useTasksRemaining
  193. />)
  194. }
  195. emptyListImage={migrationLargeImage}
  196. emptyListMessage="It seems like you don’t have any Migrations in this project."
  197. emptyListExtraMessage="A Coriolis Migration is a full virtual machine migration between two cloud endpoints."
  198. emptyListButtonLabel="Create a Migration"
  199. onEmptyListButtonClick={() => { this.handleEmptyListButtonClick() }}
  200. />
  201. }
  202. headerComponent={
  203. <PageHeader
  204. title="Coriolis Migrations"
  205. onProjectChange={() => { this.handleProjectChange() }}
  206. onModalOpen={() => { this.handleModalOpen() }}
  207. onModalClose={() => { this.handleModalClose() }}
  208. />
  209. }
  210. />
  211. {this.state.showDeleteMigrationModal ? (
  212. <AlertModal
  213. isOpen
  214. title="Delete Selected Migrations?"
  215. message="Are you sure you want to delete the selected migrations?"
  216. extraMessage="Deleting a Coriolis Migration is permanent!"
  217. onConfirmation={() => { this.deleteSelectedMigrations() }}
  218. onRequestClose={() => { this.setState({ showDeleteMigrationModal: false }) }}
  219. />
  220. ) : null}
  221. {this.state.showCancelMigrationModal ? (
  222. <AlertModal
  223. isOpen
  224. title="Cancel Selected Migrations?"
  225. message="Are you sure you want to cancel the selected migrations?"
  226. extraMessage="Canceling a Coriolis Migration is permanent!"
  227. onConfirmation={() => { this.cancelSelectedMigrations() }}
  228. onRequestClose={() => { this.setState({ showCancelMigrationModal: false }) }}
  229. />
  230. ) : null}
  231. </Wrapper>
  232. )
  233. }
  234. }
  235. export default MigrationsPage