Skip to content

Commit 22ecbfc

Browse files
author
Rustam Sadykov
committed
provide comments for scripts
1 parent d53e7aa commit 22ecbfc

3 files changed

Lines changed: 95 additions & 16 deletions

File tree

monitoring/build_aggregated_data.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,31 @@
33
from os import listdir
44
from os.path import isfile, join
55
from time import time
6+
from typing import Iterator
67

78
from monitoring_settings import JSON_VERSION
89
from utils import *
910

1011

11-
def get_file_seq(input_data_dir):
12+
def get_file_seq(input_data_dir: str) -> Iterator[str]:
13+
"""
14+
Get all files from specified directory
15+
:param input_data_dir: path to directory with files
16+
:return: sequence of filepaths
17+
"""
1218
for filename in listdir(input_data_dir):
1319
path = join(input_data_dir, filename)
1420
if isfile(path):
1521
yield path
1622

1723

18-
def check_stats(stats, args):
24+
def check_stats(stats: dict, args: argparse.Namespace) -> bool:
25+
"""
26+
Checks timestamp and version of given statistics
27+
:param stats: dictionary with statistics and metadata
28+
:param args: parsed program arguments
29+
:return: is timestamp and version match
30+
"""
1931
try:
2032
timestamp = stats["metadata"]["timestamp"]
2133
timestamp_match = args.timestamp_from <= timestamp <= args.timestamp_to
@@ -25,15 +37,25 @@ def check_stats(stats, args):
2537
return False
2638

2739

28-
def get_stats_seq(args):
40+
def get_stats_seq(args: argparse.Namespace) -> Iterator[dict]:
41+
"""
42+
Get statistics with metadata matched specified period
43+
:param args: parsed program arguments
44+
:return: sequence of statistics with metadata filtered by version and timestamp
45+
"""
2946
for file in get_file_seq(args.input_data_dir):
3047
with open(file, "r") as f:
3148
stats = json.load(f)
3249
if check_stats(stats, args):
3350
yield stats
3451

3552

36-
def transform_target_stats(stats):
53+
def transform_target_stats(stats: dict) -> dict:
54+
"""
55+
Transform metrics by computing total coverage
56+
:param stats: metrics
57+
:return: transformed metrics
58+
"""
3759
common_prefix = "covered_instructions"
3860
denum = stats["total_instructions"]
3961

@@ -49,7 +71,12 @@ def transform_target_stats(stats):
4971
return stats
5072

5173

52-
def aggregate_stats(stats_seq):
74+
def aggregate_stats(stats_seq: Iterator[dict]) -> List[dict]:
75+
"""
76+
Aggregate list of metrics and parameters into list of transformed metrics and parameters grouped by targets
77+
:param stats_seq: sequence of metrics and parameters
78+
:return: list of metrics and parameters grouped by targets
79+
"""
5380
result = get_default_metrics_dict()
5481

5582
for stats in stats_seq:

monitoring/insert_metadata.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,43 @@
66
from os.path import exists
77
from platform import uname
88
from time import time
9+
from typing import Optional
910

1011
from monitoring_settings import JSON_VERSION
1112
from utils import *
1213

1314

14-
def load(json_file):
15+
def load(json_file: str) -> Optional[any]:
16+
"""
17+
Try load object from json file
18+
:param json_file: path to json file
19+
:return: object from given json file or None
20+
"""
1521
if exists(json_file):
1622
with open(json_file, "r") as f:
1723
return json.load(f)
1824
return None
1925

2026

21-
def try_get_output(args):
27+
def try_get_output(args: str) -> Optional[str]:
28+
"""
29+
Try to run subprocess with specified arguments
30+
:param args: arguments for execution
31+
:return: result output of execution or None
32+
"""
2233
try:
2334
return subprocess.check_output(args, stderr=subprocess.STDOUT, shell=True).decode()
2435
except Exception as e:
2536
print(f'Error in command "{args}":\n\t{e}')
2637
return None
2738

2839

