-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
166 lines (140 loc) · 4.85 KB
/
app.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
var express = require('express');
var app = express();
var fs = require('fs');
var path = require('path');
var mongoose = require('mongoose');
var moment = require('moment-timezone');
var simulation = require('./controllers/simulation')
var tools = require('./controllers/tools')
var basicAuth = require('./controllers/auth');
// DATABASE
mongoose.connect(process.env.MONGODB_URI_NEW, {useMongoClient: true}); // Set up default mongoose connection
mongoose.Promise = global.Promise; // fix promise thing
PriceRecordModels = require('./models/pricerecordmodel')
// SET TEMPLATING
app.set('view engine', 'ejs');
app.set('views',__dirname + '/views');
// SET STATIC FILE DIRECTORIES
app.use(express.static('static'))
app.use('/results', express.static('results'))
// INDEX
app.get('/', basicAuth, function(req, res) {
res.render('index');
//res.sendFile(path.join(__dirname+'/index.html'));
})
// FULL SIMULATION
app.get('/run-simulation', basicAuth, function(req, res) {
if (typeof req.query.currency === 'undefined') {
res.send('No currency vars present.')
} else if (req.query.currency !== 'BTC' && req.query.currency !== 'ETH' && req.query.currency !== 'LTC') {
res.send('Currency must be BTC ETH or LTC')
} else if (typeof req.query.days === 'undefined') {
res.send('No days var present.')
}
PriceRecordModels[req.query.currency].find({}).sort('datetime').exec(function(error, price_data) {
if (error) {
res.json(error);
process.exit(1);
}
else {
simulation.runFullSimulation(price_data, req.query.currency, req.query.days, 'browser');
res.render('result', {
currency : req.query.currency, // BTC, ETH or LTC
data : simulation.browser_output,
table_data : simulation.table_data,
average_chart_data : simulation.average_chart_data
});
}
});
})
// SINGLE SIMULATION
app.get('/run-simulation-single', basicAuth, function(req, res) {
if ((typeof req.query.hrs_in_period === 'undefined') || (typeof req.query.offset === 'undefined') || (typeof req.query.low_threshold === 'undefined') ||
(typeof req.query.high_threshold === 'undefined') || (typeof req.query.currency === 'undefined')) {
res.send('get vars not present')
} else if (typeof req.query.days === 'undefined') {
res.send('No days var present.')
}
PriceRecordModels[req.query.currency].find({}).sort('datetime').exec(function(error, price_data) {
if (error) {
res.json(error);
}
else {
simulation.runSingleSimulation(
parseFloat(req.query.hrs_in_period), parseFloat(req.query.offset),
parseFloat(req.query.low_threshold), parseFloat(req.query.high_threshold),
price_data, req.query.days
);
res.render('result-single', {
currency : req.query.currency,
data : simulation.browser_output,
chart_data : simulation.chart_data,
summary_output : simulation.summary_output
});
}
});
})
// LIVE RESULTS
app.get('/live-result', basicAuth, function(req, res) {
if (typeof req.query.bot_name === 'undefined') {
res.send('No bot_name var present.')
}
var liveDataModels = require('./models/livedatamodel')
liveDataModels['ETH'].find({'bot_name' : req.query.bot_name}).sort('-datetime_updated').limit(60).exec(function(error, live_data_eth) {
if (error) {
//res.json(error);
process.exit(1);
}
else {
if (! live_data_eth.length) {
res.end('no data found')
process.exit(1);
}
res.render('live-result', {
data : live_data_eth,
moment : moment
});
}
});
})
// SIM VARS
app.get('/sim-vars', basicAuth, function(req, res) {
var simVarsModelETH = require('./models/simvarsmodel')
simVarsModelETH.find({}).sort('-datetime').limit(1).exec(function(error, sim_vars_eth) {
if (error) {
//res.json(error);
process.exit(1);
}
else {
res.render('sim-vars', {
data_string : JSON.stringify(sim_vars_eth, null, " "),
data : sim_vars_eth,
moment : moment
});
}
});
})
// SIM VAR RESULTS
app.get('/sim-vars-results', basicAuth, function(req, res) {
var simVarsModelETH = require('./models/simvarsmodel')
PriceRecordModels['ETH'].find({}).sort('datetime').exec(function(error, price_data) {
if (error) { res.json(error); }
else {
// ----------
simVarsModelETH.find({}).sort('-datetime').limit(1).exec(function(error, sim_vars_eth) {
if (error) { res.json(error); }
else {
var data = simulation.simVarsResults(sim_vars_eth[0], price_data)
res.render('sim-vars-results', {
data: data,
data_string: JSON.stringify(data, null, " ")
});
}
});
// ----------
}
});
})
app.listen(process.env.PORT, function() {
console.log('Cryptobot2000 is running at http://localhost:' + process.env.PORT)
})