-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatusData.java
More file actions
159 lines (128 loc) · 4.84 KB
/
Copy pathStatusData.java
File metadata and controls
159 lines (128 loc) · 4.84 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
159
package dashboard;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class StatusData {
//Variables:
private String serverName;
private String serverAddress;
private String cpuUsage;
private String cycles;
private String io;
private String lastUpdate;
private String serverHealth;
private static final String DELIMITER = ",";
private static final int STATUS_LIMIT = 10;
private StatusData[] statuses;
// Default constructor
public StatusData() {
}
public void create(String dest) {
// Data for the CSV file
String[] headers = {"ServerName", "ServerAddress", "CPUUsage", "Cycles", "IO", "LastUpdate", "ServerHealth"};
String[][] data = {
{"Server A", "192.168.1.101", "30%", "5000", "4GB", "30-10-2023 08:00", "Good"},
{"Server B", "192.168.1.102", "20%", "4000", "8GB", "30-10-2023 08:01", "Fair"},
{"Server C", "192.168.1.103", "40%", "6000", "2GB", "30-10-2023 08:02", "Excellent"},
{"Server D", "192.168.1.104", "25%", "4500", "6GB", "30-10-2023 08:03", "Good"}
};
createCsvFile(dest, headers, data);
System.out.println("statuses.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<StatusData> statusList = new ArrayList<>();
while (sc.hasNextLine() && statusList.size() < STATUS_LIMIT) {
String line = sc.nextLine();
// 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 == 7) {
String serverName = fields[0].trim();
String serverAddress = fields[1].trim();
String cpuUsage = fields[2].trim();
String cycles = fields[3].trim();
String io = fields[4].trim();
String lastUpdate = fields[5].trim();
String serverHealth = fields[6].trim();
statusList.add(new StatusData(serverName, serverAddress, cpuUsage, cycles, io, lastUpdate, serverHealth));
} else {
System.out.println("Error: Missing tokens in the input file. Skipping the current line.");
}
}
statuses = statusList.toArray(new StatusData[0]);
System.out.println("\nNo. of statuses: " + statusList.size());
sc.close();
} catch (FileNotFoundException e) {
System.out.println("Error: File not found.");
System.out.println("Creating file...");
create(source);
load(source);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
//Constructors:
public StatusData(String serverName, String serverAddress, String cpuUsage, String cycles, String io, String lastUpdate, String serverHealth) {
this.serverName = serverName;
this.serverAddress = serverAddress;
this.cpuUsage = cpuUsage;
this.cycles = cycles;
this.io = io;
this.lastUpdate = lastUpdate;
this.serverHealth = serverHealth;
}
public StatusData[] getStatuses() {
return statuses;
}
public String getServerName() {
return serverName;
}
public String getServerAddress() {
return serverAddress;
}
public String getCpuUsage() {
return cpuUsage;
}
public String getCycles() {
return cycles;
}
public String getIo() {
return io;
}
public String getLastUpdate() {
return lastUpdate;
}
public String getServerHealth() {
return serverHealth;
}
}