refactoring, plus /bugs bug-id-only interface
authorMike Shaver <shaver@mozilla.org>
Fri, 18 Feb 2011 04:57:32 +0000
changeset 47 27f72a12a0f4
parent 46 81a637791dcf
child 48 4158fb343520
push id36
push usershaver@mozilla.com
push dateFri, 18 Feb 2011 04:59:43 +0000
refactoring, plus /bugs bug-id-only interface
server/websocket.js
--- a/server/websocket.js
+++ b/server/websocket.js
@@ -9,16 +9,24 @@ var indexer = require('../lib/indexer');
 
 var redis = require('redis');
 var redisConn = redis.createClient();
 
 redisConn.on("error",  function (err) {
     util.log("Redis connection error to " + redisConn.host + ":" + redisConn.port + " - " + 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 runSearch(message, cb)
 {
     var startTime = Date.now();
     var resultmsg = { bugs: [] };
 
     function sendResponse()
     {
         var now = Date.now();
@@ -83,54 +91,93 @@ function typeForPathname(path)
     switch (suffix) {
     case '.html': return 'text/html';
     case '.css': return 'text/css';
     case '.js': return 'application/javascript';
     default: return 'text/plain';
     }
 }
 
+function reqMatchingBugs(req, res)
+{
+}
+
 function handleRequest(req, res)
 {
+    var startTime = Date.now();
+   
+    function send404()
+    {
+        res.writeHead(404, {'Content-type': 'text/plain'});
+        res.write("404 Not found\n");
+        res.end();
+    }
+
+    function send500(err)
+    {
+        res.writeHead(500, {'Content-type': 'text/plain'});
+        res.write("500 ISE\n");
+        res.write(err);
+        res.end();
+    }
+    
+    function sendResponse(body)
+    {
+        res.writeHead(200, {'Content-type': 'text/plain'});
+        res.write(body);
+        res.end();
+    }
+    
     var urlx = url.parse(req.url);
     var pathname = urlx.pathname;
     
     if (pathname == "/")
         pathname = "/static/index.html";
     
     pathname.replace(/\.\.\//g, ''); // naughty, naughty
 
-    if (req.method == "GET" && pathname.substr(0, 7) == "/static") {
+    if (req.method != "GET")
+        return; // let socket.io handle
+
+    if (pathname.substr(0, 7) == "/static") {
         var filename = path.join(process.cwd(), pathname);
         path.exists(filename, function (exists) {
-            function send404()
-            {
-                res.writeHead(404, {'Content-type': 'text/plain'});
-                res.write("404 Not found\n");
-                res.end();
-            }
             
-            if (!exists) {
-                send404();
-                return;
-            }
+            if (!exists)
+                return send404();
             
             res.writeHead(200, {'Content-Type': typeForPathname(pathname)});
             fs.createReadStream(pathname.substr(1), { flags: 'r', bufferSize: 4*1024 }).
                 addListener('data', function(chunk) { res.write(chunk); }).
                 addListener('end', res.end.bind(res)).
                 addListener('error', function (err) { util.log(err); send404(); });
             return;
         });
     }
+
+    var terms;
     
-    if (req.method == "GET" && pathname == "/search") {
+    if (pathname == "/search") {
+        terms = decodeURIComponent(urlx.query);
+        runSearch(terms, function (data) { sendResponse(data); });
+        return;
+    }
+
+    if (pathname == "/bugs") {
         terms = decodeURIComponent(urlx.query);
-        res.writeHead(200, {'Content-Type': 'text/plain'});
-        runSearch(terms, function (data) { res.write(data); res.end(); });
+        findMatchingBugs(terms, buildResponse);
+        function buildResponse(err, bugs)
+        {
+            if (err) return send500(err);
+            var msg = { times: { server: Date.now() - startTime } };
+            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;
     }
     
     // fall through to socket.io's handling
 }
 
 var httpServer = http.createServer(handleRequest);