|
11 | 11 | from pathlib import Path |
12 | 12 | import os |
13 | 13 | import shutil |
| 14 | +import sys |
14 | 15 | import tempfile |
15 | 16 | from typing import Any, Callable, Dict, Iterable, List, Optional |
16 | 17 |
|
@@ -59,6 +60,39 @@ def __init__( |
59 | 60 | } |
60 | 61 |
|
61 | 62 |
|
| 63 | +def transform_metrics(objects): |
| 64 | + """ |
| 65 | + Transform the object model from one that represents the YAML definitions |
| 66 | + to one that reflects the type specifics needed by code generators. |
| 67 | +
|
| 68 | + e.g. This will transform a `rate` to be a `numerator` if its denominator is |
| 69 | + external. |
| 70 | + """ |
| 71 | + counters = {} |
| 72 | + numerators_by_denominator: Dict[str, Any] = {} |
| 73 | + for category_val in objects.values(): |
| 74 | + for metric in category_val.values(): |
| 75 | + fqmn = metric.identifier() |
| 76 | + if getattr(metric, "type", None) == "counter": |
| 77 | + counters[fqmn] = metric |
| 78 | + denominator_name = getattr(metric, "denominator_metric", None) |
| 79 | + if denominator_name: |
| 80 | + metric.type = "numerator" |
| 81 | + numerators_by_denominator.setdefault(denominator_name, []) |
| 82 | + numerators_by_denominator[denominator_name].append(fqmn) |
| 83 | + |
| 84 | + for denominator_name, numerator_names in numerators_by_denominator.items(): |
| 85 | + if denominator_name not in counters: |
| 86 | + print( |
| 87 | + f"No `counter` named {denominator_name} found to be used as" |
| 88 | + "denominator for {numerator_names}", |
| 89 | + file=sys.stderr, |
| 90 | + ) |
| 91 | + return 1 |
| 92 | + counters[denominator_name].type = "denominator" |
| 93 | + counters[denominator_name].numerators = numerator_names |
| 94 | + |
| 95 | + |
62 | 96 | def translate_metrics( |
63 | 97 | input_filepaths: Iterable[Path], |
64 | 98 | output_dir: Path, |
|
0 commit comments