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 b97f725 commit c411eee
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
8 changes: 6 additions & 2 deletions build-scripts/profile_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import argparse

try:
from utils.profile_tool import command_stats, command_sub
from utils.profile_tool import command_stats, command_sub, command_most_used_rules
except ImportError:
print("The ssg module could not be found.")
print(
Expand Down Expand Up @@ -300,7 +300,11 @@ def parse_args():
return args


SUBCMDS = dict(stats=command_stats, sub=command_sub)
SUBCMDS = {
"stats": command_stats,
"sub": command_sub,
"most-used-rules": command_most_used_rules,
}


def main():
Expand Down
1 change: 1 addition & 0 deletions utils/profile_tool/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .sub import command_sub
from .stats import command_stats
from .most_used_rules import command_most_used_rules
39 changes: 39 additions & 0 deletions utils/profile_tool/most_used_rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import json

from ssg.build_profile import XCCDFBenchmark


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 = XCCDFBenchmark(benchmark)
for profile in benchmark.get_all_profile_stats():
_count_rules_per_profile(profile, rules)


def command_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 sorted_rules.items():
print(f_string.format(rule_id, rule_count))

0 comments on commit c411eee

Please sign in to comment.