All files feedback-middleware.js

91.73% Statements 111/121
86.81% Branches 79/91
93.54% Functions 29/31
93.39% Lines 99/106

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                      1x 1x       1x                                                   32x 32x 32x           32x 32x 31x   32x 32x 1x 1x               56x 56x                   1x     1x 1x 1x   1x                                   59x 59x 3x       56x 56x 1x       55x 54x     55x                     55x     55x 1x     55x     55x 1x     55x           54x   54x 4x   50x 1x     49x 15x                     7x 6x     7x 1x                     6x 30x 15x     6x 6x 15x       6x 6x 15x   6x 2x   3x     6x 15x     1x             15x   6x                               1x 1x 3x 2x   3x 3x     1x 2x         1x                 6x 13x 13x 11x 10x 7x 7x 6x 6x                   2x   2x 4x   4x   4x         4x     4x 2x   4x 4x 4x 2x       2x 2x                         1x 1x       1x  
/**
 * FeedbackMiddleware - 用户反馈收集机制
 *
 * v2.4.0 新增:
 * - 任务完成后收集用户评分(1-5星)和文字反馈
 * - 简单的反馈存储和统计接口
 * - 反馈趋势分析和常见问题识别
 *
 * @module middleware/feedback-middleware
 */
 
const fs = require('fs');
const path = require('path');
 
// ─── 默认配置 ────────────────────────────────────────────
 
const DEFAULT_CONFIG = {
  storagePath: path.join(__dirname, '../../.feedback'),
  maxFeedbackPerTask: 10,       // 每个任务最大反馈数
  maxTotalFeedback: 1000,       // 总反馈上限(防止无限增长)
  ratingScale: { min: 1, max: 5 },
  onFeedbackReceived: null,     // 回调
};
 
// ─── 反馈数据模型 ─────────────────────────────────────────
 
/**
 * @typedef {Object} Feedback
 * @property {string} id           - 唯一 ID
 * @property {string} taskId       - 关联任务 ID
 * @property {string} skillName    - 被评价的技能
 * @property {number} rating       - 评分 (1-5)
 * @property {string} comment      - 文字反馈
 * @property {string[]} tags       - 反馈标签
 * @property {string} timestamp    - ISO 时间戳
 * @property {string} category     - 分类: praise|issue|suggestion|bug
 */
 
// ─── FeedbackMiddleware 类 ───────────────────────────────
 
class FeedbackMiddleware {
  constructor(config = {}) {
    this.config = { ...DEFAULT_CONFIG, ...config };
    this.feedbacks = [];
    this._initStorage();
  }
 
  // ── 初始化 ──────────────────────────────────────────────
 
  _initStorage() {
    const dir = this.config.storagePath;
    if (!fs.existsSync(dir)) {
      fs.mkdirSync(dir, { recursive: true });
    }
    const file = path.join(dir, 'feedbacks.json');
    if (fs.existsSync(file)) {
      try {
        this.feedbacks = JSON.parse(fs.readFileSync(file, 'utf-8'));
      } catch (e) {
        this.feedbacks = [];
      }
    }
  }
 
  save() {
    const file = path.join(this.config.storagePath, 'feedbacks.json');
    fs.writeFileSync(file, JSON.stringify(this.feedbacks, null, 2));
  }
 
  // ── 中间件接口:execute ─────────────────────────────────
 
  /**
   * Middleware 标准接口
   * 在任务完成后附加反馈收集接口
   */
  async execute(context, next) {
    const result = await next(context);
 
    // 标记任务已完成,准备收集反馈
    context._feedbackReady = true;
    context._feedbackSkill = context.skillName || 'unknown';
    context._feedbackTaskId = context.taskId || 'unknown';
 
    return result;
  }
 
  // ── 核心:提交反馈 ──────────────────────────────────────
 
