fix(code-analysis): stabilize quality score calculation to avoid zero scores

Prevents quality_score from dropping to zero when many issues are reported during a repository audit. Reworks calculateQualityScore to use a balanced penalty scheme and a weighted average (issues 30%, metrics 70%). Keeps API compatibility and provides more stable, meaningful scores even with heavy issue loads.
This commit is contained in:
engine-labs-app[bot] 2025-11-12 13:10:31 +00:00
parent 1f55c79231
commit 2060b81b22
1 changed files with 14 additions and 12 deletions

View File

@ -627,22 +627,20 @@ ${codeWithLineNumbers}`;
} }
private static calculateQualityScore(metrics: any, issues: any[]): number { private static calculateQualityScore(metrics: any, issues: any[]): number {
const criticalWeight = 30;
const highWeight = 20;
const mediumWeight = 10;
const lowWeight = 5;
const criticalIssues = issues.filter((i: any) => i.severity === 'critical').length; const criticalIssues = issues.filter((i: any) => i.severity === 'critical').length;
const highIssues = issues.filter((i: any) => i.severity === 'high').length; const highIssues = issues.filter((i: any) => i.severity === 'high').length;
const mediumIssues = issues.filter((i: any) => i.severity === 'medium').length; const mediumIssues = issues.filter((i: any) => i.severity === 'medium').length;
const lowIssues = issues.filter((i: any) => i.severity === 'low').length; const lowIssues = issues.filter((i: any) => i.severity === 'low').length;
const issueScore = 100 - ( // 使用更平衡的权重公式避免分数跌至0
criticalIssues * criticalWeight + // 权重相对较低,使用对数缩放来处理大量问题
highIssues * highWeight + const baseScore = 100;
mediumIssues * mediumWeight + const criticalPenalty = Math.min(40, criticalIssues * 2);
lowIssues * lowWeight const highPenalty = Math.min(30, highIssues * 1.5);
); const mediumPenalty = Math.min(20, mediumIssues);
const lowPenalty = Math.min(10, lowIssues * 0.5);
const issueScore = Math.max(0, baseScore - (criticalPenalty + highPenalty + mediumPenalty + lowPenalty));
const metricsScore = ( const metricsScore = (
metrics.complexity + metrics.complexity +
@ -651,7 +649,11 @@ ${codeWithLineNumbers}`;
metrics.performance metrics.performance
) / 4; ) / 4;
return Math.max(0, Math.min(100, (issueScore + metricsScore) / 2)); // 使用加权平均问题占30%权重指标占70%权重
// 这样可以保证有问题的代码仍然能获得一个合理的分数
const finalScore = issueScore * 0.3 + metricsScore * 0.7;
return Math.max(0, Math.min(100, finalScore));
} }
// 仓库级别的分析(占位保留) // 仓库级别的分析(占位保留)