在前面的章节中,我们已经完成了低代码平台的核心功能开发。本章将重点介绍如何将低代码平台部署到生产环境,以及如何进行有效的运维管理。我们将涵盖容器化部署、微服务架构、监控告警、日志管理、性能优化、安全加固、备份恢复等关键主题。

14.1 部署架构概览

14.1.1 部署管理器接口


## 14.4 配置管理

```typescript
// 配置管理器接口
interface ConfigurationManager {
  // 网络配置
  applyNetworkConfig(environmentId: string, config: NetworkingConfig): Promise<void>;
  removeNetworkConfig(environmentId: string): Promise<void>;
  
  // 安全配置
  applySecurityConfig(environmentId: string, config: SecurityConfig): Promise<void>;
  removeSecurityConfig(environmentId: string): Promise<void>;
  
  // 配置模板
  createConfigTemplate(template: ConfigTemplate): Promise<ConfigTemplate>;
  getConfigTemplates(query?: ConfigTemplateQuery): Promise<ConfigTemplate[]>;
  applyConfigTemplate(templateId: string, environmentId: string, variables?: Record<string, any>): Promise<void>;
  
  // 配置验证
  validateConfig(config: any, schema: ConfigSchema): Promise<ValidationResult>;
  
  // 配置同步
  syncConfig(environmentId: string): Promise<void>;
}

// Kubernetes配置管理器实现
class KubernetesConfigManager implements ConfigurationManager {
  private k8sApi: k8s.CoreV1Api;
  private networkingApi: k8s.NetworkingV1Api;
  private rbacApi: k8s.RbacAuthorizationV1Api;
  private templates: Map<string, ConfigTemplate> = new Map();
  
  constructor() {
    const kc = new k8s.KubeConfig();
    kc.loadFromDefault();
    
    this.k8sApi = kc.makeApiClient(k8s.CoreV1Api);
    this.networkingApi = kc.makeApiClient(k8s.NetworkingV1Api);
    this.rbacApi = kc.makeApiClient(k8s.RbacAuthorizationV1Api);
  }
  
  async applyNetworkConfig(environmentId: string, config: NetworkingConfig): Promise<void> {
    const namespace = environmentId;
    
    // 应用Ingress配置
    if (config.ingress?.enabled) {
      await this.createIngress(namespace, config.ingress);
    }
    
    // 应用网络策略
    if (config.networkPolicy?.enabled) {
      await this.createNetworkPolicy(namespace, config.networkPolicy);
    }
  }
  
  async removeNetworkConfig(environmentId: string): Promise<void> {
    const namespace = environmentId;
    
    try {
      // 删除Ingress
      const ingresses = await this.networkingApi.listNamespacedIngress(namespace);
      for (const ingress of ingresses.body.items) {
        await this.networkingApi.deleteNamespacedIngress(ingress.metadata!.name!, namespace);
      }
      
      // 删除网络策略
      const networkPolicies = await this.networkingApi.listNamespacedNetworkPolicy(namespace);
      for (const policy of networkPolicies.body.items) {
        await this.networkingApi.deleteNamespacedNetworkPolicy(policy.metadata!.name!, namespace);
      }
    } catch (error) {
      console.error('Failed to remove network config:', error);
    }
  }
  
  async applySecurityConfig(environmentId: string, config: SecurityConfig): Promise<void> {
    const namespace = environmentId;
    
    // 应用RBAC配置
    if (config.rbac?.enabled) {
      await this.createRBAC(namespace, config.rbac);
    }
    
    // 应用Pod安全策略
    if (config.podSecurityPolicy?.enabled) {
      await this.createPodSecurityPolicy(namespace, config.podSecurityPolicy);
    }
  }
  
  async removeSecurityConfig(environmentId: string): Promise<void> {
    const namespace = environmentId;
    
    try {
      // 删除RBAC资源
      const roles = await this.rbacApi.listNamespacedRole(namespace);
      for (const role of roles.body.items) {
        await this.rbacApi.deleteNamespacedRole(role.metadata!.name!, namespace);
      }
      
      const roleBindings = await this.rbacApi.listNamespacedRoleBinding(namespace);
      for (const binding of roleBindings.body.items) {
        await this.rbacApi.deleteNamespacedRoleBinding(binding.metadata!.name!, namespace);
      }
    } catch (error) {
      console.error('Failed to remove security config:', error);
    }
  }
  
  async createConfigTemplate(template: ConfigTemplate): Promise<ConfigTemplate> {
    const newTemplate: ConfigTemplate = {
      ...template,
      id: generateId(),
      createdAt: new Date(),
      updatedAt: new Date()
    };
    
    this.templates.set(newTemplate.id, newTemplate);
    return newTemplate;
  }
  
  async getConfigTemplates(query?: ConfigTemplateQuery): Promise<ConfigTemplate[]> {
    let templates = Array.from(this.templates.values());
    
    if (query?.type) {
      templates = templates.filter(t => t.type === query.type);
    }
    
    if (query?.category) {
      templates = templates.filter(t => t.category === query.category);
    }
    
    return templates;
  }
  
  async applyConfigTemplate(templateId: string, environmentId: string, variables?: Record<string, any>): Promise<void> {
    const template = this.templates.get(templateId);
    if (!template) {
      throw new Error(`Template ${templateId} not found`);
    }
    
    // 渲染模板
    const renderedConfig = this.renderTemplate(template.content, variables || {});
    
    // 应用配置
    await this.applyRenderedConfig(environmentId, renderedConfig, template.type);
  }
  
  async validateConfig(config: any, schema: ConfigSchema): Promise<ValidationResult> {
    const errors: ValidationError[] = [];
    const warnings: ValidationWarning[] = [];
    
    // 验证必需字段
    for (const field of schema.required || []) {
      if (!config[field]) {
        errors.push({
          field,
          message: `Required field '${field}' is missing`,
          code: 'REQUIRED_FIELD_MISSING'
        });
      }
    }
    
    // 验证字段类型
    for (const [field, fieldSchema] of Object.entries(schema.properties || {})) {
      if (config[field] !== undefined) {
        const validationResult = this.validateField(config[field], fieldSchema, field);
        errors.push(...validationResult.errors);
        warnings.push(...validationResult.warnings);
      }
    }
    
    return {
      valid: errors.length === 0,
      errors,
      warnings
    };
  }
  
  async syncConfig(environmentId: string): Promise<void> {
    // 同步配置到目标环境
    const namespace = environmentId;
    
    try {
      // 获取当前配置
      const configMaps = await this.k8sApi.listNamespacedConfigMap(namespace);
      const secrets = await this.k8sApi.listNamespacedSecret(namespace);
      
      // 验证配置一致性
      for (const configMap of configMaps.body.items) {
        // 验证ConfigMap
        console.log(`Syncing ConfigMap: ${configMap.metadata?.name}`);
      }
      
      for (const secret of secrets.body.items) {
        // 验证Secret
        console.log(`Syncing Secret: ${secret.metadata?.name}`);
      }
    } catch (error) {
      throw new Error(`Failed to sync config: ${error.message}`);
    }
  }
  
  // 私有方法
  private async createIngress(namespace: string, config: IngressConfig): Promise<void> {
    const ingress = {
      metadata: {
        name: `${namespace}-ingress`,
        namespace,
        annotations: config.annotations || {}
      },
      spec: {
        rules: [
          {
            host: config.host,
            http: {
              paths: [
                {
                  path: '/',
                  pathType: 'Prefix',
                  backend: {
                    service: {
                      name: `${namespace}-frontend`,
                      port: {
                        number: 80
                      }
                    }
                  }
                }
              ]
            }
          }
        ],
        tls: config.tls ? [
          {
            hosts: [config.host!],
            secretName: `${namespace}-tls`
          }
        ] : undefined
      }
    };
    
    await this.networkingApi.createNamespacedIngress(namespace, ingress);
  }
  
  private async createNetworkPolicy(namespace: string, config: NetworkPolicyConfig): Promise<void> {
    const networkPolicy = {
      metadata: {
        name: `${namespace}-network-policy`,
        namespace
      },
      spec: {
        podSelector: {},
        policyTypes: ['Ingress', 'Egress'],
        ingress: config.ingress?.map(rule => ({
          from: rule.from?.map(selector => ({
            namespaceSelector: selector.namespaceSelector,
            podSelector: selector.podSelector
          })),
          ports: rule.ports?.map(port => ({
            port: port.port,
            protocol: port.protocol
          }))
        })),
        egress: config.egress?.map(rule => ({
          to: rule.to?.map(selector => ({
            namespaceSelector: selector.namespaceSelector,
            podSelector: selector.podSelector
          })),
          ports: rule.ports?.map(port => ({
            port: port.port,
            protocol: port.protocol
          }))
        }))
      }
    };
    
    await this.networkingApi.createNamespacedNetworkPolicy(namespace, networkPolicy);
  }
  
  private async createRBAC(namespace: string, config: RBACConfig): Promise<void> {
    // 创建ServiceAccount
    if (config.serviceAccount) {
      const serviceAccount = {
        metadata: {
          name: config.serviceAccount,
          namespace
        }
      };
      
      await this.k8sApi.createNamespacedServiceAccount(namespace, serviceAccount);
    }
    
    // 创建Role
    if (config.roles) {
      for (const roleName of config.roles) {
        const role = {
          metadata: {
            name: roleName,
            namespace
          },
          rules: [
            {
              apiGroups: [''],
              resources: ['pods', 'services'],
              verbs: ['get', 'list', 'watch']
            }
          ]
        };
        
        await this.rbacApi.createNamespacedRole(namespace, role);
        
        // 创建RoleBinding
        const roleBinding = {
          metadata: {
            name: `${roleName}-binding`,
            namespace
          },
          subjects: [
            {
              kind: 'ServiceAccount',
              name: config.serviceAccount || 'default',
              namespace
            }
          ],
          roleRef: {
            kind: 'Role',
            name: roleName,
            apiGroup: 'rbac.authorization.k8s.io'
          }
        };
        
        await this.rbacApi.createNamespacedRoleBinding(namespace, roleBinding);
      }
    }
  }
  
  private async createPodSecurityPolicy(namespace: string, config: PodSecurityPolicyConfig): Promise<void> {
    // 创建PodSecurityPolicy(注意:在Kubernetes 1.25+中已被弃用)
    // 这里可以创建SecurityContext约束或其他安全策略
    console.log(`Creating Pod Security Policy for namespace: ${namespace}`);
  }
  
  private renderTemplate(template: string, variables: Record<string, any>): any {
    let rendered = template;
    
    // 简单的模板渲染(实际项目中可以使用更强大的模板引擎)
    for (const [key, value] of Object.entries(variables)) {
      const regex = new RegExp(`\\{\\{\\s*${key}\\s*\\}\\}`, 'g');
      rendered = rendered.replace(regex, String(value));
    }
    
    try {
      return JSON.parse(rendered);
    } catch (error) {
      throw new Error(`Failed to parse rendered template: ${error.message}`);
    }
  }
  
  private async applyRenderedConfig(environmentId: string, config: any, type: ConfigTemplateType): Promise<void> {
    const namespace = environmentId;
    
    switch (type) {
      case ConfigTemplateType.CONFIGMAP:
        await this.k8sApi.createNamespacedConfigMap(namespace, config);
        break;
      case ConfigTemplateType.SECRET:
        await this.k8sApi.createNamespacedSecret(namespace, config);
        break;
      case ConfigTemplateType.SERVICE:
        await this.k8sApi.createNamespacedService(namespace, config);
        break;
      default:
        throw new Error(`Unsupported config type: ${type}`);
    }
  }
  
  private validateField(value: any, schema: FieldSchema, fieldName: string): { errors: ValidationError[], warnings: ValidationWarning[] } {
    const errors: ValidationError[] = [];
    const warnings: ValidationWarning[] = [];
    
    // 类型验证
    if (schema.type && typeof value !== schema.type) {
      errors.push({
        field: fieldName,
        message: `Expected type '${schema.type}' but got '${typeof value}'`,
        code: 'TYPE_MISMATCH'
      });
    }
    
    // 范围验证
    if (schema.minimum !== undefined && typeof value === 'number' && value < schema.minimum) {
      errors.push({
        field: fieldName,
        message: `Value ${value} is less than minimum ${schema.minimum}`,
        code: 'VALUE_TOO_SMALL'
      });
    }
    
    if (schema.maximum !== undefined && typeof value === 'number' && value > schema.maximum) {
      errors.push({
        field: fieldName,
        message: `Value ${value} is greater than maximum ${schema.maximum}`,
        code: 'VALUE_TOO_LARGE'
      });
    }
    
    // 长度验证
    if (schema.minLength !== undefined && typeof value === 'string' && value.length < schema.minLength) {
      errors.push({
        field: fieldName,
        message: `String length ${value.length} is less than minimum ${schema.minLength}`,
        code: 'STRING_TOO_SHORT'
      });
    }
    
    if (schema.maxLength !== undefined && typeof value === 'string' && value.length > schema.maxLength) {
      errors.push({
        field: fieldName,
        message: `String length ${value.length} is greater than maximum ${schema.maxLength}`,
        code: 'STRING_TOO_LONG'
      });
    }
    
    // 模式验证
    if (schema.pattern && typeof value === 'string' && !new RegExp(schema.pattern).test(value)) {
      errors.push({
        field: fieldName,
        message: `Value '${value}' does not match pattern '${schema.pattern}'`,
        code: 'PATTERN_MISMATCH'
      });
    }
    
    return { errors, warnings };
  }
}

// 配置模板
interface ConfigTemplate {
  id: string;
  name: string;
  description?: string;
  type: ConfigTemplateType;
  category: string;
  content: string;
  variables: TemplateVariable[];
  createdAt: Date;
  updatedAt: Date;
}

// 配置模板类型
enum ConfigTemplateType {
  CONFIGMAP = 'configmap',
  SECRET = 'secret',
  SERVICE = 'service',
  DEPLOYMENT = 'deployment',
  INGRESS = 'ingress'
}

// 模板变量
interface TemplateVariable {
  name: string;
  type: 'string' | 'number' | 'boolean';
  description?: string;
  defaultValue?: any;
  required?: boolean;
}

// 配置模板查询
interface ConfigTemplateQuery {
  type?: ConfigTemplateType;
  category?: string;
}

// 配置模式
interface ConfigSchema {
  type: 'object';
  properties?: Record<string, FieldSchema>;
  required?: string[];
}

// 字段模式
interface FieldSchema {
  type?: 'string' | 'number' | 'boolean' | 'object' | 'array';
  minimum?: number;
  maximum?: number;
  minLength?: number;
  maxLength?: number;
  pattern?: string;
  enum?: any[];
}

// 验证结果
interface ValidationResult {
  valid: boolean;
  errors: ValidationError[];
  warnings: ValidationWarning[];
}

// 验证错误
interface ValidationError {
  field: string;
  message: string;
  code: string;
}

// 验证警告
interface ValidationWarning {
  field: string;
  message: string;
  code: string;
}

14.5 监控服务

// 监控服务接口
interface MonitoringService {
  // 指标收集
  collectMetrics(): Promise<void>;
  getResourceMetrics(): Promise<ResourceMetrics>;
  getPerformanceMetrics(): Promise<PerformanceMetrics>;
  getServiceMetrics(serviceId: string, query?: MetricsQuery): Promise<ServiceMetrics>;
  
  // 告警管理
  createAlert(alert: AlertDefinition): Promise<Alert>;
  getAlerts(query?: AlertQuery): Promise<Alert[]>;
  updateAlert(id: string, updates: Partial<AlertDefinition>): Promise<Alert>;
  deleteAlert(id: string): Promise<void>;
  
  // 告警规则
  createAlertRule(rule: AlertRule): Promise<AlertRule>;
  getAlertRules(): Promise<AlertRule[]>;
  evaluateAlertRules(): Promise<void>;
  
  // 仪表板
  createDashboard(dashboard: Dashboard): Promise<Dashboard>;
  getDashboards(): Promise<Dashboard[]>;
  updateDashboard(id: string, updates: Partial<Dashboard>): Promise<Dashboard>;
  
  // 健康检查
  performHealthCheck(target: HealthCheckTarget): Promise<HealthCheckResult>;
}

// Prometheus监控服务实现
class PrometheusMonitoringService implements MonitoringService {
  private prometheusUrl: string;
  private alerts: Map<string, Alert> = new Map();
  private alertRules: Map<string, AlertRule> = new Map();
  private dashboards: Map<string, Dashboard> = new Map();
  private httpClient: HttpClient;
  
  constructor(options: PrometheusOptions) {
    this.prometheusUrl = options.url || 'http://localhost:9090';
    this.httpClient = new HttpClient();
    
    this.initializeDefaultAlertRules();
    this.initializeDefaultDashboards();
  }
  
  async collectMetrics(): Promise<void> {
    // 收集系统指标
    const timestamp = Date.now();
    
    // 收集CPU指标
    const cpuMetrics = await this.collectCPUMetrics();
    await this.pushMetrics('cpu_usage', cpuMetrics, timestamp);
    
    // 收集内存指标
    const memoryMetrics = await this.collectMemoryMetrics();
    await this.pushMetrics('memory_usage', memoryMetrics, timestamp);
    
    // 收集磁盘指标
    const diskMetrics = await this.collectDiskMetrics();
    await this.pushMetrics('disk_usage', diskMetrics, timestamp);
    
    // 收集网络指标
    const networkMetrics = await this.collectNetworkMetrics();
    await this.pushMetrics('network_usage', networkMetrics, timestamp);
  }
  
  async getResourceMetrics(): Promise<ResourceMetrics> {
    const queries = {
      cpuUsed: 'sum(rate(cpu_usage_total[5m]))',
      cpuTotal: 'count(cpu_usage_total)',
      memoryUsed: 'sum(memory_usage_bytes)',
      memoryTotal: 'sum(memory_total_bytes)',
      storageUsed: 'sum(disk_usage_bytes)',
      storageTotal: 'sum(disk_total_bytes)'
    };
    
    const results = await Promise.all([
      this.queryPrometheus(queries.cpuUsed),
      this.queryPrometheus(queries.cpuTotal),
      this.queryPrometheus(queries.memoryUsed),
      this.queryPrometheus(queries.memoryTotal),
      this.queryPrometheus(queries.storageUsed),
      this.queryPrometheus(queries.storageTotal)
    ]);
    
    const [cpuUsed, cpuTotal, memoryUsed, memoryTotal, storageUsed, storageTotal] = results;
    
    return {
      cpu: {
        used: cpuUsed,
        total: cpuTotal,
        percentage: (cpuUsed / cpuTotal) * 100
      },
      memory: {
        used: memoryUsed,
        total: memoryTotal,
        percentage: (memoryUsed / memoryTotal) * 100
      },
      storage: {
        used: storageUsed,
        total: storageTotal,
        percentage: (storageUsed / storageTotal) * 100
      }
    };
  }
  
  async getPerformanceMetrics(): Promise<PerformanceMetrics> {
    const queries = {
      requestTotal: 'sum(rate(http_requests_total[5m]))',
      requestRate: 'sum(rate(http_requests_total[1m]))',
      latencyAvg: 'avg(http_request_duration_seconds)',
      latencyP95: 'histogram_quantile(0.95, http_request_duration_seconds_bucket)',
      latencyP99: 'histogram_quantile(0.99, http_request_duration_seconds_bucket)',
      errorTotal: 'sum(rate(http_requests_total{status=~"5.."}[5m]))',
      errorRate: 'sum(rate(http_requests_total{status=~"5.."}[1m]))'
    };
    
    const results = await Promise.all([
      this.queryPrometheus(queries.requestTotal),
      this.queryPrometheus(queries.requestRate),
      this.queryPrometheus(queries.latencyAvg),
      this.queryPrometheus(queries.latencyP95),
      this.queryPrometheus(queries.latencyP99),
      this.queryPrometheus(queries.errorTotal),
      this.queryPrometheus(queries.errorRate)
    ]);
    
    const [requestTotal, requestRate, latencyAvg, latencyP95, latencyP99, errorTotal, errorRate] = results;
    
    return {
      requests: {
        total: requestTotal,
        rate: requestRate
      },
      latency: {
        average: latencyAvg * 1000, // 转换为毫秒
        p95: latencyP95 * 1000,
        p99: latencyP99 * 1000
      },
      errors: {
        total: errorTotal,
        rate: errorRate
      }
    };
  }
  
  async getServiceMetrics(serviceId: string, query?: MetricsQuery): Promise<ServiceMetrics> {
    const timeRange = this.buildTimeRange(query);
    
    const queries = {
      cpu: `avg(rate(container_cpu_usage_seconds_total{pod=~"${serviceId}.*"}[5m]))`,
      memory: `avg(container_memory_usage_bytes{pod=~"${serviceId}.*"})`,
      networkIn: `sum(rate(container_network_receive_bytes_total{pod=~"${serviceId}.*"}[5m]))`,
      networkOut: `sum(rate(container_network_transmit_bytes_total{pod=~"${serviceId}.*"}[5m]))`,
      requests: `sum(rate(http_requests_total{service="${serviceId}"}[5m]))`,
      errors: `sum(rate(http_requests_total{service="${serviceId}",status=~"5.."}[5m]))`,
      latency: `avg(http_request_duration_seconds{service="${serviceId}"})`
    };
    
    const results = await Promise.all([
      this.queryPrometheus(queries.cpu, timeRange),
      this.queryPrometheus(queries.memory, timeRange),
      this.queryPrometheus(queries.networkIn, timeRange),
      this.queryPrometheus(queries.networkOut, timeRange),
      this.queryPrometheus(queries.requests, timeRange),
      this.queryPrometheus(queries.errors, timeRange),
      this.queryPrometheus(queries.latency, timeRange)
    ]);
    
    const [cpu, memory, networkIn, networkOut, requests, errors, latency] = results;
    
    return {
      name: serviceId,
      cpu: cpu * 100, // 转换为百分比
      memory: memory / (1024 * 1024), // 转换为MB
      network: {
        in: networkIn,
        out: networkOut
      },
      requests,
      errors,
      latency: latency * 1000 // 转换为毫秒
    };
  }
  
  async createAlert(alert: AlertDefinition): Promise<Alert> {
    const newAlert: Alert = {
      ...alert,
      id: generateId(),
      status: AlertStatus.ACTIVE,
      createdAt: new Date(),
      updatedAt: new Date()
    };
    
    this.alerts.set(newAlert.id, newAlert);
    return newAlert;
  }
  
  async getAlerts(query?: AlertQuery): Promise<Alert[]> {
    let alerts = Array.from(this.alerts.values());
    
    if (query?.status) {
      alerts = alerts.filter(alert => alert.status === query.status);
    }
    
    if (query?.severity) {
      alerts = alerts.filter(alert => alert.severity === query.severity);
    }
    
    return alerts;
  }
  
  async updateAlert(id: string, updates: Partial<AlertDefinition>): Promise<Alert> {
    const alert = this.alerts.get(id);
    if (!alert) {
      throw new Error(`Alert ${id} not found`);
    }
    
    Object.assign(alert, updates, { updatedAt: new Date() });
    this.alerts.set(id, alert);
    
    return alert;
  }
  
  async deleteAlert(id: string): Promise<void> {
    this.alerts.delete(id);
  }
  
  async createAlertRule(rule: AlertRule): Promise<AlertRule> {
    const newRule: AlertRule = {
      ...rule,
      id: generateId(),
      createdAt: new Date(),
      updatedAt: new Date()
    };
    
    this.alertRules.set(newRule.id, newRule);
    return newRule;
  }
  
  async getAlertRules(): Promise<AlertRule[]> {
    return Array.from(this.alertRules.values());
  }
  
  async evaluateAlertRules(): Promise<void> {
    for (const rule of this.alertRules.values()) {
      try {
        const result = await this.queryPrometheus(rule.query);
        
        if (this.evaluateCondition(result, rule.condition)) {
          // 触发告警
          await this.triggerAlert(rule);
        }
      } catch (error) {
        console.error(`Failed to evaluate alert rule ${rule.name}:`, error);
      }
    }
  }
  
  async createDashboard(dashboard: Dashboard): Promise<Dashboard> {
    const newDashboard: Dashboard = {
      ...dashboard,
      id: generateId(),
      createdAt: new Date(),
      updatedAt: new Date()
    };
    
    this.dashboards.set(newDashboard.id, newDashboard);
    return newDashboard;
  }
  
  async getDashboards(): Promise<Dashboard[]> {
    return Array.from(this.dashboards.values());
  }
  
  async updateDashboard(id: string, updates: Partial<Dashboard>): Promise<Dashboard> {
    const dashboard = this.dashboards.get(id);
    if (!dashboard) {
      throw new Error(`Dashboard ${id} not found`);
    }
    
    Object.assign(dashboard, updates, { updatedAt: new Date() });
    this.dashboards.set(id, dashboard);
    
    return dashboard;
  }
  
  async performHealthCheck(target: HealthCheckTarget): Promise<HealthCheckResult> {
    const startTime = Date.now();
    
    try {
      const response = await this.httpClient.get(target.url, {
        timeout: target.timeout || 5000
      });
      
      const duration = Date.now() - startTime;
      
      return {
        target: target.name,
        status: response.status === 200 ? 'healthy' : 'unhealthy',
        duration,
        message: `HTTP ${response.status}`,
        timestamp: new Date()
      };
    } catch (error) {
      const duration = Date.now() - startTime;
      
      return {
        target: target.name,
        status: 'unhealthy',
        duration,
        message: error.message,
        timestamp: new Date()
      };
    }
  }
  
  // 私有方法
  private async queryPrometheus(query: string, timeRange?: string): Promise<number> {
    try {
      const url = `${this.prometheusUrl}/api/v1/query`;
      const params = new URLSearchParams({
        query,
        time: timeRange || new Date().toISOString()
      });
      
      const response = await this.httpClient.get(`${url}?${params}`);
      const data = response.data;
      
      if (data.status === 'success' && data.data.result.length > 0) {
        return parseFloat(data.data.result[0].value[1]);
      }
      
      return 0;
    } catch (error) {
      console.error(`Failed to query Prometheus: ${error.message}`);
      return 0;
    }
  }
  
  private async pushMetrics(metricName: string, value: number, timestamp: number): Promise<void> {
    // 推送指标到Prometheus(通过Pushgateway)
    const pushgatewayUrl = `${this.prometheusUrl.replace(':9090', ':9091')}/metrics/job/lowcode-platform`;
    
    const metric = `${metricName} ${value} ${timestamp}`;
    
    try {
      await this.httpClient.post(pushgatewayUrl, metric, {
        headers: {
          'Content-Type': 'text/plain'
        }
      });
    } catch (error) {
      console.error(`Failed to push metrics: ${error.message}`);
    }
  }
  
  private async collectCPUMetrics(): Promise<number> {
    // 收集CPU使用率
    const os = require('os');
    const cpus = os.cpus();
    
    let totalIdle = 0;
    let totalTick = 0;
    
    for (const cpu of cpus) {
      for (const type in cpu.times) {
        totalTick += cpu.times[type];
      }
      totalIdle += cpu.times.idle;
    }
    
    return (1 - totalIdle / totalTick) * 100;
  }
  
  private async collectMemoryMetrics(): Promise<number> {
    // 收集内存使用率
    const os = require('os');
    const totalMemory = os.totalmem();
    const freeMemory = os.freemem();
    
    return ((totalMemory - freeMemory) / totalMemory) * 100;
  }
  
  private async collectDiskMetrics(): Promise<number> {
    // 收集磁盘使用率(简化实现)
    const fs = require('fs');
    
    try {
      const stats = fs.statSync('/');
      // 这里需要更复杂的逻辑来获取磁盘使用情况
      return 50; // 示例值
    } catch (error) {
      return 0;
    }
  }
  
  private async collectNetworkMetrics(): Promise<number> {
    // 收集网络使用率(简化实现)
    return 10; // 示例值
  }
  
  private buildTimeRange(query?: MetricsQuery): string {
    if (query?.endTime) {
      return query.endTime.toISOString();
    }
    return new Date().toISOString();
  }
  
  private evaluateCondition(value: number, condition: AlertCondition): boolean {
    switch (condition.operator) {
      case 'gt':
        return value > condition.threshold;
      case 'gte':
        return value >= condition.threshold;
      case 'lt':
        return value < condition.threshold;
      case 'lte':
        return value <= condition.threshold;
      case 'eq':
        return value === condition.threshold;
      case 'ne':
        return value !== condition.threshold;
      default:
        return false;
    }
  }
  
  private async triggerAlert(rule: AlertRule): Promise<void> {
    const alert: Alert = {
      id: generateId(),
      name: `Alert: ${rule.name}`,
      description: rule.description,
      severity: rule.severity,
      status: AlertStatus.FIRING,
      source: rule.id,
      createdAt: new Date(),
      updatedAt: new Date()
    };
    
    this.alerts.set(alert.id, alert);
    
    // 发送通知
    await this.sendNotification(alert);
  }
  
  private async sendNotification(alert: Alert): Promise<void> {
    // 发送告警通知(邮件、短信、Webhook等)
    console.log(`Alert triggered: ${alert.name} - ${alert.description}`);
  }
  
  private initializeDefaultAlertRules(): void {
    const defaultRules: Omit<AlertRule, 'id' | 'createdAt' | 'updatedAt'>[] = [
      {
        name: 'High CPU Usage',
        description: 'CPU usage is above 80%',
        query: 'avg(cpu_usage) * 100',
        condition: {
          operator: 'gt',
          threshold: 80
        },
        severity: AlertSeverity.WARNING,
        duration: '5m'
      },
      {
        name: 'High Memory Usage',
        description: 'Memory usage is above 90%',
        query: 'avg(memory_usage) * 100',
        condition: {
          operator: 'gt',
          threshold: 90
        },
        severity: AlertSeverity.CRITICAL,
        duration: '5m'
      },
      {
        name: 'Service Down',
        description: 'Service is not responding',
        query: 'up',
        condition: {
          operator: 'eq',
          threshold: 0
        },
        severity: AlertSeverity.CRITICAL,
        duration: '1m'
      }
    ];
    
    for (const rule of defaultRules) {
      this.createAlertRule({
        ...rule,
        id: generateId(),
        createdAt: new Date(),
        updatedAt: new Date()
      });
    }
  }
  
  private initializeDefaultDashboards(): void {
    const defaultDashboards: Omit<Dashboard, 'id' | 'createdAt' | 'updatedAt'>[] = [
      {
        name: 'System Overview',
        description: '系统总览仪表板',
        panels: [
          {
            id: 'cpu-usage',
            title: 'CPU Usage',
            type: 'graph',
            query: 'avg(cpu_usage) * 100',
            position: { x: 0, y: 0, w: 6, h: 4 }
          },
          {
            id: 'memory-usage',
            title: 'Memory Usage',
            type: 'graph',
            query: 'avg(memory_usage) * 100',
            position: { x: 6, y: 0, w: 6, h: 4 }
          },
          {
            id: 'request-rate',
            title: 'Request Rate',
            type: 'graph',
            query: 'sum(rate(http_requests_total[5m]))',
            position: { x: 0, y: 4, w: 12, h: 4 }
          }
        ]
      }
    ];
    
    for (const dashboard of defaultDashboards) {
      this.createDashboard({
        ...dashboard,
        id: generateId(),
        createdAt: new Date(),
        updatedAt: new Date()
      });
    }
  }
}

// 监控相关接口
interface PrometheusOptions {
  url?: string;
}

interface AlertDefinition {
  name: string;
  description?: string;
  severity: AlertSeverity;
  source?: string;
}

interface Alert extends AlertDefinition {
  id: string;
  status: AlertStatus;
  createdAt: Date;
  updatedAt: Date;
}

enum AlertStatus {
  ACTIVE = 'active',
  FIRING = 'firing',
  RESOLVED = 'resolved',
  SILENCED = 'silenced'
}

enum AlertSeverity {
  INFO = 'info',
  WARNING = 'warning',
  CRITICAL = 'critical'
}

interface AlertRule {
  id: string;
  name: string;
  description?: string;
  query: string;
  condition: AlertCondition;
  severity: AlertSeverity;
  duration: string;
  createdAt: Date;
  updatedAt: Date;
}

interface AlertCondition {
  operator: 'gt' | 'gte' | 'lt' | 'lte' | 'eq' | 'ne';
  threshold: number;
}

interface Dashboard {
  id: string;
  name: string;
  description?: string;
  panels: DashboardPanel[];
  createdAt: Date;
  updatedAt: Date;
}

interface DashboardPanel {
  id: string;
  title: string;
  type: 'graph' | 'stat' | 'table' | 'heatmap';
  query: string;
  position: PanelPosition;
  options?: Record<string, any>;
}

interface PanelPosition {
  x: number;
  y: number;
  w: number;
  h: number;
}

interface HealthCheckTarget {
  name: string;
  url: string;
  timeout?: number;
}

interface HealthCheckResult {
  target: string;
  status: 'healthy' | 'unhealthy';
  duration: number;
  message?: string;
  timestamp: Date;
}

interface AlertQuery {
  status?: AlertStatus;
  severity?: AlertSeverity;
}

interface HttpClient {
  get(url: string, options?: any): Promise<any>;
  post(url: string, data: any, options?: any): Promise<any>;
}

14.6 日志管理

// 日志管理器接口
interface LogManager {
  // 日志收集
  collectLogs(source: LogSource): Promise<void>;
  getLogs(query: LogQuery): Promise<LogEntry[]>;
  streamLogs(query: LogQuery): AsyncIterable<LogEntry>;
  
  // 日志聚合
  aggregateLogs(query: AggregationQuery): Promise<AggregationResult>;
  
  // 日志存储
  storeLogs(logs: LogEntry[]): Promise<void>;
  deleteLogs(query: LogQuery): Promise<number>;
  
  // 日志索引
  createIndex(indexName: string, mapping: IndexMapping): Promise<void>;
  deleteIndex(indexName: string): Promise<void>;
  
  // 日志搜索
  searchLogs(searchQuery: SearchQuery): Promise<SearchResult>;
  
  // 日志导出
  exportLogs(query: LogQuery, format: ExportFormat): Promise<string>;
}

// Elasticsearch日志管理器实现
class ElasticsearchLogManager implements LogManager {
  private client: ElasticsearchClient;
  private indexPrefix: string;
  
  constructor(options: ElasticsearchOptions) {
    this.client = new ElasticsearchClient({
      node: options.url || 'http://localhost:9200',
      auth: options.auth
    });
    this.indexPrefix = options.indexPrefix || 'lowcode-logs';
  }
  
  async collectLogs(source: LogSource): Promise<void> {
    try {
      let logs: LogEntry[] = [];
      
      switch (source.type) {
        case LogSourceType.FILE:
          logs = await this.collectFileSystemLogs(source);
          break;
        case LogSourceType.CONTAINER:
          logs = await this.collectContainerLogs(source);
          break;
        case LogSourceType.SYSLOG:
          logs = await this.collectSyslogLogs(source);
          break;
        case LogSourceType.APPLICATION:
          logs = await this.collectApplicationLogs(source);
          break;
      }
      
      if (logs.length > 0) {
        await this.storeLogs(logs);
      }
    } catch (error) {
      console.error(`Failed to collect logs from ${source.name}:`, error);
    }
  }
  
  async getLogs(query: LogQuery): Promise<LogEntry[]> {
    const searchBody: any = {
      query: {
        bool: {
          must: [],
          filter: []
        }
      },
      sort: [{ timestamp: { order: 'desc' } }],
      size: query.limit || 100
    };
    
    // 时间范围过滤
    if (query.startTime || query.endTime) {
      const timeRange: any = {};
      if (query.startTime) timeRange.gte = query.startTime.toISOString();
      if (query.endTime) timeRange.lte = query.endTime.toISOString();
      
      searchBody.query.bool.filter.push({
        range: { timestamp: timeRange }
      });
    }
    
    // 日志级别过滤
    if (query.level) {
      searchBody.query.bool.filter.push({
        term: { level: query.level }
      });
    }
    
    // 服务过滤
    if (query.service) {
      searchBody.query.bool.filter.push({
        term: { service: query.service }
      });
    }
    
    // 关键词搜索
    if (query.keyword) {
      searchBody.query.bool.must.push({
        multi_match: {
          query: query.keyword,
          fields: ['message', 'error.message']
        }
      });
    }
    
    const indexPattern = this.buildIndexPattern(query.startTime, query.endTime);
    
    try {
      const response = await this.client.search({
        index: indexPattern,
        body: searchBody
      });
      
      return response.body.hits.hits.map((hit: any) => ({
        ...hit._source,
        id: hit._id
      }));
    } catch (error) {
      console.error('Failed to get logs:', error);
      return [];
    }
  }
  
  async *streamLogs(query: LogQuery): AsyncIterable<LogEntry> {
    const searchBody = {
      query: {
        bool: {
          filter: [
            {
              range: {
                timestamp: {
                  gte: query.startTime?.toISOString() || 'now-1h'
                }
              }
            }
          ]
        }
      },
      sort: [{ timestamp: { order: 'asc' } }]
    };
    
    if (query.service) {
      searchBody.query.bool.filter.push({
        term: { service: query.service }
      });
    }
    
    const indexPattern = this.buildIndexPattern(query.startTime, query.endTime);
    
    try {
      const response = await this.client.search({
        index: indexPattern,
        body: searchBody,
        scroll: '1m',
        size: 100
      });
      
      let scrollId = response.body._scroll_id;
      let hits = response.body.hits.hits;
      
      while (hits.length > 0) {
        for (const hit of hits) {
          yield {
            ...hit._source,
            id: hit._id
          };
        }
        
        const scrollResponse = await this.client.scroll({
          scroll_id: scrollId,
          scroll: '1m'
        });
        
        scrollId = scrollResponse.body._scroll_id;
        hits = scrollResponse.body.hits.hits;
      }
      
      // 清理scroll
      await this.client.clearScroll({ scroll_id: scrollId });
    } catch (error) {
      console.error('Failed to stream logs:', error);
    }
  }
  
  async aggregateLogs(query: AggregationQuery): Promise<AggregationResult> {
    const searchBody: any = {
      query: {
        bool: {
          filter: []
        }
      },
      aggs: {},
      size: 0
    };
    
    // 时间范围过滤
    if (query.startTime || query.endTime) {
      const timeRange: any = {};
      if (query.startTime) timeRange.gte = query.startTime.toISOString();
      if (query.endTime) timeRange.lte = query.endTime.toISOString();
      
      searchBody.query.bool.filter.push({
        range: { timestamp: timeRange }
      });
    }
    
    // 构建聚合查询
    switch (query.type) {
      case AggregationType.COUNT_BY_LEVEL:
        searchBody.aggs.levels = {
          terms: { field: 'level' }
        };
        break;
      case AggregationType.COUNT_BY_SERVICE:
        searchBody.aggs.services = {
          terms: { field: 'service' }
        };
        break;
      case AggregationType.TIMELINE:
        searchBody.aggs.timeline = {
          date_histogram: {
            field: 'timestamp',
            interval: query.interval || '1h'
          }
        };
        break;
      case AggregationType.ERROR_RATE:
        searchBody.aggs.total = {
          value_count: { field: 'timestamp' }
        };
        searchBody.aggs.errors = {
          filter: {
            terms: { level: ['error', 'fatal'] }
          }
        };
        break;
    }
    
    const indexPattern = this.buildIndexPattern(query.startTime, query.endTime);
    
    try {
      const response = await this.client.search({
        index: indexPattern,
        body: searchBody
      });
      
      return this.parseAggregationResponse(response.body.aggregations, query.type);
    } catch (error) {
      console.error('Failed to aggregate logs:', error);
      return { type: query.type, data: [] };
    }
  }
  
  async storeLogs(logs: LogEntry[]): Promise<void> {
    const operations: any[] = [];
    
    for (const log of logs) {
      const indexName = this.getIndexName(log.timestamp);
      
      operations.push({
        index: {
          _index: indexName,
          _id: log.id || generateId()
        }
      });
      
      operations.push(log);
    }
    
    try {
      await this.client.bulk({
        body: operations,
        refresh: 'wait_for'
      });
    } catch (error) {
      console.error('Failed to store logs:', error);
      throw error;
    }
  }
  
  async deleteLogs(query: LogQuery): Promise<number> {
    const deleteQuery: any = {
      query: {
        bool: {
          filter: []
        }
      }
    };
    
    // 时间范围过滤
    if (query.startTime || query.endTime) {
      const timeRange: any = {};
      if (query.startTime) timeRange.gte = query.startTime.toISOString();
      if (query.endTime) timeRange.lte = query.endTime.toISOString();
      
      deleteQuery.query.bool.filter.push({
        range: { timestamp: timeRange }
      });
    }
    
    // 服务过滤
    if (query.service) {
      deleteQuery.query.bool.filter.push({
        term: { service: query.service }
      });
    }
    
    const indexPattern = this.buildIndexPattern(query.startTime, query.endTime);
    
    try {
      const response = await this.client.deleteByQuery({
        index: indexPattern,
        body: deleteQuery
      });
      
      return response.body.deleted || 0;
    } catch (error) {
      console.error('Failed to delete logs:', error);
      return 0;
    }
  }
  
  async createIndex(indexName: string, mapping: IndexMapping): Promise<void> {
    try {
      await this.client.indices.create({
        index: indexName,
        body: {
          mappings: {
            properties: mapping.properties
          },
          settings: {
            number_of_shards: mapping.shards || 1,
            number_of_replicas: mapping.replicas || 0
          }
        }
      });
    } catch (error) {
      if (error.meta?.body?.error?.type !== 'resource_already_exists_exception') {
        console.error(`Failed to create index ${indexName}:`, error);
        throw error;
      }
    }
  }
  
  async deleteIndex(indexName: string): Promise<void> {
    try {
      await this.client.indices.delete({
        index: indexName
      });
    } catch (error) {
      console.error(`Failed to delete index ${indexName}:`, error);
      throw error;
    }
  }
  
  async searchLogs(searchQuery: SearchQuery): Promise<SearchResult> {
    const searchBody: any = {
      query: {
        bool: {
          must: [],
          filter: []
        }
      },
      highlight: {
        fields: {
          message: {},
          'error.message': {}
        }
      },
      sort: [{ timestamp: { order: 'desc' } }],
      size: searchQuery.size || 20,
      from: searchQuery.from || 0
    };
    
    // 全文搜索
    if (searchQuery.query) {
      searchBody.query.bool.must.push({
        multi_match: {
          query: searchQuery.query,
          fields: ['message^2', 'error.message', 'service', 'component']
        }
      });
    }
    
    // 过滤条件
    if (searchQuery.filters) {
      for (const [field, value] of Object.entries(searchQuery.filters)) {
        searchBody.query.bool.filter.push({
          term: { [field]: value }
        });
      }
    }
    
    const indexPattern = this.buildIndexPattern(searchQuery.startTime, searchQuery.endTime);
    
    try {
      const response = await this.client.search({
        index: indexPattern,
        body: searchBody
      });
      
      return {
        total: response.body.hits.total.value,
        logs: response.body.hits.hits.map((hit: any) => ({
          ...hit._source,
          id: hit._id,
          highlights: hit.highlight
        })),
        aggregations: response.body.aggregations
      };
    } catch (error) {
      console.error('Failed to search logs:', error);
      return { total: 0, logs: [] };
    }
  }
  
  async exportLogs(query: LogQuery, format: ExportFormat): Promise<string> {
    const logs = await this.getLogs({ ...query, limit: 10000 });
    
    switch (format) {
      case ExportFormat.JSON:
        return JSON.stringify(logs, null, 2);
      case ExportFormat.CSV:
        return this.convertToCSV(logs);
      case ExportFormat.TEXT:
        return logs.map(log => 
          `${log.timestamp} [${log.level}] ${log.service}: ${log.message}`
        ).join('\n');
      default:
        throw new Error(`Unsupported export format: ${format}`);
    }
  }
  
  // 私有方法
  private async collectFileSystemLogs(source: LogSource): Promise<LogEntry[]> {
    const fs = require('fs');
    const readline = require('readline');
    const logs: LogEntry[] = [];
    
    try {
      const fileStream = fs.createReadStream(source.path!);
      const rl = readline.createInterface({
        input: fileStream,
        crlfDelay: Infinity
      });
      
      for await (const line of rl) {
        const logEntry = this.parseLogLine(line, source);
        if (logEntry) {
          logs.push(logEntry);
        }
      }
    } catch (error) {
      console.error(`Failed to read log file ${source.path}:`, error);
    }
    
    return logs;
  }
  
  private async collectContainerLogs(source: LogSource): Promise<LogEntry[]> {
    // 使用Docker API或kubectl获取容器日志
    const logs: LogEntry[] = [];
    
    try {
      // 这里应该调用Docker API或kubectl命令
      // 简化实现
      console.log(`Collecting logs from container: ${source.containerId}`);
    } catch (error) {
      console.error(`Failed to collect container logs:`, error);
    }
    
    return logs;
  }
  
  private async collectSyslogLogs(source: LogSource): Promise<LogEntry[]> {
    // 收集系统日志
    const logs: LogEntry[] = [];
    
    try {
      // 这里应该连接到syslog服务器
      console.log(`Collecting syslog from: ${source.host}:${source.port}`);
    } catch (error) {
      console.error(`Failed to collect syslog:`, error);
    }
    
    return logs;
  }
  
  private async collectApplicationLogs(source: LogSource): Promise<LogEntry[]> {
    // 收集应用程序日志
    const logs: LogEntry[] = [];
    
    try {
      // 这里应该通过HTTP API或其他方式收集应用日志
      console.log(`Collecting application logs from: ${source.endpoint}`);
    } catch (error) {
      console.error(`Failed to collect application logs:`, error);
    }
    
    return logs;
  }
  
  private parseLogLine(line: string, source: LogSource): LogEntry | null {
    try {
      // 尝试解析JSON格式的日志
      if (line.startsWith('{')) {
        const parsed = JSON.parse(line);
        return {
          id: generateId(),
          timestamp: new Date(parsed.timestamp || parsed.time || Date.now()),
          level: parsed.level || 'info',
          service: parsed.service || source.service || 'unknown',
          component: parsed.component,
          message: parsed.message || parsed.msg || line,
          metadata: parsed,
          source: source.name
        };
      }
      
      // 解析标准格式的日志
      const match = line.match(/^(\S+)\s+(\S+)\s+\[(\w+)\]\s+(.+)$/);
      if (match) {
        const [, timestamp, service, level, message] = match;
        return {
          id: generateId(),
          timestamp: new Date(timestamp),
          level: level.toLowerCase(),
          service: service || source.service || 'unknown',
          message,
          source: source.name
        };
      }
      
      // 默认处理
      return {
        id: generateId(),
        timestamp: new Date(),
        level: 'info',
        service: source.service || 'unknown',
        message: line,
        source: source.name
      };
    } catch (error) {
      return null;
    }
  }
  
  private buildIndexPattern(startTime?: Date, endTime?: Date): string {
    if (!startTime && !endTime) {
      return `${this.indexPrefix}-*`;
    }
    
    const start = startTime || new Date(Date.now() - 24 * 60 * 60 * 1000);
    const end = endTime || new Date();
    
    const indices: string[] = [];
    const current = new Date(start);
    
    while (current <= end) {
      indices.push(this.getIndexName(current));
      current.setDate(current.getDate() + 1);
    }
    
    return indices.join(',');
  }
  
  private getIndexName(timestamp: Date): string {
    const dateStr = timestamp.toISOString().split('T')[0];
    return `${this.indexPrefix}-${dateStr}`;
  }
  
  private parseAggregationResponse(aggregations: any, type: AggregationType): AggregationResult {
    switch (type) {
      case AggregationType.COUNT_BY_LEVEL:
        return {
          type,
          data: aggregations.levels.buckets.map((bucket: any) => ({
            key: bucket.key,
            count: bucket.doc_count
          }))
        };
      case AggregationType.COUNT_BY_SERVICE:
        return {
          type,
          data: aggregations.services.buckets.map((bucket: any) => ({
            key: bucket.key,
            count: bucket.doc_count
          }))
        };
      case AggregationType.TIMELINE:
        return {
          type,
          data: aggregations.timeline.buckets.map((bucket: any) => ({
            timestamp: new Date(bucket.key),
            count: bucket.doc_count
          }))
        };
      case AggregationType.ERROR_RATE:
        const total = aggregations.total.value;
        const errors = aggregations.errors.doc_count;
        return {
          type,
          data: [{
            total,
            errors,
            errorRate: total > 0 ? (errors / total) * 100 : 0
          }]
        };
      default:
        return { type, data: [] };
    }
  }
  
  private convertToCSV(logs: LogEntry[]): string {
    if (logs.length === 0) return '';
    
    const headers = ['timestamp', 'level', 'service', 'component', 'message'];
    const csvLines = [headers.join(',')];
    
    for (const log of logs) {
      const values = [
        log.timestamp.toISOString(),
        log.level,
        log.service,
        log.component || '',
        `"${(log.message || '').replace(/"/g, '""')}"`
      ];
      csvLines.push(values.join(','));
    }
    
    return csvLines.join('\n');
  }
}

