Files
makemd/server/src/services/TraceService.ts
wurenzhi 136c2fa579 feat: 初始化项目结构并添加核心功能模块
- 新增文档模板和导航结构
- 实现服务器基础API路由和控制器
- 添加扩展插件配置和前端框架
- 引入多租户和权限管理模块
- 集成日志和数据库配置
- 添加核心业务模型和类型定义
2026-03-17 22:07:19 +08:00

86 lines
2.8 KiB
TypeScript

import db from '../config/database';
import { logger } from '../utils/logger';
/**
* [CORE_LOG_02] 全局操作流水线追踪后端 (Trace Tracking Backend)
* @description 提供跨模块的 Pipeline 与 Audit 日志聚合查询
*/
export class TraceService {
private static readonly INSTANCE_TABLE = 'cf_pipeline_instances';
private static readonly STEP_TABLE = 'cf_pipeline_steps';
private static readonly AUDIT_TABLE = 'cf_audit_log'; // 假设异步处理后的表名
/**
* 根据 Trace ID 获取关联的流水线实例
*/
static async getPipelineByTraceId(traceId: string, tenantId: string) {
try {
const instance = await db(this.INSTANCE_TABLE)
.where({ trace_id: traceId, tenant_id: tenantId })
.first();
if (!instance) return null;
const steps = await db(this.STEP_TABLE)
.where({ instance_id: instance.id })
.orderBy('created_at', 'asc');
return {
...instance,
steps: steps.map(s => ({
...s,
config: s.config ? JSON.parse(s.config) : null,
output: s.output ? JSON.parse(s.output) : null,
error: s.error ? JSON.parse(s.error) : null
}))
};
} catch (err: any) {
logger.error(`[TraceService] Failed to get pipeline by trace: ${err.message}`);
throw err;
}
}
/**
* 获取租户最近的自治活动流
*/
static async getRecentActivities(tenantId: string, limit = 20) {
try {
return await db(this.INSTANCE_TABLE)
.where({ tenant_id: tenantId })
.orderBy('created_at', 'desc')
.limit(limit);
} catch (err: any) {
logger.error(`[TraceService] Failed to get recent activities: ${err.message}`);
throw err;
}
}
/**
* [CORE_TELE_01] 获取 AI 自愈遥测数据
* @description 筛选出包含自愈行为 (Self-Healing) 的步骤
*/
static async getSelfHealingTelemetry(tenantId: string, limit = 50) {
try {
// 假设自愈日志记录在 metadata 或特定的自愈字段中
// 这里从步骤表中筛选出包含 retry 或 healing 标记的记录
return await db(this.STEP_TABLE)
.join(this.INSTANCE_TABLE, `${this.STEP_TABLE}.instance_id`, `${this.INSTANCE_TABLE}.id`)
.where(`${this.INSTANCE_TABLE}.tenant_id`, tenantId)
.whereNotNull(`${this.STEP_TABLE}.error`)
.orderBy(`${this.STEP_TABLE}.updated_at`, 'desc')
.limit(limit)
.select(
`${this.STEP_TABLE}.id`,
`${this.STEP_TABLE}.instance_id`,
`${this.STEP_TABLE}.type`,
`${this.STEP_TABLE}.status`,
`${this.STEP_TABLE}.error`,
`${this.STEP_TABLE}.updated_at as timestamp`
);
} catch (err: any) {
logger.error(`[TraceService] Failed to get telemetry: ${err.message}`);
throw err;
}
}
}