-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
221 lines (180 loc) · 7.47 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
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
const express = require('express');
const app = express();
const fs = require('fs');
const path = require('path');
const multer = require('multer');
const cron = require('node-cron');
const socketServer = require('./socketServer');
const createNamespace = socketServer.createNamespace;
// Setting up express config
app.use(express.static(__dirname + '/public/'));
app.use('/static', express.static(path.join(__dirname, '/public/')))
app.set('views', path.join(__dirname, '/views/'));
app.set('view engine', 'ejs');
app.use(express.json());
app.use(express.urlencoded({extended: false}));
var upload = multer({dest:'tmp/'});
process.env.PORT = process.env.PORT ? process.env.PORT : 8080;
server = app.listen(process.env.PORT,()=>{
console.log(`[INFO] listening at port ${process.env.PORT}`);
});
// Setting up socket io
const io = require('socket.io')(server);
console.log("[INFO] Initializing needed files....");
// Getting current problems dynamically, as they are stored in public folder
const getProblemsList = () => {
const problemsListPath = path.join(__dirname, `/public/`);
const problemsList = fs.readdirSync(problemsListPath)
return problemsList;
}
const configDict = {}
const resultDict = {}
const problemsList = getProblemsList();
// Initial creation of configDict and resultDict which will be updated
// Update configDict when POST route hit
// Update resultDict when processingDone received
// Writing to file performed asynchronously, through chron jobs
// For config check if new key added to configDict
// For resullt for now directly write every result, later can put a flush flag or change flag
// Thus, file operations done only while initialisation and in chron jobs
// Eliminating complex thread handling issue
// TODO: Research how databases can handle mutation to same rows concurrently, like the result for a problem
problemsList.forEach((problem) => {
if(fs.lstatSync(path.join(__dirname, `/public/${problem}`)).isDirectory()){
const configPath = path.join(__dirname, `/public/${problem}/config.json`);
const resultPath = path.join(__dirname, `/public/${problem}/results.json`);
const configJson = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
if(!fs.existsSync(resultPath)){
const numSegments = Math.ceil((configJson.end - configJson.start)/configJson.step);
const answers = new Array();
for(let i=0; i<numSegments; i++){
answers.push(new Array());
}
const clients = new Object();
const data = JSON.stringify({clients, answers});
fs.writeFileSync(resultPath, data);
resultDict[configJson.id] = {clients, answers};
configDict[configJson.id] = configJson;
}
else{
const resultJson = JSON.parse(fs.readFileSync(resultPath, 'utf-8'));
var flag = true;
for(let i=0; i<resultJson.answers.length; i++){
if(Array.isArray(resultJson.answers[i]) && resultJson.answers[i].length == 0){
flag = false;
break;
}
}
if(!flag){
resultDict[configJson.id] = resultJson;
configDict[configJson.id] = configJson;
createNamespace(io, resultDict, configJson, configDict);
}
}
}
})
console.log("[INFO] All files created, can start now...");
// When root location hit, display all current problems available dynamically
// All current problems fetched and data extracted from config.json
// Render the main view with this dynamic data
app.get("/", (_req,res) => {
// calculate number of total steps and completed ones
const resTotal = [];
const resDone = [];
Object.keys(resultDict).forEach((prb) => {
resTotal.push(resultDict[prb].answers.length);
let temp = 0;
resultDict[prb].answers.forEach((res) => {
if(res.length > 0){
temp += 1;
}
})
resDone.push(temp);
})
// get all files config
res.render("main", {config: Object.values(configDict), resTotal, resDone});
})
// Setting up routes for default requests, so that they do not hamper process
app.get("/favicon.ico", (_req,res) => res.status(204));
app.get("/robots.txt", (_req,res) => res.status(204));
app.get("/addFile", (req,res) => {
res.render("fileupload");
})
app.post("/addFile", upload.fields([{
name: 'workerFile', maxCount: 1
}, {
name: 'dataFile', maxCount: 1
}]),(req,res) => {
let config = new Object();
config['email'] = req.body.email;
config['seriesId'] = req.body.seriesId;
config['id'] = req.body.seriesId;
config['name'] = req.body.name;
config['description'] = req.body.description;
config['reward'] = parseInt(req.body.reward);
config['start'] = parseInt(req.body.start);
config['end'] = parseInt(req.body.end);
config['step'] = parseInt(req.body.step);
config['maxTime'] = parseInt(req.body.maxTime);
config['workerURL'] = req.files['workerFile'][0].originalname;
config['readFile'] = req.files['dataFile'] ? req.files['dataFile'][0].originalname : null;
fs.mkdirSync(__dirname+"/public/"+req.body.seriesId);
fs.rename(__dirname+"/"+req.files.workerFile[0].path,__dirname+"/public/"+req.body.seriesId+"/worker.js",()=>{
console.log("[INFO] Renamed worker file");
})
// Check whether dataFile is present, only then create
if("dataFile" in req.files){
fs.rename(__dirname+"/"+req.files.dataFile[0].path,__dirname+"/public/"+req.body.seriesId+"/"+req.files.dataFile[0].originalname,()=>{
console.log("[INFO] Renamed data file");
config['readFile'] = req.files.dataFile[0].originalname;
})
}
else{
config['readFile'] = null;
}
var configString = JSON.stringify(config);
fs.writeFileSync(__dirname+"/public/"+req.body.seriesId+"/config.json",configString,(err)=>{
if(err) {
console.log("[DEBUG] Write error", err);
}
});
// Updating global structures and creating result.json
const resultPath = path.join(__dirname, `/public/${config.id}/results.json`)
const numSegments = Math.ceil((config.end - config.start)/config.step);
const answers = new Array();
for(let i=0; i<numSegments; i++){
answers.push(new Array());
}
const clients = new Object();
const data = JSON.stringify({clients, answers});
fs.writeFileSync(resultPath, data);
configDict[config.id] = config;
resultDict[config.id] = {clients, answers};
createNamespace(io, resultDict, config, configDict);
res.redirect("/");
})
app.get("/landing",(req,res)=>{
res.render("home")
})
// Finally executing some namespace, we are starting that problem
// Get config file and setup namespace for that problem
// Render the worker view, with config file
app.get("/:namespace", (req,res) => {
const namespace = req.params.namespace;
if(configDict.hasOwnProperty(namespace)){
const configJson = configDict[namespace];
res.render("worker",{config: configJson});
}
else{
res.render("error");
}
})
cron.schedule('*/5 * * * *', () => {
Object.keys(resultDict).forEach((key) => {
const res = JSON.stringify(resultDict[key]);
fs.writeFile( path.join(__dirname, `/public/${key}/results.json`), res, 'utf-8', () => console.log(`Results updated to file for ${key}`));
})
})
module.exports = {
getProblemsList
};