// 日志相关接口
interface ElasticsearchOptions {
  url?: string;
  auth?: {
    username: string;
    password: string;
  };
  indexPrefix?: string;
}

interface LogSource {
  name: string;
  type: LogSourceType;
  service?: string;
  path?: string;
  containerId?: string;
  host?: string;
  port?: number;
  endpoint?: string;
}

enum LogSourceType {
  FILE = 'file',
  CONTAINER = 'container',
  SYSLOG = 'syslog',
  APPLICATION = 'application'
}

interface LogEntry {
  id?: string;
  timestamp: Date;
  level: string;
  service: string;
  component?: string;
  message: string;
  error?: {
    message: string;
    stack?: string;
  };
  metadata?: Record<string, any>;
  source?: string;
}

interface LogQuery {
  startTime?: Date;
  endTime?: Date;
  level?: string;
  service?: string;
  keyword?: string;
  limit?: number;
}

interface AggregationQuery {
  type: AggregationType;
  startTime?: Date;
  endTime?: Date;
  interval?: string;
}

enum AggregationType {
  COUNT_BY_LEVEL = 'count_by_level',
  COUNT_BY_SERVICE = 'count_by_service',
  TIMELINE = 'timeline',
  ERROR_RATE = 'error_rate'
}

interface AggregationResult {
  type: AggregationType;
  data: any[];
}

