-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerStats.java
More file actions
142 lines (111 loc) · 4.2 KB
/
Copy pathServerStats.java
File metadata and controls
142 lines (111 loc) · 4.2 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
package dashboard;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class ServerStats {
private String serverName;
private int errorCount;
private int queriesPerMonth;
private static final String DELIMITER = ",";
private static final int SERVER_LIMIT = 10;
private ServerStats[] servers;
public void create(String dest) {
// Data for the CSV file
String[] headers = {"ServerName", "ErrorCount", "QueriesPerMonth"};
String[][] data = {
{"Server A", "2", "1500"},
{"Server B", "2", "2000"},
{"Server C", "1", "1000"},
{"Server D", "3", "1800"}
};
createCsvFile(dest, headers, data);
System.out.println("monthstats.csv file created successfully.");
}
private static void createCsvFile(String fileName, String[] headers, String[][] data) {
try (FileWriter writer = new FileWriter(fileName)) {
// Write headers to the CSV file
for (String header : headers) {
writer.append(header).append(",");
}
writer.append("\n");
// Write data to the CSV file
for (String[] row : data) {
for (String value : row) {
writer.append(value).append(",");
}
writer.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void load(String source) {
File sourceFile = new File(source);
try {
Scanner sc = new Scanner(sourceFile);
sc.useDelimiter(DELIMITER);
// Skip the header line
if (sc.hasNextLine()) {
sc.nextLine();
}
ArrayList<ServerStats> serverList = new ArrayList<>();
while (sc.hasNextLine() && serverList.size() < SERVER_LIMIT) {
String line = sc.nextLine();
// System.out.println("Line read: " + line); // Debug print
// Split the line into individual fields using ","
String[] fields = line.split(DELIMITER);
// Check if the line has the expected number of fields
if (fields.length == 3) {
String serverName = fields[0].trim();
int errorCount = Integer.parseInt(fields[1].trim());
int queriesPerMonth = Integer.parseInt(fields[2].trim());
serverList.add(new ServerStats(serverName, errorCount, queriesPerMonth));
} else {
System.out.println("Error: Missing tokens in the input file. Skipping the current line.");
}
}
servers = serverList.toArray(new ServerStats[0]);
System.out.println("\nNo. of servers: " + serverList.size());
sc.close();
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
e.printStackTrace();
}
}
public ServerStats(String serverName, int errorCount, int queriesPerMonth) {
this.serverName = serverName;
this.errorCount = errorCount;
this.queriesPerMonth = queriesPerMonth;
}
//appropriate constructors:
public ServerStats() {
}
public ServerStats[] getServers() {
return servers;
}
public String getServerName() {
return serverName;
}
public int getErrorCount() {
return errorCount;
}
public int getQueriesPerMonth() {
return queriesPerMonth;
}
//displays graph using "|" and "*":
public static void displayBarGraph(ServerStats[] servers) {
System.out.println("Server Statistics Bar Graph:\n");
for (ServerStats server : servers) {
System.out.printf("%-15s | %s\n", server.getServerName(), generateBar(server.getErrorCount()));
}
}
private static String generateBar(int count) {
StringBuilder bar = new StringBuilder();
for (int i = 0; i < count; i++) {
bar.append("*");
}
return bar.toString();
}
}