MjLogsTable.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. import React, {useEffect, useState} from 'react';
  2. import {API, copy, isAdmin, showError, showSuccess, timestamp2string} from '../helpers';
  3. import {
  4. Table,
  5. Avatar,
  6. Tag,
  7. Form,
  8. Button,
  9. Layout,
  10. Select,
  11. Popover,
  12. Modal,
  13. ImagePreview,
  14. Typography, Progress
  15. } from '@douyinfe/semi-ui';
  16. import {ITEMS_PER_PAGE} from '../constants';
  17. import {renderNumber, renderQuota, stringToColor} from '../helpers/render';
  18. const colors = ['amber', 'blue', 'cyan', 'green', 'grey', 'indigo',
  19. 'light-blue', 'lime', 'orange', 'pink',
  20. 'purple', 'red', 'teal', 'violet', 'yellow'
  21. ]
  22. function renderType(type) {
  23. switch (type) {
  24. case 'IMAGINE':
  25. return <Tag color="blue" size='large'>绘图</Tag>;
  26. case 'UPSCALE':
  27. return <Tag color="orange" size='large'>放大</Tag>;
  28. case 'VARIATION':
  29. return <Tag color="purple" size='large'>变换</Tag>;
  30. case 'DESCRIBE':
  31. return <Tag color="yellow" size='large'>图生文</Tag>;
  32. case 'BLEAND':
  33. return <Tag color="lime" size='large'>图混合</Tag>;
  34. default:
  35. return <Tag color="white" size='large'>未知</Tag>;
  36. }
  37. }
  38. function renderCode(code) {
  39. switch (code) {
  40. case 1:
  41. return <Tag color="green" size='large'>已提交</Tag>;
  42. case 21:
  43. return <Tag color="lime" size='large'>排队中</Tag>;
  44. case 22:
  45. return <Tag color="orange" size='large'>重复提交</Tag>;
  46. default:
  47. return <Tag color="white" size='large'>未知</Tag>;
  48. }
  49. }
  50. function renderStatus(type) {
  51. // Ensure all cases are string literals by adding quotes.
  52. switch (type) {
  53. case 'SUCCESS':
  54. return <Tag color="green" size='large'>成功</Tag>;
  55. case 'NOT_START':
  56. return <Tag color="grey" size='large'>未启动</Tag>;
  57. case 'SUBMITTED':
  58. return <Tag color="yellow" size='large'>队列中</Tag>;
  59. case 'IN_PROGRESS':
  60. return <Tag color="blue" size='large'>执行中</Tag>;
  61. case 'FAILURE':
  62. return <Tag color="red" size='large'>失败</Tag>;
  63. default:
  64. return <Tag color="white" size='large'>未知</Tag>;
  65. }
  66. }
  67. const renderTimestamp = (timestampInSeconds) => {
  68. const date = new Date(timestampInSeconds * 1000); // 从秒转换为毫秒
  69. const year = date.getFullYear(); // 获取年份
  70. const month = ('0' + (date.getMonth() + 1)).slice(-2); // 获取月份,从0开始需要+1,并保证两位数
  71. const day = ('0' + date.getDate()).slice(-2); // 获取日期,并保证两位数
  72. const hours = ('0' + date.getHours()).slice(-2); // 获取小时,并保证两位数
  73. const minutes = ('0' + date.getMinutes()).slice(-2); // 获取分钟,并保证两位数
  74. const seconds = ('0' + date.getSeconds()).slice(-2); // 获取秒钟,并保证两位数
  75. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; // 格式化输出
  76. };
  77. const LogsTable = () => {
  78. const [isModalOpen, setIsModalOpen] = useState(false);
  79. const [modalContent, setModalContent] = useState('');
  80. const columns = [
  81. {
  82. title: '提交时间',
  83. dataIndex: 'submit_time',
  84. render: (text, record, index) => {
  85. return (
  86. <div>
  87. {renderTimestamp(text / 1000)}
  88. </div>
  89. );
  90. },
  91. },
  92. {
  93. title: '渠道',
  94. dataIndex: 'channel_id',
  95. className: isAdmin() ? 'tableShow' : 'tableHiddle',
  96. render: (text, record, index) => {
  97. return (
  98. <div>
  99. <Tag color={colors[parseInt(text) % colors.length]} size='large' onClick={() => {
  100. copyText(text); // 假设copyText是用于文本复制的函数
  101. }}> {text} </Tag>
  102. </div>
  103. );
  104. },
  105. },
  106. {
  107. title: '类型',
  108. dataIndex: 'action',
  109. render: (text, record, index) => {
  110. return (
  111. <div>
  112. {renderType(text)}
  113. </div>
  114. );
  115. },
  116. },
  117. {
  118. title: '任务ID',
  119. dataIndex: 'mj_id',
  120. render: (text, record, index) => {
  121. return (
  122. <div>
  123. {text}
  124. </div>
  125. );
  126. },
  127. },
  128. {
  129. title: '提交结果',
  130. dataIndex: 'code',
  131. className: isAdmin() ? 'tableShow' : 'tableHiddle',
  132. render: (text, record, index) => {
  133. return (
  134. <div>
  135. {renderCode(text)}
  136. </div>
  137. );
  138. },
  139. },
  140. {
  141. title: '任务状态',
  142. dataIndex: 'status',
  143. className: isAdmin() ? 'tableShow' : 'tableHiddle',
  144. render: (text, record, index) => {
  145. return (
  146. <div>
  147. {renderStatus(text)}
  148. </div>
  149. );
  150. },
  151. },
  152. {
  153. title: '进度',
  154. dataIndex: 'progress',
  155. render: (text, record, index) => {
  156. return (
  157. <div>
  158. {
  159. // 转换例如100%为数字100,如果text未定义,返回0
  160. <Progress stroke={record.status === "FAILURE"?"var(--semi-color-warning)":null} percent={text ? parseInt(text.replace('%', '')) : 0} showInfo={true}
  161. aria-label="drawing progress"/>
  162. }
  163. </div>
  164. );
  165. },
  166. },
  167. {
  168. title: '结果图片',
  169. dataIndex: 'image_url',
  170. render: (text, record, index) => {
  171. if (!text) {
  172. return '无';
  173. }
  174. return (
  175. <Button
  176. onClick={() => {
  177. setModalImageUrl(text); // 更新图片URL状态
  178. setIsModalOpenurl(true); // 打开模态框
  179. }}
  180. >
  181. 查看图片
  182. </Button>
  183. );
  184. }
  185. },
  186. {
  187. title: 'Prompt',
  188. dataIndex: 'prompt',
  189. render: (text, record, index) => {
  190. // 如果text未定义,返回替代文本,例如空字符串''或其他
  191. if (!text) {
  192. return '无';
  193. }
  194. return (
  195. <Typography.Text
  196. ellipsis={{showTooltip: true}}
  197. style={{width: 100}}
  198. onClick={() => {
  199. setModalContent(text);
  200. setIsModalOpen(true);
  201. }}
  202. >
  203. {text}
  204. </Typography.Text>
  205. );
  206. }
  207. },
  208. {
  209. title: 'PromptEn',
  210. dataIndex: 'prompt_en',
  211. render: (text, record, index) => {
  212. // 如果text未定义,返回替代文本,例如空字符串''或其他
  213. if (!text) {
  214. return '无';
  215. }
  216. return (
  217. <Typography.Text
  218. ellipsis={{showTooltip: true}}
  219. style={{width: 100}}
  220. onClick={() => {
  221. setModalContent(text);
  222. setIsModalOpen(true);
  223. }}
  224. >
  225. {text}
  226. </Typography.Text>
  227. );
  228. }
  229. },
  230. {
  231. title: '失败原因',
  232. dataIndex: 'fail_reason',
  233. render: (text, record, index) => {
  234. // 如果text未定义,返回替代文本,例如空字符串''或其他
  235. if (!text) {
  236. return '无';
  237. }
  238. return (
  239. <Typography.Text
  240. ellipsis={{showTooltip: true}}
  241. style={{width: 100}}
  242. onClick={() => {
  243. setModalContent(text);
  244. setIsModalOpen(true);
  245. }}
  246. >
  247. {text}
  248. </Typography.Text>
  249. );
  250. }
  251. }
  252. ];
  253. const [logs, setLogs] = useState([]);
  254. const [loading, setLoading] = useState(true);
  255. const [activePage, setActivePage] = useState(1);
  256. const [logCount, setLogCount] = useState(ITEMS_PER_PAGE);
  257. const [logType, setLogType] = useState(0);
  258. const isAdminUser = isAdmin();
  259. const [isModalOpenurl, setIsModalOpenurl] = useState(false);
  260. // 定义模态框图片URL的状态和更新函数
  261. const [modalImageUrl, setModalImageUrl] = useState('');
  262. let now = new Date();
  263. // 初始化start_timestamp为前一天
  264. const [inputs, setInputs] = useState({
  265. channel_id: '',
  266. mj_id: '',
  267. start_timestamp: timestamp2string(now.getTime() / 1000 - 2592000),
  268. end_timestamp: timestamp2string(now.getTime() / 1000 + 3600),
  269. });
  270. const {channel_id, mj_id, start_timestamp, end_timestamp} = inputs;
  271. const [stat, setStat] = useState({
  272. quota: 0,
  273. token: 0
  274. });
  275. const handleInputChange = (value, name) => {
  276. setInputs((inputs) => ({...inputs, [name]: value}));
  277. };
  278. const setLogsFormat = (logs) => {
  279. for (let i = 0; i < logs.length; i++) {
  280. logs[i].timestamp2string = timestamp2string(logs[i].created_at);
  281. logs[i].key = '' + logs[i].id;
  282. }
  283. // data.key = '' + data.id
  284. setLogs(logs);
  285. setLogCount(logs.length + ITEMS_PER_PAGE);
  286. // console.log(logCount);
  287. }
  288. const loadLogs = async (startIdx) => {
  289. setLoading(true);
  290. let url = '';
  291. let localStartTimestamp = Date.parse(start_timestamp);
  292. let localEndTimestamp = Date.parse(end_timestamp);
  293. if (isAdminUser) {
  294. url = `/api/mj/?p=${startIdx}&channel_id=${channel_id}&mj_id=${mj_id}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`;
  295. } else {
  296. url = `/api/mj/self/?p=${startIdx}&mj_id=${mj_id}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`;
  297. }
  298. const res = await API.get(url);
  299. const {success, message, data} = res.data;
  300. if (success) {
  301. if (startIdx === 0) {
  302. setLogsFormat(data);
  303. } else {
  304. let newLogs = [...logs];
  305. newLogs.splice(startIdx * ITEMS_PER_PAGE, data.length, ...data);
  306. setLogsFormat(newLogs);
  307. }
  308. } else {
  309. showError(message);
  310. }
  311. setLoading(false);
  312. };
  313. const pageData = logs.slice((activePage - 1) * ITEMS_PER_PAGE, activePage * ITEMS_PER_PAGE);
  314. const handlePageChange = page => {
  315. setActivePage(page);
  316. if (page === Math.ceil(logs.length / ITEMS_PER_PAGE) + 1) {
  317. // In this case we have to load more data and then append them.
  318. loadLogs(page - 1).then(r => {
  319. });
  320. }
  321. };
  322. const refresh = async () => {
  323. // setLoading(true);
  324. setActivePage(1);
  325. await loadLogs(0);
  326. };
  327. const copyText = async (text) => {
  328. if (await copy(text)) {
  329. showSuccess('已复制:' + text);
  330. } else {
  331. // setSearchKeyword(text);
  332. Modal.error({title: '无法复制到剪贴板,请手动复制', content: text});
  333. }
  334. }
  335. useEffect(() => {
  336. refresh().then();
  337. }, [logType]);
  338. return (
  339. <>
  340. <Layout>
  341. <Form layout='horizontal' style={{marginTop: 10}}>
  342. <>
  343. <Form.Input field="channel_id" label='渠道 ID' style={{width: 176}} value={channel_id}
  344. placeholder={'可选值'} name='channel_id'
  345. onChange={value => handleInputChange(value, 'channel_id')}/>
  346. <Form.Input field="mj_id" label='任务 ID' style={{width: 176}} value={mj_id}
  347. placeholder='可选值'
  348. name='mj_id'
  349. onChange={value => handleInputChange(value, 'mj_id')}/>
  350. <Form.DatePicker field="start_timestamp" label='起始时间' style={{width: 272}}
  351. initValue={start_timestamp}
  352. value={start_timestamp} type='dateTime'
  353. name='start_timestamp'
  354. onChange={value => handleInputChange(value, 'start_timestamp')}/>
  355. <Form.DatePicker field="end_timestamp" fluid label='结束时间' style={{width: 272}}
  356. initValue={end_timestamp}
  357. value={end_timestamp} type='dateTime'
  358. name='end_timestamp'
  359. onChange={value => handleInputChange(value, 'end_timestamp')}/>
  360. <Form.Section>
  361. <Button label='查询' type="primary" htmlType="submit" className="btn-margin-right"
  362. onClick={refresh}>查询</Button>
  363. </Form.Section>
  364. </>
  365. </Form>
  366. <Table style={{marginTop: 5}} columns={columns} dataSource={pageData} pagination={{
  367. currentPage: activePage,
  368. pageSize: ITEMS_PER_PAGE,
  369. total: logCount,
  370. pageSizeOpts: [10, 20, 50, 100],
  371. onPageChange: handlePageChange,
  372. }} loading={loading}/>
  373. <Modal
  374. visible={isModalOpen}
  375. onOk={() => setIsModalOpen(false)}
  376. onCancel={() => setIsModalOpen(false)}
  377. closable={null}
  378. bodyStyle={{height: '400px', overflow: 'auto'}} // 设置模态框内容区域样式
  379. width={800} // 设置模态框宽度
  380. >
  381. <p style={{whiteSpace: 'pre-line'}}>{modalContent}</p>
  382. </Modal>
  383. <ImagePreview
  384. src={modalImageUrl}
  385. visible={isModalOpenurl}
  386. onVisibleChange={(visible) => setIsModalOpenurl(visible)}
  387. />
  388. </Layout>
  389. </>
  390. );
  391. };
  392. export default LogsTable;