interface IndexMapping {
  properties: Record<string, any>;
  shards?: number;
  replicas?: number;
}

interface SearchQuery {
  query?: string;
  filters?: Record<string, any>;
  startTime?: Date;
  endTime?: Date;
  size?: number;
  from?: number;
}

interface SearchResult {
  total: number;
  logs: LogEntry[];
  aggregations?: any;
}

enum ExportFormat {
  JSON = 'json',
  CSV = 'csv',
  TEXT = 'text'
}

interface ElasticsearchClient {
  search(params: any): Promise<any>;
  bulk(params: any): Promise<any>;
  scroll(params: any): Promise<any>;
  clearScroll(params: any): Promise<any>;
  deleteByQuery(params: any): Promise<any>;
  indices: {
    create(params: any): Promise<any>;
    delete(params: any): Promise<any>;
  };
}

14.7 备份与恢复

// 备份管理器接口
interface BackupManager {
  // 备份操作
  createBackup(config: BackupConfig): Promise<Backup>;
  getBackups(query?: BackupQuery): Promise<Backup[]>;
  deleteBackup(backupId: string): Promise<void>;
  
  // 恢复操作
  restoreBackup(backupId: string, options?: RestoreOptions): Promise<RestoreResult>;
  validateBackup(backupId: string): Promise<ValidationResult>;
  
