-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
258 lines (208 loc) · 8.31 KB
/
Copy pathserver.js
File metadata and controls
258 lines (208 loc) · 8.31 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
// require includes the packages that were installed with npm
var path = require('path');
const fs = require('fs');
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
//const _ = require('lodash');
/* =========== */
/* GLOBAL VARS */
/* =========== */
global.root_dir = __dirname;
global.collections_dir = global.root_dir + '/data/collections-data/';
global.vizualizations_dir = global.root_dir + '/data/viz-data';
global.json_dir = global.root_dir + '/data/collections-json/';
global.supported_ext = ['.rdf', '.xml'];
global.start_date = null;
global.port = 8000;
const { DataFile, FileCollection } = require(global.root_dir + '/scripts/parsing.js');
/* ============== */
/* EXPRESS CONFIG */
/* ============== */
var app = express();
app.use('/', express.static(global.root_dir + '/public'));
// OTHER MIDDLEWARE
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.enable('trust proxy');
// ejs
app.set('view engine', 'ejs');
app.get('/', function (req, res) {
res.render('pages/index');
});
app.get('/pull-parse-data', function (req, res) {
console.log(`Pulling files from all the collections and parsing them...`);
try {
// parse the directory of collections of files
let collections_dir = fs.readdirSync(global.collections_dir);
//let collectionsFiles = [];
let collections_filesnames = [];
var count_dir = 0;
// cycle ds directory
collections_dir.forEach(path_dir => {
// get current sub directory
const full_path = path.join(global.collections_dir, path_dir);
// check if it's a directory
if (!(fs.statSync(full_path).isFile())) {
// parse the directory to get files
const collection = fs.readdirSync(full_path);
// creating file collection object
const file_collection = new FileCollection(count_dir);
// parsing the collection of files
collection.forEach(function (file) {
file_path = path.join(full_path, file);
file_content = fs.readFileSync(file_path, 'utf-8');
file_format = path.extname(file_path);
const data_file = new DataFile(file, file_content, file_format);
data_file.parse_file();
file_collection.push_DataFile(data_file);
});
// saving all the data about the collection in a json file
const json_string = JSON.stringify(file_collection, null, "\t");
fs.writeFile(global.json_dir + path_dir + ".json", json_string, (err) => {
if (err) {
console.error('Error writing ' + path_dir + ".json" + ' file:', err);
} else {
console.log('File ' + path_dir + ".json" + ' of objects created.');
}
});
//collectionsFiles.push(fileCollection.collFiles); //TODO: rivedere meglio cosa ha senso avere nel client
collections_filesnames.push(collection);
count_dir++;
}
});
res.send({
status: true,
file_names: collections_filesnames
})
}
catch (e) {
res.status(500).send(e);
}
});
app.get('/pull-viz', function (req, res) {
console.log(`Pulling visualization data...`);
try {
let vizualizations = JSON.parse(fs.readFileSync(global.vizualizations_dir + "/viz.json"));
res.send({
status: true,
vizualizations: vizualizations
})
}
catch (e) {
res.send({
status: false,
error: e.message
});
}
});
app.post('/save-chart-data', function (req, res) {
console.log('Saving chart data to viz.json...');
const chart_data = req.body.chart_data;
const chart_type = req.body.chart_type;
try {
let json_viz = fs.readFileSync(global.vizualizations_dir + "/viz.json");
let vizualizations = JSON.parse(json_viz);
vizualizations[req.body.viz_tag].chart_data = chart_data;
vizualizations[req.body.viz_tag].chart_type = chart_type;
fs.writeFile(global.vizualizations_dir + "/viz.json", JSON.stringify(vizualizations, null, "\t"), err => {
if (err) {
console.error(err);
console.log('Error saving chart data.');
} else {
console.log('Chart data saved to viz.json.');
}
});
}
catch (e) {
console.error(e);
console.log('Error saving chart data.');
}
});
app.get('/viz/:id', (req, res) => {
const viz_tag = req.params.id;
let vizualizations = JSON.parse(fs.readFileSync(global.vizualizations_dir + "/viz.json"));
var viz_data = vizualizations[viz_tag];
res.render('pages/viz', { viz: viz_data });
});
app.post('/queries', function (req, res) {
console.log(`Executing queries on some of the collections of files...`);
try {
let vizualizations = JSON.parse(fs.readFileSync(global.vizualizations_dir + "/viz.json"));
const queries_by_ds = req.body.queries_by_datasets;
var results_colls = [];
queries_by_ds.forEach(async (queries_ds) => {
var results_ds = {
ds: queries_ds.ds,
queries: []
}
let ds_dir = fs.readdirSync(global.json_dir)[queries_ds.ds];
let ds_json = JSON.parse(fs.readFileSync(global.json_dir + ds_dir));
let ds_filecoll = new FileCollection(queries_ds.ds);
ds_filecoll.construct_from_json(ds_json);
await new Promise(function (resolve) {
// parsing the collection of files
ds_filecoll.coll_files.forEach(async function (file) {
parsed_file = await file.parse_file_Saxon();
// Resolve the Promise once the loop is done
if (ds_filecoll.coll_files.indexOf(file) === ds_filecoll.coll_files.length - 1) {
resolve();
}
});
});
ds_filecoll.coll_files.forEach((file) => {
var results_file = [];
file.queries_file(queries_ds.queries_text, queries_ds.query_language);
results_file = file.file_queries.at(-1);
results_file.forEach(result => {
if (result == null) {
res.send({
status: false,
error: "Invalid query or an error occurred during execution of it."
})
}
});
results_ds.queries.push(results_file);
vizualizations[req.body.viz_tag].queries_by_ds[queries_ds.ds].queries = results_ds.queries;
})
ds_filecoll.coll_files.forEach((file) => {
file.file_parsed_Saxon = {};
})
/* const jsonString = JSON.stringify(ds_filecoll, null, "\t");
fs.writeFile(global.jsonDir + ds_dir, jsonString, (err) => {
if (err) {
console.error('Error writing ' + ds_dir + ' file:', err);
} else {
console.log('File ' + ds_dir + ' of objects updated.');
}
}); */
const json_viz = JSON.stringify(vizualizations, null, "\t");
fs.writeFile(global.vizualizations_dir + "/viz.json", json_viz, (err) => {
if (err) {
console.error('Error writing viz.json file:', err);
} else {
console.log('File viz.json updated.');
}
});
results_colls.push(results_ds);
})
res.send({
status: true,
results_queries: results_colls
})
}
catch (e) {
res.send({
status: false,
error: e.message
});
}
});
/* ==================== */
/* ACTIVATE NODE SERVER */
/* ==================== */
app.listen(global.port, function () {
global.start_date = new Date();
console.log(`App is listening on port ${global.port} started ${global.start_date.toLocaleString()}.`);
});