/ easyshell-web / src / utils / status.ts
status.ts
  1  import React from 'react';
  2  import {
  3    SyncOutlined,
  4    LoadingOutlined,
  5    CheckCircleOutlined,
  6    CloseCircleOutlined,
  7  } from '@ant-design/icons';
  8  
  9  export interface StatusEntry {
 10    color: string;
 11    text: string;
 12    icon?: React.ReactNode;
 13  }
 14  
 15  export interface StatusEntryWithLabel {
 16    color: string;
 17    label: string;
 18  }
 19  
 20  /**
 21   * Task status: 0-待执行, 1-执行中, 2-成功, 3-部分失败, 4-失败, 5-超时
 22   */
 23  export const taskStatusMap: Record<number, StatusEntry> = {
 24    0: { color: 'default', text: 'status.task.pending' },
 25    1: { color: 'processing', text: 'status.task.running' },
 26    2: { color: 'success', text: 'status.task.success' },
 27    3: { color: 'warning', text: 'status.task.partialFailed' },
 28    4: { color: 'error', text: 'status.task.failed' },
 29    5: { color: 'orange', text: 'status.task.timeout' },
 30  };
 31  
 32  /**
 33   * Job status (same values as task status)
 34   */
 35  export const jobStatusMap: Record<number, StatusEntry> = taskStatusMap;
 36  
 37  /**
 38   * Host/Agent status: 0-离线, 1-在线, 2-不稳定
 39   */
 40  export const hostStatusMap: Record<number, StatusEntry> = {
 41    0: { color: 'red', text: 'status.host.offline' },
 42    1: { color: 'green', text: 'status.host.online' },
 43    2: { color: 'orange', text: 'status.host.unstable' },
 44  };
 45  
 46  /**
 47   * Provision status for host deployment
 48   */
 49  export const provisionStatusMap: Record<string, StatusEntry> = {
 50    PENDING: { color: 'default', text: 'status.provision.pending', icon: React.createElement(SyncOutlined, { spin: true }) },
 51    CONNECTING: { color: 'processing', text: 'status.provision.connecting', icon: React.createElement(LoadingOutlined) },
 52    UPLOADING: { color: 'processing', text: 'status.provision.uploading', icon: React.createElement(LoadingOutlined) },
 53    INSTALLING: { color: 'processing', text: 'status.provision.installing', icon: React.createElement(LoadingOutlined) },
 54    STARTING: { color: 'processing', text: 'status.provision.starting', icon: React.createElement(LoadingOutlined) },
 55    SUCCESS: { color: 'success', text: 'status.provision.success', icon: React.createElement(CheckCircleOutlined) },
 56    FAILED: { color: 'error', text: 'status.provision.failed', icon: React.createElement(CloseCircleOutlined) },
 57    UNINSTALLING: { color: 'processing', text: 'status.provision.uninstalling', icon: React.createElement(LoadingOutlined) },
 58    UNINSTALL_FAILED: { color: 'error', text: 'status.provision.uninstallFailed', icon: React.createElement(CloseCircleOutlined) },
 59    UNINSTALLED: { color: 'default', text: 'status.provision.uninstalled', icon: React.createElement(CheckCircleOutlined) },
 60  };
 61  
 62  /**
 63   * Risk level mappings
 64   */
 65  export const riskLevelMap: Record<string, StatusEntry> = {
 66    LOW: { color: 'green', text: 'status.risk.low' },
 67    MEDIUM: { color: 'orange', text: 'status.risk.medium' },
 68    HIGH: { color: 'red', text: 'status.risk.high' },
 69    BANNED: { color: 'magenta', text: 'status.risk.banned' },
 70  };
 71  
 72  /**
 73   * Approval status mappings
 74   */
 75  export const approvalStatusMap: Record<string, StatusEntry> = {
 76    pending: { color: 'processing', text: 'status.approval.pending' },
 77    approved: { color: 'success', text: 'status.approval.approved' },
 78    rejected: { color: 'error', text: 'status.approval.rejected' },
 79  };
 80  
 81  /**
 82   * AI task type mappings
 83   */
 84  export const taskTypeMap: Record<string, StatusEntryWithLabel> = {
 85    inspect: { color: 'blue', label: 'status.taskType.inspect' },
 86    detect: { color: 'cyan', label: 'status.taskType.detect' },
 87    security: { color: 'orange', label: 'status.taskType.security' },
 88    disk: { color: 'purple', label: 'status.taskType.disk' },
 89    docker_health: { color: 'geekblue', label: 'status.taskType.dockerHealth' },
 90    custom: { color: 'default', label: 'status.taskType.custom' },
 91  };
 92  
 93  /**
 94   * Software type mappings for host detail
 95   */
 96  export const softwareTypeMap: Record<string, StatusEntry> = {
 97    database: { color: 'blue', text: 'status.software.database' },
 98    service: { color: 'green', text: 'status.software.service' },
 99    runtime: { color: 'purple', text: 'status.software.runtime' },
100    container_engine: { color: 'cyan', text: 'status.software.containerEngine' },
101    container: { color: 'orange', text: 'status.software.container' },
102    other: { color: 'default', text: 'status.software.other' },
103  };
104  
105  /**
106   * Report status mappings
107   */
108  export const reportStatusMap: Record<string, StatusEntryWithLabel> = {
109    running: { color: 'processing', label: 'status.report.running' },
110    success: { color: 'green', label: 'status.report.success' },
111    failed: { color: 'red', label: 'status.report.failed' },
112    partial: { color: 'orange', label: 'status.report.partial' },
113  };
114  
115  /**
116   * Get resource usage color based on threshold
117   * > 80% → red, > 60% → yellow, else → green
118   */
119  export const getResourceColor = (value: number): string => {
120    if (value > 80) return '#ff4d4f';
121    if (value > 60) return '#faad14';
122    return '#52c41a';
123  };
124  
125  export const provisionStepIndex: Record<string, number> = {
126    PENDING: -1,
127    CONNECTING: 0,
128    UPLOADING: 1,
129    INSTALLING: 2,
130    STARTING: 3,
131    SUCCESS: 4,
132    FAILED: -1,
133  };
134  
135  export function getProvisionStep(status: string): { current: number; status: 'process' | 'finish' | 'error' | 'wait' } {
136    if (status === 'SUCCESS') return { current: 4, status: 'finish' };
137    if (status === 'FAILED') return { current: 0, status: 'error' };
138    const idx = provisionStepIndex[status] ?? -1;
139    if (idx < 0) return { current: 0, status: 'wait' };
140    return { current: idx, status: 'process' };
141  }
142  
143  export const provisionStepItems = [
144    { title: 'status.provisionStep.connect' },
145    { title: 'status.provisionStep.upload' },
146    { title: 'status.provisionStep.install' },
147    { title: 'status.provisionStep.start' },
148    { title: 'status.provisionStep.complete' },
149  ];
150  
151  export const uninstallStepItems = [
152    { title: 'status.uninstallStep.connect' },
153    { title: 'status.uninstallStep.stopService' },
154    { title: 'status.uninstallStep.cleanup' },
155    { title: 'status.uninstallStep.complete' },
156  ];
157  
158  export const uninstallStepIndex: Record<string, number> = {
159    UNINSTALLING: 1,
160    UNINSTALLED: 3,
161    UNINSTALL_FAILED: -1,
162  };
163  
164  export function getUninstallStep(status: string): { current: number; status: 'process' | 'finish' | 'error' | 'wait' } {
165    if (status === 'UNINSTALLED') return { current: 3, status: 'finish' };
166    if (status === 'UNINSTALL_FAILED') return { current: 1, status: 'error' };
167    const idx = uninstallStepIndex[status] ?? -1;
168    if (idx < 0) return { current: 0, status: 'wait' };
169    return { current: idx, status: 'process' };
170  }