|
| 1 | +import json |
| 2 | +from collections import defaultdict |
| 3 | +from typing import Any, Dict, List, Tuple |
| 4 | + |
| 5 | +from .sourcemap_utils import _flatten_dict, _to_ranges, _unflatten_dict |
| 6 | + |
| 7 | +# Fields that are expected to vary but are not useful to list out in the diff. |
| 8 | +SUMMARY_FIELDS = ["pid", "timestamp", "stream", "function", "data_ptr"] |
| 9 | + |
| 10 | + |
| 11 | +def _generate_launch_diff( |
| 12 | + launches: List[Tuple[Dict[str, Any], int]], |
| 13 | +) -> Tuple[Dict[str, Any], Dict[str, Any], List[Dict[str, int]]]: |
| 14 | + """ |
| 15 | + Compares a list of launch events and returns sames, diffs, and an index map. |
| 16 | + """ |
| 17 | + if not launches: |
| 18 | + return {}, {}, [] |
| 19 | + |
| 20 | + launch_events = [launch[0] for launch in launches] |
| 21 | + launch_index_map = [launch[1] for launch in launches] |
| 22 | + |
| 23 | + if len(launch_events) == 1: |
| 24 | + return ( |
| 25 | + _unflatten_dict(_flatten_dict(launch_events[0])), |
| 26 | + {}, |
| 27 | + _to_ranges(launch_index_map), |
| 28 | + ) |
| 29 | + |
| 30 | + # Group values by key |
| 31 | + data_by_key = defaultdict(lambda: defaultdict(list)) |
| 32 | + for i, launch in enumerate(launch_events): |
| 33 | + launch_flat = _flatten_dict(launch) |
| 34 | + for key, value in launch_flat.items(): |
| 35 | + # JSON doesn't support all Python types as values directly, str is safer |
| 36 | + value_str = json.dumps(value, sort_keys=True) |
| 37 | + data_by_key[key][value_str].append(i) |
| 38 | + |
| 39 | + sames_flat = {} |
| 40 | + diffs_flat = {} |
| 41 | + |
| 42 | + for key, value_groups in data_by_key.items(): |
| 43 | + if len(value_groups) == 1: |
| 44 | + # This key has the same value across all launches |
| 45 | + value_str = list(value_groups.keys())[0] |
| 46 | + sames_flat[key] = json.loads(value_str) |
| 47 | + else: |
| 48 | + # This key has different values |
| 49 | + is_summary = any(summary_key in key for summary_key in SUMMARY_FIELDS) |
| 50 | + if is_summary: |
| 51 | + diffs_flat[key] = { |
| 52 | + "diff_type": "summary", |
| 53 | + "summary_text": f"Varies across {len(value_groups)} unique values", |
| 54 | + } |
| 55 | + else: |
| 56 | + values_dist = [] |
| 57 | + for value_str, indices in value_groups.items(): |
| 58 | + values_dist.append( |
| 59 | + { |
| 60 | + "value": json.loads(value_str), |
| 61 | + "count": len(indices), |
| 62 | + "launches": _to_ranges(indices), |
| 63 | + } |
| 64 | + ) |
| 65 | + # Sort by first occurrence |
| 66 | + values_dist.sort(key=lambda x: x["launches"][0]["start"]) |
| 67 | + diffs_flat[key] = { |
| 68 | + "diff_type": "distribution", |
| 69 | + "values": values_dist, |
| 70 | + } |
| 71 | + |
| 72 | + # Unflatten the results |
| 73 | + sames_unflattened = _unflatten_dict(sames_flat) |
| 74 | + diffs_unflattened = _unflatten_dict(diffs_flat) |
| 75 | + |
| 76 | + # Special handling for extracted_args to create argument_diff structures |
| 77 | + if "extracted_args" in sames_unflattened or "extracted_args" in diffs_unflattened: |
| 78 | + sames_args = sames_unflattened.pop("extracted_args", {}) |
| 79 | + diffs_args_flat = diffs_unflattened.pop("extracted_args", {}) |
| 80 | + |
| 81 | + all_arg_names = set(sames_args.keys()) | set(diffs_args_flat.keys()) |
| 82 | + |
| 83 | + final_arg_diffs = {} |
| 84 | + |
| 85 | + for arg_name in all_arg_names: |
| 86 | + if arg_name in diffs_args_flat: |
| 87 | + # This argument has at least one differing sub-field. |
| 88 | + arg_sames = {} |
| 89 | + arg_diffs_internal = {} |
| 90 | + |
| 91 | + # Collect all sub-fields for this argument from the original data |
| 92 | + all_sub_fields = set() |
| 93 | + for launch in launch_events: |
| 94 | + arg_data = launch.get("extracted_args", {}).get(arg_name, {}) |
| 95 | + all_sub_fields.update(arg_data.keys()) |
| 96 | + |
| 97 | + for sub_field in all_sub_fields: |
| 98 | + flat_key = f"extracted_args.{arg_name}.{sub_field}" |
| 99 | + if flat_key in diffs_flat: |
| 100 | + arg_diffs_internal[sub_field] = diffs_flat[flat_key] |
| 101 | + elif flat_key in sames_flat: |
| 102 | + arg_sames[sub_field] = sames_flat[flat_key] |
| 103 | + |
| 104 | + if arg_sames or arg_diffs_internal: |
| 105 | + final_arg_diffs[arg_name] = { |
| 106 | + "diff_type": "argument_diff", |
| 107 | + "sames": arg_sames, |
| 108 | + "diffs": arg_diffs_internal, |
| 109 | + } |
| 110 | + elif arg_name in sames_args: |
| 111 | + # This argument is entirely the same across all launches. |
| 112 | + # We move it back to the main sames dict for consistency. |
| 113 | + if "extracted_args" not in sames_unflattened: |
| 114 | + sames_unflattened["extracted_args"] = {} |
| 115 | + sames_unflattened["extracted_args"][arg_name] = sames_args[arg_name] |
| 116 | + |
| 117 | + if final_arg_diffs: |
| 118 | + diffs_unflattened["extracted_args"] = final_arg_diffs |
| 119 | + |
| 120 | + return sames_unflattened, diffs_unflattened, _to_ranges(launch_index_map) |
0 commit comments