All files / src repository.js

48.8% Statements 61/125
54.83% Branches 34/62
58.97% Functions 23/39
50.43% Lines 58/115

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335                        1x 1x                   1x                   22x 22x 22x 22x 22x 22x 22x 22x 22x 22x       12x       1x 1x       10x                   10x       16x 16x       5x       6x 6x 3x   6x 2x   6x       1x 1x 1x 1x 1x       1x       1x                                                                                                                                                                                                                           5x   5x 5x               5x                 10x 10x 10x 10x                 1x 1x                 4x 9x                                                                     1x                 1x 1x 2x                 1x 1x 1x   1x 3x 3x     1x                 1x              
/**
 * Repository - 仓库模式实现
 * 
 * 封装分析历史的持久化存储逻辑
 * 灵感来源:meta-skill-weaver 状态持久化系统
 * 
 * 设计模式:仓库模式 (Repository Pattern)
 * - 抽象数据存储细节
 * - 提供统一的 CRUD 接口
 * - 支持多种存储后端(内存/文件/数据库)
 */
 
const fs = require('fs');
const path = require('path');
 
// ============================================================
// 存储接口定义
// ============================================================
 
/**
 * 存储后端接口
 * 定义仓库可使用的存储方式
 */
const STORAGE_BACKENDS = {
  MEMORY: 'memory',    // 内存存储(临时)
  FILE: 'file'         // 文件存储(持久)
};
 
/**
 * 分析记录结构
 */
class AnalysisRecord {
  constructor(data) {
    this.id = data.id || this._generateId();
    this.problem = data.problem;
    this.problemType = data.problemType;
    this.status = data.status || 'idle';
    this.result = data.result || null;
    this.report = data.report || null;
    this.stateSnapshot = data.stateSnapshot || null;
    this.createdAt = data.createdAt || new Date().toISOString();
    this.updatedAt = data.updatedAt || new Date().toISOString();
    this.metadata = data.metadata || {};
  }
  
