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
website/web/static/d3*.js
*.swp

View File

@ -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

View File

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

View File

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

View File

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