X-Git-Url: https://git.sesse.net/?p=remoteglot;a=blobdiff_plain;f=www%2Fserve-analysis.js;h=cc1d2dd4303bdf6e0eb9428ec5072ea81c962c42;hp=19ab93667861d28ad2d2d974e0d6af3c73f30328;hb=70bc196b94f2959ea9726f62019e5f0caa696eab;hpb=d5d7dc50fe8f44711a2e4d52563ff40db029e819 diff --git a/www/serve-analysis.js b/www/serve-analysis.js index 19ab936..cc1d2dd 100644 --- a/www/serve-analysis.js +++ b/www/serve-analysis.js @@ -8,14 +8,18 @@ var url = require('url'); var querystring = require('querystring'); var path = require('path'); var zlib = require('zlib'); +var delta = require('./js/json_delta.js'); // Constants. var json_filename = '/srv/analysis.sesse.net/www/analysis.json'; // The current contents of the file to hand out, and its last modified time. -var json_contents = undefined; -var json_contents_gz = undefined; -var json_last_modified = undefined; +var json = undefined; + +// The last five timestamps, and diffs from them to the latest version. +var history_to_keep = 5; +var historic_json = []; +var diff_json = {}; // The list of clients that are waiting for new data to show up. // Uniquely keyed by request_id so that we can take them out of @@ -36,6 +40,63 @@ var touch_timer = undefined; // ourselves, so some external log-tailing daemon needs to tell us. var viewer_count_override = undefined; +var replace_json = function(new_json_contents, mtime) { + // Generate the list of diffs from the last five versions. + if (json !== undefined) { + // If two versions have the same mtime, clients could have either. + // Note the fact, so that we never insert it. + if (json.last_modified == mtime) { + json.invalid_base = true; + } + if (!json.invalid_base) { + historic_json.push(json); + if (historic_json.length > history_to_keep) { + historic_json.shift(); + } + } + } + + var new_json = { + parsed: JSON.parse(new_json_contents), + plain: new_json_contents, + last_modified: mtime + }; + create_json_historic_diff(new_json, historic_json.slice(0), {}, function(new_diff_json) { + // gzip the new version (non-delta), and put it into place. + zlib.gzip(new_json_contents, function(err, buffer) { + if (err) throw err; + + new_json.gzip = buffer; + json = new_json; + diff_json = new_diff_json; + + // Finally, wake up any sleeping clients. + possibly_wakeup_clients(); + }); + }); +} + +var create_json_historic_diff = function(new_json, history_left, new_diff_json, cb) { + if (history_left.length == 0) { + cb(new_diff_json); + return; + } + + var histobj = history_left.shift(); + var diff = delta.JSON_delta.diff(histobj.parsed, new_json.parsed); + var diff_text = JSON.stringify(diff); + zlib.gzip(diff_text, function(err, buffer) { + if (err) throw err; + new_diff_json[histobj.last_modified] = { + plain: diff, + text: diff_text, + gzip: buffer, + last_modified: new_json.last_modified, + }; + create_json_historic_diff(new_json, history_left, new_diff_json, cb); + }); +} + var reread_file = function(event, filename) { if (filename != path.basename(json_filename)) { return; @@ -50,13 +111,7 @@ var reread_file = function(event, filename) { if (err) throw err; fs.close(fd, function() { var new_json_contents = buffer.toString('utf8', 0, bytesRead); - zlib.gzip(new_json_contents, function(err, buffer) { - if (err) throw err; - json_contents = new_json_contents; - json_contents_gz = buffer; - json_last_modified = st.mtime.getTime(); - possibly_wakeup_clients(); - }); + replace_json(new_json_contents, st.mtime.getTime()); }); }); }); @@ -76,6 +131,7 @@ var possibly_wakeup_clients = function() { for (var i in sleeping_clients) { mark_recently_seen(sleeping_clients[i].unique); send_json(sleeping_clients[i].response, + sleeping_clients[i].ims, sleeping_clients[i].accept_gzip, num_viewers); } @@ -103,23 +159,25 @@ var handle_viewer_override = function(request, u, response) { response.end(); } } -var send_json = function(response, accept_gzip, num_viewers) { +var send_json = function(response, ims, accept_gzip, num_viewers) { + var this_json = diff_json[ims] || json; + var headers = { 'Content-Type': 'text/json', - 'X-Remoteglot-Last-Modified': json_last_modified, + 'X-Remoteglot-Last-Modified': this_json.last_modified, 'X-Remoteglot-Num-Viewers': num_viewers, - 'Access-Control-Allow-Origin': 'http://analysis.sesse.net', 'Access-Control-Expose-Headers': 'X-Remoteglot-Last-Modified, X-Remoteglot-Num-Viewers', 'Expires': 'Mon, 01 Jan 1970 00:00:00 UTC', + 'Vary': 'Accept-Encoding', }; if (accept_gzip) { headers['Content-Encoding'] = 'gzip'; response.writeHead(200, headers); - response.write(json_contents_gz); + response.write(this_json.gzip); } else { response.writeHead(200, headers); - response.write(json_contents); + response.write(this_json.text); } response.end(); } @@ -191,8 +249,8 @@ server.on('request', function(request, response) { // If we already have something newer than what the user has, // just send it out and be done with it. - if (json_last_modified !== undefined && (!ims || json_last_modified > ims)) { - send_json(response, accept_gzip, count_viewers()); + if (json !== undefined && (!ims || json.last_modified > ims)) { + send_json(response, ims, accept_gzip, count_viewers()); return; } @@ -203,6 +261,7 @@ server.on('request', function(request, response) { client.request_id = request_id; client.accept_gzip = accept_gzip; client.unique = unique; + client.ims = ims; sleeping_clients[request_id++] = client; request.socket.client = client;