Execute rule checks in multi-threads for each service

This commit is contained in:
EC2 Default User 2024-08-14 04:23:10 +00:00
parent f7923b4890
commit 6e04f6a7a9

46
main.py
View File

@ -1,9 +1,10 @@
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor
from InquirerLib import prompt from InquirerLib import prompt
from InquirerLib.InquirerPy.utils import InquirerPyKeybindings from InquirerLib.InquirerPy.utils import InquirerPyKeybindings
from InquirerLib.InquirerPy.base import Choice from InquirerLib.InquirerPy.base import Choice
from colorama import Style, Fore from colorama import Style, Fore
from datetime import datetime
from importlib import import_module
from utils import * from utils import *
import services import services
@ -36,27 +37,34 @@ def ask_services_to_enable(bp):
def perform_bp_rules_check(bp): def perform_bp_rules_check(bp):
for service_name, service in bp.items(): with ThreadPoolExecutor() as executor:
if not service["enabled"]: futures = [
continue executor.submit(_rule_check, service_name, service)
if service_name == "Lambda": for service_name, service in bp.items()
service_name = "_lambda" ]
now = datetime.now() [future.result() for future in futures]
rule_checker = getattr(
services, convert_snake_case(service_name)
).rule_checker()
for rule_name, rule in service["rules"].items():
if not rule["enabled"]:
continue
rule["result"] = rule_checker.check_rule(convert_snake_case(rule_name))
elapsed_time = datetime.now() - now
print(convert_snake_case(service_name), elapsed_time.total_seconds())
return bp return bp
def _rule_check(service_name, service):
now = datetime.now()
if not service["enabled"]:
return
if service_name == "Lambda":
service_name = "_lambda"
rule_checker = getattr(services, convert_snake_case(service_name)).rule_checker()
for rule_name, rule in service["rules"].items():
if not rule["enabled"]:
continue
rule["result"] = rule_checker.check_rule(convert_snake_case(rule_name))
elapsed_time = datetime.now() - now
print(convert_snake_case(service_name), elapsed_time.total_seconds())
def show_bp_result(bp): def show_bp_result(bp):
for service_name, service in bp.items(): for service_name, service in bp.items():
if not service["enabled"]: if not service["enabled"]: