ReplicaStore.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 { observable, action, runInAction } from 'mobx'
  16. import notificationStore from '../stores/NotificationStore'
  17. import ReplicaSource from '../sources/ReplicaSource'
  18. import type { MainItem, UpdateData } from '../types/MainItem'
  19. import type { Execution } from '../types/Execution'
  20. import type { Endpoint } from '../types/Endpoint'
  21. import type { Field } from '../types/Field'
  22. class ReplicaStoreUtils {
  23. static getNewReplica(replicaDetails: MainItem, execution: Execution): MainItem {
  24. if (replicaDetails.executions) {
  25. return {
  26. ...replicaDetails,
  27. executions: [...replicaDetails.executions.filter(e => e.id !== execution.id), execution],
  28. }
  29. }
  30. return {
  31. ...replicaDetails,
  32. executions: [execution],
  33. }
  34. }
  35. }
  36. class ReplicaStore {
  37. @observable replicas: MainItem[] = []
  38. @observable replicaDetails: ?MainItem = null
  39. @observable loading: boolean = true
  40. @observable backgroundLoading: boolean = false
  41. @observable detailsLoading: boolean = true
  42. @observable executionsLoading: boolean = false
  43. replicasLoaded: boolean = false
  44. @action async getReplicas(options?: { showLoading?: boolean, skipLog?: boolean }): Promise<void> {
  45. this.backgroundLoading = true
  46. if ((options && options.showLoading) || !this.replicasLoaded) {
  47. this.loading = true
  48. }
  49. try {
  50. let replicas = await ReplicaSource.getReplicas(options && options.skipLog)
  51. this.getReplicasSuccess(replicas)
  52. } finally {
  53. this.getReplicasDone()
  54. }
  55. }
  56. @action getReplicasSuccess(replicas: MainItem[]) {
  57. this.replicasLoaded = true
  58. this.replicas = replicas
  59. }
  60. @action getReplicasDone() {
  61. this.loading = false
  62. this.backgroundLoading = false
  63. }
  64. @action async getReplicaExecutions(replicaId: string, options?: { showLoading?: boolean, skipLog?: boolean }): Promise<void> {
  65. if (options && options.showLoading) this.executionsLoading = true
  66. try {
  67. let executions = await ReplicaSource.getReplicaExecutions(replicaId, options && options.skipLog)
  68. this.getReplicaExecutionsSuccess(replicaId, executions)
  69. } finally {
  70. runInAction(() => { this.executionsLoading = false })
  71. }
  72. }
  73. @action getReplicaExecutionsSuccess(replicaId: string, executions: Execution[]) {
  74. let replica = this.replicas.find(replica => replica.id === replicaId)
  75. if (replica) {
  76. replica.executions = executions
  77. }
  78. if (this.replicaDetails && this.replicaDetails.id === replicaId) {
  79. this.replicaDetails = {
  80. ...this.replicaDetails,
  81. executions,
  82. }
  83. }
  84. }
  85. @action async getReplica(replicaId: string, options?: { showLoading?: boolean, skipLog?: boolean }): Promise<void> {
  86. this.detailsLoading = Boolean(options && options.showLoading)
  87. try {
  88. let replica = await ReplicaSource.getReplica(replicaId, options && options.skipLog)
  89. runInAction(() => { this.replicaDetails = replica })
  90. } finally {
  91. runInAction(() => { this.detailsLoading = false })
  92. }
  93. }
  94. @action async execute(replicaId: string, fields?: Field[]): Promise<void> {
  95. let execution = await ReplicaSource.execute(replicaId, fields)
  96. this.executeSuccess(replicaId, execution)
  97. }
  98. @action executeSuccess(replicaId: string, execution: Execution) {
  99. if (this.replicaDetails && this.replicaDetails.id === replicaId) {
  100. this.replicaDetails = ReplicaStoreUtils.getNewReplica(this.replicaDetails, execution)
  101. }
  102. let replicasItemIndex = this.replicas ? this.replicas.findIndex(r => r.id === replicaId) : -1
  103. if (replicasItemIndex > -1) {
  104. const updatedReplica = ReplicaStoreUtils.getNewReplica(this.replicas[replicasItemIndex], execution)
  105. this.replicas[replicasItemIndex] = updatedReplica
  106. }
  107. }
  108. async cancelExecution(replicaId: string, executionId: string): Promise<void> {
  109. await ReplicaSource.cancelExecution(replicaId, executionId)
  110. notificationStore.alert('Cancelled', 'success')
  111. }
  112. async deleteExecution(replicaId: string, executionId: string): Promise<void> {
  113. await ReplicaSource.deleteExecution(replicaId, executionId)
  114. this.deleteExecutionSuccess(replicaId, executionId)
  115. }
  116. @action deleteExecutionSuccess(replicaId: string, executionId: string) {
  117. let executions = []
  118. if (this.replicaDetails && this.replicaDetails.id === replicaId) {
  119. if (this.replicaDetails.executions) {
  120. executions = [...this.replicaDetails.executions.filter(e => e.id !== executionId)]
  121. }
  122. this.replicaDetails = {
  123. ...this.replicaDetails,
  124. executions,
  125. }
  126. }
  127. }
  128. async delete(replicaId: string) {
  129. await ReplicaSource.delete(replicaId)
  130. runInAction(() => { this.replicas = this.replicas.filter(r => r.id !== replicaId) })
  131. }
  132. async deleteDisks(replicaId: string) {
  133. let execution = await ReplicaSource.deleteDisks(replicaId)
  134. this.deleteDisksSuccess(replicaId, execution)
  135. }
  136. @action deleteDisksSuccess(replicaId: string, execution: Execution) {
  137. if (this.replicaDetails && this.replicaDetails.id === replicaId) {
  138. this.replicaDetails = ReplicaStoreUtils.getNewReplica(this.replicaDetails, execution)
  139. }
  140. let replicasItemIndex = this.replicas ? this.replicas.findIndex(r => r.id === replicaId) : -1
  141. if (replicasItemIndex > -1) {
  142. const updatedReplica = ReplicaStoreUtils.getNewReplica(this.replicas[replicasItemIndex], execution)
  143. this.replicas[replicasItemIndex] = updatedReplica
  144. }
  145. }
  146. @action clearDetails() {
  147. this.detailsLoading = true
  148. this.replicaDetails = null
  149. }
  150. async update(replica: MainItem, destinationEndpoint: Endpoint, updateData: UpdateData, storageConfigDefault: string) {
  151. await ReplicaSource.update(replica, destinationEndpoint, updateData, storageConfigDefault)
  152. }
  153. }
  154. export default new ReplicaStore()