ResetOffsetDrawer.tsx 5.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import React, { useState, useEffect } from 'react';
import { Button, DatePicker, Drawer, Form, notification, Radio, Utils, Space, Divider, message } from 'knowdesign';
import { useParams } from 'react-router-dom';
import EditTable from '../TestingProduce/component/EditTable';
import Api from '@src/api/index';
import moment from 'moment';

const CustomSelectResetTime = (props: { value?: string; onChange?: (val: Number | String) => void }) => {
  const { value, onChange } = props;
  const [timeSetMode, setTimeSetMode] = useState('newest');
  useEffect(() => {
    onChange('newest');
  }, []);
  return (
    <>
      <Radio.Group
        style={{
          marginBottom: 20,
        }}
        onChange={(e) => {
          setTimeSetMode(e.target.value);
22 23
          if (e.target.value === 'newest' || e.target.value === 'oldest') {
            onChange(e.target.value);
24 25 26 27 28
          }
        }}
        value={timeSetMode}
      >
        <Radio value={'newest'}>最新Offset</Radio>
29
        <Radio value={'oldest'}>最旧Offset</Radio>
30 31 32 33
        <Radio value={'custom'}>自定义</Radio>
      </Radio.Group>
      {timeSetMode === 'custom' && (
        <DatePicker
34
          value={moment(value === 'newest' || value === 'oldest' ? Date.now() : value)}
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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
          style={{ width: '100%' }}
          showTime={true}
          onChange={(v) => {
            onChange(v.valueOf());
          }}
        ></DatePicker>
      )}
    </>
  );
};

export default (props: any) => {
  const { record, visible, setVisible } = props;
  const routeParams = useParams<{
    clusterId: string;
  }>();
  const [form] = Form.useForm();
  const defaultResetType = 'assignedTime';
  const [resetType, setResetType] = useState(defaultResetType);
  const customFormRef: any = React.createRef();
  const clusterPhyId = Number(routeParams.clusterId);
  const [partitionIdList, setPartitionIdList] = useState([]);
  useEffect(() => {
    form.setFieldsValue({
      resetType: defaultResetType,
    });
  }, []);

  useEffect(() => {
    visible &&
      Utils.request(Api.getTopicsMetaData(record?.topicName, +routeParams.clusterId))
        .then((res: any) => {
          const partitionLists = (res?.partitionIdList || []).map((item: any) => {
            return {
              label: item,
              value: item,
            };
          });
          setPartitionIdList(partitionLists);
        })
        .catch((err) => {
          message.error(err);
        });
  }, [visible]);
  const confirm = () => {
    let tableData;
    if (customFormRef.current) {
      tableData = customFormRef.current.getTableData();
    }
    const formData = form.getFieldsValue();
    let resetParams: any = {
      clusterId: clusterPhyId,
      createIfNotExist: false,
      groupName: record.groupName,
      topicName: record.topicName,
    };
    if (formData.resetType === 'assignedTime') {
92
      resetParams.resetType = formData.timestamp === 'newest' ? 0 : formData.timestamp === 'oldest' ? 1 : 2;
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188
      if (resetParams.resetType === 2) {
        resetParams.timestamp = formData.timestamp;
      }
    }
    if (formData.resetType === 'partition') {
      resetParams.resetType = 3;
      resetParams.offsetList = tableData
        ? tableData.map((item: { key: string; value: string }) => ({ partitionId: item.key, offset: item.value }))
        : [];
    }
    Utils.put(Api.resetGroupOffset(), resetParams).then((data) => {
      if (data === null) {
        notification.success({
          message: '重置offset成功',
        });
        setVisible(false);
      } else {
        notification.error({
          message: '重置offset失败',
        });
        setVisible(false);
      }
    });
  };
  return (
    <>
      <Drawer
        title="重置Offset"
        width={480}
        visible={visible}
        maskClosable={false}
        extra={
          <Space>
            <Button
              size="small"
              style={{ marginRight: 8 }}
              onClick={(_) => {
                setVisible(false);
              }}
            >
              取消
            </Button>
            <Button size="small" type="primary" onClick={confirm}>
              确定
            </Button>
            <Divider type="vertical" />
          </Space>
        }
        className="cluster-detail-consumer-resetoffset"
        onClose={(_) => {
          setVisible(false);
        }}
      >
        <Form form={form} labelCol={{ span: 5 }} layout="vertical" className="reset-offset-form">
          <Form.Item name="resetType" label="重置类型" required>
            <Radio.Group
              defaultValue="assignedTime"
              value={resetType}
              onChange={(e) => {
                setResetType(e.target.value);
              }}
            >
              <Radio value={'assignedTime'}>重置到指定时间</Radio>
              <Radio value={'partition'}>重置分区</Radio>
            </Radio.Group>
          </Form.Item>
          {resetType === 'assignedTime' && (
            <Form.Item name="timestamp" label="时间" required>
              <CustomSelectResetTime />
            </Form.Item>
          )}
          {resetType === 'partition' && (
            <Form.Item name="partition" label="分区及偏移" required>
              <EditTable
                ref={customFormRef}
                colCustomConfigs={[
                  {
                    title: 'PartitionID',
                    inputType: 'select',
                    placeholder: '请输入Partition',
                    options: partitionIdList,
                  },
                  {
                    title: 'Offset',
                    inputType: 'number',
                    placeholder: '请输入Offset',
                  },
                ]}
              ></EditTable>
            </Form.Item>
          )}
        </Form>
      </Drawer>
    </>
  );
};