-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
248 lines (189 loc) · 6.57 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
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
var http = require('http');
var portfinder = require('portfinder');
var ml = require('machine_learning');
mysql = require('mysql');
deepcopy = require('deepcopy');
// Code to include external javascript files.
var fs = require('fs');
var vm = require('vm');
var includeInThisContext = function(path) {
var code = fs.readFileSync(path);
vm.runInThisContext(code, path);
}.bind(this);
// frontend
var jade = require('jade');
var express = require('express');
var app = express();
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
var decisionTree = null;
var decisionData = null;
/**************************** Code start After This Line********************************* */
// Include javascript files.
//includeInThisContext(__dirname + "/machinelearning.js");
//includeInThisContext(__dirname + "/dataConnect2.js");
// Runs the mlFunciton with the data from the dataQuery and resultQuery.
function queryDB(mysql, dataQuery, resultQuery, mlFunction){
var connection = mysql.createConnection({
host : 'address.to.sql.server',
user : 'username',
password : 'password',
database : 'databaseName'
});
connection.connect();
// Get the data to use in the tree
connection.query(dataQuery, [resultQuery, mlFunction], function(err, rows, fields) {
if (!err){
mlData = [];
// turn the data into an array
Object.keys(rows).forEach(function(key) {
rowData = [];
var row = rows[key];
Object.keys(row).forEach(function(key) {
rowData.push(row[key]);
});
mlData.push(rowData);
});
//Get the data about the results
connection.query(resultQuery, [mlData, mlFunction], function(err, rows, fields) {
if (!err){
var result = [];
Object.keys(rows).forEach(function(key) {
var row = rows[key];
var val = row[Object.keys(row)[0]];
result.push(val);
});
// run the machine learning function
mlFunction(mlData, result);
}
else{
console.log(err)
}
});
}
else
console.log('Error while performing Query.');
});
//connection.end();
}
Array.prototype.contains = function(v) {
for(var i = 0; i < this.length; i++) {
if(this[i] === v) return true;
}
return false;
};
Array.prototype.unique = function() {
var arr = [];
for(var i = 0; i < this.length; i++) {
if(!arr.contains(this[i])) {
arr.push(this[i]);
}
}
return arr;
}
function foodShelterPredictor()
{
// Convert to single value, instead of array
var formatData = [];
for(var i =0; i < decisionData.length; i++) {
formatData.push(decisionData[i][0]);
}
var months = formatData.unique();
var predictions = [];
for(var i =0; i < months.length; i++) {
var month = months[i];
var predict = [month, decisionTree.classify([month])];
predictions.push(predict);
}
return predictions;
}
function getDataAndBuildDecisionTree(queryResultData,
queryTestData, callback, callbackRender )
{
//var ml = require('machine_learning');
var mlFunct = function(mlData, resultData){
// Formatting of data
for(i = 0; i < mlData.length; ++i )
{
var month = mlData[i].toString();
month = month.slice(4,7);
mlData[i] = [month];
}
decisionData = deepcopy(mlData);
var dt = new ml.DecisionTree({
data : mlData,
result : resultData
});
dt.build();
//dt.print();
console.log( "\nTree Depth:" + dt.getDepth() );
//dt.prune(1.0); // 1.0 : mingain.
decisionTree = dt;
var data = null;
if( callback != null )
data = callback();
if( callbackRender != null )
callbackRender( data );
}
// Data: Month, Pounds
// Test: DonorID
queryDB(mysql, queryResultData, queryTestData,
mlFunct);
}
portfinder.getPort(function (err, port) {
console.log( 'Port open:' + port);
// // // learning
console.log( ' \n \n \n ');
/*
getDataAndBuildDecisionTree( 'SELECT TrxDate FROM sql478053.FoodDonations order by TrxID limit 20',
'SELECT Name FROM FoodDonations left outer join Donors on FoodDonations.DonorID = Donors.ID order by TrxID limit 20',
foodShelterPredictor );
*/
// Views
// // //
app.get('/', function(req, res) {
res.render('index', {
title: 'Home'
});
});
app.get('/donor', function(req, res) {
res.render('donor', {
title: 'Donor'
});
});
app.get('/household', function(req, res) {
res.render('household', {
title: 'Household'
});
});
app.get('/shelter', function(req, res) {
pageRenderShelter = function( data )
{
//console.log(data + '\n');
var finalData = [];
for( var i = 0; i < data.length; ++i )
{
var month = new Object();
month.name = data[i][0];
// Adding donors
month.donors = {};
for( var key in data[i][1] )
{
month.donors[ key ] = data[i][1][key];
}
finalData.push( month );
}
// console.log("\n\n\n" );
//console.log( finalData );
// Page generation
res.render('shelter', {
title: 'Shelter Future Projections',
months: finalData
});
}
getDataAndBuildDecisionTree( 'SELECT TrxDate FROM sql478053.FoodDonations order by TrxID limit 20000',
'SELECT Name FROM FoodDonations left outer join Donors on FoodDonations.DonorID = Donors.ID order by TrxID limit 20000',
foodShelterPredictor, pageRenderShelter );
});
app.listen(8000);
});