-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
286 lines (271 loc) · 10.9 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
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
'use strict';
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const reload = require('require-reload')(require);
const stringify = require('fast-stringify');
let config = reload('./config_private');
const configLab = require('./config_lab');
const path = require('path');
const app = express();
const jsonParser = bodyParser.json();
const helper = require('./helpers/helper');
app.use(jsonParser);
let running = false;
let gbRunning = false;
let mysqlConnected = false;
let blockchainReady = false;
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static('public'));
const http = require('http').Server(app);
const io = require('socket.io')(http);
const contractGenerator = require('./helpers/contractGenerator');
const contractDeployer = require('./helpers/contractDeployer');
const contractController = require('./controllers/contractController');
const cacheController = require('./controllers/cacheController');
const computationsController = require('./controllers/computationsController');
const viewMaterializationController = require('./controllers/viewMaterializationController');
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.WebsocketProvider(config.blockchainIP));
let createTable = '';
const contractsDeployed = new Map();
web3.eth.defaultAccount = web3.eth.accounts[0];
let contract = null;
let account = null;
app.get('/', function (req, res) {
fs.readdir('./templates', function (err, items) {
if (err) {
/* istanbul ignore next */
console.error('error reading templates directory: ' + err.stack);
return;
}
web3.eth.getBlockNumber().then(blockNum => {
if (blockNum >= 0) {
blockchainReady = true;
}
return res.render('index', { 'templates': items,
'redisStatus': cacheController.getRedisStatus(),
'sqlStatus': mysqlConnected,
'blockchainStatus': blockchainReady });
});
});
});
app.get('/dashboard', function (req, res) {
fs.readdir('./templates', function (err, items) {
if (err) {
/* istanbul ignore next */
console.error('error reading templates directory: ' + err.stack);
return;
}
let jsonFiles = helper.getJSONFiles(items);
web3.eth.getBlockNumber().then(blockNum => {
res.render('dashboard', { 'templates': jsonFiles, 'blockNum': blockNum });
});
});
});
app.get('/form/:contract', function (req, res) {
let factTbl = require('./templates/' + req.params.contract);
let templ = {};
if ('template' in factTbl) {
templ = factTbl.template;
} else {
templ = factTbl;
}
let address = contractsDeployed.get(factTbl.name).address;
let readyViews = factTbl.views;
readyViews = readyViews.map(x => x.name);
res.render('form', { 'template': templ, 'name': factTbl.name, 'address': address, 'readyViews': readyViews });
});
http.timeout = 0;
http.listen(3000, () => {
console.log(`Smart-Views listening on http://localhost:3000/dashboard`);
console.log(`Visit http://localhost:3000/ to view Blockchain, mySQL and Redis cache status`);
let validations = helper.configFileValidations();
if (process.env.ENVIRONMENT === 'LAB') {
config = configLab;
}
if (validations.passed) {
computationsController.connectToSQL().then(connected => {
mysqlConnected = true;
console.log('mySQL connected');
helper.welcomeMessage();
}).catch(err => {
/* istanbul ignore next */
console.error('error connecting to mySQL: ' + err.stack);
});
} else {
/* istanbul ignore next */
console.log('Config file validations failed');
/* istanbul ignore next */
process.exit(1);
}
});
app.get('/deployContract/:fn', function (req, res) {
web3.eth.getAccounts(function (err, accounts) {
if (!err) {
account = accounts[1];
contractDeployer.deployContract(accounts[0], './contracts/' + req.params.fn, contract)
.then(options => {
helper.log('******************');
helper.log('Contract Deployed!');
helper.log('******************');
contractsDeployed.set(options.contractDeployed.contractName, options.contractDeployed);
contract = options.contractObject;
contractController.setContract(contract, account);
viewMaterializationController.setContract(contract, account);
res.send({ status: 'OK', options: options.options });
})
.catch(err => {
/* istanbul ignore next */
helper.log('error on deploy ' + err);
/* istanbul ignore next */
res.status(400);
/* istanbul ignore next */
res.send({ status: 'ERROR', message: 'Deployment failed' });
});
}
});
});
app.get('/load_dataset/:dt', contractController.contractChecker, function (req, res) {
let dt = require('./test_data/benchmarks/' + req.params.dt);
if (!running) {
// a guard to check that this asynchronous process will not start again if called while loading data
running = true;
let startTime = helper.time();
contractController.addManyFacts(dt, config.recordsSlice, io).then(retval => {
let endTime = helper.time();
let timeDiff = endTime - startTime;
running = false;
io.emit('DONE', 'TRUE');
helper.log('Added ' + dt.length + ' records in ' + timeDiff + ' seconds');
res.send({ message: 'OK' });
}).catch(error => {
/* istanbul ignore next */
helper.log(error);
/* istanbul ignore next */
res.send(helper.errorToJson(error));
});
}
});
app.get('/new_contract/:fn', function (req, res) {
contractGenerator.generateContract(req.params.fn).then(result => {
computationsController.setCreateTable(result.createTable);
computationsController.setTableName(result.tableName);
createTable = result.createTable;
return res.send({ message: 'OK', 'filename': result.filename + '.sol', 'template': result.template });
}).catch(err => {
/* istanbul ignore next */
console.log(err);
/* istanbul ignore next */
return res.send(helper.errorToJson(err));
});
});
app.get('/getFactById/:id', contractController.contractChecker, function (req, res) {
contractController.getFactById(req.params.id).then(result => {
res.send(stringify(result).replace(/\\/g, ''));
}).catch(error => {
/* istanbul ignore next */
helper.log(error);
/* istanbul ignore next */
res.send(helper.errorToJson(error));
});
});
app.get('/getFactsFromTo/:from/:to', contractController.contractChecker, function (req, res) {
let timeStart = helper.time();
contractController.getFactsFromTo(parseInt(req.params.from), parseInt(req.params.to)).then(retval => {
let timeFinish = helper.time() - timeStart;
retval.push({ time: timeFinish });
res.send(stringify(retval).replace(/\\/g, ''));
}).catch(err => {
/* istanbul ignore next */
res.send(helper.errorToJson(err));
});
});
app.get('/allfacts', contractController.contractChecker, function (req, res) {
contractController.getLatestId().then(result => {
// async loop waiting to get all the facts separately
let timeStart = helper.time();
contractController.getAllFactsHeavy(result).then(retval => {
let timeFinish = helper.time() - timeStart;
retval.push({ time: timeFinish });
res.send(stringify(retval).replace(/\\/g, ''));
}).catch(error => {
/* istanbul ignore next */
console.log(error);
});
}).catch(err => {
/* istanbul ignore next */
helper.log(err);
/* istanbul ignore next */
res.send(helper.errorToJson(err));
});
});
app.get('/groupbyId/:id', contractController.contractChecker, function (req, res) {
contractController.getGroupByWithId(req.params.id).then(result => {
return res.send(stringify(result).replace(/\\/g, ''));
}).catch(error => {
/* istanbul ignore next */
helper.log(error);
/* istanbul ignore next */
return res.send(helper.errorToJson(error));
});
});
app.get('/getViewByName/:viewName/:contract', contractController.contractChecker, async function (req, res) {
if (process.env.TESTS) {
config = reload('./config_private');
}
const totalStart = helper.time();
let factTbl = require('./templates/' + req.params.contract);
const viewsDefined = factTbl.views;
let viewMap = new Map();
for (let crnView in viewsDefined) {
factTbl.views[crnView].id = crnView;
viewMap.set(factTbl.views[crnView].name, factTbl.views[crnView]);
}
const view = helper.checkViewExists(viewMap, req.params.viewName);
// returns an empty object if view not exist, otherwise it returns the view object
if (Object.keys(view).length === 0 && view.constructor === Object) {
res.status(200);
return res.send({ status: 'ERROR', message: 'view not found' });
}
await helper.updateViewFrequency(factTbl, req.params.contract, view.id);
if (!gbRunning && !running) {
gbRunning = true;
viewMaterializationController.materializeView(view,
req.params.contract, totalStart, createTable)
.then(result => {
gbRunning = false;
res.status(200);
io.emit('view_results', stringify(result).replace(/\\/g, ''));
return res.send(stringify(result).replace(/\\/g, ''));
}).catch(err => {
/* istanbul ignore next */
gbRunning = false;
/* istanbul ignore next */
return res.send(stringify(err));
});
}
});
app.get('/getcount', contractController.contractChecker, function (req, res) {
contractController.getFactsCount().then(result => {
if (result === -1) {
return res.send({ status: 'ERROR', message: 'Error getting count' });
} else {
return res.send(result);
}
});
});
app.post('/addFact', contractController.contractChecker, function (req, res) {
contractController.addFact(req.body).then(receipt => {
res.send(receipt);
}).catch(error => {
/* istanbul ignore next */
helper.log(error);
/* istanbul ignore next */
res.send(helper.errorToJson(error));
})
});
module.exports = app;