add get-summaries service default tip
authorMike Shaver <shaver@mozilla.org>
Fri, 18 Feb 2011 05:21:25 +0000
changeset 48 4158fb343520343b8c44990459d6f7151b654406
parent 47 27f72a12a0f4bf42faab95c0b5a5201792fc01d1
push id37
push usershaver@mozilla.com
push dateFri, 18 Feb 2011 05:23:15 +0000
add get-summaries service
server/websocket.js
--- a/server/websocket.js
+++ b/server/websocket.js
@@ -17,16 +17,21 @@ redisConn.on("error",  function (err) {
 function findMatchingBugs(message, cb)
 {
     var words = indexer.keywordsForSummary(message);
     if (words.length == 0)
         cb(false, []);
     redisConn.sinter(words.map(function (v) { return "word:" + v; }), cb);
 }
 
+function getSummaries(list, cb)
+{
+    redisConn.mget(list.map(function (v) { return "bug:" + v; }), cb);
+}
+
 function runSearch(message, cb)
 {
     var startTime = Date.now();
     var resultmsg = { bugs: [] };
 
     function sendResponse()
     {
         var now = Date.now();
@@ -171,30 +176,45 @@ function handleRequest(req, res)
             util.log("bugs: " + terms + "; times: " + JSON.stringify(msg.times));
             msg.bugs = bugs.slice(0, 100).map(function(v) { return {id: v}; });
             sendResponse(JSON.stringify(msg));
         }
 
         return;
     }
     
+    if (pathname == "/summaries") {
+        var ids = decodeURIComponent(urlx.query).split(',');
+        getSummaries(ids, buildResponse);
+        function buildResponse(err, summaries)
+        {
+            if (err) return send500(err);
+            var msg = { times: { server: Date.now() - startTime } };
+            msg.bugs = summaries.map(function (v, i) { return { id: ids[i], summary: v }; });
+            sendResponse(JSON.stringify(msg));
+        }
+        
+    }
+    
     // fall through to socket.io's handling
 }
 
 var httpServer = http.createServer(handleRequest);
 
 var clientCount = 0;
 
+/*
 var socket = io.listen(httpServer);
 socket.on("clientConnect",
           function(client) {
               util.log("connect (" + ++clientCount + ")");
               client.on("message", 
                   function(message) { runSearch(message, function(data) { client.send(data); }); });
               client.on("error", function(err) { util.log("ERROR " + err); });
               client.on("disconnect", function() { util.log ("disconnect (" + --clientCount + ")");});
           }); 
+*/
 
 httpServer.on("close", function(conn) { util.log("connection closed"); });
 httpServer.on("error", function (conn, err) { util.log("server error: " + err); });
 
 httpServer.listen(parseInt(process.argv[2]) || 8000);