-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
224 lines (178 loc) · 6.36 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
var express = require('express');
var app = express();
var postRequest = require('request');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
//25x40 cube is the entire spaceaa
var DATASET = [];
var hSet = new Set();
var MAX_HASH = 800;
var MAX_EPOCH_WAIT = 4;
var EPOCHS_WAITED = 0;
var TOTAL_GROUPS = 30;
var EPOCH_TIME = 2000;
var ENTIRE_WORLD_SIZE_X = 60;
var ENTIRE_WORLD_SIZE_Y = 100;
var REQUEST_EPOCH = 0;
var PIVOT_POINT = 7; //Assuming Exponential(7, 49) distribution
var dss = [];
app.options("/*", function(req, res, next){
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.send(200);
});
app.post('/', function(req, res) {
console.log(req.body);
console.log(req.body.personIdx);
updatePrices(req.body.personIdx);
var REQUEST_TIMER = setInterval(function(){
if((DATASET.length>5 && REQUEST_EPOCH>3)||(DATASET.length>20 && REQUEST_EPOCH>2)||(DATASET.length>60)){
formDataSets(res);
clearInterval(REQUEST_TIMER);
REQUEST_EPOCH = 0;
DATASET = [];
dss = [];
}
REQUEST_EPOCH++;
if(REQUEST_EPOCH > 5){
clearInterval(REQUEST_TIMER);
try {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', '*');
res.set('Content-Type', 'text/plain');
res.sendStatus(203);
res.send(JSON.stringify({
datasets: 'Unavailable'
}));
}catch(e){
console.log('Error in sending 203 response ');
}
}
console.log('request epoch ' + REQUEST_EPOCH + ' AND DATASET LENGTH IS ' + DATASET.length);
}, EPOCH_TIME);
});
function formDataSets(res){
var j = 0;
for(var i=0; i<DATASET.length; i++){
if(DATASET[i]==null){continue;}
dss[j] = {
label: DATASET[i].label,
data:[{
x:DATASET[i].x,
y:DATASET[i].y,
r:DATASET[i].r
}],
backgroundColor:DATASET[i].backgroundColor,
idx:DATASET[i].idx,
department: DATASET[i].department,
firstname: DATASET[i].firstname,
surname: DATASET[i].surname,
title: DATASET[i].title,
initials: DATASET[i].initials
};
j++;
}
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', '*');
res.set('Content-Type', 'text/plain');
res.send(JSON.stringify({
datasets: dss
}));
}
var SERVER_ADDRESS = "http://battle.horse/oblong";
var SERVER_ADDRESS_NOOBLONG = "http://battle.horse";
function updatePrices(startIdx) {
addDataSetGroupByLinkReturnInterest('/api/people/'+startIdx);
var HASH_ADD_TIMER = setInterval(function(){
if((hSet.size>8 && EPOCHS_WAITED>MAX_EPOCH_WAIT) || (hSet.size>30)){
EPOCHS_WAITED = 0;
clearInterval(HASH_ADD_TIMER);
addDataSetGroupByHash(generateRandomColour(), Math.random()*ENTIRE_WORLD_SIZE_Y, Math.random()*ENTIRE_WORLD_SIZE_X);
}
EPOCHS_WAITED++;
console.log('hset size is ---------a-----' + hSet.size + ' --- epochs ' + EPOCHS_WAITED);
}, EPOCH_TIME);
}
function addDataSetGroupByLinkReturnInterest(link){
postRequest.get(SERVER_ADDRESS + link, function (err, response, body) {
try {
var parBody = JSON.parse(body);
console.log(link);
var keywords = parBody.keywords;
for(var keys in keywords){
if(keywords[keys]>Math.random()*7) {
getPeopleOfSimilarInterests(keys, keywords[keys]);
}
}
}catch(e){
console.log(link + ' failed ' + e);
}
});
}
function getPeopleOfSimilarInterests(topicKeyword, k){
console.log(topicKeyword);
postRequest.get(SERVER_ADDRESS + '/api/keywords/' + topicKeyword, function (err, response, body) {
try {
var parBody = JSON.parse(body);
var t = 0;
while(parBody.profiles[t]!=null && hSet.size<MAX_HASH) {
hSet.add([parBody.profiles[t].link, k]);
t++;
}
}catch(e){console.log(e);}
});
}
function addDataSetGroupByHash(dotColor, xOrigin, yOrigin){
var i = 0;
console.log('at adding pt' + hSet.size);
hSet.forEach(function(link){
console.log(link);
addDataSetGroupWithLink(dotColor, xOrigin, yOrigin, link[0], link[1], i);
i++;
hSet.delete(link);
});
}
function generateRandomColour(){
var col = '#' + Math.floor(Math.random() * 16777215).toString(16);
while(col.length<7){
col += 'F';
}
return col;
}
function addDataSetGroupWithLink(dotColor, xOrigin, yOrigin, link, k, i){
postRequest.get(SERVER_ADDRESS_NOOBLONG + link, function (err, response, body) {
try {
var parBody = JSON.parse(body);
var name = parBody.name.title + ' ' + parBody.name.first
+ ' ' + parBody.name.initials + ' ' + parBody.name.last;
var department = parBody.department;
var label = ' [' + department + '] ' + name;
DATASET[i] = {
label: label,
x: xOrigin + 8*Math.random() - Math.abs(k-PIVOT_POINT)*(Math.random()-0.5),
y: yOrigin + 8*Math.random() - Math.abs(k-PIVOT_POINT)*(Math.random()-0.5),
r: 9,
backgroundColor: dotColor,
idx: link,
department: parBody.department,
firstname: parBody.name.first,
surname: parBody.name.last,
title: parBody.name.title,
initials: parBody.name.initials
};
console.log(i);
}catch(e){
console.log(' ' + SERVER_ADDRESS_NOOBLONG + link + ' failed ' + e);
}
});
}
var PORT = process.env.PORT || 1140;
var server = app.listen(PORT, function(){
var host = server.address().address;
var port = server.address().port;
console.log('Started Server at %s:%s', host, port);
});