-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
460 lines (388 loc) · 15.2 KB
/
server.js
File metadata and controls
460 lines (388 loc) · 15.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// To run: type 'npm start' in the command or terminal
// Go to http://localhost:3000/ || {host}/feedback
const { networkInterfaces } = require('os');
const { body, validationResult } = require('express-validator');
const { Server } = require('socket.io');
const { createServer } = require('node:http');
const { exec, execSync } = require('child_process');
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const fs = require('fs');
const util = require('util');
const sqlite3 = require('sqlite3').verbose();
const app = express();
const server = createServer(app);
const io = new Server(server);
const PORT = process.env.PORT || 5000;
// Convert exec to Promise-based function
const execPromise = util.promisify(exec);
// Function to check Python and required packages
async function checkPythonDependencies() {
console.log("Checking Python installation and dependencies...");
try {
// Check if Python is installed
try {
execSync('python --version', { stdio: 'pipe' });
console.log("Python is installed.");
} catch (pythonError) {
try {
execSync('py -3 --version', { stdio: 'pipe' });
console.log("Python 3 is installed (using py launcher).");
} catch (py3Error) {
console.error("Error: Python is not installed or not in PATH.");
console.error("Please install Python 3 and try again.");
process.exit(1);
}
}
// Check if required packages are installed
const requiredPackages = ['pandas', 'openpyxl'];
const missingPackages = [];
for (const pkg of requiredPackages) {
try {
// Use a Python command to check if the package is installed
const pythonCmd = process.platform === 'win32' ? 'py -3' : 'python';
execSync(`${pythonCmd} -c "import ${pkg}"`, { stdio: 'pipe' });
console.log(`Package ${pkg} is installed.`);
} catch (error) {
console.log(`Package ${pkg} is missing.`);
missingPackages.push(pkg);
}
}
// If there are missing packages, install them
if (missingPackages.length > 0) {
console.log(`Installing missing packages: ${missingPackages.join(', ')}`);
const pythonCmd = process.platform === 'win32' ? 'py -3' : 'python';
await execPromise(`${pythonCmd} -m pip install ${missingPackages.join(' ')}`);
console.log("All required Python packages are now installed.");
} else {
console.log("All required Python packages are already installed.");
}
return true;
} catch (error) {
console.error("Error checking Python dependencies:", error.message);
process.exit(1);
}
}
// database
let db = new sqlite3.Database('./feedback.db', (err) => {
if (err) {
console.error(err.message);
}
// console.log('Connected to the feedback database.');
});
const createFeedbackTableSql = `
CREATE TABLE IF NOT EXISTS Feedback (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user TEXT NOT NULL,
department TEXT NOT NULL,
rating TEXT NOT NULL,
comment TEXT,
date TEXT NOT NULL
)`;
const alterBadToNeedsImprovementSql = `
UPDATE Feedback
SET rating = 'Needs Improvement'
WHERE rating = 'Bad'
`;
const alterEmployeeToFacultyImprovementSql = `
UPDATE Feedback
SET user = 'faculty'
WHERE user = 'employee'
`;
db.run(createFeedbackTableSql, function (err) {
if (err) {
return console.error('Error creating table:', err.message);
}
// console.log('Table created successfully');
});
db.run(alterBadToNeedsImprovementSql, function (err) {
if (err) {
return console.error('Error altering table bad to needs improvement:', err.message);
}
});
db.run(alterEmployeeToFacultyImprovementSql, function (err) {
if (err) {
return console.error('Error altering table employee to faculty:', err.message);
}
});
function unescapeHTML(str) {
const htmlEntities = {
nbsp: ' ',
cent: '¢',
pound: '£',
yen: '¥',
euro: '€',
copy: '©',
reg: '®',
lt: '<',
gt: '>',
quot: '"',
amp: '&',
apos: '\''
};
str = str.replace(/\&([^;]+);/g, function (entity, entityCode) {
var match;
if (entityCode in htmlEntities) {
return htmlEntities[entityCode];
/*eslint no-cond-assign: 0*/
} else if (match = entityCode.match(/^#x([\da-fA-F]+)$/)) {
return String.fromCharCode(parseInt(match[1], 16));
/*eslint no-cond-assign: 0*/
} else if (match = entityCode.match(/^#(\d+)$/)) {
return String.fromCharCode(~~match[1]);
} else {
return entity;
}
});
return str.replace(/,/g, '[comma]');
};
async function getFeedbackStats(req, res) {
const getRows = () => {
return new Promise((resolve, reject) => {
let sql = `SELECT user,department,rating,comment,date FROM Feedback`;
if (req.query.start && req.query.end) {
sql += ` WHERE date BETWEEN '${req.query.start}' AND '${req.query.end}'`
}
if (req.query.department === 'highschool') {
sql += ` AND department = 'highschool'`;
} else if (req.query.department === 'college') {
sql += ` AND department = 'college'`;
}
sql += " ORDER BY id DESC";
// console.log(sql)
db.all(sql, [], (err, rows) => {
if (err) reject(err);
resolve(rows);
});
});
};
try {
const rows = await getRows();
let badCount = 0;
let averageCount = 0;
let goodCount = 0;
let ntpFeedbackCount = 0;
let facultyFeedbackCount = 0;
let studentFeedbackCount = 0;
let visitorFeedbackCount = 0;
let feedbackItems = [];
rows.forEach((row) => {
switch (row.rating) {
case 'Bad':
badCount++;
break;
case 'Needs Improvement':
badCount++;
break;
case 'Average':
averageCount++;
break;
case 'Good':
goodCount++;
break;
}
const feedbackItem = {
rating: row.rating,
comment: row.comment,
user: row.user
};
if (row.user === "student") {
studentFeedbackCount += 1;
} else if (row.user === "ntp") {
ntpFeedbackCount += 1;
} else if (row.user === "faculty" || row.user === "employee") {
facultyFeedbackCount += 1;
} else if (row.user === "visitor") {
visitorFeedbackCount += 1;
}
feedbackItems.push(feedbackItem)
});
// Set Content-Type to 'application/json' for JSON response
res.setHeader('Content-Type', 'application/json');
// Send JSON response
res.json({
feedback_items: feedbackItems,
ntp_feedback_count: ntpFeedbackCount,
faculty_feedback_count: facultyFeedbackCount,
visitor_feedback_count: visitorFeedbackCount,
student_feedback_count: studentFeedbackCount,
bad_count: badCount, // Needs Improvement or Bad
average_count: averageCount,
good_count: goodCount
});
} catch (err) {
// Set Content-Type to 'application/json' for error response
res.setHeader('Content-Type', 'application/json');
res.status(500).json({ error: 'An error occurred while fetching feedbacks.' });
}
}
app.use(bodyParser.json());
app.use(`/`, express.static(path.join(__dirname, 'public/')));
// Route to handle the form submission (POST request)
app.post('/submit-rating', body('comment').trim().escape(), (req, res) => {
const rating = req.body.rating;
const comment = req.body.comment;
const user = req.body.user;
const department = req.body.department;
const result = validationResult(req);
if (!result.isEmpty()) {
return res.send({ errors: result.array() });
}
// Log the received rating to the console
// console.log(`Received user: ${user}`);
// console.log(`Recieved department: ${department}`);
// console.log(`Received rating: ${rating}`);
// console.log(`Received comment: ${comment}`);
var date = new Date().toISOString();
let sql = `INSERT INTO Feedback(user, department, rating, comment, date) VALUES('${user}', '${department}', '${rating}', '${comment}', '${date.split('T')[0]}')`;
// console.log(sql);
db.run(sql, function (err) {
if (err) {
return console.error('Error inserting feedback:', err.message);
}
// console.log('Feedback inserted successfully');
});
// Send a response back to the client
res.json({ message: `Received user: ${user}\nRecieved department: ${department}\nReceived rating: ${rating}\nReceived comment: ${comment}` });
});
app.get('/getfeedbacks', async (req, res) => {
// Check if the 'Accept' header is set to 'application/json'
const isJsonRequested = req.get('Content-Type') === 'application/json';
// If JSON is requested, handle the JSON response
if (isJsonRequested) {
return getFeedbackStats(req, res);
}
// If JSON is not requested, send the HTML page
return res.sendFile(path.join(__dirname, 'public/getfeedbacks.html'));
});
io.on('connection', (socket) => {
socket.on('submit-rating', (arg) => {
io.emit('submit-rating', true);
});
});
function generateCsvFromSql(params) {
return new Promise((resolve, reject) => {
let sql = `SELECT user,department,rating,comment,date FROM Feedback`;
let conditions = [];
if (params.start && params.end) {
conditions.push(`date BETWEEN '${params.start}' AND '${params.end}'`);
}
if (params.department === 'highschool') {
conditions.push(`department = 'highschool'`);
} else if (params.department === 'college') {
conditions.push(`department = 'college'`);
}
if (conditions.length > 0) {
sql += ` WHERE ${conditions.join(' AND ')}`;
}
db.all(sql, [], (err, rows) => {
if (err) {
return reject(err);
}
const csvRows = ["user,department,rating,comment,date"];
rows.forEach((row) => {
let rowString = `${row.user},${row.department},${row.rating},${unescapeHTML(row.comment)},${row.date}`;
csvRows.push(rowString);
});
const csvData = csvRows.join('\n');
resolve(csvData);
});
});
}
app.get('/download-csv', async (req, res) => {
try {
const csvData = await generateCsvFromSql(req.query);
res.header('Content-Type', 'text/csv');
res.header('Content-Disposition', 'attachment; filename="feedbacks.csv"');
res.send(csvData);
} catch (err) {
console.error('Error generating CSV:', err);
res.status(500).send('Error generating CSV file');
}
});
app.get('/download-excel', async (req, res) => {
const scriptFilePath = path.join(process.cwd(), 'csv_to_xlsx.py');
const csvFilePath = path.join(process.cwd(), 'temp_csv_data.csv');
const excelFilePath = path.join(process.cwd(), 'final_xlsx_data.xlsx');
try {
// Generate CSV data
const csvData = await generateCsvFromSql(req.query);
// Save CSV data to a temporary file
await fs.promises.writeFile(csvFilePath, csvData);
// Execute Python script and wait for it to finish
const pythonCmd = process.platform === 'win32' ? 'py -3' : 'python';
await execPromise(`${pythonCmd} ${scriptFilePath}`);
var date = new Date();
var dateString = new Date(date.getTime() - (date.getTimezoneOffset() * 60000 ))
.toISOString()
.split("T")[0];
// Check if Excel file was created
if (fs.existsSync(excelFilePath)) {
// Send the Excel file to the user
res.header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.header('Content-Disposition', `attachment; filename="feedbacks-${dateString}.xlsx"`);
// Stream the file to the response
const fileStream = fs.createReadStream(excelFilePath);
fileStream.pipe(res);
// Clean up files after response is sent
fileStream.on('end', () => {
cleanupFiles(csvFilePath, excelFilePath);
});
} else {
throw new Error('Excel file was not created');
}
} catch (err) {
console.error('Error generating Excel file:', err);
// Clean up any temporary files
cleanupFiles(csvFilePath, excelFilePath);
res.status(500).send('Error generating Excel file');
}
});
// Helper function to clean up temporary files
function cleanupFiles(csvPath, excelPath) {
// Delete CSV file if it exists
if (fs.existsSync(csvPath)) {
fs.unlink(csvPath, (err) => {
if (err) console.error('Error deleting CSV file:', err);
});
}
// Delete Excel file if it exists
if (fs.existsSync(excelPath)) {
fs.unlink(excelPath, (err) => {
if (err) console.error('Error deleting Excel file:', err);
});
}
}
// 404 handler: keep this at the end, so it only catches requests that don't match any defined routes
app.use((req, res) => {
res.status(404);
res.send(`<h1>Error 404: Resource not found</h1>`);
});
// Check Python dependencies before starting the server
async function startServer() {
try {
await checkPythonDependencies();
console.log("");
// Start server only after dependencies are checked
server.listen(PORT, () => {
console.log("Server is running on these interfaces:");
const nets = networkInterfaces();
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
const familyV4Value = typeof net.family === 'string' ? 'IPv4' : 4
if (net.family === familyV4Value && !net.internal) {
console.log(`${name}: http://${net.address}:${PORT}/`);
console.log(`${name}: http://${net.address}:${PORT}/getfeedbacks`);
}
}
}
console.log("");
});
} catch (error) {
console.error("Failed to start server:", error);
process.exit(1);
}
}
// Start the server with dependency checking
startServer();