Deloitte | COGNIZANT | IBM | CAPGEMINI
def process_usage_logs(usage_records, reference_day, region_details, profiles, region_stats):
for record in usage_records:
usage_id, region_id, usage_day, cpu_usage, memory_usage = record
usage_day = int(usage_day)
cpu_usage = int(cpu_usage)
memory_usage = int(memory_usage)
if region_id not in region_details:
continue
if not (1 <= usage_day <= reference_day):
continue
if cpu_usage < 0 or memory_usage < 0:
continue
region_type = region_details[region_id][1]
max_cpu, max_mem = profiles[region_type]
stats = region_stats[region_id]
stats["valid_usage_count"] += 1
stats["cpu_sum"] += cpu_usage
stats["memory_sum"] += memory_usage
if cpu_usage > max_cpu:
stats["cpu_breach_count"] += 1
if memory_usage > max_mem:
stats["memory_breach_count"] += 1
def process_deployments(deployment_records, reference_day, region_details, region_stats):
for record in deployment_records:
deployment_id, region_id, deployment_day, deployment_status = record
deployment_day = int(deployment_day)
if region_id not in region_details:
continue
if deployment_status not in VALID_DEPLOYMENT_STATUSES:
continue
if not (1 <= deployment_day <= reference_day):
continue
if deployment_status == "ACTIVE":
region_stats[region_id]["active_deployment_count"] += 1
def process_incident_reports(incident_records, reference_day, region_details, region_stats):
for record in incident_records:
incident_id, region_id, incident_day, severity = record
incident_day = int(incident_day)
if region_id not in region_details:
continue
if severity not in VALID_INCIDENT_SEVERITIES:
continue
if not (1 <= incident_day <= reference_day):
continue
if severity == "HIGH":
region_stats[region_id]["high_severity_incident_count"] += 1
def calculate_type_average_cpu(profiles, type_cpu_sum, type_usage_count):
type_average_cpu = {}
for region_type in profiles:
count = type_usage_count.get(region_type, 0)
if count == 0:
type_average_cpu[region_type] = 0
else:
type_average_cpu[region_type] = type_cpu_sum.get(region_type, 0) // count
return type_average_cpu
def calculate_region_risk(stats, max_memory_usage, type_average_cpu):
pass
def build_output(regions, profiles, region_stats, type_average_cpu):
output_regions = []
for region_id, region_name, region_type, input_index in regions:
stats = region_stats[region_id]
max_cpu, max_mem = profiles[region_type]
valid_count = stats["valid_usage_count"]
if valid_count == 0:
avg_cpu = 0
avg_mem = 0
else:
avg_cpu = stats["cpu_sum"] // valid_count
avg_mem = stats["memory_sum"] // valid_count
active_deployments = stats["active_deployment_count"]
cpu_breaches = stats["cpu_breach_count"]
mem_breaches = stats["memory_breach_count"]
high_incidents = stats["high_severity_incident_count"]
type_avg_cpu = type_average_cpu.get(region_type, 0)
is_above_cpu_baseline = (valid_count > 0 and avg_cpu > type_avg_cpu)
score = 0
if active_deployments < 2:
score += 4
if active_deployments == 0:
score += 3
if cpu_breaches >= 2:
score += 3
if mem_breaches >= 2:
score += 3
if high_incidents >= 1:
score += 3
if is_above_cpu_baseline:
score += 2
if valid_count > 0 and avg_mem > max_mem:
score += 2
566 ·