  _generateId() {
    return `analysis_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
  }
  
  update(data) {
    Object.assign(this, data, { updatedAt: new Date().toISOString() });
    return this;
  }
  
  toJSON() {
    return { ...this };
  }
}
 
// ============================================================
// 内存存储后端
// ============================================================
 
class MemoryStorage {
  constructor() {
    this.store = new Map();
  }
  
  save(id, data) {
    this.store.set(id, data);
    return true;
  }
  
  findById(id) {
    return this.store.get(id) || null;
  }
  
  findAll(query = {}) {
    let records = Array.from(this.store.values());
    if (query.status) {
      records = records.filter(r => r.status === query.status);
    }
    if (query.problemType) {
      records = records.filter(r => r.problemType === query.problemType);
    }
    return records;
  }
  
  update(id, data) {
    Iif (!this.store.has(id)) return false;
    const record = this.store.get(id);
    Object.assign(record, data, { updatedAt: new Date().toISOString() });
    this.store.set(id, record);
    return true;
  }
  
  delete(id) {
    return this.store.delete(id);
  }
  
  count() {
    return this.store.size;
  }
}
 
// ============================================================
// 文件存储后端
// ============================================================
 
class FileStorage {
  constructor(basePath) {
    this.basePath = basePath;
    this._ensureDirectory();
  }
  
  _ensureDirectory() {
    if (!fs.existsSync(this.basePath)) {
      fs.mkdirSync(this.basePath, { recursive: true });
    }
  }
  
  _filePath(id) {
    return path.join(this.basePath, `${id}.json`);
  }
  
  save(id, data) {
    try {
      const filePath = this._filePath(id);
      fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
      return true;
    } catch (error) {
      console.error('[FileStorage] 保存失败:', error);
      return false;
    }
  }
  
  findById(id) {
    try {
      const filePath = this._filePath(id);
      if (!fs.existsSync(filePath)) return null;
      const content = fs.readFileSync(filePath, 'utf8');
      return JSON.parse(content);
    } catch (error) {
      console.error('[FileStorage] 读取失败:', error);
      return null;
    }
  }
  
  findAll(query = {}) {
    try {
      const files = fs.readdirSync(this.basePath).filter(f => f.endsWith('.json'));
      let records = files.map(f => {
        const content = fs.readFileSync(path.join(this.basePath, f), 'utf8');
        return JSON.parse(content);
      });
      
      if (query.status) {
        records = records.filter(r => r.status === query.status);
      }
      if (query.problemType) {
        records = records.filter(r => r.problemType === query.problemType);
      }
      
      return records;
    } catch (error) {
      console.error('[FileStorage] 查询失败:', error);
      return [];
    }
  }
  
  update(id, data) {
    const existing = this.findById(id);
    if (!existing) return false;
    return this.save(id, { ...existing, ...data, updatedAt: new Date().toISOString() });
  }
  
  delete(id) {
    try {
      const filePath = this._filePath(id);
      if (fs.existsSync(filePath)) {
        fs.unlinkSync(filePath);
        return true;
      }
      return false;
    } catch (error) {
      console.error('[FileStorage] 删除失败:', error);
      return false;
    }
  }
  
  count() {
    try {
      const files = fs.readdirSync(this.basePath).filter(f => f.endsWith('.json'));
      return files.length;
    } catch (error) {
      return 0;
    }
  }
}
 
// ============================================================
// 仓库类
// ============================================================
 
class AnalysisRepository {
  /**
   * 创建仓库
   * @param {string} backend - 存储后端类型 ('memory' | 'file')
   * @param {Object} options - 配置选项
   */
  constructor(backend = STORAGE_BACKENDS.MEMORY, options = {}) {
    this.backend = backend;
    
    if (backend === STORAGE_BACKENDS.MEMORY) {
      this.storage = new MemoryStorage();
    } else Eif (backend === STORAGE_BACKENDS.FILE) {
      const basePath = options.basePath || path.join(__dirname, '../data/analyses');
      this.storage = new FileStorage(basePath);
    } else {
      throw new Error(`未知的存储后端:${backend}`);
    }
    
    console.log(`[Repository] 初始化仓库,后端:${backend}`);
  }
  
  /**
   * 创建分析记录
   * @param {Object} data - 分析数据
   * @returns {AnalysisRecord} 创建的分析记录
   */
  create(data) {
    const record = new AnalysisRecord(data);
    this.storage.save(record.id, record.toJSON());
    console.log(`[Repository] 创建分析记录:${record.id}`);
    return record;
  }
  
  /**
   * 查找分析记录
   * @param {string} id - 分析 ID
   * @returns {AnalysisRecord|null} 分析记录
   */
  findById(id) {
    const data = this.storage.findById(id);
    return data ? new AnalysisRecord(data) : null;
  }
  
  /**
   * 查询分析记录
   * @param {Object} query - 查询条件
   * @returns {Array<AnalysisRecord>} 分析记录列表
   */
  findAll(query = {}) {
    const records = this.storage.findAll(query);
    return records.map(r => new AnalysisRecord(r));
  }
  
  /**
   * 更新分析记录
   * @param {string} id - 分析 ID
   * @param {Object} data - 更新数据
   * @returns {boolean} 是否成功
   */
  update(id, data) {
    const success = this.storage.update(id, data);
    if (success) {
      console.log(`[Repository] 更新分析记录:${id}`);
    }
    return success;
  }
  
  /**
   * 删除分析记录
   * @param {string} id - 分析 ID
   * @returns {boolean} 是否成功
   */
  delete(id) {
    const success = this.storage.delete(id);
    if (success) {
      console.log(`[Repository] 删除分析记录:${id}`);
    }
    return success;
  }
  
  /**
   * 获取分析记录总数
   * @returns {number} 记录总数
   */
  count() {
    return this.storage.count();
  }
  
  /**
   * 获取最近的分析记录
   * @param {number} limit - 数量限制
   * @returns {Array<AnalysisRecord>} 分析记录列表
   */
  findRecent(limit = 10) {
    const all = this.findAll();
    return all
      .sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
      .slice(0, limit);
  }
  
  /**
   * 获取统计信息
   * @returns {Object} 统计信息
   */
  getStats() {
    const all = this.findAll();
    const byStatus = {};
    const byProblemType = {};
    
    all.forEach(r => {
      byStatus[r.status] = (byStatus[r.status] || 0) + 1;
      byProblemType[r.problemType] = (byProblemType[r.problemType] || 0) + 1;
    });
    
    return {
      total: all.length,
      byStatus,
      byProblemType,
      completedRate: all.length > 0 ? (byStatus['completed'] || 0) / all.length : 0
    };
  }
}
 
module.exports = {
  STORAGE_BACKENDS,
  AnalysisRecord,
  MemoryStorage,
  FileStorage,
  AnalysisRepository
};