Ticket #24045: 0001-Adds-additional-map-views-for-aggregated-bw-and-cw-b.patch

File 0001-Adds-additional-map-views-for-aggregated-bw-and-cw-b.patch, 6.4 KB (added by cypherpunks, 22 months ago)

(reviewed)

  • js/views/aggregate/map.js

    From 9e1fec8fa106d7382ddc53c52a8d21fffa6d82e6 Mon Sep 17 00:00:00 2001
    From: "Ana C. Custura" <ana@netstat.org.uk>
    Date: Mon, 18 Dec 2017 23:41:31 +0000
    Subject: [PATCH] Adds additional map views for aggregated bw and cw/bw
     fractions
    
    ---
     js/views/aggregate/map.js    | 85 ++++++++++++++++++++++++++++++++++++++------
     templates/aggregate/map.html |  2 ++
     2 files changed, 76 insertions(+), 11 deletions(-)
    
    diff --git a/js/views/aggregate/map.js b/js/views/aggregate/map.js
    index 9789913..37e65a9 100644
    a b define([ 
    4949
    5050      var g = svg.append("g");
    5151
    52       var maximum_value = 0;
    53       _.each(aggregates, function(aggregate) {
    54         if (aggregate[aggregate_property] > maximum_value) maximum_value = aggregate[aggregate_property];
    55       });
     52      var maximum_value = Number.NEGATIVE_INFINITY;
     53      var minimum_value = Number.POSITIVE_INFINITY;
    5654
    57       var getCountryAggregate = function(code, aggregate_property) {
     55      if (aggregate_property == "cw_bw") {
     56        _.each(aggregates, function(aggregate) {
     57          if (aggregate["advertised_bandwidth"] == 0) current_val = 0;
     58            else current_val = (aggregate["consensus_weight"]/aggregate["advertised_bandwidth"]);
     59          if (current_val > maximum_value) maximum_value = current_val;
     60          if (current_val < minimum_value) minimum_value = current_val;
     61       });
     62        var getCountryAggregate = function(code, aggregate_property) {
    5863        var found = 0;
    5964        _.each(aggregates, function(aggregate) {
    60           if (aggregate.country.toUpperCase() == code) found = aggregate[aggregate_property];
     65          if (aggregate.country.toUpperCase() == code)
     66            if (aggregate["advertised_bandwidth"] == 0) found = 0;
     67            else found=aggregate["consensus_weight"]/aggregate["advertised_bandwidth"];
    6168        });
    62 
     69        if (found < 1 && found > 0)  found = 1/(Math.sqrt(found/minimum_value));
     70          else found = 0-Math.sqrt(found/maximum_value);
     71        return (found == 0) ? found : found;
     72        }
     73     }
     74      else {
     75        _.each(aggregates, function(aggregate) {
     76          if (aggregate[aggregate_property] > maximum_value) maximum_value = aggregate[aggregate_property];
     77        });
     78        var getCountryAggregate = function(code, aggregate_property) {
     79          var found = 0;
     80          _.each(aggregates, function(aggregate) {
     81            if (aggregate.country.toUpperCase() == code) found = aggregate[aggregate_property];
     82          });
    6383        return (found == 0) ? found : Math.sqrt(found/maximum_value);
     84        }
    6485      }
    6586
    6687      d3.json("json/countries.topo.json", function(error, us) {
    define([ 
    7899
    79100      g.append("g")
    80101        .attr("id", "countries")
    81         .style("fill","#7d4698")
     102        .style("fill", "#7d4698")
    82103        .style("stroke", "#484848")
    83104        .style("stroke-linejoin", "round")
    84105        .style("stroke-linecap", "round")
    define([ 
    87108        .enter()
    88109          .append("path")
    89110            .attr("id", function(d) { return d.id; })
    90             .style("fill-opacity", function(d) { return getCountryAggregate(d.id, aggregate_property); })
     111            .style("fill", function(d) { return (getCountryAggregate(d.id, aggregate_property) > 0) ? "#7d4698" : "#68b030"; })
     112            .style("fill-opacity", function(d) { return Math.abs(getCountryAggregate(d.id, aggregate_property)); })
    91113            .attr("d", path)
    92114          .append("svg:title")
    93115            .text( function(d) { return d.id; });
    94116
     117
     118    function append_legend() {
    95119      for (var i = 0; i <= 1; i += 0.2) {
    96120        svg.append("rect")
    97121          .attr("x", 10)
    define([ 
    114138          .attr("y", height-(i*5+0.5)*20 )
    115139          .style("font-size", "12px")
    116140          .style("fill", "#484848")
    117           .text("" + (Math.pow(i,2)* maximum_value*100).toFixed(3) + "%");
    118       }
     141          .text( function() {
     142            return (aggregate_property == "advertised_bandwidth") ?
     143             "" + (Math.pow(i,2)* maximum_value/(1024*1024)).toFixed(2) + "MiB/s" :
     144             "" + (Math.pow(i,2)* maximum_value*100).toFixed(3) + "%";
     145          });
     146       }
     147    }
     148  if (aggregate_property == "cw_bw") {
     149      legend = (maximum_value >1) ? 0 : 1;
     150      for (var i = legend; i <= 2 ; i += 0.2) {
     151        j = Math.abs(i-1);
     152        svg.append("rect")
     153          .attr("x", 10)
     154          .attr("y", height-((i-legend)*5+1)*20)
     155          .attr("height", "10")
     156          .attr("width", "15")
     157          .style("fill", "#fff");
     158
     159        svg.append("rect")
     160          .attr("x", 10)
     161          .attr("y", height-((i-legend)*5+1)*20)
     162          .attr("height", "10")
     163          .attr("width", "15")
     164          .style("fill", function() { return (i>1) ? "#7d4698" : "#68b090"; })
     165          .style("fill-opacity", function() { return j; })
     166          .style("stroke", "#484848");
     167
     168        svg.append("text")
     169          .attr("x", 30)
     170          .attr("y", height-((i-legend)*5+0.5)*20)
     171          .style("font-size", "12px")
     172          .style("fill", "#484848")
     173          .text(function(){
     174           if (j==0) return "1:1";
     175           return (i<1) ? "" + (Math.pow(j,2)*maximum_value).toFixed(0) + ":1" :
     176                          "1:" + (Math.pow(j,2)*(1/minimum_value)).toFixed(0);
     177
     178         });
     179       }
     180     }
     181   else append_legend();
    119182
    120183      $("#aggregate-map").html("");
    121184      document.getElementById("aggregate-map").appendChild(svg.node());
  • templates/aggregate/map.html

    diff --git a/templates/aggregate/map.html b/templates/aggregate/map.html
    index 8282e74..aadbddb 100644
    a b  
    5151     <label class="radio-inline"><input type="radio" name="aggregate-property" value="guard_probability"> Guard Probability</label>
    5252     <label class="radio-inline"><input type="radio" name="aggregate-property" value="middle_probability"> Middle Probability</label>
    5353     <label class="radio-inline"><input type="radio" name="aggregate-property" value="exit_probability"> Exit Probability</label>
     54     <label class="radio-inline"><input type="radio" name="aggregate-property" value="advertised_bandwidth"> Advertised Bandwidth</label>
     55     <label class="radio-inline"><input type="radio" name="aggregate-property" value="cw_bw"> Consensus Weight versus Bandwidth</label>
    5456   </form>
    5557   <a class="btn btn-primary" href="#aggregate/cc<%= (query) ? "/" + query : "" %>">View Table</a>
    5658   <a class="btn btn-secondary" id="save_svg">Save Map</a>