Skip to content

Commit

Permalink
Add calculation of most used rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Honny1 committed Jan 10, 2024
1 parent a0fee29 commit e92662f
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion build-scripts/profile_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,43 @@ def stats(args):
print(",".join([str(value) for value in line.values()]))


SUBCMDS = dict(stats=stats, sub=sub)
def _count_rules_per_profile(profile, rules):
for rule in profile.get("rules", []):
if rule in rules:
rules[rule] += 1
else:
rules[rule] = 1


def _count_rules_per_benchmark(benchmark, rules):
benchmark = ssg.build_profile.XCCDFBenchmark(benchmark)
for profile in benchmark.get_all_profile_stats():
_count_rules_per_profile(profile, rules)


def most_used_rules(args):
rules = {}
for benchmark in args.benchmarks:
_count_rules_per_benchmark(benchmark, rules)

sorted_rules = {
k: v for k, v in sorted(rules.items(), key=lambda x: x[1], reverse=True)
}

f_string = "{}: {}"

if args.format == "json":
print(json.dumps(sorted_rules, indent=4))
return
elif args.format == "csv":
print("rule_id,count_of_profiles")
f_string = "{},{}"

for rule_id, rule_count in rules.items():
print(f_string.format(rule_id, rule_count))


SUBCMDS = {"stats": stats, "sub": sub, "most-used-rules": most_used_rules}


def main():
Expand Down

0 comments on commit e92662f

Please sign in to comment.