map page load improvements

main
lza_menace 7 months ago
parent b014ba2e31
commit 28ef3e2fe1

@ -1,6 +1,8 @@
import re import re
from random import shuffle from random import shuffle
from math import ceil
import arrow
from flask import request, redirect, Blueprint from flask import request, redirect, Blueprint
from flask import render_template, flash, Response from flask import render_template, flash, Response
from urllib.parse import urlparse from urllib.parse import urlparse
@ -63,16 +65,36 @@ def index():
@bp.route("/map") @bp.route("/map")
def map(): def map():
try: fetch = request.args.get("fetch")
peers = Peer.select() if fetch:
nodes = Node.select().where(Node.datetime_checked) _peers = {}
except: next = None
flash("Couldn't load the map. Try again later.") limit = 100
return redirect("/") rgb = "238,111,45"
offset = request.args.get("offset", 0)
offset = int(offset)
peers = Peer.select().order_by(Peer.datetime.asc())
paginated_peers = peers.paginate(offset, limit)
total = ceil(peers.count() / limit)
if offset < total:
next = offset + 1
for peer in paginated_peers:
opacity = 1 - (peer.hours_elapsed() / 100)
_peers[peer.url] = {
"rgba": f"rgba({rgb},{opacity})",
"lat": peer.lat,
"lon": peer.lon,
"last_seen": arrow.get(peer.datetime).humanize()
}
return {
"offset": offset,
"next": next,
"total": total,
"peers": _peers
}
return render_template( return render_template(
"map.html", "map.html",
peers=peers, recent_peers=Peer.select().count(),
nodes=nodes,
source_node=config.NODE_HOST source_node=config.NODE_HOST
) )

@ -56,7 +56,7 @@
{% endwith %} {% endwith %}
<div class="center info"> <div class="center info">
<p>Recent Peers: {{ peers | length }}</p> <p>Recent Peers: {{ recent_peers }}</p>
<p>Source Node: {{ source_node }}</p> <p>Source Node: {{ source_node }}</p>
<p> <p>
This is not a full representation of the entire Monero network, This is not a full representation of the entire Monero network,
@ -89,7 +89,8 @@
target: 'map', target: 'map',
layers: [ layers: [
new ol.layer.Tile({ new ol.layer.Tile({
source: new ol.source.OSM() source: new ol.source.OSM(),
preload: 4
}), }),
markerLayer markerLayer
], ],
@ -99,73 +100,43 @@
}) })
}); });
{% for peer in nodes %}
{% if peer.datetime_checked | hours_elapsed > 24 %}
{% set fill_color = 'rgba(1,1,45,.2)' %}
{% elif peer.datetime_checked | hours_elapsed > 16 %}
{% set fill_color = 'rgba(1,1,45,.4)' %}
{% elif peer.datetime_checked | hours_elapsed > 8 %}
{% set fill_color = 'rgba(1,1,45,.6)' %}
{% elif peer.datetime_checked | hours_elapsed > 4 %}
{% set fill_color = 'rgba(1,1,45,.8)' %}
{% else %}
{% set fill_color = 'rgba(1,1,45,1)' %}
{% endif %}
var feature = new ol.Feature(
new ol.geom.Point(ol.proj.transform(['{{ peer.lon }}', '{{ peer.lat }}'], 'EPSG:4326', 'EPSG:3857'))
);
feature.description = [
'Node {{ peer.url }}',
'Last seen {{ peer.datetime_checked | humanize }}'
];
feature.setStyle(new ol.style.Style({
image: new ol.style.Circle({
radius: 6,
fill: new ol.style.Fill({
color: '{{ fill_color }}',
}),
stroke: new ol.style.Stroke({
color: '#fff',
width: 1
})
})
}));
markerLayer.getSource().addFeature(feature);
{% endfor %}
{% for peer in peers %} fetch('?fetch=1')
{% if peer.datetime | hours_elapsed > 24 %} .then((response) => response.json())
{% set fill_color = 'rgba(238,111,45,.2)' %} .then((data) => {
{% elif peer.datetime | hours_elapsed > 16 %} let total = data.total;
{% set fill_color = 'rgba(238,111,45,.4)' %} for(i=0;i<total+1;i++) {
{% elif peer.datetime | hours_elapsed > 8 %} fetch(`?fetch=1&offset=${i}`)
{% set fill_color = 'rgba(238,111,45,.6)' %} .then((res) => res.json())
{% elif peer.datetime | hours_elapsed > 4 %} .then((d) => {
{% set fill_color = 'rgba(238,111,45,.8)' %} setTimeout(function() {
{% else %} for (var url in d.peers) {
{% set fill_color = 'rgba(238,111,45,1)' %} let peer = d.peers[url];
{% endif %} var feature = new ol.Feature(
var feature = new ol.Feature( new ol.geom.Point(ol.proj.transform([peer.lon, peer.lat], 'EPSG:4326', 'EPSG:3857'))
new ol.geom.Point(ol.proj.transform(['{{ peer.lon }}', '{{ peer.lat }}'], 'EPSG:4326', 'EPSG:3857')) );
); feature.description = [
feature.description = [ `Node ${url}`,
'Peer {{ peer.url }}', `Last Seen ${peer.last_seen}`
'Last seen {{ peer.datetime | humanize }}' ];
]; feature.setStyle(new ol.style.Style({
feature.setStyle(new ol.style.Style({ image: new ol.style.Circle({
image: new ol.style.Circle({ radius: 6,
radius: 6, fill: new ol.style.Fill({
fill: new ol.style.Fill({ color: peer.rgba,
color: '{{ fill_color }}', }),
}), stroke: new ol.style.Stroke({
stroke: new ol.style.Stroke({ color: '#fff',
color: '#fff', width: 1
width: 1 })
}) })
}));
markerLayer.getSource().addFeature(feature);
}
}, 400)
})
}
}) })
}));
markerLayer.getSource().addFeature(feature);
{% endfor %}
// Setup popup // Setup popup
var popup = new ol.Overlay({ var popup = new ol.Overlay({
@ -199,7 +170,7 @@
// Wait for full load // Wait for full load
addEventListener("DOMContentLoaded", (event) => { addEventListener("DOMContentLoaded", (event) => {
setTimeout(loadMap, 1000); setTimeout(loadMap, 1500);
}); });
</script> </script>

Loading…
Cancel
Save