-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput.py
More file actions
158 lines (135 loc) · 5.62 KB
/
output.py
File metadata and controls
158 lines (135 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import pathlib
import click
import inquirer
from src.output.DataOutput import *
class HandleInput:
"""
Handles the inputs given.
"""
valid_inputs = False
def __init__(
self, output_type, path, full, detailed, median, normal
) -> None:
"""
Sets all values to the given inputs and checks the values.
:param output_type: type of the output, for example as 'graphs'
:param path: path of the file or directory of the data
:param full: True if full graphs should be generated, False otherwise
:param detailed: True if detailed graphs should be generated, False otherwise
:param median: True if median graphs should be generated, False otherwise
:param normal: True if normal graphs should be generated, False otherwise
"""
self.type = output_type
self.path = path
self.full = full
self.detailed = detailed
self.median = median
self.normal = normal
if self.__check_values():
self.valid_inputs = True
def execute(self, value_types: list):
"""
If inputs are valid, generates the output with the respective class.
:return: True for success, False otherwise
"""
if self.type == "graphs":
output = GraphOutput(
self.path,
value_types,
[self.detailed, self.full],
[self.normal, self.median]
)
output.generate()
def __check_values(self) -> bool:
"""
Checks if the given inputs are in the defined scope. Returns False otherwise.
:return: True for success, False otherwise
"""
if not self.type == "graphs":
print_err(
"Invalid TYPE argument. Has to be graphs."
)
return False
if not os.path.exists(self.path):
print_err(
"Invalid PATH argument. Has to be an existent directory or file."
)
return False
if not (self.detailed or self.full):
print_err(
"Either --detailed, --full or both must be set."
)
return False
if not (self.normal or self.median):
print_err(
"Either --normal, --median or both must be set."
)
return False
return True
@click.command()
@click.option("-f", "--full", help="generate relative graphs from 0 to 100 percent", is_flag=True)
@click.option("-d", "--detailed", help="generate relative graphs in the relevant scope", is_flag=True)
@click.option("-m", "--median", help="generate min-max-median graphs", is_flag=True)
@click.option("-n", "--normal", help="generate normal graphs", is_flag=True)
@click.argument("output_type", type=str)
@click.argument("path", type=pathlib.Path)
def cli(output_type, path, full, detailed, median, normal):
"""
Takes arguments from the command line, asks the user for the values for which graphs should be generated and
calls the InputHandler with the arguments and user inputs.
With output_type, the user can specify which kind of output he wants to generate. The output_type 'graphs' uses
the flags -f, -d, -m and -n to limit the scope of what graphs should be generated. The pairs -f, -d and -n, -m
specify the y-limits and the type of graph, 'normal' graphs with all values or min-max-median graphs with 8
intervals, respectively. At least one option of each pair has to be set, possibly both. All different combinations
that are checked create a graph, for example if flags -f, -d and -m are set, the program generates full and detailed
min-max-median graphs, but no 'normal' graphs.
:param output_type: type of the output, for example as 'graphs'
:param path: path of the file or directory of the data
:param full: True if full graphs should be generated, False otherwise
:param detailed: True if detailed graphs should be generated, False otherwise
:param median: True if median graphs should be generated, False otherwise
:param normal: True if normal graphs should be generated, False otherwise
"""
questions = [
inquirer.Checkbox(
"values",
message="Choose value types to create graphs for",
choices=[
"all",
"CPU usage (%)",
"RAM usage (%)",
"Total received bytes",
"Total sent bytes",
"Received packets per second",
"Sent packets per second",
],
),
]
answers = inquirer.prompt(questions)
if not answers:
print_err("Something went wrong!")
return
all_set = "all" in answers["values"]
cpu_usage = "CPU usage (%)" in answers["values"]
ram_usage = "RAM usage (%)" in answers["values"]
recv_bytes = "Total received bytes" in answers["values"]
sent_bytes = "Total sent bytes" in answers["values"]
recv_pps = "Received packets per second" in answers["values"]
sent_pps = "Sent packets per second" in answers["values"]
value_types = []
if cpu_usage or all_set:
value_types.append(CPUPercent)
if ram_usage or all_set:
value_types.append(RAMPercent)
if recv_bytes or all_set:
value_types.append(RecvBytes)
if sent_bytes or all_set:
value_types.append(SentBytes)
if recv_pps or all_set:
value_types.append(RecvPPS)
if sent_pps or all_set:
value_types.append(SentPPS)
handler = HandleInput(output_type, path, full, detailed, median, normal)
handler.execute(value_types)
if __name__ == "__main__":
cli()