DeleteDeployModal.tsx 7.8 KB
Newer Older
R
Rongfeng Fu 已提交
1
import { intl } from '@/utils/intl';
R
Rongfeng Fu 已提交
2 3 4 5 6
import { useEffect, useState } from 'react';
import { useModel } from 'umi';
import { Modal, Progress, message } from 'antd';
import { getDestroyTaskInfo } from '@/services/ob-deploy-web/Deployments';
import useRequest from '@/utils/useRequest';
R
Rongfeng Fu 已提交
7
import { checkLowVersion, getErrorInfo } from '@/utils';
R
Rongfeng Fu 已提交
8
import NP from 'number-precision';
R
Rongfeng Fu 已提交
9
import { oceanbaseComponent, obproxyComponent } from '../constants';
R
Rongfeng Fu 已提交
10 11 12 13 14 15
import { getLocale } from 'umi';
import EnStyles from './indexEn.less';
import ZhStyles from './indexZh.less';

const locale = getLocale();
const styles = locale === 'zh-CN' ? ZhStyles : EnStyles;
R
Rongfeng Fu 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

interface Props {
  visible: boolean;
  name: string;
  onCancel: () => void;
  setOBVersionValue: (value: string) => void;
}

let timerProgress: NodeJS.Timer;
let timerFetch: NodeJS.Timer;

const statusConfig = {
  RUNNING: 'normal',
  SUCCESSFUL: 'success',
  FAILED: 'exception',
};

