index.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import { Button, Card, Col, Divider, message, Row } from 'antd';
  3. import TitleComponent from '@/components/TitleComponent';
  4. import { createSchemaField } from '@formily/react';
  5. import { ArrayItems, Form, FormButtonGroup, FormGrid, FormItem, Input } from '@formily/antd';
  6. import { ISchema } from '@formily/json-schema';
  7. import { useMemo, useState } from 'react';
  8. import { createForm, onFieldReact, onFormInit } from '@formily/core';
  9. import FLevelInput from '@/components/FLevelInput';
  10. import { IOConfigItem } from '@/pages/rule-engine/Alarm/Config/typing';
  11. import Service from '@/pages/rule-engine/Alarm/Config/service';
  12. export const service = new Service('alarm/config');
  13. const Config = () => {
  14. const [tab, setTab] = useState<'io' | 'config' | string>('config');
  15. const SchemaField = createSchemaField({
  16. components: {
  17. FormItem,
  18. Input,
  19. ArrayItems,
  20. FormGrid,
  21. FLevelInput,
  22. },
  23. });
  24. const levelForm = useMemo(
  25. () =>
  26. createForm({
  27. validateFirst: true,
  28. effects() {
  29. onFormInit(async (form) => {
  30. const resp: any = await service.queryLevel();
  31. if (resp.status === 200) {
  32. const _level = resp.result.levels.map(
  33. (item: { level: number; title: string }) => item.title,
  34. );
  35. form.setValuesIn('level', _level);
  36. }
  37. });
  38. onFieldReact('level.0.remove', (state, f1) => {
  39. state.setState({ display: 'none' });
  40. f1.setFieldState('level.add', (state1) => {
  41. const length = f1.values.level?.length;
  42. if (length > 4) {
  43. state1.display = 'none';
  44. } else {
  45. state1.display = 'visible';
  46. }
  47. });
  48. });
  49. },
  50. }),
  51. [],
  52. );
  53. const inputForm = useMemo(() => createForm(), []);
  54. const outputForm = useMemo(() => createForm(), []);
  55. const levelSchema: ISchema = {
  56. type: 'object',
  57. properties: {
  58. level: {
  59. type: 'array',
  60. 'x-component': 'ArrayItems',
  61. 'x-decorator': 'FormItem',
  62. maxItems: 5,
  63. items: {
  64. type: 'void',
  65. 'x-decorator': 'FormGrid',
  66. 'x-decorator-props': {
  67. maxColumns: 24,
  68. minColumns: 24,
  69. columnGap: 2,
  70. },
  71. properties: {
  72. input: {
  73. type: 'string',
  74. 'x-decorator': 'FormItem',
  75. 'x-component': 'FLevelInput',
  76. 'x-decorator-props': {
  77. gridSpan: 23,
  78. },
  79. },
  80. remove: {
  81. type: 'void',
  82. title: <div style={{ width: '20px' }} />,
  83. 'x-decorator': 'FormItem',
  84. 'x-component': 'ArrayItems.Remove',
  85. 'x-decorator-props': {
  86. gridSpan: 1,
  87. },
  88. 'x-component-props': {
  89. style: { marginTop: '40px' },
  90. },
  91. },
  92. },
  93. },
  94. properties: {
  95. add: {
  96. type: 'void',
  97. title: '添加级别',
  98. 'x-component': 'ArrayItems.Addition',
  99. },
  100. },
  101. },
  102. },
  103. };
  104. const ioSchema: ISchema = {
  105. type: 'object',
  106. properties: {
  107. kafka: {
  108. title: 'kafka地址',
  109. type: 'string',
  110. required: true,
  111. 'x-decorator': 'FormItem',
  112. 'x-component': 'Input',
  113. 'x-component-props': {
  114. placeholder: '请输入kafka地址',
  115. },
  116. },
  117. topic: {
  118. title: 'topic',
  119. type: 'string',
  120. required: true,
  121. 'x-decorator': 'FormItem',
  122. 'x-component': 'Input',
  123. 'x-component-props': {
  124. placeholder: '请输入topic',
  125. },
  126. },
  127. layout2: {
  128. type: 'void',
  129. 'x-decorator': 'FormGrid',
  130. 'x-decorator-props': {
  131. maxColumns: 2,
  132. minColumns: 2,
  133. columnGap: 24,
  134. },
  135. properties: {
  136. username: {
  137. title: '用户名',
  138. type: 'string',
  139. required: true,
  140. 'x-decorator': 'FormItem',
  141. 'x-component': 'Input',
  142. 'x-component-props': {
  143. placeholder: '请输入用户名',
  144. },
  145. 'x-decorator-props': {
  146. gridSpan: 1,
  147. },
  148. },
  149. password: {
  150. title: '密码',
  151. type: 'string',
  152. required: true,
  153. 'x-decorator': 'FormItem',
  154. 'x-component': 'Input',
  155. 'x-decorator-props': {
  156. gridSpan: 1,
  157. },
  158. 'x-component-props': {
  159. placeholder: '请输入密码',
  160. },
  161. },
  162. },
  163. },
  164. },
  165. };
  166. const handleSaveIO = async () => {
  167. const inputConfig: IOConfigItem = await inputForm.submit();
  168. const outputConfig: IOConfigItem = await outputForm.submit();
  169. const inputResp = await service.saveOutputData({
  170. config: {
  171. type: 'kafka',
  172. config: inputConfig,
  173. },
  174. exchangeType: 'producer',
  175. });
  176. const outputResp = await service.saveOutputData({
  177. config: {
  178. type: 'kafka',
  179. config: outputConfig,
  180. },
  181. exchangeType: 'consume',
  182. });
  183. if (inputResp.status === 200 && outputResp.status === 200) {
  184. message.success('操作成功');
  185. }
  186. };
  187. const handleSaveLevel = async () => {
  188. const values: { level: string[] } = await levelForm.submit();
  189. const _level = values?.level.map((l: string, i: number) => ({ level: i + 1, title: l }));
  190. const resp = await service.saveLevel(_level);
  191. if (resp.status === 200) {
  192. message.success('操作成功');
  193. }
  194. };
  195. const level = (
  196. <Card>
  197. <TitleComponent data="告警级别配置" />
  198. <Form form={levelForm}>
  199. <SchemaField schema={levelSchema} />
  200. <FormButtonGroup.Sticky>
  201. <FormButtonGroup.FormItem>
  202. <Button type="primary" onClick={handleSaveLevel}>
  203. 保存
  204. </Button>
  205. </FormButtonGroup.FormItem>
  206. </FormButtonGroup.Sticky>
  207. </Form>
  208. </Card>
  209. );
  210. const io = (
  211. <div>
  212. <Card>
  213. <TitleComponent data="告警数据输出" />
  214. <Form form={outputForm} layout="vertical">
  215. <SchemaField schema={ioSchema} />
  216. </Form>
  217. <Divider />
  218. <TitleComponent data="告警处理结果输入" />
  219. <Form form={inputForm} layout="vertical">
  220. <SchemaField schema={ioSchema} />
  221. <FormButtonGroup.Sticky>
  222. <FormButtonGroup.FormItem>
  223. <Button type="primary" onClick={handleSaveIO}>
  224. 保存
  225. </Button>
  226. </FormButtonGroup.FormItem>
  227. </FormButtonGroup.Sticky>
  228. </Form>
  229. </Card>
  230. </div>
  231. );
  232. const list = [
  233. { key: 'config', tab: '告警级别', component: level },
  234. { key: 'io', tab: '数据流转', component: io },
  235. ];
  236. return (
  237. <PageContainer onTabChange={setTab} tabActiveKey={tab} tabList={list}>
  238. <Row>
  239. <Col span={16}>{list.find((k) => k.key === tab)?.component}</Col>
  240. <Col span={8}></Col>
  241. </Row>
  242. </PageContainer>
  243. );
  244. };
  245. export default Config;