-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReport.java
More file actions
95 lines (74 loc) · 3.23 KB
/
Copy pathReport.java
File metadata and controls
95 lines (74 loc) · 3.23 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
package dashboard;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
public class Report {
private static final String FILE_SEPARATOR = System.getProperty("file.separator");
public static void page() {
CodeUtil.clearScreen();
String path = getPath();
File serverStatsFile = new File(path + FILE_SEPARATOR + "dashboard" + FILE_SEPARATOR + "files" + FILE_SEPARATOR + "monthstats.csv");
//Gets data:
ServerStats serverStats = new ServerStats();
serverStats.load(serverStatsFile.getAbsolutePath());
ServerStats[] servers = serverStats.getServers();
if (servers != null && servers.length > 0) {
displayGraph(servers);
} else {
System.out.println("No server statistics available.");
}
System.out.print("\nPress any key to go to the Navigation page...");
waitForUserInput();
Navigation.navbar();
}
private static void waitForUserInput() {
try {
System.in.read();
} catch (IOException e) {
System.out.println("Error while waiting for user input: " + e.getMessage());
}
}
private static String getPath() {
try {
return new File(".").getCanonicalPath();
} catch (IOException e) {
System.out.println("Error: while getting the path of the program.");
return "";
}
}
private static void displayGraph(ServerStats[] servers) {
System.out.println("Server Statistics Bar Graph:\n");
// Find the maximum value for scaling
int maxErrorCount = getMaxValue(servers, ServerStats::getErrorCount);
int maxQueriesPerMonth = getMaxValue(servers, ServerStats::getQueriesPerMonth);
// Display the headers with proper formatting
System.out.printf("%-15s%-15s%-15s%-15s\n", "ServerName", "ErrorCount", "QueriesPerMonth", "Graph");
// Display the data with proper formatting
for (ServerStats server : servers) {
// Calculate the length of the graph bars based on the scaling
int errorCountLength = scaleValue(server.getErrorCount(), maxErrorCount, 15);
int queriesPerMonthLength = scaleValue(server.getQueriesPerMonth(), maxQueriesPerMonth, 15);
// Display the data along with the graph bars
System.out.printf("%-15s%-15s%-15s%-15s\n",
server.getServerName(), server.getErrorCount(), server.getQueriesPerMonth(),
getGraphBar(errorCountLength) + " " + getGraphBar(queriesPerMonthLength));
}
}
private static int getMaxValue(ServerStats[] servers, ServerStatsValueExtractor extractor) {
return Arrays.stream(servers)
.mapToInt(extractor::extract)
.max()
.orElse(0);
}
private static int scaleValue(int value, int maxValue, int maxLength) {
return (int) ((double) value / maxValue * maxLength);
}
//gets asterisks according to data:
private static String getGraphBar(int length) {
return "*".repeat(Math.max(0, length));
}
//used an interface to get stats:
interface ServerStatsValueExtractor {
int extract(ServerStats serverStats);
}
}