InstallProcess.tsx 9.0 KB
Newer Older
R
Rongfeng Fu 已提交
1
import { intl } from '@/utils/intl';
R
Rongfeng Fu 已提交
2 3 4 5 6 7 8 9
import { useEffect, useState } from 'react';
import { useModel } from 'umi';
import { ProCard } from '@ant-design/pro-components';
import useRequest from '@/utils/useRequest';
import {
  queryInstallStatus,
  queryInstallLog,
} from '@/services/ob-deploy-web/Deployments';
R
Rongfeng Fu 已提交
10
import { getErrorInfo } from '@/utils';
R
Rongfeng Fu 已提交
11 12
import lottie from 'lottie-web';
import NP from 'number-precision';
R
Rongfeng Fu 已提交
13 14 15 16 17 18 19 20
import videojs from 'video.js';
import 'video.js/dist/video-js.css';
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 已提交
21 22 23 24 25

let timerLogScroll: NodeJS.Timer;
let timerProgress: NodeJS.Timer;

export default function InstallProcess() {
R
Rongfeng Fu 已提交
26 27 28 29 30 31 32 33 34
  const {
    setCurrentStep,
    configData,
    installStatus,
    setInstallStatus,
    setErrorVisible,
    setErrorsList,
    errorsList,
  } = useModel('global');
R
Rongfeng Fu 已提交
35
  const name = configData?.components?.oceanbase?.appname;
R
Rongfeng Fu 已提交
36
  const progressCoverInitWidth = 282;
R
Rongfeng Fu 已提交
37 38 39
  const [toBottom, setToBottom] = useState(true);
  const [progress, setProgress] = useState(0);
  const [showProgress, setShowProgress] = useState(0);
R
Rongfeng Fu 已提交
40 41 42 43 44
  const [progressCoverStyle, setProgreddCoverStyle] = useState({
    width: progressCoverInitWidth,
    background: '#fff',
    borderRadius: '5px',
  });
R
Rongfeng Fu 已提交
45
  const [currentPage, setCurrentPage] = useState(true);
R
Rongfeng Fu 已提交
46 47 48
  const [statusData, setStatusData] = useState<API.TaskInfo>({});
  const [logData, setLogData] = useState<API.InstallLog>({});
  let Video: any;
R
Rongfeng Fu 已提交
49

R
Rongfeng Fu 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63
  const { run: fetchInstallStatus } = useRequest(queryInstallStatus, {
    onSuccess: ({ success, data }: API.OBResponseTaskInfo_) => {
      if (success) {
        setStatusData(data || {});
        clearInterval(timerProgress);
        if (data?.status !== 'RUNNING') {
          setInstallStatus(data?.status);
          setCurrentPage(false);
          setTimeout(() => {
            setCurrentStep(6);
            setErrorVisible(false);
            setErrorsList([]);
          }, 2000);
        } else {
R
Rongfeng Fu 已提交
64 65 66 67
          setTimeout(() => {
            fetchInstallStatus({ name });
          }, 1000);
        }
R
Rongfeng Fu 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        const newProgress = NP.divide(data?.finished, data?.total).toFixed(2);
        setProgress(newProgress);
        let step = NP.minus(newProgress, progress);
        let stepNum = 1;
        timerProgress = setInterval(() => {
          const currentProgressNumber = NP.plus(
            progress,
            NP.times(NP.divide(step, 100), stepNum),
          );

          if (currentProgressNumber >= 1) {
            clearInterval(timerProgress);
          } else {
            stepNum += 1;
            setShowProgress(currentProgressNumber);
          }
        }, 10);
      }
R
Rongfeng Fu 已提交
86
    },
R
Rongfeng Fu 已提交
87 88 89 90 91 92 93 94 95 96 97
    onError: (e: any) => {
      if (currentPage) {
        setTimeout(() => {
          fetchInstallStatus({ name });
        }, 1000);
      }
      const errorInfo = getErrorInfo(e);
      setErrorVisible(true);
      setErrorsList([...errorsList, errorInfo]);
    },
  });
R
Rongfeng Fu 已提交
98

R
Rongfeng Fu 已提交
99 100
  const { run: handleInstallLog } = useRequest(queryInstallLog, {
    onSuccess: ({ success, data }: API.OBResponseInstallLog_) => {
R
Rongfeng Fu 已提交
101
      if (success && installStatus === 'RUNNING') {
R
Rongfeng Fu 已提交
102
        setLogData(data || {});
R
Rongfeng Fu 已提交
103 104 105 106 107
        setTimeout(() => {
          handleInstallLog({ name });
        }, 1000);
      }
    },
R
Rongfeng Fu 已提交
108
    onError: (e: any) => {
R
Rongfeng Fu 已提交
109 110 111 112 113
      if (installStatus === 'RUNNING' && currentPage) {
        setTimeout(() => {
          handleInstallLog({ name });
        }, 1000);
      }
R
Rongfeng Fu 已提交
114 115 116
      const errorInfo = getErrorInfo(e);
      setErrorVisible(true);
      setErrorsList([...errorsList, errorInfo]);
R
Rongfeng Fu 已提交
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
    },
  });

  const toLogBottom = () => {
    const log = document.getElementById('installLog');
    if (log) {
      log.scrollTop = log.scrollHeight;
    }
  };

  const handleScroll = (e?: any) => {
    e = e || window.event;
    const dom = e.target;
    if (dom.scrollTop + dom.clientHeight === dom.scrollHeight) {
      setToBottom(true);
    } else {
      setToBottom(false);
    }
  };

  const getAnimate = () => {
    const computerAnimate = document.querySelector('.computer-animate');
    const progressAnimate = document.querySelector('.progress-animate');
    const spacemanAnimate = document.querySelector('.spaceman-animate');
    const sqlAnimate = document.querySelector('.database-animate');

R
Rongfeng Fu 已提交
143 144 145 146 147 148 149 150 151
    if (progressAnimate) {
      Video = videojs(progressAnimate, {
        controls: false,
        autoplay: true,
        loop: true,
        preload: 'auto',
      });
    }

R
Rongfeng Fu 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    lottie.loadAnimation({
      prefetch: true,
      container: computerAnimate,
      renderer: 'svg',
      loop: true,
      autoplay: true,
      path: '/assets/computer/data.json',
    });

    lottie.loadAnimation({
      prefetch: true,
      container: spacemanAnimate,
      renderer: 'svg',
      loop: true,
      autoplay: true,
      path: '/assets/spaceman/data.json',
    });
R
Rongfeng Fu 已提交
169

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
    lottie.loadAnimation({
      prefetch: true,
      container: sqlAnimate,
      renderer: 'svg',
      loop: true,
      autoplay: true,
      path: '/assets/database/data.json',
    });
  };

  useEffect(() => {
    if (name) {
      fetchInstallStatus({ name });
      handleInstallLog({ name });
    }
  }, [name]);

  useEffect(() => {
    getAnimate();
    const log = document.querySelector('#installLog');
    log.addEventListener('scroll', handleScroll);
    return () => {
      log.removeEventListener('DOMMouseScroll', handleScroll);
      clearInterval(timerLogScroll);
      clearInterval(timerProgress);
R
Rongfeng Fu 已提交
195
      Video.dispose();
R
Rongfeng Fu 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208
    };
  }, []);

  useEffect(() => {
    if (toBottom) {
      toLogBottom();
      timerLogScroll = setInterval(() => toLogBottom());
    } else {
      clearInterval(timerLogScroll);
    }
  }, [toBottom]);

  useEffect(() => {
R
Rongfeng Fu 已提交
209 210 211 212 213 214 215 216 217 218 219 220
    let newCoverStyle: any = { ...progressCoverStyle };
    const newCoverWidth = NP.times(
      NP.minus(1, showProgress),
      progressCoverInitWidth,
    );

    if (showProgress > 0) {
      newCoverStyle = {
        width: `${newCoverWidth}px`,
        background:
          'linear-gradient( to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1) 10px, rgba(255, 255, 255, 1) )',
      };
R
Rongfeng Fu 已提交
221
    }
R
Rongfeng Fu 已提交
222 223 224 225 226 227 228 229 230 231 232 233
    setProgreddCoverStyle(newCoverStyle);
  }, [showProgress]);

  const getText = (name?: string) => {
    return intl.formatMessage(
      {
        id: 'OBD.pages.components.InstallProcess.DeployingName',
        defaultMessage: '正在部署 {name}',
      },
      { name: name },
    );
  };
R
Rongfeng Fu 已提交
234 235 236 237 238

  return (
    <ProCard direction="column" className="card-padding-bottom-24">
      <ProCard>
        <div className={styles.progressEffectContainer}>
R
Rongfeng Fu 已提交
239 240 241 242 243 244
          <div className={styles.deployTitle}>
            {intl.formatMessage({
              id: 'OBD.pages.components.InstallProcess.Deploying',
              defaultMessage: '部署中...',
            })}
          </div>
R
Rongfeng Fu 已提交
245 246
          <div className={styles.computer}>
            <div
R
Rongfeng Fu 已提交
247
              className={`computer-animate ${styles.computerAnimate} `}
R
Rongfeng Fu 已提交
248 249 250 251
              data-anim-path="/assets/computer/data.json"
            ></div>
          </div>
          <div className={styles.progress}>
R
Rongfeng Fu 已提交
252 253 254 255 256 257
            <video
              className={`${styles.progressVedio} progress-animate video-js`}
              muted
            >
              <source src="/assets/progress/data.mp4" type="video/mp4"></source>
            </video>
R
Rongfeng Fu 已提交
258
            <div
R
Rongfeng Fu 已提交
259 260 261 262 263
              className={styles.progressCover}
              style={{ ...progressCoverStyle }}
            >
              <div className={styles.progressStart}></div>
            </div>
R
Rongfeng Fu 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
          </div>
          <div className={styles.spaceman}>
            <div
              className={`spaceman-animate ${styles.spacemanAnimate}`}
              data-anim-path="/assets/spaceman/data.json"
            ></div>
          </div>
          <div className={styles.database}>
            <div
              className={`database-animate ${styles.sqlAnimate}`}
              data-anim-path="/assets/database/data.json"
            ></div>
          </div>
        </div>
        <span
          className={styles.deploymentName}
          data-aspm-click="c307512.d317290"
R
Rongfeng Fu 已提交
281 282 283 284
          data-aspm-desc={intl.formatMessage({
            id: 'OBD.pages.components.InstallProcess.DeployingDeploying',
            defaultMessage: '部署中-正在部署',
          })}
R
Rongfeng Fu 已提交
285 286 287
          data-aspm-param={``}
          data-aspm-expo
        >
R
Rongfeng Fu 已提交
288
          {getText(statusData?.current)}
R
Rongfeng Fu 已提交
289 290
        </span>
      </ProCard>
R
Rongfeng Fu 已提交
291 292 293 294 295 296 297
      <ProCard
        title={intl.formatMessage({
          id: 'OBD.pages.components.InstallProcess.DeploymentLogs',
          defaultMessage: '部署日志',
        })}
        className={styles.installSubCard}
      >
R
Rongfeng Fu 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
        <pre className={styles.installLog} id="installLog">
          {logData?.log}
          {installStatus === 'RUNNING' ? (
            <div className={styles.shapeContainer}>
              <div className={styles.shape}></div>
              <div className={styles.shape}></div>
              <div className={styles.shape}></div>
              <div className={styles.shape}></div>
            </div>
          ) : null}
        </pre>
      </ProCard>
    </ProCard>
  );
}