From a2bdc4722d471da1a6275a10e984fb422c78095d Mon Sep 17 00:00:00 2001 From: dethe Date: Wed, 24 Oct 2012 14:42:59 -0700 Subject: [PATCH] code cleanup --- data/ui/graphrunner.js | 77 ++++++++++++++--------------- lib/main.js | 110 +++++++---------------------------------- 2 files changed, 54 insertions(+), 133 deletions(-) diff --git a/data/ui/graphrunner.js b/data/ui/graphrunner.js index 6aedd43..553495c 100644 --- a/data/ui/graphrunner.js +++ b/data/ui/graphrunner.js @@ -1,11 +1,7 @@ -var GraphRunner = (function(jQuery, d3) { - - - +var GraphRunner = (function(jQuery, d3) { function Runner(options) { var trackers = options.trackers; - // FIXME: Make this.width / this.height properties on the runner and add an accessor to allow them to be updated when the window resizes this.width = options.width; this.height = options.height; var runner = this; @@ -84,7 +80,7 @@ var GraphRunner = (function(jQuery, d3) { $("#domain-infos .info").hide(); - // Instead of just cleraing out the domain info div and puttig in the new info each time, + // Instead of just clearing out the domain info div and puttig in the new info each time, // create a clone of the template for each new domain, and re-use that create a clone for each domain and then re-use it if it's already // created. An optimization? if (!info.length) { @@ -194,7 +190,7 @@ var GraphRunner = (function(jQuery, d3) { var fontSize = Math.floor(4 * r / 5); var labelWidth = Math.floor( d.name.length * fontSize / 2 ) + 4; /* rough heuristic for calculating size of label based on font size and character count - * (wish svg had the equivalent to cavnas's measureText!) */ + * (wish svg had the equivalent to canvas' measureText!) */ /* [dethe]: it does: getComputedTextLength() * http://www.w3.org/TR/SVG/text.html#__svg__SVGTextContentElement__getComputedTextLength */ @@ -412,7 +408,7 @@ var GraphRunner = (function(jQuery, d3) { // bind links to d3 lines - use source name + target name as 'primary key': var link = vis.select("g.links").selectAll("line.link") .data(links, function(d) { return d.sourceDomain + "-" + d.targetDomain; }); - link.enter().append("svg:line") + link.enter().insert("svg:line", ':first-child') .attr("class", getClassForLink) .style("stroke-width", 1) .attr("x1", function(d) { return d.source.x; }) @@ -488,10 +484,10 @@ var GraphRunner = (function(jQuery, d3) { var len = Math.sqrt( (d.source.x - d.target.x) * (d.source.x - d.target.x) + (d.source.y - d.target.y) * (d.source.y - d.target.y) ); var r = nodeRadius(d.target); - line.attr("x1", function(d) { return d.source.x; }) - .attr("y1", function(d) { return d.source.y; }) - .attr("x2", function(d) { return d.target.x + Math.floor((d.source.x - d.target.x) * r / len); }) - .attr("y2", function(d) { return d.target.y + Math.floor((d.source.y - d.target.y) * r / len); }); + line.attr("x1", d.source.x) + .attr("y1", d.source.y) + .attr("x2", d.target.x) + .attr("y2", d.target.y); }); vis.selectAll("g.node").attr("transform", function(d) { @@ -625,25 +621,30 @@ var GraphRunner = (function(jQuery, d3) { // Json contains list of domains, each with referrers. Each pair constitutes a link. // For each pair, add a link if it does not already exist. - for (var domain in json) { - for (var referrer in json[domain].referrers) { - var usedCookie = json[domain].referrers[referrer].cookie; - var usedNonCookie = json[domain].referrers[referrer].noncookie; - var userNavigated = (json[domain].referrers[referrer].datatypes.indexOf("user_navigation") > -1); - if (!userNavigated) { - // Don't add links if they were user-navigated: - addLink({from: referrer, to: domain, cookie: usedCookie, noncookie: usedNonCookie, + for (var domainName in json) { + var domain = json[domainName]; + for (var referrerName in domain.referrers) { + var referrer = domain.referrers[referrerName]; + var usedCookie = referrer.cookie; + var usedNonCookie = referrer.noncookie; + if (!referrer.datatypes){ + referrer.datatypes = []; + } + var userNavigated = referrer.datatypes.indexOf("user_navigation") > -1; + if (!userNavigated) { + // Don't add links if they were user-navigated: + addLink({from: referrerName, to: domainName, cookie: usedCookie, noncookie: usedNonCookie, userNavigated: userNavigated}); - } else { - /* If we find out about a user-navigated connection, remove any link that - * already exists from that referrer to that domain: */ - for (var l = 0; l < links.length; l++) { - if (referrer == links[l].sourceDomain && domain == links[l].targetDomain) { - links.splice(l, 1); - break; - } + }else{ + /* If we find out about a user-navigated connection, remove any link that + * already exists from that referrer to that domain: */ + for (var l = 0; l < links.length; l++) { + if (referrerName === links[l].sourceDomain && domainName === links[l].targetDomain) { + links.splice(l, 1); + break; + } + } } - } } } @@ -661,8 +662,8 @@ var GraphRunner = (function(jQuery, d3) { * Note that initializing them all exactly at center causes there to be zero distance, * which makes the repulsive force explode!! So add some random factor. */ if (typeof nodes[n].x == "undefined") { - nodes[n].x = nodes[n].px = runner.width / 2 + Math.floor( Math.random() * 50 ) ; - nodes[n].y = nodes[n].py = runner.height / 2 + Math.floor( Math.random() * 50 ); + nodes[n].x = nodes[n].px = runner.width / 2 + Math.floor( Math.random() * 100 ) - 50; + nodes[n].y = nodes[n].py = runner.height / 2 + Math.floor( Math.random() * 100 ) - 50; } } @@ -705,15 +706,12 @@ var GraphRunner = (function(jQuery, d3) { return function(json) { // TODO this comparison sometimes throws: // TypeError: attempt to run compile-and-go script on a cleared scope - if (timeoutID !== null) - clearTimeout(timeoutID); + if (timeoutID !== null){ + clearTimeout(timeoutID); + timeoutID = null; + } timeoutID = setTimeout(function() { - timeoutID = null; - - // This is for debugging purposes only! - self.lastJSON = json; - - graph.update(json); + graph.update(json); }, 250); // TODO the setTimeout call sometimes throws: // Illegal operation on WrappedNative prototype object @@ -745,7 +743,6 @@ var GraphRunner = (function(jQuery, d3) { rescaleSvg(); } }; - return self; } diff --git a/lib/main.js b/lib/main.js index c95e64b..10ccfa3 100644 --- a/lib/main.js +++ b/lib/main.js @@ -102,15 +102,21 @@ function attachToCollusionPage(worker) { }); worker.port.on("import", function(data) { var graph = JSON.parse(data); + // console.log('Graph size: ', Object.keys(graph).length); var maxTime = 0; for (var domain in graph) { - var referrers = graph[domain]; + var referrers = graph[domain].referrers; for (var referrer in referrers) { - if (referrers[referrer].timestamp > maxTime) - maxTime = referrers[referrer].timestamp; + if (referrers[referrer]){ + if (referrers[referrer].timestamp > maxTime){ + maxTime = referrers[referrer].timestamp; + } + }else{ + console.log('no referrer named ' + referrer + ' in domain ' + domain); + } } - } + } startTime = new Date() - maxTime; log = graph; @@ -118,9 +124,9 @@ function attachToCollusionPage(worker) { worker.port.on("whitelistDomain", function(data) { whitelistDomain(data.domain); }); - worker.port.on("uploadGraph", function() { - submitToMozilla(log); - }); + // worker.port.on("uploadGraph", function() { + // submitToMozilla(log); + // }); } @@ -201,7 +207,7 @@ function getDomain(host) { } function whitelistDomain(domain) { - console.log("Whitelisting domain " + domain); + // console.log("Whitelisting domain " + domain); deleteNode(domain); whitelist.push(domain); // TODO TEST storage.whitelist = JSON.stringify(whitelist); @@ -210,13 +216,13 @@ function whitelistDomain(domain) { function deleteNode(nodeDomain) { for (var domain in log) { if (log[domain].referrers[nodeDomain]) { - console.log("Removed link to " + domain + " from " + nodeDomain); + // console.log("Removed link to " + domain + " from " + nodeDomain); delete log[domain].referrers[nodeDomain]; } } if (log[nodeDomain]) { delete log[nodeDomain]; - console.log("Removed all links to " + nodeDomain); + // console.log("Removed all links to " + nodeDomain); } workers.forEach(function(worker) { @@ -224,89 +230,7 @@ function deleteNode(nodeDomain) { }); } -function submitToMozilla(graph) { - var submitUrl = "https://testpilot.mozillalabs.com/submit/collusion_graph"; - - /* Randomly split up the graph; schedule submission of each piece after random delay; - * mark each submitted piece as submitted so we don't submit it again. */ - - // make list of all domain-referrer pairs that have not yet been uploaded - console.log("Looking for unsubmitted edges."); - var edges = []; - for (var domain in graph) { - for (var referrer in graph[domain].referrers) { - console.log("Considering: " + graph[domain].referrers[referrer]); - if (!graph[domain].referrers[referrer].uploaded) { - edges.push([domain, referrer]); - } - } - } - console.log(edges); - if (edges.length == 0) { - // nothing to submit - return; - } - - function randomTime() { - var time = Math.floor(Math.random() * 20000); // up to 1 minute, for testing--- longer later - console.log("Waiting " + time + "ms"); - return time; - } - - var onTimeout = function(edges) { - console.log("Timeout done -- i'm doing this."); - var someEdges = [], restOfTheEdges = []; - if (edges.length < 10) { - // if there's only a few, just send them all - console.log("Less than 10, i send them all."); - someEdges = edges; - } else { - // otherwise, split: - for (var i = 0; i < edges.length; i++) { - if (Math.random() < 0.4) { // 2 in 5 chance of taking each edge - someEdges.push(edges[i]); - } else { - restOfTheEdges.push(edges[i]); - } - } - console.log("Here is the subset I will send: " + someEdges); - } - - var request = new xhr.XMLHttpRequest(); - request.open('POST', submitUrl, true); - request.onreadystatechange = function(e) { - if (request.readyState == 4) { - if (request.status == 200 || request.status == 201 || request.status == 202) { - console.log("Success!"); - for (var i = 0; i < someEdges.length; i++) { - // mark the edge uploaded so we don't upload it again - console.log("Marking " + someEdges[i][0] + "--" + someEdges[i][1] + " uploaded."); - graph[ someEdges[i][0] ].referrers[ someEdges[i][1] ].uploaded = true; - } - console.log("Graph: " + JSON.stringify(graph)); - if (restOfTheEdges.length > 0) { - // still some left? Do the rest at a random time later - timers.setTimeout(function() { onTimeout(restOfTheEdges);}, randomTime()); - } - } else { - console.log("Was error! " + request.status); - if (!(request.status == 404 || request.status == 401)) { - // give up on a 401 or 404 - it means we're submitting somewhere that will never work! - // on any other error code, try the graph again later: - timers.setTimeout(function() { onTimeout(edges);}, randomTime()); - // TODO use a longer random time here? - } - } - } - }; - request.send(JSON.stringify(someEdges)); - }; - - var timer = timers.setTimeout(function() { onTimeout(edges);}, randomTime()); -} - - - + // Main entry point. Will be called when Firefox starts or when Collusion is installed: function initCollusion() {