📊 按状态计数
CountByStatus 返回指定 Status 状态的弱点数量。常用于「有效弱点数」「已弃用数」等指标。
📐 函数签名
go
func CountByStatus(r *Registry, status Status) int| 参数 | 类型 | 说明 |
|---|---|---|
r | *Registry | 注册表 |
status | Status | 状态枚举 |
| 返回 | int | 该状态的弱点数量 |
✅ 示例
go
package main
import (
"fmt"
cweskills "github.com/scagogogo/cwe-skills"
)
func main() {
r := cweskills.NewRegistry()
s := cweskills.NewCWE(79, "XSS")
s.Status = cweskills.StatusStable
r.Register(s)
d := cweskills.NewCWE(100, "Old")
d.Status = cweskills.StatusDeprecated
r.Register(d)
fmt.Println("Stable:", cweskills.CountByStatus(r, cweskills.StatusStable)) // 1
fmt.Println("Deprecated:", cweskills.CountByStatus(r, cweskills.StatusDeprecated)) // 1
}🆚 与 ComputeStatistics 的取舍
| 维度 | CountByStatus | ComputeStatistics |
|---|---|---|
| 返回 | 单个 int | 全维度 *Statistics |
| 适用 | 只需某一状态数 | 需全状态分布 |
需生成「状态分布饼图」用 ComputeStatistics().ByStatus;只需「有效弱点数」用本函数。
⚠️ 注意事项
零值状态
未设置 Status 的弱点会被计入零值枚举的计数。若零值不是合法状态,报表里会出现「未知」数。
不依赖索引
本函数遍历 GetAll(),无需 BuildIndexes()。