-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
152 lines (129 loc) · 3.63 KB
/
app.js
File metadata and controls
152 lines (129 loc) · 3.63 KB
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
var webshot = require('webshot');
var fs = require('fs');
var http = require('http');
var dispatcher = require('httpdispatcher');
var request = require('request');
var events = require('events');
var EventEmitter= new events.EventEmitter();
const PORT = 1337;
const index = 'tester';
var documentCount = 0;
var documentArray = [];
var graph = fs.readFileSync('barChart.html', 'utf8');
/**
* function: http-server
*
*
**/
dispatcher.onGet("/getbar", function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
request({
url : 'http://localhost:9200/tester/text/'+req.params.id+'/_termvectors',
method : 'POST',
body : '{"fields": ["Inhalt"],"offsets": true,"payloads": true,"positions": true,"term_statistics": true,"field_statistics": true,"filter" : {"min_term_freq" : 2}}'
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var jsonData = JSON.parse(body)
var inhalt = [];
var buckets = [];
for (key in jsonData.term_vectors.Inhalt.terms){
var object = {};
object.doc_count = jsonData.term_vectors.Inhalt.terms[key].term_freq;
object.key = key;
buckets.push(object);
}
buckets.sort(function(a,b){
return b.doc_count - a.doc_count;
})
inhalt['buckets'] = buckets;
graph = graph.replace(/var data = .*?\;/g, "var data = "+JSON.stringify(inhalt.buckets)+';');
res.end(graph);
}
else {
console.log(error);
}
})
});
function handleRequest(request, response){
try {
dispatcher.dispatch(request, response);
} catch(err) {
console.log(err);
}
}
var server = http.createServer(handleRequest);
server.listen(PORT, function(){
console.log("Server listening on: http://localhost:%s", PORT);
starter();
});
/**
* function: start
*
*
**/
var starter = function() {
getDocumentCount();
}
var getDocumentCount = function() {
request('http://localhost:9200/_stats', function (error, response, body) {
if (!error && response.statusCode == 200) {
var json = JSON.parse(body);
documentCount = json._all.total.docs.count;
getAllDocuments();
}
else {
console.log(error);
}
})
}
var getAllDocuments = function() {
request({
url : 'http://localhost:9200/'+index+'/_search?size='+documentCount,
method : 'GET',
qs : {"_source" : true, "query" : {"match_all" : {}}}
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var json = JSON.parse(body);
var data = json.hits.hits;
for (key in data) {
var tempObject = new Object();
tempObject.dateiname = data[key]._source.Dateiname;
tempObject.id = data[key]._id;
documentArray.push(tempObject);
}
EventEmitter.emit('nextScreenshot', {'filename' : documentArray[0].dateiname, 'id' : documentArray[0].id});
}
else {
console.log(error);
}
})
}
var queue = 0;
var x = 0;
EventEmitter.on('finishedShot', function(){
queue = queue - 1;
while(queue <= 5) {
EventEmitter.emit('nextScreenshot', {'filename' : documentArray[x].dateiname, 'id' : documentArray[x].id});
queue = queue + 1;
}
});
EventEmitter.on('nextScreenshot', function(object) {
console.log('Aktuelles Dokument', x);
x = x + 1;
if(x<=documentCount){
var options = {
screenSize: {
width: 650,
height: 427
},
siteType : 'url'
}
webshot('http://localhost:1337/getbar?id='+object.id, 'screenshots/'+object.filename+'.png', options, function(err) {
if(err)
console.log('error', err);
else
console.log('Screenshot', object.filename);
EventEmitter.emit('finishedShot');
})
}
})