  // 备份策略
  createBackupPolicy(policy: BackupPolicy): Promise<BackupPolicy>;
  getBackupPolicies(): Promise<BackupPolicy[]>;
  updateBackupPolicy(policyId: string, updates: Partial<BackupPolicy>): Promise<BackupPolicy>;
  deleteBackupPolicy(policyId: string): Promise<void>;
  
  // 自动备份
  scheduleBackup(policyId: string): Promise<void>;
  cancelScheduledBackup(policyId: string): Promise<void>;
}

// 备份管理器实现
class LowCodeBackupManager implements BackupManager {
  private backups: Map<string, Backup> = new Map();
  private policies: Map<string, BackupPolicy> = new Map();
  private scheduledJobs: Map<string, NodeJS.Timeout> = new Map();
  private storageProvider: StorageProvider;
  
  constructor(storageProvider: StorageProvider) {
    this.storageProvider = storageProvider;
    this.initializeDefaultPolicies();
  }
  
  async createBackup(config: BackupConfig): Promise<Backup> {
    const backup: Backup = {
      id: generateId(),
      name: config.name,
      description: config.description,
      type: config.type,
      status: BackupStatus.RUNNING,
      createdAt: new Date(),
      updatedAt: new Date(),
      size: 0,
      metadata: {}
    };
    
    this.backups.set(backup.id, backup);
    
    try {
      // 执行备份
      await this.performBackup(backup, config);
      
      backup.status = BackupStatus.COMPLETED;
      backup.completedAt = new Date();
      backup.updatedAt = new Date();
    } catch (error) {
      backup.status = BackupStatus.FAILED;
      backup.error = error.message;
      backup.updatedAt = new Date();
      throw error;
    }
    
    return backup;
  }
  
  async getBackups(query?: BackupQuery): Promise<Backup[]> {
    let backups = Array.from(this.backups.values());
    
    if (query?.type) {
      backups = backups.filter(backup => backup.type === query.type);
    }
    
    if (query?.status) {
      backups = backups.filter(backup => backup.status === query.status);
    }
    
    if (query?.startDate) {
      backups = backups.filter(backup => backup.createdAt >= query.startDate!);
    }
    
    if (query?.endDate) {
      backups = backups.filter(backup => backup.createdAt <= query.endDate!);
    }
    
    return backups.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
  }
  
  async deleteBackup(backupId: string): Promise<void> {
    const backup = this.backups.get(backupId);
    if (!backup) {
      throw new Error(`Backup ${backupId} not found`);
    }
    
    try {
      // 删除存储中的备份文件
      if (backup.location) {
        await this.storageProvider.delete(backup.location);
      }
      
      this.backups.delete(backupId);
    } catch (error) {
      throw new Error(`Failed to delete backup: ${error.message}`);
    }
  }
  
  async restoreBackup(backupId: string, options?: RestoreOptions): Promise<RestoreResult> {
    const backup = this.backups.get(backupId);
    if (!backup) {
      throw new Error(`Backup ${backupId} not found`);
    }
    
    if (backup.status !== BackupStatus.COMPLETED) {
      throw new Error(`Backup ${backupId} is not completed`);
    }
    
    const restoreResult: RestoreResult = {
      backupId,
      status: RestoreStatus.RUNNING,
      startedAt: new Date(),
      restoredItems: []
    };
    
    try {
      // 执行恢复
      await this.performRestore(backup, options, restoreResult);
      
      restoreResult.status = RestoreStatus.COMPLETED;
      restoreResult.completedAt = new Date();
    } catch (error) {
      restoreResult.status = RestoreStatus.FAILED;
      restoreResult.error = error.message;
      throw error;
    }
    
    return restoreResult;
  }
  
  async validateBackup(backupId: string): Promise<ValidationResult> {
    const backup = this.backups.get(backupId);
    if (!backup) {
      throw new Error(`Backup ${backupId} not found`);
    }
    
    const errors: string[] = [];
    const warnings: string[] = [];
    
    try {
      // 验证备份文件存在
      if (backup.location) {
        const exists = await this.storageProvider.exists(backup.location);
        if (!exists) {
          errors.push('Backup file not found');
        }
      }
      
      // 验证备份完整性
      if (backup.checksum) {
        const actualChecksum = await this.calculateChecksum(backup.location!);
        if (actualChecksum !== backup.checksum) {
          errors.push('Backup checksum mismatch');
        }
      }
      
      // 验证备份内容
      const contentValidation = await this.validateBackupContent(backup);
      errors.push(...contentValidation.errors);
      warnings.push(...contentValidation.warnings);
      
    } catch (error) {
      errors.push(`Validation failed: ${error.message}`);
    }
    
    return {
      valid: errors.length === 0,
      errors,
      warnings
    };
  }
  
  async createBackupPolicy(policy: BackupPolicy): Promise<BackupPolicy> {
    const newPolicy: BackupPolicy = {
      ...policy,
      id: generateId(),
      createdAt: new Date(),
      updatedAt: new Date()
    };
    
    this.policies.set(newPolicy.id, newPolicy);
    
    // 如果策略启用,则安排自动备份
    if (newPolicy.enabled) {
      await this.scheduleBackup(newPolicy.id);
    }
    
    return newPolicy;
  }
  
  async getBackupPolicies(): Promise<BackupPolicy[]> {
    return Array.from(this.policies.values());
  }
  
  async updateBackupPolicy(policyId: string, updates: Partial<BackupPolicy>): Promise<BackupPolicy> {
    const policy = this.policies.get(policyId);
    if (!policy) {
      throw new Error(`Backup policy ${policyId} not found`);
    }
    
    Object.assign(policy, updates, { updatedAt: new Date() });
    this.policies.set(policyId, policy);
    
    // 重新安排备份
    await this.cancelScheduledBackup(policyId);
    if (policy.enabled) {
      await this.scheduleBackup(policyId);
    }
    
    return policy;
  }
  
  async deleteBackupPolicy(policyId: string): Promise<void> {
    await this.cancelScheduledBackup(policyId);
    this.policies.delete(policyId);
  }
  
  async scheduleBackup(policyId: string): Promise<void> {
    const policy = this.policies.get(policyId);
    if (!policy || !policy.enabled) {
      return;
    }
    
    // 取消现有的调度
    await this.cancelScheduledBackup(policyId);
    
    // 计算下次执行时间
    const nextRun = this.calculateNextRun(policy.schedule);
    const delay = nextRun.getTime() - Date.now();
    
    if (delay > 0) {
      const timeout = setTimeout(async () => {
        try {
          await this.executeScheduledBackup(policy);
          // 重新安排下次备份
          await this.scheduleBackup(policyId);
        } catch (error) {
          console.error(`Scheduled backup failed for policy ${policyId}:`, error);
        }
      }, delay);
      
      this.scheduledJobs.set(policyId, timeout);
    }
  }
  
  async cancelScheduledBackup(policyId: string): Promise<void> {
    const timeout = this.scheduledJobs.get(policyId);
    if (timeout) {
      clearTimeout(timeout);
      this.scheduledJobs.delete(policyId);
    }
  }
  
  // 私有方法
  private async performBackup(backup: Backup, config: BackupConfig): Promise<void> {
    switch (backup.type) {
      case BackupType.DATABASE:
        await this.backupDatabase(backup, config);
        break;
      case BackupType.FILES:
        await this.backupFiles(backup, config);
        break;
      case BackupType.CONFIGURATION:
        await this.backupConfiguration(backup, config);
        break;
      case BackupType.FULL:
        await this.backupDatabase(backup, config);
        await this.backupFiles(backup, config);
        await this.backupConfiguration(backup, config);
        break;
    }
    
    // 计算校验和
    if (backup.location) {
      backup.checksum = await this.calculateChecksum(backup.location);
    }
  }
  
  private async backupDatabase(backup: Backup, config: BackupConfig): Promise<void> {
    // 数据库备份逻辑
    const dumpPath = `/tmp/db-backup-${backup.id}.sql`;
    
    try {
      // 执行数据库导出
      // 这里应该根据数据库类型执行相应的备份命令
      console.log(`Creating database backup: ${dumpPath}`);
      
      // 上传到存储
      const location = `backups/database/${backup.id}.sql`;
      await this.storageProvider.upload(dumpPath, location);
      
      backup.location = location;
      backup.size = await this.getFileSize(dumpPath);
      
      // 清理临时文件
      await this.deleteFile(dumpPath);
    } catch (error) {
      throw new Error(`Database backup failed: ${error.message}`);
    }
  }
  
  private async backupFiles(backup: Backup, config: BackupConfig): Promise<void> {
    // 文件备份逻辑
    const archivePath = `/tmp/files-backup-${backup.id}.tar.gz`;
    
    try {
      // 创建文件归档
      console.log(`Creating files backup: ${archivePath}`);
      
      // 上传到存储
      const location = `backups/files/${backup.id}.tar.gz`;
      await this.storageProvider.upload(archivePath, location);
      
      backup.location = location;
      backup.size = await this.getFileSize(archivePath);
      
      // 清理临时文件
      await this.deleteFile(archivePath);
    } catch (error) {
      throw new Error(`Files backup failed: ${error.message}`);
    }
  }
  
  private async backupConfiguration(backup: Backup, config: BackupConfig): Promise<void> {
    // 配置备份逻辑
    const configPath = `/tmp/config-backup-${backup.id}.json`;
    
    try {
      // 导出配置
      const configData = {
        environments: [], // 从配置管理器获取
        services: [], // 从服务管理器获取
        policies: Array.from(this.policies.values())
      };
      
      await this.writeFile(configPath, JSON.stringify(configData, null, 2));
      
      // 上传到存储
      const location = `backups/config/${backup.id}.json`;
      await this.storageProvider.upload(configPath, location);
      
      backup.location = location;
      backup.size = await this.getFileSize(configPath);
      
      // 清理临时文件
      await this.deleteFile(configPath);
    } catch (error) {
      throw new Error(`Configuration backup failed: ${error.message}`);
    }
  }
  
  private async performRestore(backup: Backup, options: RestoreOptions | undefined, result: RestoreResult): Promise<void> {
    switch (backup.type) {
      case BackupType.DATABASE:
        await this.restoreDatabase(backup, options, result);
        break;
      case BackupType.FILES:
        await this.restoreFiles(backup, options, result);
        break;
      case BackupType.CONFIGURATION:
        await this.restoreConfiguration(backup, options, result);
        break;
      case BackupType.FULL:
        await this.restoreDatabase(backup, options, result);
        await this.restoreFiles(backup, options, result);
        await this.restoreConfiguration(backup, options, result);
        break;
    }
  }
  
  private async restoreDatabase(backup: Backup, options: RestoreOptions | undefined, result: RestoreResult): Promise<void> {
    try {
      // 下载备份文件
      const tempPath = `/tmp/restore-db-${backup.id}.sql`;
      await this.storageProvider.download(backup.location!, tempPath);
      
      // 执行数据库恢复
      console.log(`Restoring database from: ${tempPath}`);
      
      result.restoredItems.push({
        type: 'database',
        name: 'main_database',
        status: 'success'
      });
      
      // 清理临时文件
      await this.deleteFile(tempPath);
    } catch (error) {
      result.restoredItems.push({
        type: 'database',
        name: 'main_database',
        status: 'failed',
        error: error.message
      });
      throw error;
    }
  }
  
  private async restoreFiles(backup: Backup, options: RestoreOptions | undefined, result: RestoreResult): Promise<void> {
    try {
      // 下载备份文件
      const tempPath = `/tmp/restore-files-${backup.id}.tar.gz`;
      await this.storageProvider.download(backup.location!, tempPath);
      
      // 解压文件
      console.log(`Restoring files from: ${tempPath}`);
      
      result.restoredItems.push({
        type: 'files',
        name: 'application_files',
        status: 'success'
      });
      
      // 清理临时文件
      await this.deleteFile(tempPath);
    } catch (error) {
      result.restoredItems.push({
        type: 'files',
        name: 'application_files',
        status: 'failed',
        error: error.message
      });
      throw error;
    }
  }
  
  private async restoreConfiguration(backup: Backup, options: RestoreOptions | undefined, result: RestoreResult): Promise<void> {
    try {
      // 下载备份文件
      const tempPath = `/tmp/restore-config-${backup.id}.json`;
      await this.storageProvider.download(backup.location!, tempPath);
      
      // 读取配置
      const configData = JSON.parse(await this.readFile(tempPath));
      
      // 恢复配置
      console.log(`Restoring configuration from: ${tempPath}`);
      
      result.restoredItems.push({
        type: 'configuration',
        name: 'system_config',
        status: 'success'
      });
      
      // 清理临时文件
      await this.deleteFile(tempPath);
    } catch (error) {
      result.restoredItems.push({
        type: 'configuration',
        name: 'system_config',
        status: 'failed',
        error: error.message
      });
      throw error;
    }
  }
  
  private async validateBackupContent(backup: Backup): Promise<{ errors: string[], warnings: string[] }> {
    const errors: string[] = [];
    const warnings: string[] = [];
    
    try {
      // 下载并验证备份内容
      const tempPath = `/tmp/validate-${backup.id}`;
      await this.storageProvider.download(backup.location!, tempPath);
      
      // 根据备份类型进行验证
      switch (backup.type) {
        case BackupType.DATABASE:
          // 验证SQL文件格式
          break;
        case BackupType.FILES:
          // 验证归档文件完整性
          break;
        case BackupType.CONFIGURATION:
          // 验证JSON格式
          const content = await this.readFile(tempPath);
          JSON.parse(content); // 验证JSON格式
          break;
      }
      
      // 清理临时文件
      await this.deleteFile(tempPath);
    } catch (error) {
      errors.push(`Content validation failed: ${error.message}`);
    }
    
    return { errors, warnings };
  }
  
