-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
160 lines (134 loc) · 4.41 KB
/
index.js
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
//--------------------------------Libraries---------------------------------------
const express = require("express");
const multer = require("multer");
const path = require("path");
const fs = require("fs");
const cors = require("cors");
const bodyParser = require("body-parser");
//-------------spawning child process to run the python script-------------------
const { spawn } = require("child_process");
//------------------setting up app and port--------------------------------------
const port = 5173;
const app = express();
app.use(express.static(__dirname));
app.use(cors());
app.use(bodyParser.json());
//-----------------------Multer for storing the uploads---------------------------
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, path.join(__dirname, "uploads"));
},
filename: (req, file, cb) => {
cb(null, file.originalname);
},
});
const upload = multer({ storage: storage });
//------------------------resetting middleware----------------------------
function totalReset(req, res, next) {
//to delete uploaded file
const fileDirectoryPath = path.join(__dirname, "uploads");
fs.readdir(fileDirectoryPath, (err, files) => {
if (err) {
console.error(`Error reading directory: ${err}`);
return;
}
for (let i = 0; i < files.length; i++) {
const filePath = path.join(fileDirectoryPath, files[i]);
if (files[i] == ".gitkeep") {
continue;
}
// Deleting the file
fs.unlink(filePath, (err) => {
if (err) {
console.error(`Error deleting file ${files[i]}: ${err}`);
} else {
console.log(`File ${files[i]} deleted successfully`);
}
});
}
});
//to delete generated graphs
const graphDirectoryPath = path.join(__dirname, "graphs");
fs.readdir(graphDirectoryPath, (err, files) => {
if (err) {
console.error(`Error reading directory: ${err}`);
return;
}
for (let i = 0; i < files.length; i++) {
const filePath = path.join(graphDirectoryPath, files[i]);
if (files[i] == ".gitkeep") {
continue;
}
// Deleting the file
fs.unlink(filePath, (err) => {
if (err) {
console.error(`Error deleting file ${files[i]}: ${err}`);
} else {
console.log(`File ${files[i]} deleted successfully`);
}
});
}
});
next();
}
//------------------------setting up endpoints-----------------------------
app.get("/xtract", totalReset, (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
});
app.post("/upload", upload.single("csvFile"), (req, res) => {
console.log("file uploaded successfullly!");
res.send("File uploaded successfully!");
});
app.get("/reset", totalReset);
//------------------------PYTHON SCRIPT ------------------------
// Function to handle communication with Python script
function runPythonScript(file, args, callback) {
const pythonProcess = spawn("python", [file, ...args]);
let pyData;
pythonProcess.stdout.on("data", (data) => {
pyData = data;
//if you want to check the output in the logs
//console.log(`Python script output: ${pyData}`);
});
pythonProcess.stderr.on("data", (data) => {
pyData = data;
console.error(`Error from Python script: ${pyData}`);
});
pythonProcess.on("close", (code) => {
console.log(`Python script exited with code ${code}`);
// Invoke the callback with the collected data
callback(pyData);
});
}
//--------------------setting up get endpoint for retrieving the statistical data from the python script
app.get("/getData", (req, res) => {
const attribute = req.query.attribute;
const args = [attribute];
runPythonScript("analysis.py", args, (pyData) => {
res.json({
output: pyData,
});
});
});
//--------------------setting up get endpoint for retrieving the graphs from the python script
app.post("/getGraph", (req, res) => {
const selectedGraph = req.query.graph;
const x = req.body.x;
const y = req.body.y;
const args = [selectedGraph, x, y];
runPythonScript("graph.py", args, (pyData) => {
res.json({
path: pyData,
});
});
});
app.get("/getColumns", (req, res) => {
const args = [];
runPythonScript("columns.py", args, (pyData) => {
res.send(pyData);
});
});
//-------------------keeping the server alive and running---------------
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}/xtract`);
});