-
Notifications
You must be signed in to change notification settings - Fork 6
/
sandwich_app.js
146 lines (132 loc) · 3.38 KB
/
sandwich_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
var http = require('http')
, connect = require('connect')
, kafka = require('kafka')
, pubsub = require('redis').createClient()
, redis = require('redis').createClient();
var subs = {};
pubsub.on('message',function(channel,message) {
if(subs[channel])
subs[channel](message);
});
function deep(data) {
var dict = {};
for(var i=0;i<data.length;i+=2) {
var keys = data[i].split(":");
var x = dict;
for(var j=0;j<keys.length-1;j++) {
if(!x[keys[j]]) x[keys[j]] = {};
x = x[keys[j]];
}
x[keys[keys.length-1]] = 1*data[i+1];
}
}
function top100(data) {
var keys = [];
for(var i in data) keys.push(i);
keys.sort(function(a,b) { return 1*data[b]-1*data[a]; });
console.log(keys.length+" total values");
var l = keys.length > 100 ? 100 : keys.length;
var dict = {};
for(var i=0;i<l;i++) {
dict[keys[i]] = 1*data[keys[i]];
}
return dict;
}
function words(req,res) {
req.socket.setTimeout(Infinity);
res.writeHead(200,{
'Content-Type':'text/event-stream',
'Cache-Control':'no-cache',
'Connection':'keepalive'
});
res.message = function(msg,event) {
if(event)
res.write('event: '+event+'\n');
res.write('data: '+msg+'\n\n');
}
res.json = function(obj,event) {
this.message(JSON.stringify(obj),event);
};
redis.hgetall("WordCount",function(err,data) {
if(data != null) {
res.json(top100(data));
}
subs['WordCount'] = function(message) {
console.log("update");
redis.hgetall("WordCount",function(err,data) {
if(data != null) {
res.json(top100(data));
}
});
};
pubsub.subscribe("WordCount");
});
}
function speaker(req,res) {
req.socket.setTimeout(Infinity);
res.writeHead(200,{
'Content-Type':'text/event-stream',
'Cache-Control':'no-cache',
'Connection':'keepalive'
});
res.message = function(msg,event) {
if(event)
res.write('event: '+event+'\n');
res.write('data: '+msg+'\n\n');
}
res.json = function(obj,event) {
this.message(JSON.stringify(obj),event);
};
redis.hgetall("speaker",function(err,data) {
res.json(deep(data));
subs['speaker'] = function(message) {
redis.hgetall("speaker",function(err,data) {
res.json({speaker:dict(data)});
});
};
pubsub.subscribe("speaker");
});
}
function consumer(req,res) {
console.log("Subscribing to topic");
req.socket.setTimeout(Infinity);
res.writeHead(200,{
'Content-Type':'text/event-stream',
'Cache-Control':'no-cache',
'Connection':'keepalive'
});
res.message = function(msg,event) {
if(event)
res.write('event: '+event+'\n');
res.write('data: '+msg+'\n\n');
}
res.json = function(obj,event) {
this.message(JSON.stringify(obj),event);
};
var consumer = new kafka.Consumer()
.on('message',function(topic,message,offset) {
res.message(message);
})
.on('lastmessage',function(topic,offset) {
consumer.fetchTopic({name:topic,offset:offset});
})
.on('lastoffset',function(topic,offset) {
consumer.fetchTopic({name:topic,offset:offset});
})
.connect(function() {
console.log("connected");
consumer.fetchOffsets('hamlet');
});
}
connect()
.use(connect.static(__dirname))
.use(function(req,res) {
if(req.url == '/consumer') {
consumer(req,res);
} else if(req.url == '/words') {
words(req,res);
} else if(req.url == '/speaker') {
speaker(req,res);
}
})
.listen(9000);