-
Notifications
You must be signed in to change notification settings - Fork 0
/
DidactaliaGameAPI.js
96 lines (87 loc) · 2.51 KB
/
DidactaliaGameAPI.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
var elasticsearch=require('elasticsearch');
var http = require('http');
var url = require('url');
var jsonexport = require('jsonexport');
var request = require('request');
var config = require('./config.js');
var md5 = require('md5');
var client = new elasticsearch.Client({
host:'localhost:9200',
log: 'error'
});
http.createServer((req, res) => {
var queryData = url.parse(req.url, true).query;
console.log(queryData);
var user = queryData.user;
var output = queryData.output;
var size = queryData.size;
if (!size) size=1000;
if(user) {
var query = buildQuery(user, size);
client.search({
index: config.game_index,
type: "activity",
body: query
},function (error,response,status) {
if (error){
console.log("Error in querying Elsastic Search: "+error+'\n');
result={status: 'error', message: 'Error in querying Elsastic Search: '+error};
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(result));
}
else {
var result = [];
var allHits = response.hits.hits;
for(var i in allHits){
var doc = allHits[i]["_source"];
var act = doc;
result.push(doc);
// var rid = doc.resource_id;
// console.log(rid);
// getResourceData(rid, doc, result, res);
}
console.log("returning "+result.length+" activities");
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(result));
}
});
} else {
result={status: 'error', message: 'user parameter required'};
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(result));
}
}).listen(8073, 'localhost');
console.log('Server running at http://localhost:8073/');
function buildQuery(u,s){
return {"query": {
"bool": {
"must":[
{"term":{"user_id": u}},
{"term":{"actionType":"playEnd"}}
]}},
"size": s
};
}
var callsLeft = 0;
function getResourceData(id, doc, result, res){
callsLeft++;
http.get('http://localhost:9200/didactalia-activity-base/resource/'+id, function (r) {
var body = '';
r.on('data', function(chunk){
body += chunk;
});
r.on('end', function(){
var rj = JSON.parse(body);
console.log(rj)
if (rj["_source"]){
doc.rtitle = rj["_source"].title;
doc.rurl = rj["_source"].resource_url;
}
callsLeft--;
if (callsLeft==0) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(result));
}
});
});
}