summaryrefslogtreecommitdiffstats
path: root/python/mozperftest/mozperftest/metrics/notebook/utilities.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/mozperftest/mozperftest/metrics/notebook/utilities.py')
-rw-r--r--python/mozperftest/mozperftest/metrics/notebook/utilities.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/python/mozperftest/mozperftest/metrics/notebook/utilities.py b/python/mozperftest/mozperftest/metrics/notebook/utilities.py
new file mode 100644
index 0000000000..7fd97fa3fa
--- /dev/null
+++ b/python/mozperftest/mozperftest/metrics/notebook/utilities.py
@@ -0,0 +1,63 @@
+# 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/.
+from collections.abc import Iterable
+
+
+def flat(data, parent_dir):
+ """
+ Converts a dictionary with nested entries like this
+ {
+ "dict1": {
+ "dict2": {
+ "key1": value1,
+ "key2": value2,
+ ...
+ },
+ ...
+ },
+ ...
+ "dict3": {
+ "key3": value3,
+ "key4": value4,
+ ...
+ }
+ ...
+ }
+
+ to a "flattened" dictionary like this that has no nested entries:
+ {
+ "dict1.dict2.key1": value1,
+ "dict1.dict2.key2": value2,
+ ...
+ "dict3.key3": value3,
+ "dict3.key4": value4,
+ ...
+ }
+
+ :param Iterable data : json data.
+ :param tuple parent_dir: json fields.
+
+ :return dict: {subtest: value}
+ """
+ result = {}
+
+ if not data:
+ return result
+
+ if isinstance(data, list):
+ for item in data:
+ for k, v in flat(item, parent_dir).items():
+ result.setdefault(k, []).extend(v)
+
+ if isinstance(data, dict):
+ for k, v in data.items():
+ current_dir = parent_dir + (k,)
+ subtest = ".".join(current_dir)
+ if isinstance(v, Iterable) and not isinstance(v, str):
+ for x, y in flat(v, current_dir).items():
+ result.setdefault(x, []).extend(y)
+ elif v or v == 0:
+ result.setdefault(subtest, []).append(v)
+
+ return result