def calculate_combined_progress(project_name, manifest_data): """Calculates the combined progress based on scada/drawing status for a project.""" print(f"[{project_name}] Calculating combined progress statistics...") results = { "overall": { "total_csv": 0, "found_both": 0, "found_scada_only": 0, "found_drawing_only": 0, "missing_both": 0, "percentage_found_both": 0, "missing_list": [], "found_scada_only_list": [], "found_drawing_only_list": [], "found_both_list": [] }, "panels": {} } if not manifest_data: print("Warning: No manifest data to calculate progress from.") return results # Return the empty structure results["overall"]["total_csv"] = len(manifest_data) for item in manifest_data: panel = item.get('control_panel', 'Unknown Panel') # Use get with default # Initialize panel data if not present if panel not in results["panels"]: results["panels"][panel] = { "total": 0, "found_both": 0, "found_scada_only": 0, "found_drawing_only": 0, "missing_both": 0, "percentage_found_both": 0, "missing_list": [], "found_scada_only_list": [], "found_drawing_only_list": [], "found_both_list": [] } results["panels"][panel]["total"] += 1 # Categorize and add to lists # Create a copy of the item detail to avoid modifying the original if needed later # Exclude 'normalized_alias' from the output dictionary item_detail = {k: v for k, v in item.items() if k != 'normalized_alias'} found_scada = item.get('found_scada', False) found_drawing = item.get('found_drawing', False) if found_scada and found_drawing: category = "found_both" elif found_scada and not found_drawing: category = "found_scada_only" elif not found_scada and found_drawing: category = "found_drawing_only" else: # Missing both category = "missing_both" results["overall"][category] += 1 results["panels"][panel][category] += 1 # Correct the list keys used for appending if category == "missing_both": results["overall"]["missing_list"].append(item_detail) results["panels"][panel]["missing_list"].append(item_detail) else: results["overall"][f"{category}_list"].append(item_detail) results["panels"][panel][f"{category}_list"].append(item_detail) # Calculate percentages if results["overall"]["total_csv"] > 0: results["overall"]["percentage_found_both"] = round( (results["overall"]["found_both"] / results["overall"]["total_csv"]) * 100, 1 ) for panel_data in results["panels"].values(): if panel_data["total"] > 0: panel_data["percentage_found_both"] = round( (panel_data["found_both"] / panel_data["total"]) * 100, 1 ) print("Combined progress calculation finished.") # print(json.dumps(results, indent=2)) # DEBUG: Print structure if needed return results