SettingsMonitoring.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import React, { useEffect, useState, useRef } from 'react';
  2. import { Button, Col, Form, Row, Spin } from '@douyinfe/semi-ui';
  3. import {
  4. compareObjects,
  5. API,
  6. showError,
  7. showSuccess,
  8. showWarning,
  9. verifyJSON,
  10. } from '../../../helpers';
  11. import { useTranslation } from 'react-i18next';
  12. export default function SettingsMonitoring(props) {
  13. const { t } = useTranslation();
  14. const [loading, setLoading] = useState(false);
  15. const [inputs, setInputs] = useState({
  16. ChannelDisableThreshold: '',
  17. QuotaRemindThreshold: '',
  18. AutomaticDisableChannelEnabled: false,
  19. AutomaticEnableChannelEnabled: false,
  20. AutomaticDisableKeywords: '',
  21. });
  22. const refForm = useRef();
  23. const [inputsRow, setInputsRow] = useState(inputs);
  24. function onSubmit() {
  25. const updateArray = compareObjects(inputs, inputsRow);
  26. if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
  27. const requestQueue = updateArray.map((item) => {
  28. let value = '';
  29. if (typeof inputs[item.key] === 'boolean') {
  30. value = String(inputs[item.key]);
  31. } else {
  32. value = inputs[item.key];
  33. }
  34. return API.put('/api/option/', {
  35. key: item.key,
  36. value,
  37. });
  38. });
  39. setLoading(true);
  40. Promise.all(requestQueue)
  41. .then((res) => {
  42. if (requestQueue.length === 1) {
  43. if (res.includes(undefined)) return;
  44. } else if (requestQueue.length > 1) {
  45. if (res.includes(undefined))
  46. return showError(t('部分保存失败,请重试'));
  47. }
  48. showSuccess(t('保存成功'));
  49. props.refresh();
  50. })
  51. .catch(() => {
  52. showError(t('保存失败,请重试'));
  53. })
  54. .finally(() => {
  55. setLoading(false);
  56. });
  57. }
  58. useEffect(() => {
  59. const currentInputs = {};
  60. for (let key in props.options) {
  61. if (Object.keys(inputs).includes(key)) {
  62. currentInputs[key] = props.options[key];
  63. }
  64. }
  65. setInputs(currentInputs);
  66. setInputsRow(structuredClone(currentInputs));
  67. refForm.current.setValues(currentInputs);
  68. }, [props.options]);
  69. return (
  70. <>
  71. <Spin spinning={loading}>
  72. <Form
  73. values={inputs}
  74. getFormApi={(formAPI) => (refForm.current = formAPI)}
  75. style={{ marginBottom: 15 }}
  76. >
  77. <Form.Section text={t('监控设置')}>
  78. <Row gutter={16}>
  79. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  80. <Form.InputNumber
  81. label={t('测试所有渠道的最长响应时间')}
  82. step={1}
  83. min={0}
  84. suffix={t('秒')}
  85. extraText={t(
  86. '当运行通道全部测试时,超过此时间将自动禁用通道',
  87. )}
  88. placeholder={''}
  89. field={'ChannelDisableThreshold'}
  90. onChange={(value) =>
  91. setInputs({
  92. ...inputs,
  93. ChannelDisableThreshold: String(value),
  94. })
  95. }
  96. />
  97. </Col>
  98. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  99. <Form.InputNumber
  100. label={t('额度提醒阈值')}
  101. step={1}
  102. min={0}
  103. suffix={'Token'}
  104. extraText={t('低于此额度时将发送邮件提醒用户')}
  105. placeholder={''}
  106. field={'QuotaRemindThreshold'}
  107. onChange={(value) =>
  108. setInputs({
  109. ...inputs,
  110. QuotaRemindThreshold: String(value),
  111. })
  112. }
  113. />
  114. </Col>
  115. </Row>
  116. <Row gutter={16}>
  117. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  118. <Form.Switch
  119. field={'AutomaticDisableChannelEnabled'}
  120. label={t('失败时自动禁用通道')}
  121. size='default'
  122. checkedText='|'
  123. uncheckedText='〇'
  124. onChange={(value) => {
  125. setInputs({
  126. ...inputs,
  127. AutomaticDisableChannelEnabled: value,
  128. });
  129. }}
  130. />
  131. </Col>
  132. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  133. <Form.Switch
  134. field={'AutomaticEnableChannelEnabled'}
  135. label={t('成功时自动启用通道')}
  136. size='default'
  137. checkedText='|'
  138. uncheckedText='〇'
  139. onChange={(value) =>
  140. setInputs({
  141. ...inputs,
  142. AutomaticEnableChannelEnabled: value,
  143. })
  144. }
  145. />
  146. </Col>
  147. </Row>
  148. <Row gutter={16}>
  149. <Col xs={24} sm={16}>
  150. <Form.TextArea
  151. label={t('自动禁用关键词')}
  152. placeholder={t('一行一个,不区分大小写')}
  153. extraText={t(
  154. '当上游通道返回错误中包含这些关键词时(不区分大小写),自动禁用通道',
  155. )}
  156. field={'AutomaticDisableKeywords'}
  157. autosize={{ minRows: 6, maxRows: 12 }}
  158. onChange={(value) =>
  159. setInputs({ ...inputs, AutomaticDisableKeywords: value })
  160. }
  161. />
  162. </Col>
  163. </Row>
  164. <Row>
  165. <Button size='default' onClick={onSubmit}>
  166. {t('保存监控设置')}
  167. </Button>
  168. </Row>
  169. </Form.Section>
  170. </Form>
  171. </Spin>
  172. </>
  173. );
  174. }