29-
def parse_gradle_version(s):
40+
def parse_gradle_version(s: str) -> Optional[str]:
41+
"""
42+
Parse gradle version from given string
43+
:param s: execution result of gradle --version
44+
:return: parsed gradle version or None
45+
"""
3046
if s is None:
3147
return None
3248
regex = re.compile(r'^\s*(Gradle [.\d]+)\s*$', re.MULTILINE)
@@ -36,7 +52,11 @@ def parse_gradle_version(s):
3652
return result.group(1)
3753

3854

39-
def build_environment_data():
55+
def build_environment_data() -> dict:
56+
"""
57+
Collect environment data from host
58+
:return: dictionary with environment data
59+
"""
4060
uname_result = uname()
4161
environment = {
4262
'host': uname_result.node,
@@ -50,7 +70,12 @@ def build_environment_data():
5070
return environment
5171

5272

53-
def build_metadata(args):
73+
def build_metadata(args: argparse.Namespace) -> dict:
74+
"""
75+
Collect metadata into dictionary
76+
:param args: parsed program arguments
77+
:return: dictionary with metadata
78+
"""
5479
metadata = {
5580
'source': {
5681
'type': args.source_type,
@@ -66,7 +91,12 @@ def build_metadata(args):
6691
return metadata
6792

6893

69-
def build_targets(stats_array):
94+
def build_targets(stats_array: List[dict]) -> List[dict]:
95+
"""
96+
Collect and group statistics by target
97+
:param stats_array: list of dictionaries with parameters and metrics
98+
:return: list of metrics and parameters grouped by target
99+
"""
70100
result = get_default_metrics_dict()
71101
for stats in stats_array:
72102
target = stats['parameters']['target']
@@ -76,7 +106,12 @@ def build_targets(stats_array):
76106
return postprocess_targets(result)
77107

78108

79-
def insert_metadata(args):
109+
def insert_metadata(args: argparse.Namespace) -> dict:
110+
"""
111+
Collect metadata and statistics from specified file and merge them into result
112+
:param args: parsed program arguments
113+
:return: dictionary with statistics and metadata
114+
"""
80115
stats_array = load(args.stats_file)
81116
if stats_array is None:
82117
raise FileNotFoundError("File with stats does not exist!")

monitoring/utils.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import re
22
from collections import defaultdict
3+
from typing import List
34

45
from monitoring_settings import DEFAULT_PROJECT_VERSION
56

67

7-
def parse_name_and_version(full_name):
8+
def parse_name_and_version(full_name: str) -> tuple[str, str]:
9+
"""
10+
Parse from string name and version of project
11+
:param full_name: string with format <name>-<version>
12+
:return: pair of name and version strings
13+
"""
814
regex = re.compile(r'([^.]+)-([\d.]+)')
915
result = regex.fullmatch(full_name)
1016
if result is None:
@@ -14,7 +20,12 @@ def parse_name_and_version(full_name):
1420
return name, version
1521

1622

17-
def postprocess_targets(targets):
23+
def postprocess_targets(targets: dict) -> List[dict]:
24+
"""
25+
Transform dictionary with fullname target keys into array with target objects
26+
:param targets: dictionary with fullname target keys
27+
:return: array of targets
28+
"""
1829
result = []
1930
for target in targets:
2031
(name, version) = parse_name_and_version(target)
@@ -26,14 +37,20 @@ def postprocess_targets(targets):
2637
return result
2738

2839

29-
def get_default_metrics_dict():
40+
def get_default_metrics_dict() -> dict:
3041
return defaultdict(lambda: {
3142
'parameters': [],
3243
'metrics': []
3344
})
3445

3546

36-
def update_target(target, stats):
47+
def update_target(target: dict, stats: dict) -> dict:
48+
"""
49+
Update dictionary with target by new stats
50+
:param target: dictionary with target metrics and parameters
51+
:param stats: new metrics and parameters
52+
:return: updated target dictionary
53+
"""
3754
target['parameters'].append(stats['parameters'])
3855
target['metrics'].append(stats['metrics'])
3956
return target

0 commit comments

Comments
 (0)