-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (79 loc) · 2.3 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
const express = require('express');
const app = express();
const path = require('path');
const port = 3000;
const fs = require('fs');
const { v4: uuidv4 } = require('uuid');
const {
readCsvFromFile,
readStreamCsvFromFile,
runningStats,
} = require('./controllers/loadCSV');
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));
app.get('/', (req, res) => {
res.render('index', {
method: req.body.method,
file: req.body.files,
fileSize: 0,
loadingTime: '0 - choose file and methood',
runningStats: runningStats,
});
});
app.post('/', async (req, res) => {
const csvName = req.body.files ? `${req.body.files}.csv` : 'micro.csv';
const filePath = path.join(__dirname, 'public', csvName);
const method =
req.body.method === 'read' ? readCsvFromFile : readStreamCsvFromFile;
const startTime = new Date();
const csvFileSize = fs.statSync(filePath).size;
const formattedSize = (csvFileSize / 1000000).toFixed(2);
const id = uuidv4();
////RUN CHOOSEN METHOD
try {
await method(filePath, id);
const succesTime = new Date();
const loadingTime = succesTime - startTime;
const processed = req.body.method === 'read' ? loadingTime : null;
///SAVE BENCHMARK DATA INTO runningStats Array
runningStats.push({
id,
fileName: csvName,
fileSize: formattedSize,
loadingTime,
processed,
method: req.body.method,
});
///RENDER
res.render('index', {
method: req.body.method,
file: req.body.files,
fileSize: formattedSize,
loadingTime,
runningStats: runningStats,
});
} catch (error) {
console.error('Error loading CSV data:', error);
///SAVE FAILURE BENCHMARK DATA INTO runningStats Array
runningStats.push({
id,
fileName: csvName,
fileSize: formattedSize,
loadingTime: '<error>',
processed: '-',
method: req.body.method,
});
///RENDER
res.status(500).render('index', {
method: req.body.method,
file: req.body.files,
fileSize: (csvFileSize / 1000000).toFixed(2),
loadingTime: `error during loading: ${error.message}`,
runningStats: runningStats,
});
}
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});