commit dcf39195932ab98e6e071b8c6843c03d74b2aca4 Author: Arturo Filastò arturo@filasto.net Date: Thu Aug 23 12:06:47 2012 +0200
Refactor path selection graph calculations --- js/models/graph.js | 204 +++++++++++++++++---------------------------------- 1 files changed, 68 insertions(+), 136 deletions(-)
diff --git a/js/models/graph.js b/js/models/graph.js index a3c5d37..232f651 100644 --- a/js/models/graph.js +++ b/js/models/graph.js @@ -5,6 +5,61 @@ define([ 'underscore', 'backbone' ], function($, _, Backbone){ + + function parseHistory(history, model, relay) { + _.each(_.keys(history), function(period, i) { + var first = history[period].first.split(' '); + var date = first[0].split('-'); + var time = first[1].split(':'); + //console.log(date); + //console.log(time); + first = new Date(date[0], date[1]-1, date[2], + time[0], time[1], time[2]); + var y = first.getTime(); + + _.each(history[period].values, function(value, i) { + y += history[period].interval*1000; + var x = null + if (value != null) { + x = value*history[period].factor; + } + + // This is quite a hack to conform to backbone.js + // funky way of setting and getting attributes in + // models. + // XXX probably want to refactor. + var mperiod = "bw_" + period.split("_")[1] + var newar = model.get(mperiod).write; + newar.push([y,x]); + var toset = {mperiod: {write: newar}}; + model.set(toset); + }); + }); + }; + + function parseWeightHistory(history, model, name) { + var newar; + _.each(_.keys(history), function(period, i) { + var first = history[period].first.split(' '); + var date = first[0].split('-'); + var time = first[1].split(':'); + first = new Date(date[0], date[1]-1, date[2], + time[0], time[1], time[2]); + var y = first.getTime(); + _.each(history[period].values, function(value, i) { + y += history[period].interval*1000; + var x = null + if (value != null) { + x = value*history[period].factor; + } + var mperiod = "weights_" + period.split("_")[1] + newar = model.get(mperiod)[name]; + newar.push([y,x]); + }); + }); + return newar; + }; + var graphModel = Backbone.Model.extend({ baseurl: 'https://onionoo.torproject.org', initialize: function() { @@ -44,58 +99,11 @@ define([ var model = this; var relay = data.relays[0]; this.fingerprint = relay.fingerprint; - // Parse the write history of the relay - var history = relay.write_history; - _.each(_.keys(relay.write_history), function(period, i) { - var first = history[period].first.split(' '); - var date = first[0].split('-'); - var time = first[1].split(':'); - //console.log(date); - //console.log(time); - first = new Date(date[0], date[1]-1, date[2], - time[0], time[1], time[2]); - var y = first.getTime(); - - _.each(history[period].values, function(value, i) { - y += history[period].interval*1000; - var x = null - if (value != null) { - x = value*history[period].factor; - } - - // This is quite a hack to conform to backbone.js - // funky way of setting and getting attributes in - // models. - // XXX probably want to refactor. - var mperiod = "bw_" + period.split("_")[1] - var newar = model.get(mperiod).write; - newar.push([y,x]); - var toset = {mperiod: {write: newar}}; - model.set(toset); - }); - }); - - var history = relay.read_history; - _.each(_.keys(relay.read_history), function(period, i) { - var first = history[period].first.split(' '); - var date = first[0].split('-'); - var time = first[1].split(':'); - first = new Date(date[0], date[1]-1, date[2], - time[0], time[1], time[2]); - var y = first.getTime(); - _.each(history[period].values, function(value, i) { - y += history[period].interval*1000; - var x = null - if (value != null) { - x = value*history[period].factor; - } - var mperiod = "bw_" + period.split("_")[1] - var newar = model.get(mperiod).read; - newar.push([y,x]); - var toset = {mperiod: {read: newar}}; - model.set(toset); - }); - }); + // Parse the read and write history of the relay + var write_history = parseHistory(relay.write_history, model, relay); + var read_history = parseHistory(relay.read_history, model, relay); + var toset = {mperiod: {read: read_history, write: write_history}}; + model.set(toset); }, lookup_weights: function(fingerprint, options) { var model = this; @@ -120,99 +128,23 @@ define([ this.fingerprint = relay.fingerprint;
if ("advertised_bandwidth_fraction" in relay) { - var history = relay.advertised_bandwidth_fraction; - _.each(_.keys(relay.advertised_bandwidth_fraction), function(period, i) { - var first = history[period].first.split(' '); - var date = first[0].split('-'); - var time = first[1].split(':'); - first = new Date(date[0], date[1]-1, date[2], - time[0], time[1], time[2]); - var y = first.getTime(); - _.each(history[period].values, function(value, i) { - y += history[period].interval*1000; - var x = null - if (value != null) { - x = value*history[period].factor; - } - var mperiod = "weights_" + period.split("_")[1] - var newar = model.get(mperiod).advbw; - newar.push([y,x]); - var toset = {mperiod: {advbw: newar}}; - model.set(toset); - }); - }); + var advbw = parseWeightHistory(relay.advertised_bandwidth_fraction, model, 'advbw'); + model.set({mperiod: {advbw: advbw}}); }
if ("consensus_weight_fraction" in relay) { - var history = relay.consensus_weight_fraction; - _.each(_.keys(relay.consensus_weight_fraction), function(period, i) { - var first = history[period].first.split(' '); - var date = first[0].split('-'); - var time = first[1].split(':'); - first = new Date(date[0], date[1]-1, date[2], - time[0], time[1], time[2]); - var y = first.getTime(); - _.each(history[period].values, function(value, i) { - y += history[period].interval*1000; - var x = null - if (value != null) { - x = value*history[period].factor; - } - var mperiod = "weights_" + period.split("_")[1] - var newar = model.get(mperiod).cw; - newar.push([y,x]); - var toset = {mperiod: {cw: newar}}; - model.set(toset); - }); - }); + var cw = parseWeightHistory(relay.consensus_weight_fraction, model, 'cw'); + model.set({mperiod: {cw: cw}}); }
if ("guard_probability" in relay) { - var history = relay.guard_probability; - _.each(_.keys(relay.guard_probability), function(period, i) { - var first = history[period].first.split(' '); - var date = first[0].split('-'); - var time = first[1].split(':'); - first = new Date(date[0], date[1]-1, date[2], - time[0], time[1], time[2]); - var y = first.getTime(); - _.each(history[period].values, function(value, i) { - y += history[period].interval*1000; - var x = null - if (value != null) { - x = value*history[period].factor; - } - var mperiod = "weights_" + period.split("_")[1] - var newar = model.get(mperiod).guard; - newar.push([y,x]); - var toset = {mperiod: {guard: newar}}; - model.set(toset); - }); - }); + var guard = parseWeightHistory(relay.guard_probability, model, 'guard'); + model.set({mperiod: {guard: guard}}); }
if ("exit_probability" in relay) { - var history = relay.exit_probability; - _.each(_.keys(relay.exit_probability), function(period, i) { - var first = history[period].first.split(' '); - var date = first[0].split('-'); - var time = first[1].split(':'); - first = new Date(date[0], date[1]-1, date[2], - time[0], time[1], time[2]); - var y = first.getTime(); - _.each(history[period].values, function(value, i) { - y += history[period].interval*1000; - var x = null - if (value != null) { - x = value*history[period].factor; - } - var mperiod = "weights_" + period.split("_")[1] - var newar = model.get(mperiod).exit; - newar.push([y,x]); - var toset = {mperiod: {exit: newar}}; - model.set(toset); - }); - }); + var exit = parseWeightHistory(relay.exit_probability, model, 'exit'); + model.set({mperiod: {exit: exit}}); } } })
tor-commits@lists.torproject.org