Skip to content

Commit 698f7ff

Browse files
committed
add metrics preparing for grafana
1 parent 65f5866 commit 698f7ff

File tree

3 files changed

+107
-13
lines changed

3 files changed

+107
-13
lines changed

monitoring/insert_metadata.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,12 @@
55
from collections import OrderedDict
66
from datetime import datetime
77
from os import environ
8-
from os.path import exists
98
from platform import uname
109
from time import time
1110
from typing import Optional, List
1211

1312
from monitoring_settings import JSON_VERSION
14-
15-
16-
def load(json_file: str) -> Optional[any]:
17-
"""
18-
Try load object from json file
19-
:param json_file: path to json file
20-
:return: object from given json file or None
21-
"""
22-
if exists(json_file):
23-
with open(json_file, "r") as f:
24-
return json.load(f)
25-
return None
13+
from utils import load
2614

2715

2816
def try_get_output(args: str) -> Optional[str]:

monitoring/prepare_metrics.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import argparse
2+
import json
3+
from typing import List
4+
5+
from utils import load
6+
7+
8+
def build_metric_struct(name: str, value: any, labels: dict) -> dict:
9+
return {
10+
"metric": name,
11+
"labels": labels,
12+
"value": value
13+
}
14+
15+
16+
def build_metrics_from_data(data: dict, labels: dict) -> List[dict]:
17+
result = []
18+
fuzzing_ratio = data["parameters"]["fuzzing_ratio"]
19+
new_labels = {
20+
**labels,
21+
"fuzzing_ratio": fuzzing_ratio
22+
}
23+
metrics = data["metrics"]
24+
for metric in metrics:
25+
result.append(build_metric_struct(metric, metrics[metric], new_labels))
26+
return result
27+
28+
29+
def build_metrics_from_data_array(metrics: List[dict], labels: dict) -> List[dict]:
30+
result = []
31+
for metric in metrics:
32+
result.extend(build_metrics_from_data(metric, labels))
33+
return result
34+
35+
36+
def build_metrics_from_target(target: dict) -> List[dict]:
37+
result = []
38+
project = target["target"]
39+
40+
result.extend(build_metrics_from_data_array(
41+
target["summarised"],
42+
{
43+
"project": project
44+
}
45+
))
46+
47+
for class_item in target["by_class"]:
48+
class_name = class_item["class_name"]
49+
result.extend(build_metrics_from_data_array(
50+
class_item["data"],
51+
{
52+
"project": project,
53+
"class": class_name
54+
}
55+
))
56+
57+
return result
58+
59+
60+
def build_metrics_from_targets(targets: List[dict]) -> List[dict]:
61+
metrics = []
62+
for target in targets:
63+
metrics.extend(build_metrics_from_target(target))
64+
return metrics
65+
66+
67+
def get_args():
68+
parser = argparse.ArgumentParser()
69+
parser.add_argument(
70+
'--stats_file', required=True,
71+
help='files with statistics after insertion metadata', type=str
72+
)
73+
parser.add_argument(
74+
'--output_file', required=True,
75+
help='output file', type=str
76+
)
77+
78+
args = parser.parse_args()
79+
return args
80+
81+
82+
def main():
83+
args = get_args()
84+
stats = load(args.stats_file)
85+
metrics = build_metrics_from_targets(stats["targets"])
86+
with open(args.output_file, "w") as f:
87+
json.dump(metrics, f, indent=4)
88+
89+
90+
if __name__ == "__main__":
91+
main()

monitoring/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import json
2+
from os.path import exists
3+
from typing import Optional
4+
5+
6+
def load(json_file: str) -> Optional[any]:
7+
"""
8+
Try load object from json file
9+
:param json_file: path to json file
10+
:return: object from given json file or None
11+
"""
12+
if exists(json_file):
13+
with open(json_file, "r") as f:
14+
return json.load(f)
15+
return None

0 commit comments

Comments
 (0)