sagas.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import { call, put, all, takeEvery, takeLatest } from 'redux-saga/effects'
  2. import { ActionTypes } from './constants'
  3. import { ScheduleActions, ScheduleActionType } from './actions'
  4. import request from 'utils/request'
  5. import api from 'utils/api'
  6. import { errorHandler } from 'utils/util'
  7. import { message } from 'antd'
  8. import { IScheduleRaw, ISchedule } from './components/types'
  9. import { scheduleConfigMigrationRecorder } from 'app/utils/migrationRecorders'
  10. export function* getSchedules (action: ScheduleActionType) {
  11. if (action.type !== ActionTypes.LOAD_SCHEDULES) { return }
  12. try {
  13. const asyncData = yield call(request, `${api.schedule}?projectId=${action.payload.projectId}`)
  14. const rawSchedules: IScheduleRaw[] = asyncData.payload
  15. const schedules = rawSchedules.map<ISchedule>((schedule) => ({ ...schedule, config: JSON.parse(schedule.config) }))
  16. yield put(ScheduleActions.schedulesLoaded(schedules))
  17. } catch (err) {
  18. yield put(ScheduleActions.loadSchedulesFail())
  19. errorHandler(err)
  20. }
  21. }
  22. export function* getScheduleDetail (action: ScheduleActionType) {
  23. if (action.type !== ActionTypes.LOAD_SCHEDULE_DETAIL) { return }
  24. try {
  25. const asyncData = yield call(request, `${api.schedule}/${action.payload.scheduleId}`)
  26. const schedule = asyncData.payload
  27. schedule.config = scheduleConfigMigrationRecorder(JSON.parse(schedule.config))
  28. yield put(ScheduleActions.scheduleDetailLoaded(schedule))
  29. } catch (err) {
  30. yield put(ScheduleActions.loadScheduleDetailFail())
  31. errorHandler(err)
  32. }
  33. }
  34. export function* addSchedule (action: ScheduleActionType) {
  35. if (action.type !== ActionTypes.ADD_SCHEDULE) { return }
  36. const { schedule, resolve } = action.payload
  37. const rawSchedule: IScheduleRaw = { ...schedule, config: JSON.stringify(schedule.config) }
  38. try {
  39. const asyncData = yield call(request, {
  40. method: 'post',
  41. url: api.schedule,
  42. data: rawSchedule
  43. })
  44. const result = asyncData.payload
  45. result.config = JSON.parse(result.config)
  46. yield put(ScheduleActions.scheduleAdded(result))
  47. resolve()
  48. } catch (err) {
  49. yield put(ScheduleActions.addScheduleFail())
  50. errorHandler(err)
  51. }
  52. }
  53. export function* deleteSchedule (action: ScheduleActionType) {
  54. if (action.type !== ActionTypes.DELETE_SCHEDULE) { return }
  55. const { id } = action.payload
  56. try {
  57. yield call(request, {
  58. method: 'delete',
  59. url: `${api.schedule}/${id}`
  60. })
  61. yield put(ScheduleActions.scheduleDeleted(id))
  62. } catch (err) {
  63. yield put(ScheduleActions.deleteScheduleFail())
  64. errorHandler(err)
  65. }
  66. }
  67. export function* changeScheduleStatus (action: ScheduleActionType) {
  68. if (action.type !== ActionTypes.CHANGE_SCHEDULE_STATUS) { return }
  69. const { currentStatus, id } = action.payload
  70. try {
  71. let nextStatus = ''
  72. switch (currentStatus) {
  73. case 'new':
  74. case 'stopped':
  75. case 'failed':
  76. nextStatus = 'start'
  77. break
  78. case 'started':
  79. nextStatus = 'stop'
  80. break
  81. }
  82. const asyncData = yield call(request, {
  83. method: 'post',
  84. url: `${api.schedule}/${nextStatus}/${id}`
  85. })
  86. const result = asyncData.payload
  87. result.config = JSON.parse(result.config)
  88. yield put(ScheduleActions.scheduleStatusChanged(result))
  89. } catch (err) {
  90. yield put(ScheduleActions.changeSchedulesStatusFail())
  91. errorHandler(err)
  92. }
  93. }
  94. export function* executeScheduleImmediately (action: ScheduleActionType) {
  95. if (action.type !== ActionTypes.EXECUTE_SCHEDULE_IMMEDIATELY) { return }
  96. const { id, resolve } = action.payload
  97. try {
  98. yield call(request, {
  99. method: 'post',
  100. url: `${api.schedule}/execute/${id}`
  101. })
  102. resolve()
  103. } catch (err) {
  104. errorHandler(err)
  105. }
  106. }
  107. export function* editSchedule (action: ScheduleActionType) {
  108. if (action.type !== ActionTypes.EDIT_SCHEDULE) { return }
  109. const { schedule, resolve } = action.payload
  110. try {
  111. yield call(request, {
  112. method: 'put',
  113. url: `${api.schedule}/${schedule.id}`,
  114. data: schedule
  115. })
  116. yield put(ScheduleActions.scheduleEdited(schedule))
  117. resolve()
  118. } catch (err) {
  119. yield put(ScheduleActions.editScheduleFail())
  120. errorHandler(err)
  121. }
  122. }
  123. export function* getSuggestMails (action: ScheduleActionType) {
  124. if (action.type !== ActionTypes.LOAD_SUGGEST_MAILS) { return }
  125. const { keyword } = action.payload
  126. if (!keyword) {
  127. yield put(ScheduleActions.suggestMailsLoaded([]))
  128. return
  129. }
  130. try {
  131. const asyncData = yield call(request, {
  132. method: 'get',
  133. url: `${api.user}?keyword=${keyword}&includeSelf=true`
  134. })
  135. const mails = asyncData.payload
  136. yield put(ScheduleActions.suggestMailsLoaded(mails))
  137. } catch (err) {
  138. yield put(ScheduleActions.loadSuggestMailsFail())
  139. errorHandler(err)
  140. }
  141. }
  142. // @FIXME need remove
  143. export function* getVizsData (action) {
  144. const { projectId } = action.payload
  145. try {
  146. const portalsData = yield call(request, `${api.portal}?projectId=${projectId}`)
  147. const portalsList = portalsData.payload
  148. const displayData = yield call(request, `${api.display}?projectId=${projectId}`)
  149. const displayList = displayData.payload.map((display) => ({
  150. ...display,
  151. vizType: 'display',
  152. contentType: 'display',
  153. title: `${display.name}`,
  154. key: display.name,
  155. value: `${display.id}(d)`,
  156. isLeaf: true
  157. }))
  158. const list = yield all(portalsList.map((portals, index) => {
  159. return call(request, `${api.portal}/${portals.id}/dashboards`)
  160. }))
  161. const portals = portalsList.map((portal, index) => {
  162. portal.children = buildTree(list[index].payload)
  163. return {
  164. ...portal,
  165. vizType: 'portal',
  166. contentType: 'portal',
  167. title: `${portal.name}`,
  168. key: portal.name,
  169. value: `${portal.id}(p)`,
  170. isLeaf: !portal.children.length
  171. }
  172. })
  173. const result = [{
  174. contentType: 'display',
  175. title: `Display`,
  176. key: 'DISPLAYS',
  177. value: 'display',
  178. isTitle: true,
  179. children: displayList
  180. },
  181. {
  182. contentType: 'portal',
  183. title: `Dashboard`,
  184. key: 'DASHBOARDS',
  185. value: 'portal',
  186. isTitle: true,
  187. children: portals
  188. }]
  189. yield put(ScheduleActions.vizsLoaded(result))
  190. } catch (err) {
  191. yield put(ScheduleActions.loadVizsFail())
  192. message.error('获取失败')
  193. }
  194. function buildTree (list) {
  195. const temp = {}
  196. const tree = {}
  197. const result = []
  198. list.forEach((l, index) => temp[list[index].id] = list[index])
  199. for (const i in temp) {
  200. if (temp[i].parentId) {
  201. if (!temp[temp[i].parentId].children) {
  202. temp[temp[i].parentId].children = {}
  203. }
  204. temp[temp[i].parentId].children[temp[i].id] = temp[i]
  205. } else {
  206. tree[temp[i].id] = temp[i]
  207. }
  208. }
  209. function arr (tree, wrapper) {
  210. for (const attr in tree) {
  211. if (tree[attr]['children']) {
  212. tree[attr] = {
  213. ...tree[attr],
  214. ...{
  215. vizType: 'dashboard',
  216. contentType: 'portal',
  217. label: `${tree[attr].name}`,
  218. key: tree[attr].name,
  219. value: `${tree[attr].id}(p)`,
  220. isLeaf: true
  221. }
  222. }
  223. wrapper.push(tree[attr])
  224. const children = tree[attr]['children']
  225. tree[attr]['children'] = []
  226. arr(children, tree[attr]['children'])
  227. } else {
  228. tree[attr] = {
  229. ...tree[attr],
  230. ...{
  231. vizType: 'dashboard',
  232. contentType: 'portal',
  233. label: `${tree[attr].name}`,
  234. key: tree[attr].name,
  235. value: `${tree[attr].id}(p)`,
  236. isLeaf: true
  237. }
  238. }
  239. wrapper.push(tree[attr])
  240. }
  241. }
  242. }
  243. arr(tree, result)
  244. return result
  245. }
  246. }
  247. export default function* rootScheduleSaga () {
  248. yield all([
  249. takeEvery(ActionTypes.LOAD_SCHEDULES, getSchedules),
  250. takeEvery(ActionTypes.LOAD_SCHEDULE_DETAIL, getScheduleDetail),
  251. takeEvery(ActionTypes.ADD_SCHEDULE, addSchedule),
  252. takeEvery(ActionTypes.DELETE_SCHEDULE, deleteSchedule),
  253. takeEvery(ActionTypes.CHANGE_SCHEDULE_STATUS, changeScheduleStatus),
  254. takeEvery(ActionTypes.EXECUTE_SCHEDULE_IMMEDIATELY, executeScheduleImmediately),
  255. takeEvery(ActionTypes.EDIT_SCHEDULE, editSchedule),
  256. takeLatest(ActionTypes.LOAD_SUGGEST_MAILS, getSuggestMails),
  257. takeEvery(ActionTypes.LOAD_VIZS, getVizsData)
  258. ])
  259. }