  private calculateNextRun(schedule: BackupSchedule): Date {
    const now = new Date();
    const next = new Date(now);
    
    switch (schedule.frequency) {
      case ScheduleFrequency.HOURLY:
        next.setHours(next.getHours() + 1);
        break;
      case ScheduleFrequency.DAILY:
        next.setDate(next.getDate() + 1);
        if (schedule.time) {
          const [hours, minutes] = schedule.time.split(':').map(Number);
          next.setHours(hours, minutes, 0, 0);
        }
        break;
      case ScheduleFrequency.WEEKLY:
        next.setDate(next.getDate() + 7);
        if (schedule.dayOfWeek !== undefined) {
          const daysUntilTarget = (schedule.dayOfWeek - next.getDay() + 7) % 7;
          next.setDate(next.getDate() + daysUntilTarget);
        }
        break;
      case ScheduleFrequency.MONTHLY:
        next.setMonth(next.getMonth() + 1);
        if (schedule.dayOfMonth) {
          next.setDate(schedule.dayOfMonth);
        }
        break;
    }
    
    return next;
  }
  
  private async executeScheduledBackup(policy: BackupPolicy): Promise<void> {
    const config: BackupConfig = {
      name: `Scheduled backup - ${policy.name}`,
      description: `Automatic backup created by policy: ${policy.name}`,
      type: policy.backupType
    };
    
    await this.createBackup(config);
    
    // 清理旧备份
    if (policy.retention) {
      await this.cleanupOldBackups(policy);
    }
  }
  
  private async cleanupOldBackups(policy: BackupPolicy): Promise<void> {
    if (!policy.retention) return;
    
    const backups = await this.getBackups({ type: policy.backupType });
    const cutoffDate = new Date();
    cutoffDate.setDate(cutoffDate.getDate() - policy.retention.days);
    
    const oldBackups = backups.filter(backup => 
      backup.createdAt < cutoffDate && 
      backup.status === BackupStatus.COMPLETED
    );
    
    // 保留最少数量的备份
    const toDelete = oldBackups.slice(policy.retention.minCount || 0);
    
    for (const backup of toDelete) {
      try {
        await this.deleteBackup(backup.id);
      } catch (error) {
        console.error(`Failed to delete old backup ${backup.id}:`, error);
      }
    }
  }
  
  private initializeDefaultPolicies(): void {
    const defaultPolicies: Omit<BackupPolicy, 'id' | 'createdAt' | 'updatedAt'>[] = [
      {
        name: 'Daily Database Backup',
        description: '每日数据库备份',
        backupType: BackupType.DATABASE,
        schedule: {
          frequency: ScheduleFrequency.DAILY,
          time: '02:00'
        },
        retention: {
          days: 30,
          minCount: 7
        },
        enabled: true
      },
      {
        name: 'Weekly Full Backup',
        description: '每周完整备份',
        backupType: BackupType.FULL,
        schedule: {
          frequency: ScheduleFrequency.WEEKLY,
          dayOfWeek: 0, // Sunday
          time: '01:00'
        },
        retention: {
          days: 90,
          minCount: 4
        },
        enabled: true
      }
    ];
    
    for (const policy of defaultPolicies) {
      this.createBackupPolicy({
        ...policy,
        id: generateId(),
        createdAt: new Date(),
        updatedAt: new Date()
      });
    }
  }
  
  // 工具方法
  private async calculateChecksum(filePath: string): Promise<string> {
    const crypto = require('crypto');
    const fs = require('fs');
    
    return new Promise((resolve, reject) => {
      const hash = crypto.createHash('sha256');
      const stream = fs.createReadStream(filePath);
      
      stream.on('data', (data: Buffer) => hash.update(data));
      stream.on('end', () => resolve(hash.digest('hex')));
      stream.on('error', reject);
    });
  }
  
  private async getFileSize(filePath: string): Promise<number> {
    const fs = require('fs').promises;
    const stats = await fs.stat(filePath);
    return stats.size;
  }
  
  private async writeFile(filePath: string, content: string): Promise<void> {
    const fs = require('fs').promises;
    await fs.writeFile(filePath, content, 'utf8');
  }
  
  private async readFile(filePath: string): Promise<string> {
    const fs = require('fs').promises;
    return await fs.readFile(filePath, 'utf8');
  }
  
  private async deleteFile(filePath: string): Promise<void> {
    const fs = require('fs').promises;
    try {
      await fs.unlink(filePath);
    } catch (error) {
      // 忽略文件不存在的错误
    }
  }
}

// 备份相关接口
interface BackupConfig {
  name: string;
  description?: string;
  type: BackupType;
}

interface Backup {
  id: string;
  name: string;
  description?: string;
  type: BackupType;
  status: BackupStatus;
  location?: string;
  size: number;
  checksum?: string;
  createdAt: Date;
  updatedAt: Date;
  completedAt?: Date;
  error?: string;
  metadata: Record<string, any>;
}

enum BackupType {
  DATABASE = 'database',
  FILES = 'files',
  CONFIGURATION = 'configuration',
  FULL = 'full'
}

enum BackupStatus {
  RUNNING = 'running',
  COMPLETED = 'completed',
  FAILED = 'failed'
}

interface BackupQuery {
  type?: BackupType;
  status?: BackupStatus;
  startDate?: Date;
  endDate?: Date;
}

interface RestoreOptions {
  targetEnvironment?: string;
  overwrite?: boolean;
  skipValidation?: boolean;
}

interface RestoreResult {
  backupId: string;
  status: RestoreStatus;
  startedAt: Date;
  completedAt?: Date;
  error?: string;
  restoredItems: RestoredItem[];
}

enum RestoreStatus {
  RUNNING = 'running',
  COMPLETED = 'completed',
  FAILED = 'failed'
}

interface RestoredItem {
  type: string;
  name: string;
  status: 'success' | 'failed';
  error?: string;
}

interface BackupPolicy {
  id: string;
  name: string;
  description?: string;
  backupType: BackupType;
  schedule: BackupSchedule;
  retention?: RetentionPolicy;
  enabled: boolean;
  createdAt: Date;
  updatedAt: Date;
}

interface BackupSchedule {
  frequency: ScheduleFrequency;
  time?: string; // HH:MM format
  dayOfWeek?: number; // 0-6, Sunday = 0
  dayOfMonth?: number; // 1-31
}

enum ScheduleFrequency {
  HOURLY = 'hourly',
  DAILY = 'daily',
  WEEKLY = 'weekly',
  MONTHLY = 'monthly'
}

interface RetentionPolicy {
  days: number;
  minCount?: number;
}

interface StorageProvider {
  upload(localPath: string, remotePath: string): Promise<void>;
  download(remotePath: string, localPath: string): Promise<void>;
  delete(remotePath: string): Promise<void>;
  exists(remotePath: string): Promise<boolean>;
}

interface ValidationResult { valid: boolean; errors: string[]; warnings: string[]; }


## 14.8 使用示例

```typescript
// 低代码平台部署与运维示例
class LowCodeDeploymentDemo {
  private deploymentManager: DeploymentManager;
  private logManager: LogManager;
  private backupManager: BackupManager;
  private monitoringService: MonitoringService;
  
  constructor() {
    // 初始化各个管理器
    this.deploymentManager = new LowCodeDeploymentManager();
    this.logManager = new ElasticsearchLogManager({
      url: 'http://localhost:9200',
      indexPrefix: 'lowcode-logs'
    });
    this.backupManager = new LowCodeBackupManager(new S3StorageProvider());
    this.monitoringService = new PrometheusMonitoringService({
      prometheusUrl: 'http://localhost:9090'
    });
  }
  
  async demonstrateDeployment(): Promise<void> {
    console.log('=== 部署管理示例 ===');
    
    try {
      // 1. 创建环境
      const environment = await this.deploymentManager.createEnvironment({
        name: 'production',
        description: '生产环境',
        type: EnvironmentType.PRODUCTION,
        configuration: {
          replicas: 3,
          resources: {
            cpu: '2',
            memory: '4Gi'
          },
          networking: {
            loadBalancer: true,
            ssl: true
          }
        }
      });
      console.log('Environment created:', environment.name);
      
      // 2. 部署应用
      const deployment = await this.deploymentManager.deployApplication({
        name: 'lowcode-app',
        version: '1.0.0',
        environmentId: environment.id,
        image: 'lowcode/app:1.0.0',
        configuration: {
          replicas: 3,
          resources: {
            cpu: '1',
            memory: '2Gi'
          },
          environment: {
            NODE_ENV: 'production',
            DATABASE_URL: 'postgresql://localhost:5432/lowcode'
          }
        }
      });
      console.log('Application deployed:', deployment.id);
      
      // 3. 检查部署状态
      const status = await this.deploymentManager.getDeploymentStatus(deployment.id);
      console.log('Deployment status:', status);
      
      // 4. 扩缩容
      await this.deploymentManager.scaleDeployment(deployment.id, 5);
      console.log('Deployment scaled to 5 replicas');
      
      // 5. 更新应用
      const updatedDeployment = await this.deploymentManager.updateDeployment(deployment.id, {
        version: '1.1.0',
        image: 'lowcode/app:1.1.0'
      });
      console.log('Application updated to version:', updatedDeployment.version);
      
    } catch (error) {
      console.error('Deployment failed:', error);
    }
  }
  
  async demonstrateLogging(): Promise<void> {
    console.log('\n=== 日志管理示例 ===');
    
    try {
      // 1. 收集日志
      const logSources: LogSource[] = [
        {
          name: 'app-logs',
          type: LogSourceType.FILE,
          service: 'lowcode-app',
          path: '/var/log/app.log'
        },
        {
          name: 'container-logs',
          type: LogSourceType.CONTAINER,
          service: 'lowcode-app',
          containerId: 'lowcode-app-container'
        }
      ];
      
      for (const source of logSources) {
        await this.logManager.collectLogs(source);
        console.log(`Logs collected from: ${source.name}`);
      }
      
      // 2. 查询日志
      const logs = await this.logManager.getLogs({
        service: 'lowcode-app',
        level: 'error',
        startTime: new Date(Date.now() - 24 * 60 * 60 * 1000), // 最近24小时
        limit: 100
      });
      console.log(`Found ${logs.length} error logs`);
      
      // 3. 日志聚合
      const aggregation = await this.logManager.aggregateLogs({
        type: AggregationType.COUNT_BY_LEVEL,
        startTime: new Date(Date.now() - 24 * 60 * 60 * 1000)
      });
      console.log('Log level distribution:', aggregation.data);
      
      // 4. 搜索日志
      const searchResult = await this.logManager.searchLogs({
        query: 'database connection failed',
        filters: {
          service: 'lowcode-app'
        },
        size: 50
      });
      console.log(`Search found ${searchResult.total} matching logs`);
      
      // 5. 导出日志
      const exportedLogs = await this.logManager.exportLogs({
        service: 'lowcode-app',
        startTime: new Date(Date.now() - 60 * 60 * 1000), // 最近1小时
        limit: 1000
      }, ExportFormat.JSON);
      console.log('Logs exported, size:', exportedLogs.length);
      
    } catch (error) {
      console.error('Logging operation failed:', error);
    }
  }
  
  async demonstrateBackup(): Promise<void> {
    console.log('\n=== 备份管理示例 ===');
    
    try {
      // 1. 创建备份策略
      const policy = await this.backupManager.createBackupPolicy({
        name: 'Daily Production Backup',
        description: '每日生产环境备份',
        backupType: BackupType.FULL,
        schedule: {
          frequency: ScheduleFrequency.DAILY,
          time: '02:00'
        },
        retention: {
          days: 30,
          minCount: 7
        },
        enabled: true,
        id: '',
        createdAt: new Date(),
        updatedAt: new Date()
      });
      console.log('Backup policy created:', policy.name);
      
      // 2. 手动创建备份
      const backup = await this.backupManager.createBackup({
        name: 'Manual Full Backup',
        description: '手动创建的完整备份',
        type: BackupType.FULL
      });
      console.log('Backup created:', backup.id);
      
      // 3. 验证备份
      const validation = await this.backupManager.validateBackup(backup.id);
      if (validation.valid) {
        console.log('Backup validation passed');
      } else {
        console.log('Backup validation failed:', validation.errors);
      }
      
      // 4. 查询备份
      const backups = await this.backupManager.getBackups({
        type: BackupType.FULL,
        status: BackupStatus.COMPLETED
      });
      console.log(`Found ${backups.length} completed full backups`);
      
      // 5. 恢复备份(模拟)
      if (backups.length > 0) {
        const restoreResult = await this.backupManager.restoreBackup(backups[0].id, {
          targetEnvironment: 'staging',
          overwrite: false
        });
        console.log('Restore completed:', restoreResult.status);
        console.log('Restored items:', restoreResult.restoredItems.length);
      }
      
    } catch (error) {
      console.error('Backup operation failed:', error);
    }
  }
  
  async demonstrateMonitoring(): Promise<void> {
    console.log('\n=== 监控管理示例 ===');
    
    try {
      // 1. 收集指标
      await this.monitoringService.collectMetrics({
        source: 'lowcode-app',
        metrics: {
          cpu_usage: 75.5,
          memory_usage: 60.2,
          request_count: 1000,
          error_count: 5
        },
        timestamp: new Date()
      });
      console.log('Metrics collected');
      
      // 2. 创建告警规则
      const alertRule = await this.monitoringService.createAlertRule({
        name: 'High CPU Usage',
        description: 'CPU使用率过高告警',
        condition: 'cpu_usage > 80',
        severity: AlertSeverity.WARNING,
        enabled: true,
        actions: [
          {
            type: AlertActionType.EMAIL,
            config: {
              recipients: ['admin@example.com'],
              subject: 'High CPU Usage Alert'
            }
          }
        ]
      });
      console.log('Alert rule created:', alertRule.name);
      
      // 3. 查询指标
      const metrics = await this.monitoringService.queryMetrics({
        metric: 'cpu_usage',
        startTime: new Date(Date.now() - 60 * 60 * 1000), // 最近1小时
        endTime: new Date(),
        step: '5m'
      });
      console.log(`Found ${metrics.length} metric data points`);
      
      // 4. 获取告警
      const alerts = await this.monitoringService.getAlerts({
        severity: AlertSeverity.WARNING,
        status: AlertStatus.ACTIVE
      });
      console.log(`Found ${alerts.length} active warning alerts`);
      
      // 5. 创建仪表板
      const dashboard = await this.monitoringService.createDashboard({
        name: 'Application Overview',
        description: '应用程序概览仪表板',
        panels: [
          {
            title: 'CPU Usage',
            type: 'graph',
            query: 'cpu_usage',
            timeRange: '1h'
          },
          {
            title: 'Memory Usage',
            type: 'graph',
            query: 'memory_usage',
            timeRange: '1h'
          },
          {
            title: 'Request Rate',
            type: 'stat',
            query: 'rate(request_count[5m])',
            timeRange: '1h'
          }
        ]
      });
      console.log('Dashboard created:', dashboard.name);
      
    } catch (error) {
      console.error('Monitoring operation failed:', error);
    }
  }
  
  async demonstrateHealthCheck(): Promise<void> {
    console.log('\n=== 健康检查示例 ===');
    
    try {
      // 1. 检查部署健康状态
      const deployments = await this.deploymentManager.getDeployments();
      
      for (const deployment of deployments) {
        const health = await this.deploymentManager.checkHealth(deployment.id);
        console.log(`Deployment ${deployment.name} health:`, health.status);
        
        if (health.status !== HealthStatus.HEALTHY) {
          console.log('Health issues:', health.issues);
        }
      }
      
      // 2. 系统整体健康检查
      const systemHealth = await this.monitoringService.checkHealth();
      console.log('System health:', systemHealth.status);
      
      if (systemHealth.components) {
        for (const [component, status] of Object.entries(systemHealth.components)) {
          console.log(`  ${component}: ${status}`);
        }
      }
      
    } catch (error) {
      console.error('Health check failed:', error);
    }
  }
  
  async runDemo(): Promise<void> {
    console.log('开始低代码平台部署与运维演示...');
    
    await this.demonstrateDeployment();
    await this.demonstrateLogging();
    await this.demonstrateBackup();
    await this.demonstrateMonitoring();
    await this.demonstrateHealthCheck();
    
    console.log('\n演示完成!');
  }
}

// 运行示例
async function runDeploymentDemo() {
  const demo = new LowCodeDeploymentDemo();
  await demo.runDemo();
}

// S3存储提供者示例
class S3StorageProvider implements StorageProvider {
  private s3Client: any;
  private bucketName: string;
  
  constructor(config: { accessKey: string, secretKey: string, bucketName: string }) {
    // 初始化S3客户端
    this.bucketName = config.bucketName;
  }
  
  async upload(localPath: string, remotePath: string): Promise<void> {
    // 上传文件到S3
    console.log(`Uploading ${localPath} to s3://${this.bucketName}/${remotePath}`);
  }
  
  async download(remotePath: string, localPath: string): Promise<void> {
    // 从S3下载文件
    console.log(`Downloading s3://${this.bucketName}/${remotePath} to ${localPath}`);
  }
  
  async delete(remotePath: string): Promise<void> {
    // 删除S3文件
    console.log(`Deleting s3://${this.bucketName}/${remotePath}`);
  }
  
  async exists(remotePath: string): Promise<boolean> {
    // 检查S3文件是否存在
    console.log(`Checking if s3://${this.bucketName}/${remotePath} exists`);
    return true;
  }
}