  /**
   * 提交用户反馈
   * @param {Object} params
   * @param {string} params.taskId    - 任务 ID
   * @param {string} [params.skillName] - 技能名称
   * @param {number} params.rating    - 评分 (1-5)
   * @param {string} [params.comment]  - 文字反馈
   * @param {string[]} [params.tags]  - 标签
   * @param {string} [params.category] - 分类
   * @returns {Object} 创建的反馈记录
   */
  submit({ taskId, skillName = 'unknown', rating, comment = '', tags = [], category }) {
    // 验证评分
    const { min, max } = this.config.ratingScale;
    if (typeof rating !== 'number' || rating < min || rating > max) {
      throw new Error(`Rating must be between ${min} and ${max}, got ${rating}`);
    }
 
    // 检查任务反馈数量上限
    const taskCount = this.feedbacks.filter(f => f.taskId === taskId).length;
    if (taskCount >= this.config.maxFeedbackPerTask) {
      throw new Error(`Task ${taskId} has reached max feedback limit (${this.config.maxFeedbackPerTask})`);
    }
 
    // 自动分类(如果未指定)
    if (!category) {
      category = this._autoCategorize(rating, comment);
    }
 
    const feedback = {
      id: `fb_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
      taskId,
      skillName,
      rating,
      comment: comment.slice(0, 2000), // 限制长度
      tags,
      category,
      timestamp: new Date().toISOString(),
    };
 
    this.feedbacks.push(feedback);
 
    // 清理超出上限的旧反馈
    if (this.feedbacks.length > this.config.maxTotalFeedback) {
      this.feedbacks = this.feedbacks.slice(-this.config.maxTotalFeedback);
    }
 
    this.save();
 
    // 触发回调
    if (this.config.onFeedbackReceived) {
      this.config.onFeedbackReceived(feedback);
    }
 
    return feedback;
  }
 
  // ── 内部:自动分类 ──────────────────────────────────────
 
  _autoCategorize(rating, comment) {
    const lower = comment.toLowerCase();
    // 优先检查明确的 bug 关键词(不管评分)
    if (lower.includes('bug') || lower.includes('错误') || lower.includes('error') || lower.includes('崩溃') || lower.includes('fail')) {
      return 'bug';
    }
    if (lower.includes('建议') || lower.includes('希望') || lower.includes('改进') || lower.includes('suggestion') || lower.includes('improve')) {
      return 'suggestion';
    }
    // 按评分分类
    if (rating >= 4) return 'praise';
    return 'issue';
  }
 
  // ── 查询接口 ────────────────────────────────────────────
 
  /**
   * 获取任务的反馈统计
   * @param {string} taskId - 任务 ID(可选,不传则返回总体)
   * @returns {Object} 统计数据
   */
  getStats(taskId) {
    const filtered = taskId
      ? this.feedbacks.filter(f => f.taskId === taskId)
      : this.feedbacks;
 
    if (filtered.length === 0) {
      return {
        total: 0,
        avgRating: 0,
        ratingDistribution: {},
        categoryBreakdown: {},
        topTags: [],
        recentComments: [],
      };
    }
 
    // 评分分布
    const ratingDistribution = {};
    for (let i = 1; i <= 5; i++) ratingDistribution[i] = 0;
    filtered.forEach(f => { ratingDistribution[f.rating]++; });
 
    // 分类统计
    const categoryBreakdown = {};
    filtered.forEach(f => {
      categoryBreakdown[f.category] = (categoryBreakdown[f.category] || 0) + 1;
    });
 
    // 标签统计
    const tagCounts = {};
    filtered.forEach(f => {
      f.tags.forEach(t => { tagCounts[t] = (tagCounts[t] || 0) + 1; });
    });
    const topTags = Object.entries(tagCounts)
      .sort((a, b) => b[1] - a[1])
      .slice(0, 10)
      .map(([tag, count]) => ({ tag, count }));
 
    // 最近评论
    const recentComments = [...filtered]
      .filter(f => f.comment)
      .sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp))
      .slice(0, 5)
      .map(f => ({
        rating: f.rating,
        comment: f.comment,
        category: f.category,
        timestamp: f.timestamp,
      }));
 
    const totalRating = filtered.reduce((sum, f) => sum + f.rating, 0);
 
    return {
      total: filtered.length,
      avgRating: Math.round((totalRating / filtered.length) * 100) / 100,
      ratingDistribution,
      categoryBreakdown,
      topTags,
      recentComments,
      latestUpdate: filtered[filtered.length - 1]?.timestamp,
    };
  }
 
  /**
   * 获取技能维度的评分排名
   * @returns {Array} 按平均评分排序的技能列表
   */
  getSkillRankings() {
    const skillMap = {};
    this.feedbacks.forEach(f => {
      if (!skillMap[f.skillName]) {
        skillMap[f.skillName] = { total: 0, sum: 0 };
      }
      skillMap[f.skillName].total++;
      skillMap[f.skillName].sum += f.rating;
    });
 
    return Object.entries(skillMap)
      .map(([name, data]) => ({
        skillName: name,
        avgRating: Math.round((data.sum / data.total) * 100) / 100,
        totalFeedbacks: data.total,
      }))
      .sort((a, b) => b.avgRating - a.avgRating);
  }
 
  /**
   * 搜索反馈
   * @param {Object} filters - 过滤条件
   * @returns {Array} 匹配的反馈
   */
  search(filters = {}) {
    return this.feedbacks.filter(f => {
      Iif (filters.taskId && f.taskId !== filters.taskId) return false;
      if (filters.skillName && f.skillName !== filters.skillName) return false;
      if (filters.category && f.category !== filters.category) return false;
      if (filters.minRating && f.rating < filters.minRating) return false;
      Iif (filters.maxRating && f.rating > filters.maxRating) return false;
      if (filters.tags && !filters.tags.every(t => f.tags.includes(t))) return false;
      Iif (filters.since && new Date(f.timestamp) < new Date(filters.since)) return false;
      return true;
    });
  }
 
  /**
   * 获取反馈趋势(按时间聚合)
   * @param {string} granularity - 'hourly' | 'daily' | 'weekly'
   * @returns {Array} 趋势数据
   */
  getTrend(granularity = 'daily') {
    const grouped = {};
 
    this.feedbacks.forEach(f => {
      const date = new Date(f.timestamp);
      let key;
      Iif (granularity === 'hourly') {
        key = date.toISOString().slice(0, 13); // YYYY-MM-DDTHH
      } else Iif (granularity === 'weekly') {
        const weekStart = new Date(date);
        weekStart.setDate(date.getDate() - date.getDay());
        key = weekStart.toISOString().slice(0, 10);
      } else {
        key = date.toISOString().slice(0, 10); // YYYY-MM-DD
      }
 
      if (!grouped[key]) {
        grouped[key] = { count: 0, totalRating: 0, issues: 0 };
      }
      grouped[key].count++;
      grouped[key].totalRating += f.rating;
      if (f.category === 'issue' || f.category === 'bug') {
        grouped[key].issues++;
      }
    });
 
    return Object.entries(grouped)
      .map(([period, data]) => ({
        period,
        count: data.count,
        avgRating: Math.round((data.totalRating / data.count) * 100) / 100,
        issueRate: Math.round((data.issues / data.count) * 1000) / 10,
      }))
      .sort((a, b) => a.period.localeCompare(b.period));
  }
 
  /**
   * 重置所有反馈数据(测试用)
   */
  reset() {
    this.feedbacks = [];
    this.save();
  }
}
 
module.exports = { FeedbackMiddleware, DEFAULT_CONFIG: DEFAULT_CONFIG };