|
1 | 1 | import argparse
|
2 | 2 | import json
|
| 3 | +import re |
3 | 4 | import subprocess
|
| 5 | +from collections import OrderedDict |
4 | 6 | from datetime import datetime
|
5 | 7 | from os import environ
|
6 | 8 | from os.path import exists
|
7 | 9 | from platform import uname
|
8 | 10 | from time import time
|
9 |
| -from typing import Optional |
| 11 | +from typing import Optional, List |
10 | 12 |
|
11 | 13 | from monitoring_settings import JSON_VERSION
|
12 |
| -from utils import * |
13 | 14 |
|
14 | 15 |
|
15 | 16 | def load(json_file: str) -> Optional[any]:
|
@@ -91,17 +92,142 @@ def build_metadata(args: argparse.Namespace) -> dict:
|
91 | 92 | return metadata
|
92 | 93 |
|
93 | 94 |
|
| 95 | +def build_target(target_name: str) -> dict: |
| 96 | + return { |
| 97 | + "target": target_name, |
| 98 | + "summarised": [], |
| 99 | + "by_class": OrderedDict() |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | +def transform_metrics(metrics: dict) -> dict: |
| 104 | + """ |
| 105 | + Transform given metrics with calculation coverage |
| 106 | + :param metrics: given metrics |
| 107 | + :return: transformed metrics |
| 108 | + """ |
| 109 | + result = OrderedDict() |
| 110 | + |
| 111 | + instr_count_prefix = "covered_bytecode_instructions" |
| 112 | + total_instr_count_prefix = "total_bytecode_instructions" |
| 113 | + |
| 114 | + coverage_prefix = "total_bytecode_instruction_coverage" |
| 115 | + |
| 116 | + total_count = 0 |
| 117 | + for metric in metrics: |
| 118 | + if metric.startswith(total_instr_count_prefix): |
| 119 | + total_count = metrics[metric] |
| 120 | + break |
| 121 | + |
| 122 | + for metric in metrics: |
| 123 | + if metric.startswith(total_instr_count_prefix): |
| 124 | + continue |
| 125 | + if metric.startswith(instr_count_prefix): |
| 126 | + coverage = metrics[metric] / total_count if total_count > 0 else 0.0 |
| 127 | + result[coverage_prefix + metric.removeprefix(instr_count_prefix)] = coverage |
| 128 | + else: |
| 129 | + result[metric] = metrics[metric] |
| 130 | + |
| 131 | + return result |
| 132 | + |
| 133 | + |
| 134 | +def build_data(parameters: dict, metrics: dict) -> dict: |
| 135 | + return { |
| 136 | + "parameters": { |
| 137 | + **parameters |
| 138 | + }, |
| 139 | + "metrics": { |
| 140 | + **transform_metrics(metrics) |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | +def build_by_class(class_name: str) -> dict: |
| 146 | + return { |
| 147 | + "class_name": class_name, |
| 148 | + "data": [] |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | +def update_from_class(by_class: dict, class_item: dict, parameters: dict): |
| 153 | + """ |
| 154 | + Update class object using given class_item |
| 155 | + :param by_class: dictionary with classname keys |
| 156 | + :param class_item: class metrics of current run |
| 157 | + :param parameters: parameters of current run |
| 158 | + """ |
| 159 | + class_name = class_item["class_name"] |
| 160 | + if class_name not in by_class: |
| 161 | + by_class[class_name] = build_by_class(class_name) |
| 162 | + |
| 163 | + metrics = class_item["metrics"] |
| 164 | + by_class[class_name]["data"].append( |
| 165 | + build_data(parameters, metrics) |
| 166 | + ) |
| 167 | + |
| 168 | + |
| 169 | +def update_from_target(targets: dict, target_item: dict, parameters: dict): |
| 170 | + """ |
| 171 | + Update targets using given target_item |
| 172 | + :param targets: dictionary with target keys |
| 173 | + :param target_item: metrics of current run |
| 174 | + :param parameters: parameters of current run |
| 175 | + """ |
| 176 | + target_name = target_item["target"] |
| 177 | + if target_name not in targets: |
| 178 | + targets[target_name] = build_target(target_name) |
| 179 | + |
| 180 | + summarised_metrics = target_item["summarised_metrics"] |
| 181 | + targets[target_name]["summarised"].append( |
| 182 | + build_data(parameters, summarised_metrics) |
| 183 | + ) |
| 184 | + |
| 185 | + for class_item in target_item["metrics_by_class"]: |
| 186 | + update_from_class(targets[target_name]["by_class"], class_item, parameters) |
| 187 | + |
| 188 | + |
| 189 | +def update_from_stats(targets: dict, stats: dict): |
| 190 | + """ |
| 191 | + Updates targets using given statistics |
| 192 | + :param targets: dictionary with target keys |
| 193 | + :param stats: target object |
| 194 | + """ |
| 195 | + parameters = stats["parameters"] |
| 196 | + for target_item in stats["targets"]: |
| 197 | + update_from_target(targets, target_item, parameters) |
| 198 | + |
| 199 | + |
| 200 | +def postprocess_by_class(by_class: dict) -> List[dict]: |
| 201 | + """ |
| 202 | + Transform dictionary with classname keys into array with class objects |
| 203 | + :param by_class: dictionary with classname keys |
| 204 | + :return: array of class objects |
| 205 | + """ |
| 206 | + return list(by_class.values()) |
| 207 | + |
| 208 | + |
| 209 | +def postprocess_targets(targets: dict) -> List[dict]: |
| 210 | + """ |
| 211 | + Transform dictionary with target keys into array with target objects |
| 212 | + :param targets: dictionary with target keys |
| 213 | + :return: array of targets |
| 214 | + """ |
| 215 | + result = [] |
| 216 | + for target in targets.values(): |
| 217 | + target["by_class"] = postprocess_by_class(target["by_class"]) |
| 218 | + result.append(target) |
| 219 | + return result |
| 220 | + |
| 221 | + |
94 | 222 | def build_targets(stats_array: List[dict]) -> List[dict]:
|
95 | 223 | """
|
96 | 224 | Collect and group statistics by target
|
97 | 225 | :param stats_array: list of dictionaries with parameters and metrics
|
98 | 226 | :return: list of metrics and parameters grouped by target
|
99 | 227 | """
|
100 |
| - result = get_default_metrics_dict() |
| 228 | + result = OrderedDict() |
101 | 229 | for stats in stats_array:
|
102 |
| - target = stats['parameters']['target'] |
103 |
| - del stats['parameters']['target'] |
104 |
| - update_target(result[target], stats) |
| 230 | + update_from_stats(result, stats) |
105 | 231 |
|
106 | 232 | return postprocess_targets(result)
|
107 | 233 |
|
|
0 commit comments