summaryrefslogtreecommitdiffstats
path: root/toolkit/components/glean/build_scripts/glean_parser_ext/util.py
blob: 19b3d62bc0689281c62d4844a81525ca08b9e2c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

"""
Utitlity functions for the glean_parser-based code generator
"""


def generate_ping_ids(objs):
    """
    Return a lookup function for ping IDs per ping name.

    :param objs: A tree of objects as returned from `parser.parse_objects`.
    """

    if "pings" not in objs:

        def no_ping_ids_for_you():
            assert False

        return no_ping_ids_for_you

    # Ping ID 0 is reserved (but unused) right now.
    ping_id = 1

    ping_id_mapping = {}
    for ping_name in objs["pings"].keys():
        ping_id_mapping[ping_name] = ping_id
        ping_id += 1

    return lambda ping_name: ping_id_mapping[ping_name]


def generate_metric_ids(objs):
    """
    Return a lookup function for metric IDs per metric object.

    :param objs: A tree of metrics as returned from `parser.parse_objects`.
    """

    # Metric ID 0 is reserved (but unused) right now.
    metric_id = 1

    # Mapping from a tuple of (category name, metric name) to the metric's numeric ID
    metric_id_mapping = {}
    for category_name, metrics in objs.items():
        for metric in metrics.values():
            metric_id_mapping[(category_name, metric.name)] = metric_id
            metric_id += 1

    return lambda metric: metric_id_mapping[(metric.category, metric.name)]


IMPLEMENTED_CPP_TYPES = [
    "boolean",
    "counter",
    "datetime",
    "event",
    "memory_distribution",
    "string",
    "string_list",
    "timespan",
    "timing_distribution",
    "uuid",
]


def is_implemented_metric_type(typ):
    """
    Filter out some unimplemented metric types to avoid generating C++ code for them.
    Once all types are implemented this code will be removed.
    """
    return typ in IMPLEMENTED_CPP_TYPES