diff --git a/.gitignore b/.gitignore index 1750393..620a00b 100644 --- a/.gitignore +++ b/.gitignore @@ -118,3 +118,5 @@ bgpranking/config/modules/shadowserver_*.json # Do not store the d3 lib in the repo website/web/static/d3*.js + +*.swp diff --git a/bgpranking/querying.py b/bgpranking/querying.py index 9bafe69..4657ab0 100644 --- a/bgpranking/querying.py +++ b/bgpranking/querying.py @@ -121,8 +121,12 @@ class Querying(): response['data']['countries'][0].get('routed')): logging.warning(f'Invalid response: {response}') # FIXME: return something - return - return sum([self.asn_rank(asn, d, source, ipversion) for asn in response['data']['countries'][0]['routed']]) + return 0, [(0, 0)] + routed_asns = response['data']['countries'][0]['routed'] + ranks = [self.asn_rank(asn, d, source, ipversion) for asn in routed_asns] + to_return = zip(routed_asns, ranks) + daily_sum = sum(ranks) + return daily_sum, to_return def country_history(self, country: str, period: int=30, source: str='', ipversion: str='v4', date: Dates=datetime.date.today()): to_return = [] @@ -135,8 +139,8 @@ class Querying(): for i in range(period): d = date - timedelta(days=i) - rank = self.country_rank(country, d, source, ipversion) + rank, details = self.country_rank(country, d, source, ipversion) if rank is None: rank = 0 - to_return.insert(0, (d.isoformat(), rank)) + to_return.insert(0, (d.isoformat(), rank, list(details))) return to_return diff --git a/website/web/__init__.py b/website/web/__init__.py index f21c731..0660dcc 100644 --- a/website/web/__init__.py +++ b/website/web/__init__.py @@ -9,6 +9,7 @@ from flask_bootstrap import Bootstrap from bgpranking.querying import Querying from datetime import date, timedelta import pycountry +from collections import defaultdict app = Flask(__name__) @@ -95,6 +96,32 @@ def asn_history(): return Response(json.dumps(q.get_asn_history(**session)), mimetype='application/json') +@app.route('/country_history_callback', methods=['GET', 'POST']) +def country_history_callback(): + history_data = json.loads(request.data) + to_display = [] + mapping = defaultdict(dict) + dates = [] + all_asns = set([]) + for d, r_sum, details in history_data: + dates.append(d) + for detail in details: + asn, r = detail + all_asns.add(asn) + mapping[asn][d] = r + + to_display = [[''] + dates] + for a in sorted(list(all_asns), key=int): + line = [a] + for d in dates: + if mapping[a].get(d) is not None: + line.append(round(mapping[a].get(d), 3)) + else: + line.append('N/A') + to_display.append(line) + return json.dumps(render_template('country_asn_map.html', to_display=to_display)) + + @app.route('/country_history', methods=['GET', 'POST']) def country_history(): load_session() diff --git a/website/web/static/linegraph.js b/website/web/static/linegraph.js index 57a0867..e5e6389 100644 --- a/website/web/static/linegraph.js +++ b/website/web/static/linegraph.js @@ -36,6 +36,14 @@ function linegraph(call_path) { context.lineWidth = 1.5; context.strokeStyle = "steelblue"; context.stroke(); + d3.json(call_path + '_callback', + {credentials: 'same-origin', + method: 'POST', + body: JSON.stringify(data), + // headers: {'Content-Type': 'application/json'} + }).then(function(data) { + d3.select('#asn_details').html(data); + }); }); function xAxis() { diff --git a/website/web/templates/country.html b/website/web/templates/country.html index ad245e4..7dc0b9c 100644 --- a/website/web/templates/country.html +++ b/website/web/templates/country.html @@ -19,4 +19,5 @@ {% include ['top_forms.html'] %} +
{% endblock %}