|
1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
|
|
2
|
|
|
# -*- coding: future_fstrings -*- |
|
3
|
|
|
|
|
4
|
|
|
import json |
|
5
|
|
|
import os |
|
6
|
|
|
|
|
7
|
|
|
from lighthouse_garden.utility import output, system |
|
8
|
|
|
from lighthouse_garden.lighthouse import utility |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
def get_data(target, file_suffix=''): |
|
12
|
|
|
""" |
|
13
|
|
|
Get json data file by target |
|
14
|
|
|
:param target: Dict |
|
15
|
|
|
:param file_suffix: String Additional suffix |
|
16
|
|
|
:return: Dict |
|
17
|
|
|
""" |
|
18
|
|
|
data_file = f'{utility.get_data_dir()}_{target["identifier"]}{file_suffix}.json' |
|
19
|
|
|
if os.path.isfile(data_file): |
|
20
|
|
|
with open(data_file, 'r') as read_file: |
|
21
|
|
|
return json.load(read_file) |
|
22
|
|
|
else: |
|
23
|
|
|
return [] |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
def set_data(target, data, file_suffix=''): |
|
27
|
|
|
""" |
|
28
|
|
|
Write json data by target |
|
29
|
|
|
:param target: Dict |
|
30
|
|
|
:param data: Dict |
|
31
|
|
|
:param file_suffix: String Additional suffix |
|
32
|
|
|
:return: |
|
33
|
|
|
""" |
|
34
|
|
|
data_file = f'{utility.get_data_dir()}_{target["identifier"]}{file_suffix}.json' |
|
35
|
|
|
with open(data_file, 'w') as write_file: |
|
36
|
|
|
json.dump(data, write_file) |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
def add_value_to_history(target, result): |
|
40
|
|
|
""" |
|
41
|
|
|
Adding a new result value to the history data of a specific target |
|
42
|
|
|
:param target: Dict |
|
43
|
|
|
:param result: Dict |
|
44
|
|
|
:return: |
|
45
|
|
|
""" |
|
46
|
|
|
_data = get_data(target, '.history') |
|
47
|
|
|
while len(_data) > system.config['keep_history']: |
|
48
|
|
|
output.println(f'{output.Subject.INFO} Cleaning history value', verbose_only=True) |
|
49
|
|
|
utility.remove_file(f'{utility.get_export_dir()}{_data[0]["report"]}') |
|
50
|
|
|
del _data[0] |
|
51
|
|
|
_data.append(result) |
|
52
|
|
|
set_data(target, _data, '.history') |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
def get_average_by_attribute(target, attribute): |
|
56
|
|
|
""" |
|
57
|
|
|
Calculates the average value from a specific attribute by all available history values |
|
58
|
|
|
:param target: Dict |
|
59
|
|
|
:param attribute: String |
|
60
|
|
|
:return: Float |
|
61
|
|
|
""" |
|
62
|
|
|
history_data = get_history_by_attribute(target, attribute) |
|
63
|
|
|
return sum(history_data) / float(len(history_data)) |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
def get_average_peak(target, attribute, min_max=True): |
|
67
|
|
|
""" |
|
68
|
|
|
Get the min or max value of a specific attribute by all available history values |
|
69
|
|
|
:param target: |
|
70
|
|
|
:param attribute: |
|
71
|
|
|
:param min_max: |
|
72
|
|
|
:return: |
|
73
|
|
|
""" |
|
74
|
|
|
history_data = get_history_by_attribute(target, attribute) |
|
75
|
|
|
|
|
76
|
|
|
if min_max: |
|
|
|
|
|
|
77
|
|
|
return min(history_data) |
|
78
|
|
|
else: |
|
79
|
|
|
return max(history_data) |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
def get_history_by_attribute(target, attribute): |
|
83
|
|
|
""" |
|
84
|
|
|
Fetch all history values of a specific attribute |
|
85
|
|
|
:param target: Dict |
|
86
|
|
|
:param attribute: String |
|
87
|
|
|
:return: |
|
88
|
|
|
""" |
|
89
|
|
|
history_data = [] |
|
90
|
|
|
for history in get_data(target, '.history'): |
|
91
|
|
|
history_data.append(history[attribute]) |
|
92
|
|
|
return history_data |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
def get_target_by_attribute(value, attribute): |
|
96
|
|
|
""" |
|
97
|
|
|
Return a target by a specific attribute |
|
98
|
|
|
:param value: String |
|
99
|
|
|
:param attribute: String |
|
100
|
|
|
:return: |
|
101
|
|
|
""" |
|
102
|
|
|
for target in system.config['targets']: |
|
103
|
|
|
if target[attribute] == value: |
|
104
|
|
|
return target |
|
105
|
|
|
return None |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
def get_last_results(): |
|
109
|
|
|
""" |
|
110
|
|
|
Get last results of all targets |
|
111
|
|
|
:return: Dict |
|
112
|
|
|
""" |
|
113
|
|
|
results = [] |
|
114
|
|
|
for target in system.config['targets']: |
|
115
|
|
|
_result = get_data(target) |
|
116
|
|
|
if _result: |
|
117
|
|
|
results.append(_result) |
|
118
|
|
|
|
|
119
|
|
|
# sort by performance |
|
120
|
|
|
results.sort(key=lambda x: x['performance'], reverse=True) |
|
121
|
|
|
|
|
122
|
|
|
return results |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
def get_last_value(target, attribute): |
|
126
|
|
|
""" |
|
127
|
|
|
Get the last value of a specific attribute |
|
128
|
|
|
:param target: Dict |
|
129
|
|
|
:param attribute: String |
|
130
|
|
|
:return: |
|
131
|
|
|
""" |
|
132
|
|
|
_result = get_data(target, '.history') |
|
133
|
|
|
return _result[-1][attribute] |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
def sort_by_average(results): |
|
137
|
|
|
""" |
|
138
|
|
|
Sort the dict by the average value |
|
139
|
|
|
:param results: Dict |
|
140
|
|
|
:return: Dict |
|
141
|
|
|
""" |
|
142
|
|
|
# sort by average |
|
143
|
|
|
results.sort(key=lambda x: x['average']['value'], reverse=True) |
|
144
|
|
|
return results |
|
145
|
|
|
|