14.9 Web API 集成

// Express.js API 集成示例
import express from 'express';
import { DeploymentManager, LogManager, BackupManager, MonitoringService } from './deployment';

const app = express();
app.use(express.json());

// 初始化管理器
const deploymentManager = new LowCodeDeploymentManager();
const logManager = new ElasticsearchLogManager({ url: 'http://localhost:9200' });
const backupManager = new LowCodeBackupManager(new S3StorageProvider({
  accessKey: process.env.AWS_ACCESS_KEY!,
  secretKey: process.env.AWS_SECRET_KEY!,
  bucketName: process.env.S3_BUCKET_NAME!
}));
const monitoringService = new PrometheusMonitoringService({
  prometheusUrl: 'http://localhost:9090'
});

// 部署管理 API
app.get('/api/environments', async (req, res) => {
  try {
    const environments = await deploymentManager.getEnvironments();
    res.json(environments);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.post('/api/environments', async (req, res) => {
  try {
    const environment = await deploymentManager.createEnvironment(req.body);
    res.status(201).json(environment);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.get('/api/deployments', async (req, res) => {
  try {
    const deployments = await deploymentManager.getDeployments();
    res.json(deployments);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.post('/api/deployments', async (req, res) => {
  try {
    const deployment = await deploymentManager.deployApplication(req.body);
    res.status(201).json(deployment);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.get('/api/deployments/:id/status', async (req, res) => {
  try {
    const status = await deploymentManager.getDeploymentStatus(req.params.id);
    res.json(status);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.post('/api/deployments/:id/scale', async (req, res) => {
  try {
    const { replicas } = req.body;
    await deploymentManager.scaleDeployment(req.params.id, replicas);
    res.json({ message: 'Deployment scaled successfully' });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.put('/api/deployments/:id', async (req, res) => {
  try {
    const deployment = await deploymentManager.updateDeployment(req.params.id, req.body);
    res.json(deployment);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.delete('/api/deployments/:id', async (req, res) => {
  try {
    await deploymentManager.deleteDeployment(req.params.id);
    res.json({ message: 'Deployment deleted successfully' });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 日志管理 API
app.get('/api/logs', async (req, res) => {
  try {
    const query = {
      service: req.query.service as string,
      level: req.query.level as string,
      keyword: req.query.keyword as string,
      startTime: req.query.startTime ? new Date(req.query.startTime as string) : undefined,
      endTime: req.query.endTime ? new Date(req.query.endTime as string) : undefined,
      limit: req.query.limit ? parseInt(req.query.limit as string) : undefined
    };
    
    const logs = await logManager.getLogs(query);
    res.json(logs);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.get('/api/logs/search', async (req, res) => {
  try {
    const searchQuery = {
      query: req.query.q as string,
      filters: req.query.filters ? JSON.parse(req.query.filters as string) : undefined,
      startTime: req.query.startTime ? new Date(req.query.startTime as string) : undefined,
      endTime: req.query.endTime ? new Date(req.query.endTime as string) : undefined,
      size: req.query.size ? parseInt(req.query.size as string) : undefined,
      from: req.query.from ? parseInt(req.query.from as string) : undefined
    };
    
    const result = await logManager.searchLogs(searchQuery);
    res.json(result);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.get('/api/logs/aggregate', async (req, res) => {
  try {
    const query = {
      type: req.query.type as AggregationType,
      startTime: req.query.startTime ? new Date(req.query.startTime as string) : undefined,
      endTime: req.query.endTime ? new Date(req.query.endTime as string) : undefined,
      interval: req.query.interval as string
    };
    
    const result = await logManager.aggregateLogs(query);
    res.json(result);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.get('/api/logs/export', async (req, res) => {
  try {
    const query = {
      service: req.query.service as string,
      level: req.query.level as string,
      startTime: req.query.startTime ? new Date(req.query.startTime as string) : undefined,
      endTime: req.query.endTime ? new Date(req.query.endTime as string) : undefined,
      limit: req.query.limit ? parseInt(req.query.limit as string) : undefined
    };
    
    const format = req.query.format as ExportFormat || ExportFormat.JSON;
    const exportedData = await logManager.exportLogs(query, format);
    
    res.setHeader('Content-Type', format === ExportFormat.JSON ? 'application/json' : 'text/plain');
    res.setHeader('Content-Disposition', `attachment; filename=logs.${format}`);
    res.send(exportedData);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 备份管理 API
app.get('/api/backups', async (req, res) => {
  try {
    const query = {
      type: req.query.type as BackupType,
      status: req.query.status as BackupStatus,
      startDate: req.query.startDate ? new Date(req.query.startDate as string) : undefined,
      endDate: req.query.endDate ? new Date(req.query.endDate as string) : undefined
    };
    
    const backups = await backupManager.getBackups(query);
    res.json(backups);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.post('/api/backups', async (req, res) => {
  try {
    const backup = await backupManager.createBackup(req.body);
    res.status(201).json(backup);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.post('/api/backups/:id/restore', async (req, res) => {
  try {
    const result = await backupManager.restoreBackup(req.params.id, req.body);
    res.json(result);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.get('/api/backups/:id/validate', async (req, res) => {
  try {
    const result = await backupManager.validateBackup(req.params.id);
    res.json(result);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.delete('/api/backups/:id', async (req, res) => {
  try {
    await backupManager.deleteBackup(req.params.id);
    res.json({ message: 'Backup deleted successfully' });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.get('/api/backup-policies', async (req, res) => {
  try {
    const policies = await backupManager.getBackupPolicies();
    res.json(policies);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.post('/api/backup-policies', async (req, res) => {
  try {
    const policy = await backupManager.createBackupPolicy(req.body);
    res.status(201).json(policy);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 监控管理 API
app.get('/api/metrics', async (req, res) => {
  try {
    const query = {
      metric: req.query.metric as string,
      startTime: req.query.startTime ? new Date(req.query.startTime as string) : undefined,
      endTime: req.query.endTime ? new Date(req.query.endTime as string) : undefined,
      step: req.query.step as string
    };
    
    const metrics = await monitoringService.queryMetrics(query);
    res.json(metrics);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.get('/api/alerts', async (req, res) => {
  try {
    const query = {
      severity: req.query.severity as AlertSeverity,
      status: req.query.status as AlertStatus,
      startTime: req.query.startTime ? new Date(req.query.startTime as string) : undefined,
      endTime: req.query.endTime ? new Date(req.query.endTime as string) : undefined
    };
    
    const alerts = await monitoringService.getAlerts(query);
    res.json(alerts);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.post('/api/alert-rules', async (req, res) => {
  try {
    const rule = await monitoringService.createAlertRule(req.body);
    res.status(201).json(rule);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.get('/api/dashboards', async (req, res) => {
  try {
    const dashboards = await monitoringService.getDashboards();
    res.json(dashboards);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.post('/api/dashboards', async (req, res) => {
  try {
    const dashboard = await monitoringService.createDashboard(req.body);
    res.status(201).json(dashboard);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 健康检查 API
app.get('/api/health', async (req, res) => {
  try {
    const health = await monitoringService.checkHealth();
    res.json(health);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.get('/api/deployments/:id/health', async (req, res) => {
  try {
    const health = await deploymentManager.checkHealth(req.params.id);
    res.json(health);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 启动服务器
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Deployment and Operations API server running on port ${PORT}`);
});

14.10 小结

本章详细介绍了低代码平台的部署与运维系统,涵盖了现代应用程序生命周期管理的各个方面。

核心功能回顾

  1. 部署管理

    • 环境管理和配置
    • 应用部署和更新
    • 服务管理和扩缩容
    • 健康检查和监控
  2. 容器编排

    • Docker容器管理
    • Kubernetes集群操作
    • 服务发现和负载均衡
    • 资源管理和调度
  3. 配置管理

    • 环境配置和模板
    • 安全配置和密钥管理
    • 配置验证和同步
    • 版本控制和回滚
  4. 监控服务

    • 指标收集和存储
    • 告警规则和通知
    • 仪表板和可视化
    • 性能分析和优化
  5. 日志管理

    • 日志收集和聚合
    • 全文搜索和过滤
    • 日志分析和统计
    • 导出和归档
  6. 备份恢复

    • 自动备份策略
    • 数据完整性验证
    • 灾难恢复计划
    • 备份生命周期管理

技术特色

  1. 云原生架构

    • 容器化部署
    • 微服务架构
    • 弹性扩缩容
    • 服务网格集成
  2. 自动化运维

    • CI/CD集成
    • 自动化部署
    • 智能监控
    • 自愈能力
  3. 可观测性

    • 全链路追踪
    • 指标监控
    • 日志分析
    • 性能诊断
  4. 安全性

    • 密钥管理
    • 访问控制
    • 安全扫描
    • 合规审计
  5. 高可用性

    • 多副本部署
    • 故障转移
    • 负载均衡
    • 灾难恢复
  6. 可扩展性

    • 水平扩展
    • 垂直扩展
    • 多云部署
    • 边缘计算

通过本章的学习,您已经掌握了构建企业级低代码平台部署与运维系统的核心技术。这些技术不仅适用于低代码平台,也可以应用于其他现代应用程序的运维管理。

下一章我们将学习低代码平台的安全与权限管理,探讨如何构建安全可靠的企业级应用平台。

// 部署管理器接口
interface DeploymentManager {
  // 环境管理
  createEnvironment(definition: EnvironmentDefinition): Promise<Environment>;
  getEnvironments(query?: EnvironmentQuery): Promise<Environment[]>;
  getEnvironment(id: string): Promise<Environment | null>;
  updateEnvironment(id: string, updates: Partial<EnvironmentDefinition>): Promise<Environment>;
  deleteEnvironment(id: string): Promise<void>;
  
  // 应用部署
  deployApplication(deployment: DeploymentDefinition): Promise<Deployment>;
  getDeployments(query?: DeploymentQuery): Promise<Deployment[]>;
  getDeployment(id: string): Promise<Deployment | null>;
  updateDeployment(id: string, updates: Partial<DeploymentDefinition>): Promise<Deployment>;
  rollbackDeployment(id: string, version?: string): Promise<Deployment>;
  deleteDeployment(id: string): Promise<void>;
  
  // 服务管理
  startService(serviceId: string): Promise<void>;
  stopService(serviceId: string): Promise<void>;
  restartService(serviceId: string): Promise<void>;
  scaleService(serviceId: string, replicas: number): Promise<void>;
  getServiceStatus(serviceId: string): Promise<ServiceStatus>;
  
  // 配置管理
  createConfig(config: ConfigDefinition): Promise<Config>;
  getConfigs(query?: ConfigQuery): Promise<Config[]>;
  updateConfig(id: string, updates: Partial<ConfigDefinition>): Promise<Config>;
  deleteConfig(id: string): Promise<void>;
  
  // 健康检查
  getHealthStatus(): Promise<HealthStatus>;
  checkServiceHealth(serviceId: string): Promise<ServiceHealth>;
  
  // 监控指标
  getMetrics(query?: MetricsQuery): Promise<DeploymentMetrics>;
  getServiceMetrics(serviceId: string, query?: MetricsQuery): Promise<ServiceMetrics>;
}

// 低代码部署管理器实现
class LowCodeDeploymentManager implements DeploymentManager {
  private environments: Map<string, Environment> = new Map();
  private deployments: Map<string, Deployment> = new Map();
  private configs: Map<string, Config> = new Map();
  private containerOrchestrator: ContainerOrchestrator;
  private configManager: ConfigurationManager;
  private monitoringService: MonitoringService;
  private eventBus: EventBus;
  
  constructor(options: DeploymentManagerOptions) {
    this.containerOrchestrator = options.containerOrchestrator || new DockerOrchestrator();
    this.configManager = options.configManager || new KubernetesConfigManager();
    this.monitoringService = options.monitoringService || new PrometheusMonitoringService();
    this.eventBus = options.eventBus || new LowCodeEventBus();
    
    this.initializeDefaultEnvironments();
  }
  
  // 创建环境
  async createEnvironment(definition: EnvironmentDefinition): Promise<Environment> {
    const environment: Environment = {
      id: generateId(),
      name: definition.name,
      type: definition.type,
      description: definition.description,
      config: definition.config,
      resources: definition.resources,
      networking: definition.networking,
      security: definition.security,
      status: EnvironmentStatus.CREATING,
      createdAt: new Date(),
      updatedAt: new Date()
    };
    
    try {
      // 创建环境资源
      await this.containerOrchestrator.createNamespace(environment.name);
      
      // 应用网络配置
      if (environment.networking) {
        await this.configManager.applyNetworkConfig(environment.id, environment.networking);
      }
      
      // 应用安全配置
      if (environment.security) {
        await this.configManager.applySecurityConfig(environment.id, environment.security);
      }
      
      environment.status = EnvironmentStatus.ACTIVE;
      this.environments.set(environment.id, environment);
      
      // 发布事件
      await this.eventBus.emit('environment.created', { environment });
      
      return environment;
    } catch (error) {
      environment.status = EnvironmentStatus.FAILED;
      environment.error = error.message;
      throw error;
    }
  }
  
  // 部署应用
  async deployApplication(definition: DeploymentDefinition): Promise<Deployment> {
    const deployment: Deployment = {
      id: generateId(),
      name: definition.name,
      applicationId: definition.applicationId,
      environmentId: definition.environmentId,
      version: definition.version,
      config: definition.config,
      resources: definition.resources,
      services: [],
      status: DeploymentStatus.DEPLOYING,
      createdAt: new Date(),
      updatedAt: new Date()
    };
    
    try {
      // 获取环境信息
      const environment = this.environments.get(definition.environmentId);
      if (!environment) {
        throw new Error(`Environment ${definition.environmentId} not found`);
      }
      
      // 构建服务列表
      const services = await this.buildServices(definition);
      deployment.services = services;
      
      // 部署服务
      for (const service of services) {
        await this.deployService(service, environment);
      }
      
      deployment.status = DeploymentStatus.RUNNING;
      this.deployments.set(deployment.id, deployment);
      
      // 发布事件
      await this.eventBus.emit('deployment.created', { deployment });
      
      return deployment;
    } catch (error) {
      deployment.status = DeploymentStatus.FAILED;
      deployment.error = error.message;
      throw error;
    }
  }
  
  // 构建服务
  private async buildServices(definition: DeploymentDefinition): Promise<ServiceDefinition[]> {
    const services: ServiceDefinition[] = [];
    
    // 前端服务
    if (definition.config.frontend) {
      services.push({
        id: generateId(),
        name: `${definition.name}-frontend`,
        type: ServiceType.FRONTEND,
        image: definition.config.frontend.image || 'nginx:alpine',
        ports: [{ containerPort: 80, servicePort: 80 }],
        resources: definition.resources?.frontend || {
          requests: { cpu: '100m', memory: '128Mi' },
          limits: { cpu: '500m', memory: '512Mi' }
        },
        env: definition.config.frontend.env || {},
        volumes: definition.config.frontend.volumes || []
      });
    }
    
    // 后端服务
    if (definition.config.backend) {
      services.push({
        id: generateId(),
        name: `${definition.name}-backend`,
        type: ServiceType.BACKEND,
        image: definition.config.backend.image || 'node:16-alpine',
        ports: [{ containerPort: 3000, servicePort: 3000 }],
        resources: definition.resources?.backend || {
          requests: { cpu: '200m', memory: '256Mi' },
          limits: { cpu: '1000m', memory: '1Gi' }
        },
        env: definition.config.backend.env || {},
        volumes: definition.config.backend.volumes || []
      });
    }
    
    // 数据库服务
    if (definition.config.database) {
      services.push({
        id: generateId(),
        name: `${definition.name}-database`,
        type: ServiceType.DATABASE,
        image: definition.config.database.image || 'postgres:13',
        ports: [{ containerPort: 5432, servicePort: 5432 }],
        resources: definition.resources?.database || {
          requests: { cpu: '200m', memory: '512Mi' },
          limits: { cpu: '1000m', memory: '2Gi' }
        },
        env: definition.config.database.env || {},
        volumes: definition.config.database.volumes || [
          {
            name: 'data',
            mountPath: '/var/lib/postgresql/data',
            type: VolumeType.PERSISTENT
          }
        ]
      });
    }
    
    return services;
  }
  
  // 部署服务
  private async deployService(service: ServiceDefinition, environment: Environment): Promise<void> {
    // 创建部署配置
    const deploymentConfig = {
      name: service.name,
      namespace: environment.name,
      image: service.image,
      replicas: service.replicas || 1,
      ports: service.ports,
      env: service.env,
      resources: service.resources,
      volumes: service.volumes
    };
    
    // 部署到容器编排器
    await this.containerOrchestrator.deployService(deploymentConfig);
    
    // 创建服务
    if (service.ports && service.ports.length > 0) {
      await this.containerOrchestrator.createService({
        name: service.name,
        namespace: environment.name,
        selector: { app: service.name },
        ports: service.ports
      });
    }
  }
  
  // 获取健康状态
  async getHealthStatus(): Promise<HealthStatus> {
    const environments = Array.from(this.environments.values());
    const deployments = Array.from(this.deployments.values());
    
    const healthyEnvironments = environments.filter(env => env.status === EnvironmentStatus.ACTIVE).length;
    const runningDeployments = deployments.filter(dep => dep.status === DeploymentStatus.RUNNING).length;
    
    return {
      status: healthyEnvironments > 0 && runningDeployments > 0 ? 'healthy' : 'unhealthy',
      environments: {
        total: environments.length,
        healthy: healthyEnvironments,
        unhealthy: environments.length - healthyEnvironments
      },
      deployments: {
        total: deployments.length,
        running: runningDeployments,
        failed: deployments.filter(dep => dep.status === DeploymentStatus.FAILED).length
      },
      timestamp: new Date()
    };
  }
  
  // 获取指标
  async getMetrics(query?: MetricsQuery): Promise<DeploymentMetrics> {
    const deployments = Array.from(this.deployments.values());
    
    return {
      deployments: {
        total: deployments.length,
        running: deployments.filter(d => d.status === DeploymentStatus.RUNNING).length,
        failed: deployments.filter(d => d.status === DeploymentStatus.FAILED).length,
        deploying: deployments.filter(d => d.status === DeploymentStatus.DEPLOYING).length
      },
      resources: await this.getResourceMetrics(),
      performance: await this.getPerformanceMetrics(),
      timestamp: new Date()
    };
  }
  
  // 获取资源指标
  private async getResourceMetrics(): Promise<ResourceMetrics> {
    // 从监控服务获取资源使用情况
    return await this.monitoringService.getResourceMetrics();
  }
  
  // 获取性能指标
  private async getPerformanceMetrics(): Promise<PerformanceMetrics> {
    // 从监控服务获取性能指标
    return await this.monitoringService.getPerformanceMetrics();
  }
  
  // 初始化默认环境
  private initializeDefaultEnvironments(): void {
    const defaultEnvironments = [
      {
        name: 'development',
        type: EnvironmentType.DEVELOPMENT,
        description: '开发环境',
        config: {
          autoScale: false,
          monitoring: true,
          logging: true
        },
        resources: {
          cpu: '2',
          memory: '4Gi',
          storage: '20Gi'
        }
      },
      {
        name: 'staging',
        type: EnvironmentType.STAGING,
        description: '预发布环境',
        config: {
          autoScale: true,
          monitoring: true,
          logging: true
        },
        resources: {
          cpu: '4',
          memory: '8Gi',
          storage: '50Gi'
        }
      },
      {
        name: 'production',
        type: EnvironmentType.PRODUCTION,
        description: '生产环境',
        config: {
          autoScale: true,
          monitoring: true,
          logging: true,
          backup: true
        },
        resources: {
          cpu: '8',
          memory: '16Gi',
          storage: '100Gi'
        }
      }
    ];
    
    // 这里可以异步创建默认环境
  }
  
  // 其他方法实现...
  async getEnvironments(query?: EnvironmentQuery): Promise<Environment[]> {
    let environments = Array.from(this.environments.values());
    
    if (query?.type) {
      environments = environments.filter(env => env.type === query.type);
    }
    
    if (query?.status) {
      environments = environments.filter(env => env.status === query.status);
    }
    
    return environments;
  }
  
  async getEnvironment(id: string): Promise<Environment | null> {
    return this.environments.get(id) || null;
  }
  
  async updateEnvironment(id: string, updates: Partial<EnvironmentDefinition>): Promise<Environment> {
    const environment = this.environments.get(id);
    if (!environment) {
      throw new Error(`Environment ${id} not found`);
    }
    
    Object.assign(environment, updates, { updatedAt: new Date() });
    this.environments.set(id, environment);
    
    await this.eventBus.emit('environment.updated', { environment });
    
    return environment;
  }
  
  async deleteEnvironment(id: string): Promise<void> {
    const environment = this.environments.get(id);
    if (!environment) {
      throw new Error(`Environment ${id} not found`);
    }
    
    // 删除环境资源
    await this.containerOrchestrator.deleteNamespace(environment.name);
    
    this.environments.delete(id);
    
    await this.eventBus.emit('environment.deleted', { environmentId: id });
  }
  
  async getDeployments(query?: DeploymentQuery): Promise<Deployment[]> {
    let deployments = Array.from(this.deployments.values());
    
    if (query?.environmentId) {
      deployments = deployments.filter(dep => dep.environmentId === query.environmentId);
    }
    
    if (query?.status) {
      deployments = deployments.filter(dep => dep.status === query.status);
    }
    
    return deployments;
  }
  
  async getDeployment(id: string): Promise<Deployment | null> {
    return this.deployments.get(id) || null;
  }
  
  async updateDeployment(id: string, updates: Partial<DeploymentDefinition>): Promise<Deployment> {
    const deployment = this.deployments.get(id);
    if (!deployment) {
      throw new Error(`Deployment ${id} not found`);
    }
    
    Object.assign(deployment, updates, { updatedAt: new Date() });
    this.deployments.set(id, deployment);
    
    await this.eventBus.emit('deployment.updated', { deployment });
    
    return deployment;
  }
  
  async rollbackDeployment(id: string, version?: string): Promise<Deployment> {
    const deployment = this.deployments.get(id);
    if (!deployment) {
      throw new Error(`Deployment ${id} not found`);
    }
    
    // 实现回滚逻辑
    const rollbackVersion = version || deployment.previousVersion;
    if (!rollbackVersion) {
      throw new Error('No previous version available for rollback');
    }
    
    deployment.previousVersion = deployment.version;
    deployment.version = rollbackVersion;
    deployment.status = DeploymentStatus.DEPLOYING;
    deployment.updatedAt = new Date();
    
    // 执行回滚部署
    await this.deployApplication({
      name: deployment.name,
      applicationId: deployment.applicationId,
      environmentId: deployment.environmentId,
      version: rollbackVersion,
      config: deployment.config,
      resources: deployment.resources
    });
    
    await this.eventBus.emit('deployment.rolledback', { deployment });
    
    return deployment;
  }
  
  async deleteDeployment(id: string): Promise<void> {
    const deployment = this.deployments.get(id);
    if (!deployment) {
      throw new Error(`Deployment ${id} not found`);
    }
    
    // 删除部署的服务
    const environment = this.environments.get(deployment.environmentId);
    if (environment) {
      for (const service of deployment.services) {
        await this.containerOrchestrator.deleteService(service.name, environment.name);
      }
    }
    
    this.deployments.delete(id);
    
    await this.eventBus.emit('deployment.deleted', { deploymentId: id });
  }
  
  async startService(serviceId: string): Promise<void> {
    await this.containerOrchestrator.scaleService(serviceId, 1);
  }
  
  async stopService(serviceId: string): Promise<void> {
    await this.containerOrchestrator.scaleService(serviceId, 0);
  }
  
  async restartService(serviceId: string): Promise<void> {
    await this.containerOrchestrator.restartService(serviceId);
  }
  
  async scaleService(serviceId: string, replicas: number): Promise<void> {
    await this.containerOrchestrator.scaleService(serviceId, replicas);
  }
  
  async getServiceStatus(serviceId: string): Promise<ServiceStatus> {
    return await this.containerOrchestrator.getServiceStatus(serviceId);
  }
  
  async createConfig(config: ConfigDefinition): Promise<Config> {
    const newConfig: Config = {
      id: generateId(),
      name: config.name,
      type: config.type,
      data: config.data,
      environmentId: config.environmentId,
      createdAt: new Date(),
      updatedAt: new Date()
    };
    
    this.configs.set(newConfig.id, newConfig);
    
    await this.eventBus.emit('config.created', { config: newConfig });
    
    return newConfig;
  }
  
  async getConfigs(query?: ConfigQuery): Promise<Config[]> {
    let configs = Array.from(this.configs.values());
    
    if (query?.environmentId) {
      configs = configs.filter(config => config.environmentId === query.environmentId);
    }
    
    if (query?.type) {
      configs = configs.filter(config => config.type === query.type);
    }
    
    return configs;
  }
  
  async updateConfig(id: string, updates: Partial<ConfigDefinition>): Promise<Config> {
    const config = this.configs.get(id);
    if (!config) {
      throw new Error(`Config ${id} not found`);
    }
    
    Object.assign(config, updates, { updatedAt: new Date() });
    this.configs.set(id, config);
    
    await this.eventBus.emit('config.updated', { config });
    
    return config;
  }
  
  async deleteConfig(id: string): Promise<void> {
    const config = this.configs.get(id);
    if (!config) {
      throw new Error(`Config ${id} not found`);
    }
    
    this.configs.delete(id);
    
    await this.eventBus.emit('config.deleted', { configId: id });
  }
  
  async checkServiceHealth(serviceId: string): Promise<ServiceHealth> {
    return await this.containerOrchestrator.checkServiceHealth(serviceId);
  }
  
  async getServiceMetrics(serviceId: string, query?: MetricsQuery): Promise<ServiceMetrics> {
    return await this.monitoringService.getServiceMetrics(serviceId, query);
  }
}

14.2 核心数据结构

// 环境定义
interface EnvironmentDefinition {
  name: string;
  type: EnvironmentType;
  description?: string;
  config: EnvironmentConfig;
  resources?: ResourceRequirements;
  networking?: NetworkingConfig;
  security?: SecurityConfig;
}

// 环境
interface Environment extends EnvironmentDefinition {
  id: string;
  status: EnvironmentStatus;
  error?: string;
  createdAt: Date;
  updatedAt: Date;
}

// 环境类型
enum EnvironmentType {
  DEVELOPMENT = 'development',
  STAGING = 'staging',
  PRODUCTION = 'production',
  TESTING = 'testing'
}

// 环境状态
enum EnvironmentStatus {
  CREATING = 'creating',
  ACTIVE = 'active',
  UPDATING = 'updating',
  DELETING = 'deleting',
  FAILED = 'failed'
}

// 环境配置
interface EnvironmentConfig {
  autoScale?: boolean;
  monitoring?: boolean;
  logging?: boolean;
  backup?: boolean;
  [key: string]: any;
}

// 部署定义
interface DeploymentDefinition {
  name: string;
  applicationId: string;
  environmentId: string;
  version: string;
  config: DeploymentConfig;
  resources?: DeploymentResources;
}

// 部署
interface Deployment extends DeploymentDefinition {
  id: string;
  services: ServiceDefinition[];
  status: DeploymentStatus;
  previousVersion?: string;
  error?: string;
  createdAt: Date;
  updatedAt: Date;
}

// 部署状态
enum DeploymentStatus {
  DEPLOYING = 'deploying',
  RUNNING = 'running',
  STOPPED = 'stopped',
  FAILED = 'failed',
  UPDATING = 'updating'
}

// 部署配置
interface DeploymentConfig {
  frontend?: ServiceConfig;
  backend?: ServiceConfig;
  database?: ServiceConfig;
  [key: string]: ServiceConfig | undefined;
}

// 服务配置
interface ServiceConfig {
  image?: string;
  env?: Record<string, string>;
  volumes?: VolumeMount[];
  replicas?: number;
}

// 服务定义
interface ServiceDefinition {
  id: string;
  name: string;
  type: ServiceType;
  image: string;
  ports?: PortMapping[];
  env?: Record<string, string>;
  volumes?: VolumeMount[];
  resources?: ResourceRequirements;
  replicas?: number;
}

// 服务类型
enum ServiceType {
  FRONTEND = 'frontend',
  BACKEND = 'backend',
  DATABASE = 'database',
  CACHE = 'cache',
  QUEUE = 'queue'
}

// 端口映射
interface PortMapping {
  containerPort: number;
  servicePort: number;
  protocol?: 'TCP' | 'UDP';
}

// 卷挂载
interface VolumeMount {
  name: string;
  mountPath: string;
  type: VolumeType;
  size?: string;
}

// 卷类型
enum VolumeType {
  PERSISTENT = 'persistent',
  CONFIGMAP = 'configmap',
  SECRET = 'secret',
  EMPTYDIR = 'emptydir'
}

// 资源需求
interface ResourceRequirements {
  requests?: ResourceSpec;
  limits?: ResourceSpec;
}

// 资源规格
interface ResourceSpec {
  cpu?: string;
  memory?: string;
  storage?: string;
}

// 部署资源
interface DeploymentResources {
  frontend?: ResourceRequirements;
  backend?: ResourceRequirements;
  database?: ResourceRequirements;
  [key: string]: ResourceRequirements | undefined;
}

// 网络配置
interface NetworkingConfig {
  ingress?: IngressConfig;
  loadBalancer?: LoadBalancerConfig;
  dns?: DNSConfig;
}

// Ingress配置
interface IngressConfig {
  enabled: boolean;
  host?: string;
  tls?: boolean;
  annotations?: Record<string, string>;
}

// 负载均衡配置
interface LoadBalancerConfig {
  type: 'ClusterIP' | 'NodePort' | 'LoadBalancer';
  annotations?: Record<string, string>;
}

// DNS配置
interface DNSConfig {
  domain?: string;
  subdomain?: string;
}

// 安全配置
interface SecurityConfig {
  rbac?: RBACConfig;
  networkPolicy?: NetworkPolicyConfig;
  podSecurityPolicy?: PodSecurityPolicyConfig;
}

// RBAC配置
interface RBACConfig {
  enabled: boolean;
  serviceAccount?: string;
  roles?: string[];
}

// 网络策略配置
interface NetworkPolicyConfig {
  enabled: boolean;
  ingress?: NetworkRule[];
  egress?: NetworkRule[];
}

// 网络规则
interface NetworkRule {
  from?: NetworkSelector[];
  to?: NetworkSelector[];
  ports?: NetworkPort[];
}

// 网络选择器
interface NetworkSelector {
  namespaceSelector?: Record<string, string>;
  podSelector?: Record<string, string>;
}

// 网络端口
interface NetworkPort {
  port: number;
  protocol?: 'TCP' | 'UDP';
}

// Pod安全策略配置
interface PodSecurityPolicyConfig {
  enabled: boolean;
  runAsNonRoot?: boolean;
  readOnlyRootFilesystem?: boolean;
  allowPrivilegeEscalation?: boolean;
}

// 配置定义
interface ConfigDefinition {
  name: string;
  type: ConfigType;
  data: Record<string, any>;
  environmentId?: string;
}

// 配置
interface Config extends ConfigDefinition {
  id: string;
  createdAt: Date;
  updatedAt: Date;
}

// 配置类型
enum ConfigType {
  CONFIGMAP = 'configmap',
  SECRET = 'secret',
  ENVIRONMENT = 'environment'
}

// 服务状态
interface ServiceStatus {
  name: string;
  status: 'running' | 'stopped' | 'failed' | 'pending';
  replicas: {
    desired: number;
    ready: number;
    available: number;
  };
  conditions: ServiceCondition[];
}

// 服务条件
interface ServiceCondition {
  type: string;
  status: 'True' | 'False' | 'Unknown';
  reason?: string;
  message?: string;
  lastTransitionTime: Date;
}

// 服务健康状态
interface ServiceHealth {
  name: string;
  status: 'healthy' | 'unhealthy' | 'unknown';
  checks: HealthCheck[];
  lastCheck: Date;
}

// 健康检查
interface HealthCheck {
  name: string;
  status: 'pass' | 'fail' | 'warn';
  output?: string;
  duration?: number;
}

// 健康状态
interface HealthStatus {
  status: 'healthy' | 'unhealthy' | 'degraded';
  environments: {
    total: number;
    healthy: number;
    unhealthy: number;
  };
  deployments: {
    total: number;
    running: number;
    failed: number;
  };
  timestamp: Date;
}

// 部署指标
interface DeploymentMetrics {
  deployments: {
    total: number;
    running: number;
    failed: number;
    deploying: number;
  };
  resources: ResourceMetrics;
  performance: PerformanceMetrics;
  timestamp: Date;
}

// 资源指标
interface ResourceMetrics {
  cpu: {
    used: number;
    total: number;
    percentage: number;
  };
  memory: {
    used: number;
    total: number;
    percentage: number;
  };
  storage: {
    used: number;
    total: number;
    percentage: number;
  };
}

// 性能指标
interface PerformanceMetrics {
  requests: {
    total: number;
    rate: number;
  };
  latency: {
    average: number;
    p95: number;
    p99: number;
  };
  errors: {
    total: number;
    rate: number;
  };
}

// 服务指标
interface ServiceMetrics {
  name: string;
  cpu: number;
  memory: number;
  network: {
    in: number;
    out: number;
  };
  requests: number;
  errors: number;
  latency: number;
}

// 查询接口
interface EnvironmentQuery {
  type?: EnvironmentType;
  status?: EnvironmentStatus;
}

interface DeploymentQuery {
  environmentId?: string;
  status?: DeploymentStatus;
  applicationId?: string;
}

interface ConfigQuery {
  environmentId?: string;
  type?: ConfigType;
}

interface MetricsQuery {
  startTime?: Date;
  endTime?: Date;
  interval?: string;
}

// 部署管理器选项
interface DeploymentManagerOptions {
  containerOrchestrator?: ContainerOrchestrator;
  configManager?: ConfigurationManager;
  monitoringService?: MonitoringService;
  eventBus?: EventBus;
}

14.3 容器编排器

// 容器编排器接口
interface ContainerOrchestrator {
  // 命名空间管理
  createNamespace(name: string): Promise<void>;
  deleteNamespace(name: string): Promise<void>;
  
  // 服务部署
  deployService(config: ServiceDeploymentConfig): Promise<void>;
  deleteService(name: string, namespace: string): Promise<void>;
  scaleService(name: string, replicas: number): Promise<void>;
  restartService(name: string): Promise<void>;
  
  // 服务管理
  createService(config: ServiceConfig): Promise<void>;
  getServiceStatus(name: string): Promise<ServiceStatus>;
  checkServiceHealth(name: string): Promise<ServiceHealth>;
  
  // 配置管理
  createConfigMap(name: string, namespace: string, data: Record<string, string>): Promise<void>;
  createSecret(name: string, namespace: string, data: Record<string, string>): Promise<void>;
}

// Docker编排器实现
class DockerOrchestrator implements ContainerOrchestrator {
  private docker: Docker;
  private networks: Map<string, string> = new Map();
  
  constructor() {
    this.docker = new Docker();
  }
  
  async createNamespace(name: string): Promise<void> {
    // 在Docker中,我们使用网络来模拟命名空间
    try {
      const network = await this.docker.createNetwork({
        Name: name,
        Driver: 'bridge',
        Labels: {
          'lowcode.namespace': name
        }
      });
      
      this.networks.set(name, network.id);
    } catch (error) {
      if (!error.message.includes('already exists')) {
        throw error;
      }
    }
  }
  
  async deleteNamespace(name: string): Promise<void> {
    const networkId = this.networks.get(name);
    if (networkId) {
      const network = this.docker.getNetwork(networkId);
      await network.remove();
      this.networks.delete(name);
    }
  }
  
  async deployService(config: ServiceDeploymentConfig): Promise<void> {
    const containerConfig = {
      Image: config.image,
      name: config.name,
      Env: Object.entries(config.env || {}).map(([key, value]) => `${key}=${value}`),
      ExposedPorts: this.buildExposedPorts(config.ports),
      HostConfig: {
        PortBindings: this.buildPortBindings(config.ports),
        NetworkMode: config.namespace,
        RestartPolicy: { Name: 'unless-stopped' },
        Memory: this.parseMemory(config.resources?.limits?.memory),
        CpuShares: this.parseCpu(config.resources?.limits?.cpu)
      },
      Labels: {
        'lowcode.service': config.name,
        'lowcode.namespace': config.namespace
      }
    };
    
    // 创建并启动容器
    const container = await this.docker.createContainer(containerConfig);
    await container.start();
    
    // 如果需要多个副本
    for (let i = 1; i < (config.replicas || 1); i++) {
      const replicaConfig = {
        ...containerConfig,
        name: `${config.name}-${i + 1}`
      };
      
      const replica = await this.docker.createContainer(replicaConfig);
      await replica.start();
    }
  }
  
  async deleteService(name: string, namespace: string): Promise<void> {
    const containers = await this.docker.listContainers({
      all: true,
      filters: {
        label: [`lowcode.service=${name}`, `lowcode.namespace=${namespace}`]
      }
    });
    
    for (const containerInfo of containers) {
      const container = this.docker.getContainer(containerInfo.Id);
      await container.stop();
      await container.remove();
    }
  }
  
  async scaleService(name: string, replicas: number): Promise<void> {
    // 获取当前容器
    const containers = await this.docker.listContainers({
      all: true,
      filters: {
        label: [`lowcode.service=${name}`]
      }
    });
    
    const currentReplicas = containers.length;
    
    if (replicas > currentReplicas) {
      // 扩容
      const baseContainer = containers[0];
      if (baseContainer) {
        const containerInfo = await this.docker.getContainer(baseContainer.Id).inspect();
        
        for (let i = currentReplicas; i < replicas; i++) {
          const newConfig = {
            ...containerInfo.Config,
            name: `${name}-${i + 1}`
          };
          
          const container = await this.docker.createContainer(newConfig);
          await container.start();
        }
      }
    } else if (replicas < currentReplicas) {
      // 缩容
      const containersToRemove = containers.slice(replicas);
      
      for (const containerInfo of containersToRemove) {
        const container = this.docker.getContainer(containerInfo.Id);
        await container.stop();
        await container.remove();
      }
    }
  }
  
  async restartService(name: string): Promise<void> {
    const containers = await this.docker.listContainers({
      filters: {
        label: [`lowcode.service=${name}`]
      }
    });
    
    for (const containerInfo of containers) {
      const container = this.docker.getContainer(containerInfo.Id);
      await container.restart();
    }
  }
  
  async createService(config: ServiceConfig): Promise<void> {
    // Docker中的服务发现通过网络实现
    // 这里可以创建负载均衡器或代理
  }
  
  async getServiceStatus(name: string): Promise<ServiceStatus> {
    const containers = await this.docker.listContainers({
      all: true,
      filters: {
        label: [`lowcode.service=${name}`]
      }
    });
    
    const runningContainers = containers.filter(c => c.State === 'running');
    
    return {
      name,
      status: runningContainers.length > 0 ? 'running' : 'stopped',
      replicas: {
        desired: containers.length,
        ready: runningContainers.length,
        available: runningContainers.length
      },
      conditions: []
    };
  }
  
  async checkServiceHealth(name: string): Promise<ServiceHealth> {
    const containers = await this.docker.listContainers({
      filters: {
        label: [`lowcode.service=${name}`]
      }
    });
    
    const checks: HealthCheck[] = [];
    let overallStatus: 'healthy' | 'unhealthy' | 'unknown' = 'healthy';
    
    for (const containerInfo of containers) {
      const container = this.docker.getContainer(containerInfo.Id);
      const inspect = await container.inspect();
      
      const health = inspect.State.Health;
      if (health) {
        checks.push({
          name: `container-${containerInfo.Id.substring(0, 12)}`,
          status: health.Status === 'healthy' ? 'pass' : 'fail',
          output: health.Log?.[0]?.Output
        });
        
        if (health.Status !== 'healthy') {
          overallStatus = 'unhealthy';
        }
      } else {
        checks.push({
          name: `container-${containerInfo.Id.substring(0, 12)}`,
          status: inspect.State.Running ? 'pass' : 'fail'
        });
        
        if (!inspect.State.Running) {
          overallStatus = 'unhealthy';
        }
      }
    }
    
    return {
      name,
      status: overallStatus,
      checks,
      lastCheck: new Date()
    };
  }
  
  async createConfigMap(name: string, namespace: string, data: Record<string, string>): Promise<void> {
    // Docker中可以通过卷或环境变量实现配置
    // 这里可以创建配置文件或环境变量
  }
  
  async createSecret(name: string, namespace: string, data: Record<string, string>): Promise<void> {
    // Docker中可以通过Docker secrets实现
    // 这里可以创建加密的配置
  }
  
  // 辅助方法
  private buildExposedPorts(ports?: PortMapping[]): Record<string, {}> {
    const exposedPorts: Record<string, {}> = {};
    
    if (ports) {
      for (const port of ports) {
        exposedPorts[`${port.containerPort}/${port.protocol || 'tcp'}`] = {};
      }
    }
    
    return exposedPorts;
  }
  
  private buildPortBindings(ports?: PortMapping[]): Record<string, Array<{ HostPort: string }>> {
    const portBindings: Record<string, Array<{ HostPort: string }>> = {};
    
    if (ports) {
      for (const port of ports) {
        const key = `${port.containerPort}/${port.protocol || 'tcp'}`;
        portBindings[key] = [{ HostPort: port.servicePort.toString() }];
      }
    }
    
    return portBindings;
  }
  
  private parseMemory(memory?: string): number | undefined {
    if (!memory) return undefined;
    
    const match = memory.match(/^(\d+)(\w+)?$/);
    if (!match) return undefined;
    
    const value = parseInt(match[1]);
    const unit = match[2]?.toLowerCase() || 'b';
    
    const multipliers: Record<string, number> = {
      'b': 1,
      'k': 1024,
      'kb': 1024,
      'm': 1024 * 1024,
      'mb': 1024 * 1024,
      'g': 1024 * 1024 * 1024,
      'gb': 1024 * 1024 * 1024,
      'gi': 1024 * 1024 * 1024,
      'mi': 1024 * 1024
    };
    
    return value * (multipliers[unit] || 1);
  }
  
  private parseCpu(cpu?: string): number | undefined {
    if (!cpu) return undefined;
    
    if (cpu.endsWith('m')) {
      return parseInt(cpu.slice(0, -1));
    }
    
    return parseInt(cpu) * 1000;
  }
}

// Kubernetes编排器实现
class KubernetesOrchestrator implements ContainerOrchestrator {
  private k8sApi: k8s.CoreV1Api;
  private appsApi: k8s.AppsV1Api;
  
  constructor() {
    const kc = new k8s.KubeConfig();
    kc.loadFromDefault();
    
    this.k8sApi = kc.makeApiClient(k8s.CoreV1Api);
    this.appsApi = kc.makeApiClient(k8s.AppsV1Api);
  }
  
  async createNamespace(name: string): Promise<void> {
    try {
      await this.k8sApi.createNamespace({
        metadata: {
          name,
          labels: {
            'lowcode.namespace': name
          }
        }
      });
    } catch (error) {
      if (error.response?.statusCode !== 409) {
        throw error;
      }
    }
  }
  
  async deleteNamespace(name: string): Promise<void> {
    try {
      await this.k8sApi.deleteNamespace(name);
    } catch (error) {
      if (error.response?.statusCode !== 404) {
        throw error;
      }
    }
  }
  
  async deployService(config: ServiceDeploymentConfig): Promise<void> {
    const deployment = {
      metadata: {
        name: config.name,
        namespace: config.namespace,
        labels: {
          app: config.name,
          'lowcode.service': config.name
        }
      },
      spec: {
        replicas: config.replicas || 1,
        selector: {
          matchLabels: {
            app: config.name
          }
        },
        template: {
          metadata: {
            labels: {
              app: config.name
            }
          },
          spec: {
            containers: [
              {
                name: config.name,
                image: config.image,
                ports: config.ports?.map(p => ({
                  containerPort: p.containerPort,
                  protocol: p.protocol || 'TCP'
                })),
                env: Object.entries(config.env || {}).map(([name, value]) => ({
                  name,
                  value
                })),
                resources: config.resources
              }
            ]
          }
        }
      }
    };
    
    await this.appsApi.createNamespacedDeployment(config.namespace, deployment);
  }
  
  async deleteService(name: string, namespace: string): Promise<void> {
    try {
      await this.appsApi.deleteNamespacedDeployment(name, namespace);
    } catch (error) {
      if (error.response?.statusCode !== 404) {
        throw error;
      }
    }
  }
  
  async scaleService(name: string, replicas: number): Promise<void> {
    // 实现Kubernetes服务扩缩容
    const patch = {
      spec: {
        replicas
      }
    };
    
    await this.appsApi.patchNamespacedDeploymentScale(
      name,
      'default', // 需要传入正确的namespace
      patch,
      undefined,
      undefined,
      undefined,
      undefined,
      {
        headers: {
          'Content-Type': 'application/merge-patch+json'
        }
      }
    );
  }
  
  async restartService(name: string): Promise<void> {
    // 通过更新deployment的annotation来触发重启
    const patch = {
      spec: {
        template: {
          metadata: {
            annotations: {
              'kubectl.kubernetes.io/restartedAt': new Date().toISOString()
            }
          }
        }
      }
    };
    
    await this.appsApi.patchNamespacedDeployment(
      name,
      'default',
      patch,
      undefined,
      undefined,
      undefined,
      undefined,
      {
        headers: {
          'Content-Type': 'application/merge-patch+json'
        }
      }
    );
  }
  
  async createService(config: ServiceConfig): Promise<void> {
    const service = {
      metadata: {
        name: config.name,
        namespace: config.namespace
      },
      spec: {
        selector: config.selector,
        ports: config.ports?.map(p => ({
          port: p.servicePort,
          targetPort: p.containerPort,
          protocol: p.protocol || 'TCP'
        })),
        type: 'ClusterIP'
      }
    };
    
    await this.k8sApi.createNamespacedService(config.namespace, service);
  }
  
  async getServiceStatus(name: string): Promise<ServiceStatus> {
    try {
      const deployment = await this.appsApi.readNamespacedDeployment(name, 'default');
      const status = deployment.body.status;
      
      return {
        name,
        status: status?.readyReplicas === status?.replicas ? 'running' : 'pending',
        replicas: {
          desired: status?.replicas || 0,
          ready: status?.readyReplicas || 0,
          available: status?.availableReplicas || 0
        },
        conditions: status?.conditions?.map(c => ({
          type: c.type,
          status: c.status as 'True' | 'False' | 'Unknown',
          reason: c.reason,
          message: c.message,
          lastTransitionTime: new Date(c.lastTransitionTime)
        })) || []
      };
    } catch (error) {
      throw new Error(`Failed to get service status: ${error.message}`);
    }
  }
  
  async checkServiceHealth(name: string): Promise<ServiceHealth> {
    // 实现Kubernetes健康检查
    const pods = await this.k8sApi.listNamespacedPod(
      'default',
      undefined,
      undefined,
      undefined,
      undefined,
      `app=${name}`
    );
    
    const checks: HealthCheck[] = [];
    let overallStatus: 'healthy' | 'unhealthy' | 'unknown' = 'healthy';
    
    for (const pod of pods.body.items) {
      const podStatus = pod.status?.phase === 'Running' ? 'pass' : 'fail';
      
      checks.push({
        name: pod.metadata?.name || 'unknown',
        status: podStatus,
        output: pod.status?.message
      });
      
      if (podStatus === 'fail') {
        overallStatus = 'unhealthy';
      }
    }
    
    return {
      name,
      status: overallStatus,
      checks,
      lastCheck: new Date()
    };
  }
  
  async createConfigMap(name: string, namespace: string, data: Record<string, string>): Promise<void> {
    const configMap = {
      metadata: {
        name,
        namespace
      },
      data
    };
    
    await this.k8sApi.createNamespacedConfigMap(namespace, configMap);
  }
  
  async createSecret(name: string, namespace: string, data: Record<string, string>): Promise<void> {
    const secret = {
      metadata: {
        name,
        namespace
      },
      type: 'Opaque',
      stringData: data
    };
    
    await this.k8sApi.createNamespacedSecret(namespace, secret);
  }
}

// 服务部署配置
interface ServiceDeploymentConfig {
  name: string;
  namespace: string;
  image: string;
  replicas?: number;
  ports?: PortMapping[];
  env?: Record<string, string>;
  resources?: ResourceRequirements;
  volumes?: VolumeMount[];
}

// 服务配置(用于Kubernetes Service)
interface ServiceConfig {
  name: string;
  namespace: string;
  selector: Record<string, string>;
  ports?: PortMapping[];
}