// ScriptManager 共享类型与工具函数 // Logan.Li import React from 'react'; import { Clock, Loader2, CheckCircle, XCircle } from 'lucide-react'; import { Badge } from '@/components/ui/badge'; import type { JsonData } from '@/types/common'; // ---- 类型定义 ---- export interface Script { id: number; name: string; code: string; description: string; type: string; // shell | python | powershell | yaml content: string; timeout: number; version: number; run_count: number; created_at: string; updated_at: string; } export interface ExecutionRecord { id: number; task_type: string; task_id: number; task_name: string; status: string; output: string; duration: number; started_at: string | null; finished_at: string | null; created_at: string; } // 将后端返回的脚本原始对象映射为前端结构 export function mapScript(raw: JsonData): Script { return { id: raw.id, name: raw.name, code: raw.code || '', description: raw.description || '', type: raw.type || 'shell', content: raw.content || '', timeout: raw.timeout || 300, version: raw.version || 1, run_count: raw.run_count || 0, created_at: raw.created_at, updated_at: raw.updated_at, }; } export const scriptTypes = [ { value: 'shell', label: 'Shell', sample: '#!/bin/bash\n# 系统巡检脚本\necho "===== 系统信息 ====="\nuname -a\necho ""\necho "===== 磁盘使用 ====="\ndf -h\necho ""\necho "===== 内存使用 ====="\nfree -h\n' }, { value: 'python', label: 'Python', sample: '#!/usr/bin/env python3\n# 主机信息采集\nimport platform, socket\nprint("主机名:", socket.gethostname())\nprint("系统:", platform.platform())\nprint("Python:", platform.python_version())\n' }, ]; const typeColors: Record = { shell: 'border-green-500/30 text-green-500 bg-green-500/10', bash: 'border-green-500/30 text-green-500 bg-green-500/10', python: 'border-blue-500/30 text-blue-500 bg-blue-500/10', powershell: 'border-cyan-500/30 text-cyan-500 bg-cyan-500/10', yaml: 'border-purple-500/30 text-purple-500 bg-purple-500/10', }; export const getTypeBadge = (type: string) => ( {(type || 'shell').toUpperCase()} ); // 生成唯一脚本编码 export const genCode = () => `sc-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`; // 执行状态 Badge export function getStatusBadge(status: string) { switch (status) { case 'pending': return 等待中; case 'running': return 执行中; case 'success': return 成功; case 'failed': return 失败; default: return {status || '未知'}; } } // 日期格式化 export const formatDate = (s: string | null) => { if (!s) return '-'; const d = new Date(s); return Number.isNaN(d.getTime()) ? '-' : d.toLocaleString('zh-CN', { hour12: false }); };