|
10 | 10 |
|
11 | 11 | UPSAMPLED_ERROR_AGGREGATION = "upsampled_count"
|
12 | 12 |
|
| 13 | +# Function key conversions for error upsampling results |
| 14 | +_FUNCTION_KEY_CONVERSIONS = { |
| 15 | + "count()": "sample_count()", |
| 16 | + "eps()": "sample_eps()", |
| 17 | + "epm()": "sample_epm()", |
| 18 | + "upsampled_count()": "count()", |
| 19 | + "upsampled_eps()": "eps()", |
| 20 | + "upsampled_epm()": "epm()", |
| 21 | +} |
| 22 | + |
| 23 | +# Pre-computed ordered keys to handle conversion conflicts |
| 24 | +# Keys that are targets of other conversions must be processed first |
| 25 | +_conversion_targets = set(_FUNCTION_KEY_CONVERSIONS.values()) |
| 26 | +_ORDERED_CONVERSION_KEYS = sorted( |
| 27 | + _FUNCTION_KEY_CONVERSIONS.keys(), key=lambda k: k not in _conversion_targets |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +def convert_fields_for_upsampling(data: list[dict[str, Any]], fields_meta: dict[str, str]) -> None: |
| 32 | + """ |
| 33 | + Convert field names in query results for error upsampled projects. |
| 34 | + This renames upsampled_* functions to their standard names and standard functions |
| 35 | + to sample_* equivalents to hide the conversion from the client. |
| 36 | +
|
| 37 | + Args: |
| 38 | + data: List of result dictionaries to modify in-place |
| 39 | + fields_meta: Meta fields dictionary to modify in-place |
| 40 | + """ |
| 41 | + # Collect keys that need conversion and exist in data |
| 42 | + all_present_keys = set() |
| 43 | + for result in data: |
| 44 | + all_present_keys.update(result.keys()) |
| 45 | + |
| 46 | + # Filter the pre-ordered list to only include keys actually present |
| 47 | + keys_to_convert = [key for key in _ORDERED_CONVERSION_KEYS if key in all_present_keys] |
| 48 | + |
| 49 | + # Apply conversions to data |
| 50 | + for result in data: |
| 51 | + for original_key in keys_to_convert: |
| 52 | + if original_key in result: |
| 53 | + converted_key = _FUNCTION_KEY_CONVERSIONS[original_key] |
| 54 | + result[converted_key] = result[original_key] |
| 55 | + del result[original_key] |
| 56 | + |
| 57 | + # Apply conversions to fields_meta |
| 58 | + for original_key in keys_to_convert: |
| 59 | + if original_key in fields_meta: |
| 60 | + converted_key = _FUNCTION_KEY_CONVERSIONS[original_key] |
| 61 | + fields_meta[converted_key] = fields_meta[original_key] |
| 62 | + del fields_meta[original_key] |
| 63 | + |
13 | 64 |
|
14 | 65 | def is_errors_query_for_error_upsampled_projects(
|
15 | 66 | snuba_params: SnubaParams,
|
@@ -55,6 +106,9 @@ def transform_query_columns_for_error_upsampling(
|
55 | 106 | "count()": "upsampled_count()",
|
56 | 107 | "eps()": "upsampled_eps()",
|
57 | 108 | "epm()": "upsampled_epm()",
|
| 109 | + "sample_count()": "count()", |
| 110 | + "sample_eps()": "eps()", |
| 111 | + "sample_epm()": "epm()", |
58 | 112 | }
|
59 | 113 |
|
60 | 114 | transformed_columns = []
|
|
0 commit comments