Skip to content
This repository has been archived by the owner on Sep 25, 2018. It is now read-only.

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dethe committed Oct 24, 2012
1 parent 76273cc commit a2bdc47
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 133 deletions.
77 changes: 37 additions & 40 deletions 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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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; })
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
}
}
}
}

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -745,7 +743,6 @@ var GraphRunner = (function(jQuery, d3) {
rescaleSvg();
}
};

return self;
}

Expand Down
110 changes: 17 additions & 93 deletions lib/main.js
Expand Up @@ -102,25 +102,31 @@ 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;
});
worker.port.on("whitelistDomain", function(data) {
whitelistDomain(data.domain);
});
worker.port.on("uploadGraph", function() {
submitToMozilla(log);
});
// worker.port.on("uploadGraph", function() {
// submitToMozilla(log);
// });
}


Expand Down Expand Up @@ -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);
Expand All @@ -210,103 +216,21 @@ 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) {
worker.port.emit("log", JSON.stringify(log));
});
}

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() {

Expand Down

0 comments on commit a2bdc47

Please sign in to comment.