Skip to content

Commit ecbb98d

Browse files
SBOne-KenobiRustam Sadykov
authored andcommitted
add metadata insertion script
1 parent 29df60b commit ecbb98d

File tree

2 files changed

+100
-139
lines changed

2 files changed

+100
-139
lines changed

monitoring/draw_stats_graphs.py

Lines changed: 0 additions & 139 deletions
This file was deleted.

monitoring/insert_metadata.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import json
2+
import subprocess
3+
from collections import defaultdict
4+
from os import environ
5+
from os.path import exists
6+
from platform import uname
7+
from sys import argv
8+
9+
10+
def load(json_file):
11+
if exists(json_file):
12+
with open(json_file, "r") as f:
13+
return json.load(f)
14+
return None
15+
16+
17+
def transform_stats(stats):
18+
common_prefix = "covered_instructions_count"
19+
denum = stats["total_instructions_count"]
20+
21+
nums_keys = [(key, key.removeprefix(common_prefix)) for key in stats.keys() if key.startswith(common_prefix)]
22+
23+
for (key, by) in nums_keys:
24+
num = stats[key]
25+
stats["total_coverage" + by] = 100 * num / denum if denum != 0 else 0
26+
del stats[key]
27+
28+
del stats["total_instructions_count"]
29+
30+
return stats
31+
32+
33+
def transform_and_combine_stats(stats_list):
34+
new_stats = defaultdict(lambda: 0.0)
35+
36+
# calculate average by all keys
37+
for n, stats in enumerate(stats_list, start=1):
38+
transformed = transform_stats(stats)
39+
for key in transformed:
40+
new_stats[key] = new_stats[key] + (transformed[key] - new_stats[key]) / n
41+
42+
return new_stats
43+
44+
45+
def try_get_output(*args):
46+
try:
47+
return subprocess.check_output(args, stderr=subprocess.STDOUT).decode()
48+
except Exception as e:
49+
print(f'Error in command "{" ".join(args)}":\n\t{e}')
50+
return None
51+
52+
53+
def build_environment_data():
54+
uname_result = uname()
55+
environment = {
56+
'host': uname_result.node,
57+
'OS': f'{uname_result.system} version {uname_result.version}',
58+
'java_version': try_get_output('java', '-version'),
59+
'gradle_version': try_get_output('gradle', '--version'),
60+
'JAVA_HOME': environ.get('JAVA_HOME'),
61+
'KOTLIN_HOME': environ.get('KOTLIN_HOME'),
62+
'PATH': environ.get('PATH'),
63+
}
64+
return environment
65+
66+
67+
def build_metadata(commit, build):
68+
metadata = {
69+
'commit_hash': commit,
70+
'build_number': build,
71+
'environment': build_environment_data()
72+
}
73+
return metadata
74+
75+
76+
def transform_and_insert_metadata(stats_file, commit, build):
77+
stats = load(stats_file)
78+
if stats is None:
79+
raise FileNotFoundError("File with stats does not exist!")
80+
stats = transform_and_combine_stats(stats)
81+
stats['metadata'] = build_metadata(commit, build)
82+
return stats
83+
84+
85+
def main():
86+
args = argv[1:]
87+
if len(args) != 4:
88+
raise RuntimeError(
89+
f"Expected <stats file> <output file> "
90+
f"<commit hash> <build number> "
91+
f"but got {' '.join(args)}"
92+
)
93+
(stats_file, output_file, commit, build) = args
94+
stats = transform_and_insert_metadata(stats_file, commit, build)
95+
with open(output_file, "w") as f:
96+
json.dump(stats, f, indent=4)
97+
98+
99+
if __name__ == "__main__":
100+
main()

0 commit comments

Comments
 (0)