|
8 | 8 | Outputter to generate Kotlin code for metrics. |
9 | 9 | """ |
10 | 10 |
|
11 | | -from collections import OrderedDict |
12 | 11 | import enum |
13 | 12 | import json |
14 | 13 | from pathlib import Path |
|
17 | 16 | from . import __version__ |
18 | 17 | from . import metrics |
19 | 18 | from . import pings |
20 | | -from . import tags |
21 | 19 | from . import util |
22 | | -from .util import DictWrapper |
23 | 20 |
|
24 | 21 |
|
25 | 22 | def kotlin_datatypes_filter(value: util.JSONType) -> str: |
@@ -179,105 +176,6 @@ def generate_build_date(date: Optional[str]) -> str: |
179 | 176 | return f'Calendar.getInstance(TimeZone.getTimeZone("GMT+0")).also {{ cal -> cal.set({components}) }}' # noqa |
180 | 177 |
|
181 | 178 |
|
182 | | -def output_gecko_lookup( |
183 | | - objs: metrics.ObjectTree, output_dir: Path, options: Optional[Dict[str, Any]] = None |
184 | | -) -> None: |
185 | | - """ |
186 | | - Given a tree of objects, generate a Kotlin map between Gecko histograms and |
187 | | - Glean SDK metric types. |
188 | | -
|
189 | | - :param objects: A tree of objects (metrics and pings) as returned from |
190 | | - `parser.parse_objects`. |
191 | | - :param output_dir: Path to an output directory to write to. |
192 | | - :param options: options dictionary, with the following optional keys: |
193 | | -
|
194 | | - - `namespace`: The package namespace to declare at the top of the |
195 | | - generated files. Defaults to `GleanMetrics`. |
196 | | - - `glean_namespace`: The package namespace of the glean library itself. |
197 | | - This is where glean objects will be imported from in the generated |
198 | | - code. |
199 | | - """ |
200 | | - if options is None: |
201 | | - options = {} |
202 | | - |
203 | | - template = util.get_jinja2_template( |
204 | | - "kotlin.geckoview.jinja2", |
205 | | - filters=( |
206 | | - ("kotlin", kotlin_datatypes_filter), |
207 | | - ("type_name", type_name), |
208 | | - ("class_name", class_name), |
209 | | - ), |
210 | | - ) |
211 | | - |
212 | | - namespace = options.get("namespace", "GleanMetrics") |
213 | | - glean_namespace = options.get("glean_namespace", "mozilla.components.service.glean") |
214 | | - |
215 | | - # Build a dictionary that contains data for metrics that are |
216 | | - # histogram-like/scalar-like and contain a gecko_datapoint, with this format: |
217 | | - # |
218 | | - # { |
219 | | - # "histograms": { |
220 | | - # "category": [ |
221 | | - # {"gecko_datapoint": "the-datapoint", "name": "the-metric-name"}, |
222 | | - # ... |
223 | | - # ], |
224 | | - # ... |
225 | | - # }, |
226 | | - # "other-type": {} |
227 | | - # } |
228 | | - gecko_metrics: Dict[str, Dict[str, List[Dict[str, str]]]] = DictWrapper() |
229 | | - |
230 | | - # Define scalar-like types. |
231 | | - SCALAR_LIKE_TYPES = ["boolean", "string", "quantity"] |
232 | | - |
233 | | - for category_key, category_val in objs.items(): |
234 | | - # Support exfiltration of Gecko metrics from products using both the |
235 | | - # Glean SDK and GeckoView. See bug 1566356 for more context. |
236 | | - for metric in category_val.values(): |
237 | | - # This is not a Gecko metric, skip it. |
238 | | - if ( |
239 | | - isinstance(metric, pings.Ping) |
240 | | - or isinstance(metric, tags.Tag) |
241 | | - or not getattr(metric, "gecko_datapoint", False) |
242 | | - ): |
243 | | - continue |
244 | | - |
245 | | - # Put scalars in their own categories, histogram-like in "histograms" and |
246 | | - # categorical histograms in "categoricals". |
247 | | - type_category = "histograms" |
248 | | - if metric.type in SCALAR_LIKE_TYPES: |
249 | | - type_category = metric.type |
250 | | - elif metric.type == "labeled_counter": |
251 | | - # Labeled counters with a 'gecko_datapoint' property |
252 | | - # are categorical histograms. |
253 | | - type_category = "categoricals" |
254 | | - |
255 | | - gecko_metrics.setdefault(type_category, OrderedDict()) |
256 | | - gecko_metrics[type_category].setdefault(category_key, []) |
257 | | - |
258 | | - gecko_metrics[type_category][category_key].append( |
259 | | - {"gecko_datapoint": metric.gecko_datapoint, "name": metric.name} |
260 | | - ) |
261 | | - |
262 | | - if not gecko_metrics: |
263 | | - # Bail out and don't create a file if no gecko metrics |
264 | | - # are found. |
265 | | - return |
266 | | - |
267 | | - filepath = output_dir / "GleanGeckoMetricsMapping.kt" |
268 | | - with filepath.open("w", encoding="utf-8") as fd: |
269 | | - fd.write( |
270 | | - template.render( |
271 | | - parser_version=__version__, |
272 | | - gecko_metrics=gecko_metrics, |
273 | | - namespace=namespace, |
274 | | - glean_namespace=glean_namespace, |
275 | | - ) |
276 | | - ) |
277 | | - # Jinja2 squashes the final newline, so we explicitly add it |
278 | | - fd.write("\n") |
279 | | - |
280 | | - |
281 | 179 | def output_kotlin( |
282 | 180 | objs: metrics.ObjectTree, output_dir: Path, options: Optional[Dict[str, Any]] = None |
283 | 181 | ) -> None: |
@@ -376,6 +274,3 @@ def output_kotlin( |
376 | 274 | ) |
377 | 275 | # Jinja2 squashes the final newline, so we explicitly add it |
378 | 276 | fd.write("\n") |
379 | | - |
380 | | - # TODO: Maybe this should just be a separate outputter? |
381 | | - output_gecko_lookup(objs, output_dir, options) |
0 commit comments