-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNodeManager.h
More file actions
405 lines (309 loc) · 11.9 KB
/
NodeManager.h
File metadata and controls
405 lines (309 loc) · 11.9 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#pragma once
/*
* This file will contain the methods to read and write to the database
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
class NodeManager{
private:
// get the filename we want to operate on
// removes redundancy from all the below methods
string getFileName(int node){
switch (node){
case 1: return "Node1/Projects.csv";
case 2: return "Node2/Tasks.csv";
case 3: return "Node3/Employees.csv";
default:
cerr << "Invalid FIle Access";
return "";
}
}
// helper function for Read and Write to trim whitespace when doing comparison
string trim(const string& str) {
size_t first = str.find_first_not_of(" \t\r\n");
if (first == string::npos) return ""; // no non-whitespace characters
size_t last = str.find_last_not_of(" \t\r\n");
return str.substr(first, (last - first + 1));
}
public:
// This will help us find which row we want to operate on (read/write)
int GetRecord(int ID, int node, string column){
// first get the file we are operating on
string fileName = getFileName(node);
//vif filename is empty then error
if(fileName.empty()){
cerr << "Invalid node";
return -1;
}
// open the file for reading
ifstream inFile(fileName);
// if file does not open then also error
if (!inFile){
cerr << "Error Opening File";
return -1;
}
string line; // string to hold each line of the file
vector<string> headers; // to hold column names (headers)
// we want to read the headers first to know which ID we
// are looking for cause there are multiple ID's
if(getline(inFile, line)){
// create a stringstream to process the line
stringstream ss(line);
string header;
// split the header line by the commas and
// store the column names
while (getline(ss, header, ',')){
headers.push_back(header);
}
}
// then we need to find the index of the passed in column
int colIndex = -1;
for(int i = 0; i < headers.size(); i++){
if(headers[i] == column){
colIndex = i;
break;
}
}
// now we need to search for the row with the specified column ID
int rowNum = 2;
while(getline(inFile, line)){
stringstream ss(line);
vector<string> rowValues;
string value;
while(getline(ss, value, ',')){
rowValues.push_back(value);
}
if(colIndex < rowValues.size() && stoi(rowValues[colIndex]) == ID){
cout << "Row number Found:" << rowNum;
return rowNum;
}
rowNum++;
}
cerr << "row with that ID NOT FOUND";
return -1;
}
// reading from a node/file
string Read(int node){
string filename = getFileName(node);
ifstream inFile(filename);
if(!inFile){
cerr << "error opening file";
return "";
}
int totalRows = 0;
string line;
bool firstRowSkipped = false;
getline(inFile, line);
firstRowSkipped = true;
// get total number of rows
while(getline(inFile, line)){
totalRows++;
}
srand(time(0));
int randomRow = rand() % totalRows + 1;
inFile.clear();
inFile.seekg(0, ios::beg);
getline(inFile, line);
int currentRow = 0;
while(getline(inFile, line)){
currentRow++;
if(currentRow == randomRow){
return line;
}
}
cerr << "Error: Row not Found";
return "";
}
// writing to node/file
string Write(string ID, string column, string newValue, int node) {
// Get the file name for the specified node
string fileName = getFileName(node);
if (fileName.empty()) {
return "Error: Invalid node.";
}
// Open the file for reading
ifstream inFile(fileName);
if (!inFile) {
return "Error: Unable to open file for reading.";
}
// Read all file data into memory to process changes
vector<string> fileData;
string line;
// Read header first
if (getline(inFile, line)) {
fileData.push_back(line); // Add headers back to the fileData
}
vector<string> headers;
stringstream headerStream(line);
string header;
// Parse the headers to find the column index
while (getline(headerStream, header, ',')) {
headers.push_back(trim(header));
}
// print headers
for(int i = 0; i < headers.size(); i ++){
cout << headers[i] << " ";
}
// Find the column index
int colIndex = -1;
for (int i = 0; i < headers.size(); i++) {
if (headers[i] == trim(column)) {
colIndex = i;
break;
}
}
cout << "Column index found: " << colIndex << endl;
if (colIndex == -1) {
return "Error: Column not found.";
}
// Process rows to locate the record
bool recordFound = false;
string oldRow, newRow;
while (getline(inFile, line)) {
stringstream ss(line);
vector<string> rowValues;
string value;
cout << "ID IS: " << ID << "\n";
// Split the row into values based on commas
while (getline(ss, value, ',')) {
rowValues.push_back(value);
}
// Check if the value in the specified column matches the ID
cout << "rowValues[colIndex]: " << trim(rowValues[colIndex]) << "\n";
cout << "ID: " << trim(ID) << "\n";
if (trim(rowValues[colIndex]) == trim(ID)) {
oldRow = line; // Save the old row;
rowValues[colIndex] = newValue; // Update the value in the target column
recordFound = true;
// rebuild the row with the updated value
newRow.clear(); // Clear any previous data
for (int i = 0; i < rowValues.size(); i++) {
newRow += rowValues[i];
if (i != rowValues.size() - 1) {
newRow += ",";
}
}
//cout << "Updated row: " << newRow << endl;
}
// Always add the row (updated or original) to the file data
if (recordFound && oldRow == line) {
fileData.push_back(newRow); // Add updated row
} else {
fileData.push_back(line); // Add original row
}
}
inFile.close();
if (!recordFound) {
return "Error: Record with specified ID not found.";
}
// write updated data back to the file
ofstream outFile(fileName);
if (!outFile) {
return "Error: Unable to open file for writing.";
}
// write all the data including header
for (const string& dataLine : fileData) {
outFile << dataLine << "\n";
}
outFile.close();
cout << "OLD Row: " << oldRow << "\nNEW Row: " << newRow << endl;
return "OLD Row: " + oldRow + "\nNEW Row: " + newRow;
}
//inserting a task into Task Table
string InsertTask(int node){
map<string, string> tasks;
tasks.insert({"code Review", "Review code for errors and performance"});
tasks.insert({"bux Fixes", "identify and resolve issues in software"});
tasks.insert({"Feature Development", "Design and implement features based on requirements"});
tasks.insert({"System Design", "Planning and architecting the structure of the software system"});
tasks.insert({"Unite Testing", "Writing and running tests to ensure individual components work correctly"});
tasks.insert({"Performance Optimization", "Improving the speed and efficiency of the software."});
tasks.insert({"API Integration", " Integrating third-party APIs into the software for extended functionality"});
tasks.insert({"Database Management", "Designing, managing, and optimizing databases for performance."});
tasks.insert({"DevOps Automation", "Automating the deployment and infrastructure management processes"});
tasks.insert({"Security Auditing", "Analyzing and fixing vulnerabilities to ensure software security"});
// Get the file name for the specified node
string fileName = getFileName(node);
if (fileName.empty()) {
return "Error: Invalid node.";
}
// Open the file for appending
ofstream outFile(fileName, ios::app); // Use ios::app to append to the file
if (!outFile) {
return "Error: Unable to open file for writing.";
}
srand(time(0));
size_t mapSize = tasks.size();
size_t randomIndex = rand() % mapSize;
auto it = tasks.begin();
advance(it, randomIndex);
string lineToAdd = to_string(rand()) + ", " + to_string(rand()) + ", " + it->first + ", " + it->second;
// Append the line to the file
outFile << lineToAdd << endl;
// Close the file
outFile.close();
return "Task added successfully: " + lineToAdd + "\n";
}
// deleting a row from a table
string Delete(int node){
string fileName = getFileName(node);
ifstream inFile(fileName);
vector<string> rows;
string line;
while (getline(inFile, line)) {
rows.push_back(line);
}
inFile.close();
// select a random row
srand(time(0));
int randomIndex = rand() % rows.size(); // Random index (0-based)
string deletedRow = rows[randomIndex];
// remove the random row from the vector
rows.erase(rows.begin() + randomIndex);
// Write the updated rows back to the file
std::ofstream outFile(fileName);
if (!outFile) {
std::cerr << "Error writing to file\n";
return "";
}
for (const auto& row : rows) {
outFile << row << "\n";
}
return "Deleted Row: " + deletedRow + "\n";
outFile.close();
cerr << "Error: Row not Found";
return "";
}
// inserting an employee into employee table
string InsertEmployee(int node){
vector<string> firstNames = {"Sarah", "Chris", "James", "Olivia", "Daniel", "Sophia", "Ben", "Mia", "Alex", "Lily"};
vector<string> lastNames = {"Brown", "Williams", "Miller", "Moore", "Clark", "Walker", "Lewis", "Hall", "Allen", "Young"};
vector<string> roles = {"Manager", "Developer", "Engineer", "Software Engineer", "UI Developer"};
string randFirstName = firstNames[rand() % 10];
string randLastName = lastNames[rand() % 10];
string randRole = roles[rand() % 5];
// Get the file name for the specified node
string fileName = getFileName(node);
if (fileName.empty()) {
return "Error: Invalid node.";
}
// Open the file for appending
ofstream outFile(fileName, ios::app); // Use ios::app to append to the file
if (!outFile) {
return "Error: Unable to open file for writing.";
}
string lineToAdd = to_string(rand()) + ", " + randFirstName + ", " + randLastName + ", " + randRole + "\n";
// Append the line to the file
outFile << lineToAdd << endl;
// Close the file
outFile.close();
return "Employee added successfully: " + lineToAdd + "\n";
}
};