export default function DeleteDeployModal({
  visible,
  name,
  onCancel,
  setOBVersionValue,
}: Props) {
  const {
    setConfigData,
    setIsDraft,
    setClusterMore,
    setComponentsMore,
    componentsVersionInfo,
    setComponentsVersionInfo,
    setCurrentType,
    getInfoByName,
    setLowVersion,
R
Rongfeng Fu 已提交
49 50 51
    setErrorVisible,
    setErrorsList,
    errorsList,
R
Rongfeng Fu 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  } = useModel('global');

  const [status, setStatus] = useState('RUNNING');
  const [progress, setProgress] = useState(0);
  const [showProgress, setShowProgress] = useState(0);
  const [isFinished, setIsFinished] = useState(false);

  const { run: fetchDestroyTaskInfo } = useRequest(getDestroyTaskInfo, {
    onSuccess: async ({ success, data }: API.OBResponseTaskInfo_) => {
      if (success) {
        if (data?.status !== 'RUNNING') {
          clearInterval(timerFetch);
        }
        clearInterval(timerProgress);
        if (data?.status === 'RUNNING') {
          const newProgress = NP.times(
            NP.divide(data?.finished, data?.total),
            100,
          );
R
Rongfeng Fu 已提交
71

R
Rongfeng Fu 已提交
72 73 74 75 76 77 78
          setProgress(newProgress);
          let step = NP.minus(newProgress, progress);
          let stepNum = 1;
          timerProgress = setInterval(() => {
            setShowProgress(
              NP.plus(progress, NP.times(NP.divide(step, 100), stepNum)),
            );
R
Rongfeng Fu 已提交
79

R
Rongfeng Fu 已提交
80 81 82 83 84 85 86 87 88
            stepNum += 1;
          }, 10);
        } else if (data?.status === 'SUCCESSFUL') {
          let step = NP.minus(100, progress);
          let stepNum = 1;
          timerProgress = setInterval(() => {
            setShowProgress(
              NP.plus(progress, NP.times(NP.divide(step, 100), stepNum)),
            );
R
Rongfeng Fu 已提交
89

R
Rongfeng Fu 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
            stepNum += 1;
          }, 10);
          try {
            const { success: nameSuccess, data: nameData } =
              await getInfoByName({ name });
            if (nameSuccess) {
              const { config } = nameData;
              const { components = {} } = config;
              setConfigData(config || {});
              setLowVersion(checkLowVersion(components?.oceanbase?.version));
              setClusterMore(!!components?.oceanbase?.parameters?.length);
              setComponentsMore(!!components?.obproxy?.parameters?.length);
              setIsDraft(true);
              setCurrentType(
                components?.oceanbase && !components?.obproxy ? 'ob' : 'all',
              );
R
Rongfeng Fu 已提交
106

R
Rongfeng Fu 已提交
107 108 109 110 111 112 113 114 115
              const newSelectedVersionInfo = componentsVersionInfo?.[
                oceanbaseComponent
              ]?.dataSource?.filter(
                (item: any) => item.md5 === components?.oceanbase?.package_hash,
              )[0];
              if (newSelectedVersionInfo) {
                setOBVersionValue(
                  `${components?.oceanbase?.version}-${components?.oceanbase?.release}-${components?.oceanbase?.package_hash}`,
                );
R
Rongfeng Fu 已提交
116

R
Rongfeng Fu 已提交
117 118 119 120 121 122 123 124 125 126 127
                let currentObproxyVersionInfo = {};
                componentsVersionInfo?.[
                  obproxyComponent
                ]?.dataSource?.some((item: API.service_model_components_ComponentInfo) => {
                  if (item?.version_type === newSelectedVersionInfo?.version_type) {
                    currentObproxyVersionInfo = item;
                    return true;
                  }
                  return false
                });

R
Rongfeng Fu 已提交
128 129 130 131 132 133
                setComponentsVersionInfo({
                  ...componentsVersionInfo,
                  [oceanbaseComponent]: {
                    ...componentsVersionInfo[oceanbaseComponent],
                    ...newSelectedVersionInfo,
                  },
R
Rongfeng Fu 已提交
134 135 136 137
                  [obproxyComponent]: {
                    ...componentsVersionInfo[obproxyComponent],
                    ...currentObproxyVersionInfo
                  }
R
Rongfeng Fu 已提交
138 139 140 141 142 143 144
                });
              }
              setTimeout(() => {
                onCancel();
              }, 2000);
            } else {
              setIsDraft(false);
R
Rongfeng Fu 已提交
145 146 147 148 149 150
              message.error(
                intl.formatMessage({
                  id: 'OBD.pages.components.DeleteDeployModal.FailedToObtainConfigurationInformation',
                  defaultMessage: '获取配置信息失败',
                }),
              );
R
Rongfeng Fu 已提交
151 152 153
              onCancel();
            }
          } catch (e: any) {
R
Rongfeng Fu 已提交
154 155 156
            const errorInfo = getErrorInfo(e);
            setErrorVisible(true);
            setErrorsList([...errorsList, errorInfo]);
R
Rongfeng Fu 已提交
157 158 159 160 161 162 163 164
          }
        } else {
          message.error(data?.msg);
          onCancel();
        }
        setStatus(data?.status);
      }
    },
R
Rongfeng Fu 已提交
165 166 167 168 169
    onError: (e: any) => {
      const errorInfo = getErrorInfo(e);
      setErrorVisible(true);
      setErrorsList([...errorsList, errorInfo]);
    },
R
Rongfeng Fu 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  });

  useEffect(() => {
    fetchDestroyTaskInfo({ name });
    timerFetch = setInterval(() => {
      fetchDestroyTaskInfo({ name });
    }, 1000);
    return () => {
      clearInterval(timerProgress);
      clearInterval(timerFetch);
    };
  }, []);

  useEffect(() => {
    if (status !== 'RUNNING') {
      setTimeout(() => {
        clearInterval(timerProgress);
        setIsFinished(true);
      }, 1000);
    }
  }, [status]);

  return (
    <Modal
      open={visible}
      closable={false}
      maskClosable={false}
      footer={false}
      width={424}
    >
      <div className={styles.deleteDeployContent}>
        {isFinished ? (
          <>
            <div
              className={styles.deleteDeployText}
              style={{ marginBottom: '25px' }}
            >
              {status === 'SUCCESSFUL'
R
Rongfeng Fu 已提交
208
                ? intl.formatMessage({
R
Rongfeng Fu 已提交
209 210 211
                  id: 'OBD.pages.components.DeleteDeployModal.FailedHistoryDeploymentEnvironmentCleared',
                  defaultMessage: '清理失败历史部署环境成功',
                })
R
Rongfeng Fu 已提交
212
                : intl.formatMessage({
R
Rongfeng Fu 已提交
213 214 215
                  id: 'OBD.pages.components.DeleteDeployModal.FailedToCleanUpThe',
                  defaultMessage: '清理失败历史部署环境失败',
                })}
R
Rongfeng Fu 已提交
216 217 218 219 220 221 222 223 224 225 226 227
            </div>
            <Progress
              className={styles.deleteDeployProgress}
              type="circle"
              percent={100}
              width={48}
              status={statusConfig[status]}
            />
          </>
        ) : (
          <>
            <div className={styles.deleteDeployText}>
R
Rongfeng Fu 已提交
228 229 230 231 232 233 234 235 236 237 238
              {intl.formatMessage({
                id: 'OBD.pages.components.DeleteDeployModal.CleaningFailedHistoricalDeploymentEnvironments',
                defaultMessage: '正在清理失败的历史部署环境',
              })}

              <div>
                {intl.formatMessage({
                  id: 'OBD.pages.components.DeleteDeployModal.PleaseWaitPatiently',
                  defaultMessage: '请耐心等待',
                })}
              </div>
R
Rongfeng Fu 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251
            </div>
            <Progress
              className={styles.deleteDeployProgress}
              type="circle"
              percent={showProgress}
              width={48}
            />
          </>
        )}
      </div>
    </Modal>
  );
}