sagas.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import { call, put, all, takeLatest, takeEvery } from 'redux-saga/effects'
  21. import { ActionTypes } from './constants'
  22. import { SourceActions, SourceActionType } from './actions'
  23. import omit from 'lodash/omit'
  24. import request from 'utils/request'
  25. import api from 'utils/api'
  26. import { errorHandler } from 'utils/util'
  27. import { message } from 'antd'
  28. import {
  29. ISourceBase,
  30. ISourceRaw,
  31. ISource,
  32. ISourceDatabases,
  33. IDatabaseTables,
  34. ITableColumns
  35. } from './types'
  36. export function* getSources(action: SourceActionType) {
  37. if (action.type !== ActionTypes.LOAD_SOURCES) {
  38. return
  39. }
  40. const { payload } = action
  41. try {
  42. const asyncData = yield call(
  43. request,
  44. `${api.source}?projectId=${payload.projectId}`
  45. )
  46. const sources = asyncData.payload as ISourceBase[]
  47. yield put(SourceActions.sourcesLoaded(sources))
  48. } catch (err) {
  49. yield put(SourceActions.loadSourcesFail())
  50. errorHandler(err)
  51. }
  52. }
  53. export function* addSource(action: SourceActionType) {
  54. if (action.type !== ActionTypes.ADD_SOURCE) {
  55. return
  56. }
  57. const { payload } = action
  58. try {
  59. const asyncData = yield call(request, {
  60. method: 'post',
  61. url: api.source,
  62. data: payload.source
  63. })
  64. payload.resolve()
  65. yield put(SourceActions.sourceAdded(asyncData.payload))
  66. } catch (err) {
  67. yield put(SourceActions.addSourceFail())
  68. errorHandler(err)
  69. }
  70. }
  71. export function* getSourceDetail(action: SourceActionType) {
  72. if (action.type !== ActionTypes.LOAD_SOURCE_DETAIL) {
  73. return
  74. }
  75. const { sourceId, resolve } = action.payload
  76. try {
  77. const asyncData = yield call(request, `${api.source}/${sourceId}`)
  78. const sourceRaw = asyncData.payload as ISourceRaw
  79. const source: ISource = {
  80. ...sourceRaw,
  81. config: JSON.parse(sourceRaw.config)
  82. }
  83. yield put(SourceActions.sourceDetailLoaded(source))
  84. if (resolve) {
  85. resolve(source)
  86. }
  87. } catch (err) {
  88. yield put(SourceActions.loadSourceDetailFail())
  89. errorHandler(err)
  90. }
  91. }
  92. export function* deleteSource(action: SourceActionType) {
  93. if (action.type !== ActionTypes.DELETE_SOURCE) {
  94. return
  95. }
  96. const { payload } = action
  97. try {
  98. const result = yield call(request, {
  99. method: 'delete',
  100. url: `${api.source}/${payload.id}`
  101. })
  102. const { code } = result.header
  103. yield put(SourceActions.sourceDeleted(payload.id))
  104. } catch (err) {
  105. yield put(SourceActions.deleteSourceFail())
  106. errorHandler(err)
  107. }
  108. }
  109. export function* editSource(action: SourceActionType) {
  110. if (action.type !== ActionTypes.EDIT_SOURCE) {
  111. return
  112. }
  113. const { source, resolve } = action.payload
  114. try {
  115. yield call(request, {
  116. method: 'put',
  117. url: `${api.source}/${source.id}`,
  118. data: source
  119. })
  120. const sourceBase = omit(source, 'config')
  121. yield put(SourceActions.sourceEdited(sourceBase))
  122. resolve()
  123. } catch (err) {
  124. yield put(SourceActions.editSourceFail())
  125. errorHandler(err)
  126. }
  127. }
  128. export function* testSourceConnection(action: SourceActionType) {
  129. if (action.type !== ActionTypes.TEST_SOURCE_CONNECTION) {
  130. return
  131. }
  132. const { payload } = action
  133. try {
  134. const res = yield call(request, {
  135. method: 'post',
  136. url: `${api.source}/test`,
  137. data: payload.testSource
  138. })
  139. yield put(SourceActions.sourceConnected())
  140. message.success('测试成功')
  141. } catch (err) {
  142. yield put(SourceActions.testSourceConnectionFail())
  143. errorHandler(err)
  144. }
  145. }
  146. export function* resetSourceConnection(action: SourceActionType) {
  147. if (action.type !== ActionTypes.RESET_SOURCE_CONNECTION) {
  148. return
  149. }
  150. const { properties, resolve } = action.payload
  151. const { sourceId, username, password } = properties
  152. try {
  153. yield call(request, {
  154. method: 'post',
  155. url: `${api.source}/reconnect/${sourceId}`,
  156. data: {
  157. dbUser: username,
  158. dbPassword: password
  159. }
  160. })
  161. yield put(SourceActions.sourceReset())
  162. message.success('连接重置成功')
  163. resolve()
  164. } catch (err) {
  165. yield put(SourceActions.resetSourceConnectionFail())
  166. errorHandler(err)
  167. }
  168. }
  169. export function* validateCsvTableName(action: SourceActionType) {
  170. if (action.type !== ActionTypes.VALIDATE_CSV_TABLE_NAME) {
  171. return
  172. }
  173. const { callback } = action.payload
  174. const { sourceId, mode, tableName } = action.payload.csvMeta
  175. try {
  176. yield call(request, {
  177. url: `${api.source}/${sourceId}/csvmeta`,
  178. method: 'post',
  179. data: {
  180. mode,
  181. tableName
  182. }
  183. })
  184. callback()
  185. } catch (err) {
  186. callback(err.response.data.header.msg)
  187. }
  188. }
  189. export function* uploadCsvFile(action: SourceActionType) {
  190. if (action.type !== ActionTypes.UPLOAD_CSV_FILE) {
  191. return
  192. }
  193. const { csvMeta, resolve, reject } = action.payload
  194. try {
  195. const formData = new FormData()
  196. Object.entries(csvMeta).forEach(([fieldName, fieldValue]) => {
  197. if (fieldValue || fieldValue === 0) {
  198. formData.append(fieldName, fieldValue)
  199. }
  200. })
  201. yield call(request, `${api.source}/${csvMeta.sourceId}/uploadcsv`, {
  202. method: 'POST',
  203. data: formData
  204. })
  205. resolve()
  206. } catch (err) {
  207. errorHandler(err)
  208. reject()
  209. }
  210. }
  211. export function* getSourceDatabases(action: SourceActionType) {
  212. if (action.type !== ActionTypes.LOAD_SOURCE_DATABASES) {
  213. return
  214. }
  215. const { sourceId } = action.payload
  216. try {
  217. const asyncData = yield call(request, `${api.source}/${sourceId}/databases`)
  218. const sourceDatabases: ISourceDatabases = asyncData.payload
  219. yield put(SourceActions.sourceDatabasesLoaded(sourceDatabases))
  220. } catch (err) {
  221. yield put(SourceActions.loadSourceDatabasesFail(err))
  222. errorHandler(err)
  223. }
  224. }
  225. export function* getDatabaseTables(action: SourceActionType) {
  226. if (action.type !== ActionTypes.LOAD_SOURCE_DATABASE_TABLES) {
  227. return
  228. }
  229. const { databaseName, sourceId } = action.payload
  230. try {
  231. const asyncData = yield call(
  232. request,
  233. `${api.source}/${sourceId}/tables?dbName=${databaseName}`
  234. )
  235. const databaseTables: IDatabaseTables = asyncData.payload
  236. yield put(SourceActions.databaseTablesLoaded(databaseTables))
  237. } catch (err) {
  238. yield put(SourceActions.loadDatabaseTablesFail(err))
  239. errorHandler(err)
  240. }
  241. }
  242. export function* getTableColumns(action: SourceActionType) {
  243. if (action.type !== ActionTypes.LOAD_SOURCE_TABLE_COLUMNS) {
  244. return
  245. }
  246. const { sourceId, databaseName, tableName, resolve } = action.payload
  247. try {
  248. const asyncData = yield call(
  249. request,
  250. `${api.source}/${sourceId}/table/columns?dbName=${databaseName}&tableName=${tableName}`
  251. )
  252. const tableColumns: ITableColumns = {
  253. ...asyncData.payload,
  254. dbName: databaseName
  255. }
  256. yield put(SourceActions.tableColumnsLoaded(databaseName, tableColumns))
  257. if (resolve) {
  258. resolve(tableColumns)
  259. }
  260. } catch (err) {
  261. yield put(SourceActions.loadTableColumnsFail(err))
  262. errorHandler(err)
  263. }
  264. }
  265. export function* getDatasourcesInfo(action: SourceActionType) {
  266. if (action.type !== ActionTypes.LOAD_DATASOURCES_INFO) {
  267. return
  268. }
  269. try {
  270. const asyncData = yield call(request, `${api.source}/jdbc/datasources`)
  271. yield put(SourceActions.datasourcesInfoLoaded(asyncData.payload))
  272. } catch (err) {
  273. yield put(SourceActions.loadDatasourcesInfoFail(err))
  274. errorHandler(err)
  275. }
  276. }
  277. export default function* rootSourceSaga() {
  278. yield all([
  279. takeLatest(ActionTypes.LOAD_SOURCES, getSources),
  280. takeEvery(ActionTypes.LOAD_SOURCE_DETAIL, getSourceDetail),
  281. takeEvery(ActionTypes.ADD_SOURCE, addSource),
  282. takeEvery(ActionTypes.DELETE_SOURCE, deleteSource),
  283. takeEvery(ActionTypes.EDIT_SOURCE, editSource),
  284. takeEvery(ActionTypes.TEST_SOURCE_CONNECTION, testSourceConnection),
  285. takeEvery(ActionTypes.RESET_SOURCE_CONNECTION, resetSourceConnection),
  286. takeLatest(ActionTypes.VALIDATE_CSV_TABLE_NAME, validateCsvTableName),
  287. takeEvery(ActionTypes.UPLOAD_CSV_FILE, uploadCsvFile),
  288. takeEvery(ActionTypes.LOAD_SOURCE_DATABASES, getSourceDatabases),
  289. takeEvery(ActionTypes.LOAD_SOURCE_DATABASE_TABLES, getDatabaseTables),
  290. takeEvery(ActionTypes.LOAD_SOURCE_TABLE_COLUMNS, getTableColumns),
  291. takeLatest(ActionTypes.LOAD_DATASOURCES_INFO, getDatasourcesInfo)
  292. ])
  293. }