Messages.tsx 7.0 KB
Newer Older
Z
zengqiao 已提交
1 2 3 4 5 6 7 8 9 10
import React, { useState, useEffect } from 'react';
import { Alert, Button, Checkbox, Form, IconFont, Input, ProTable, Select, Tooltip, Utils } from 'knowdesign';
import Api from '@src/api';
import { useParams, useHistory } from 'react-router-dom';
import { getTopicMessagesColmns } from './config';

const { request } = Utils;
const defaultParams: any = {
  truncate: true,
  maxRecords: 100,
Z
zengqiao 已提交
11
  pullTimeoutUnitMs: 5000,
Z
zengqiao 已提交
12
  // filterPartitionId: 1,
13
  filterOffsetReset: 0,
Z
zengqiao 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
};
const defaultpaPagination = {
  current: 1,
  pageSize: 10,
  position: 'bottomRight',
  showSizeChanger: true,
  pageSizeOptions: ['10', '20', '50', '100'],
};
const TopicMessages = (props: any) => {
  const { hashData } = props;
  const urlParams = useParams<any>(); // 获取地址栏参数
  const history = useHistory();
  const [loading, setLoading] = useState(false);
  const [data, setData] = useState([]);
  const [params, setParams] = useState(defaultParams);
  const [partitionIdList, setPartitionIdList] = useState([]);
  const [pagination, setPagination] = useState<any>(defaultpaPagination);
  const [form] = Form.useForm();

33 34
  // 获取消息开始位置
  const offsetResetList = [
35 36
    { label: 'latest', value: 0 },
    { label: 'earliest', value: 1 },
37 38
  ];

Z
zengqiao 已提交
39 40 41 42 43 44
  // 默认排序
  const defaultSorter = {
    sortField: 'timestampUnitMs',
    sortType: 'desc',
  };

45 46
  const [sorter, setSorter] = useState<any>(defaultSorter);

Z
zengqiao 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60
  // 请求接口获取数据
  const genData = async () => {
    if (urlParams?.clusterId === undefined || hashData?.topicName === undefined) return;
    setLoading(true);
    request(Api.getTopicMessagesMetadata(hashData?.topicName, urlParams?.clusterId)).then((res: any) => {
      // console.log(res, 'metadata');
      const newPartitionIdList = res?.partitionIdList.map((item: any) => {
        return {
          label: item,
          value: item,
        };
      });
      setPartitionIdList(newPartitionIdList || []);
    });
61
    request(Api.getTopicMessagesList(hashData?.topicName, urlParams?.clusterId), { data: { ...params, ...sorter }, method: 'POST' })
Z
zengqiao 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
      .then((res: any) => {
        // setPagination({
        //   current: res.pagination?.pageNo,
        //   pageSize: res.pagination?.pageSize,
        //   total: res.pagination?.total,
        // });
        setData(res || []);
        setLoading(false);
      })
      .catch((err) => {
        setLoading(false);
      });
  };

  // 查询
  const onFinish = (formData: any) => {
    setParams({ ...params, ...formData, filterKey: formData?.filterKey ? formData?.filterKey : undefined });
  };

  // 截断
  const checkBoxChange = (e: any) => {
    setParams({ ...params, truncate: e.target.checked });
  };

  // 刷新
  const refreshClick = () => {
    // genData();
    // form.resetFields();
    setPagination(defaultpaPagination);
    genData();
  };

  // 跳转Consume
  const jumpConsume = () => {
    history.push(`/cluster/${urlParams?.clusterId}/testing/consumer`);
  };

99
  const onTableChange = (pagination: any, filters: any, sorter: any, extra: any) => {
Z
zengqiao 已提交
100
    setPagination(pagination);
101
    // 只有排序事件时,触发重新请求后端数据
102
    if (extra.action === 'sort') {
103 104
      setSorter({
        sortField: sorter.field || '',
105
        sortType: sorter.order ? sorter.order.substring(0, sorter.order.indexOf('end')) : '',
106 107
      });
    }
Z
zengqiao 已提交
108 109 110 111 112 113 114
    // const asc = sorter?.order && sorter?.order === 'ascend' ? true : false;
    // const sortColumn = sorter.field && toLine(sorter.field);
    // genData({ pageNo: pagination.current, pageSize: pagination.pageSize, filters, asc, sortColumn, queryTerm: searchResult, ...allParams });
  };

  useEffect(() => {
    props.positionType === 'Messages' && genData();
115
  }, [props, params, sorter]);
Z
zengqiao 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

  return (
    <>
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
        <div>
          <span
            style={{ display: 'inline-block', padding: '0 10px', marginRight: '10px', borderRight: '1px solid #ccc', fontSize: '13px' }}
            onClick={refreshClick}
          >
            <i className="iconfont icon-shuaxin1" style={{ fontSize: '13px', cursor: 'pointer' }} />
          </span>
          <span style={{ fontSize: '13px' }}>
            <Checkbox checked={params.truncate} onChange={checkBoxChange}>
              是否要截断数据
            </Checkbox>
          </span>
          <Tooltip title={'截断数据后只展示前1024字符的数据'}>
            <IconFont style={{ fontSize: '14px' }} type="icon-zhushi" />
          </Tooltip>
        </div>
        <div className="messages-query">
          <Form form={form} layout="inline" onFinish={onFinish}>
138 139
            <Form.Item name="filterOffsetReset">
              <Select
140 141 142 143 144
                options={offsetResetList}
                size="small"
                style={{ width: '120px' }}
                className={'detail-table-select'}
                placeholder="请选择offset"
145 146
              />
            </Form.Item>
Z
zengqiao 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
            <Form.Item name="filterPartitionId">
              <Select
                options={partitionIdList}
                size="small"
                style={{ width: '140px' }}
                className={'detail-table-select'}
                placeholder="请选择分区"
              />
            </Form.Item>
            <Form.Item name="filterKey">
              <Input size="small" style={{ width: '140px' }} className={'detail-table-input'} placeholder="请输入key" />
            </Form.Item>
            <Form.Item name="filterValue">
              <Input size="small" style={{ width: '140px' }} className={'detail-table-input'} placeholder="请输入value" />
            </Form.Item>
            <Form.Item>
              <Button size="small" type="primary" ghost htmlType="submit">
                查询
              </Button>
            </Form.Item>
          </Form>
        </div>
      </div>
      <div>
        <Alert
          style={{ margin: '12px 0 4px', padding: '7px 12px', background: '#FFF9E6' }}
          message={
            <div>
175 176 177 178 179 180 181 182
              此处展示 Topic 最近的 100 条 messages。
              {process.env.BUSINESS_VERSION ? (
                <span>
                  若想获取其他 messages,可前往 <a onClick={jumpConsume}>Produce&Consume</a> 进行操作
                </span>
              ) : (
                ''
              )}
Z
zengqiao 已提交
183 184 185 186 187 188 189 190 191 192
            </div>
          }
          type="warning"
          closable
        />
      </div>
      <ProTable
        showQueryForm={false}
        tableProps={{
          showHeader: false,
193
          rowKey: 'offset',
Z
zengqiao 已提交
194 195 196 197 198 199 200 201 202 203
          loading: loading,
          columns: getTopicMessagesColmns(),
          dataSource: data,
          paginationProps: pagination,
          // noPagination: true,
          attrs: {
            // className: 'frameless-table', // 纯无边框表格类名
            bordered: false,
            onChange: onTableChange,
            scroll: { x: 'max-content' },
204
            sortDirections: ['descend', 'ascend', 'default'],
Z
zengqiao 已提交
205 206 207 208 209 210 211 212
          },
        }}
      />
    </>
  );
};

export default TopicMessages;