new: Details in the country view

pull/12/head
Raphaël Vinot 2018-07-31 10:54:38 +02:00
parent 130f6a85c7
commit c42e9d1e18
5 changed files with 46 additions and 4 deletions

2
.gitignore vendored
View File

@ -118,3 +118,5 @@ bgpranking/config/modules/shadowserver_*.json
# Do not store the d3 lib in the repo # Do not store the d3 lib in the repo
website/web/static/d3*.js website/web/static/d3*.js
*.swp

View File

@ -121,8 +121,12 @@ class Querying():
response['data']['countries'][0].get('routed')): response['data']['countries'][0].get('routed')):
logging.warning(f'Invalid response: {response}') logging.warning(f'Invalid response: {response}')
# FIXME: return something # FIXME: return something
return return 0, [(0, 0)]
return sum([self.asn_rank(asn, d, source, ipversion) for asn in response['data']['countries'][0]['routed']]) 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()): def country_history(self, country: str, period: int=30, source: str='', ipversion: str='v4', date: Dates=datetime.date.today()):
to_return = [] to_return = []
@ -135,8 +139,8 @@ class Querying():
for i in range(period): for i in range(period):
d = date - timedelta(days=i) 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: if rank is None:
rank = 0 rank = 0
to_return.insert(0, (d.isoformat(), rank)) to_return.insert(0, (d.isoformat(), rank, list(details)))
return to_return return to_return

View File

@ -9,6 +9,7 @@ from flask_bootstrap import Bootstrap
from bgpranking.querying import Querying from bgpranking.querying import Querying
from datetime import date, timedelta from datetime import date, timedelta
import pycountry import pycountry
from collections import defaultdict
app = Flask(__name__) app = Flask(__name__)
@ -95,6 +96,32 @@ def asn_history():
return Response(json.dumps(q.get_asn_history(**session)), mimetype='application/json') 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']) @app.route('/country_history', methods=['GET', 'POST'])
def country_history(): def country_history():
load_session() load_session()

View File

@ -36,6 +36,14 @@ function linegraph(call_path) {
context.lineWidth = 1.5; context.lineWidth = 1.5;
context.strokeStyle = "steelblue"; context.strokeStyle = "steelblue";
context.stroke(); 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() { function xAxis() {

View File

@ -19,4 +19,5 @@
</center> </center>
{% include ['top_forms.html'] %} {% include ['top_forms.html'] %}
<canvas width="1024" height="800"></canvas> <canvas width="1024" height="800"></canvas>
<div id="asn_details"></div>
{% endblock %} {% endblock %}