Super basic real time monitor; updates states.

pull/691/head
jMyles 2019-01-26 20:01:49 -05:00
parent b46858ef18
commit b86474ad3d
2 changed files with 236 additions and 0 deletions

View File

@ -0,0 +1,63 @@
import sys
from flask import Flask, render_template
from twisted.logger import globalLogPublisher
from hendrix.deploy.base import HendrixDeploy
from hendrix.experience import crosstown_traffic, hey_joe
from nucypher.characters.base import Character
from nucypher.characters.lawful import Ursula
from nucypher.config.constants import GLOBAL_DOMAIN
from nucypher.network.middleware import RestMiddleware
from nucypher.utilities.logging import SimpleObserver
websocket_service = hey_joe.WebSocketService("127.0.0.1", 9000)
globalLogPublisher.addObserver(SimpleObserver())
known_node = Ursula.from_seed_and_stake_info(seed_uri=sys.argv[1],
federated_only=True,
minimum_stake=0)
rest_app = Flask("fleet-monitor")
class Moe(Character):
"""
A monitor (lizard?)
"""
monitor = Moe(
domains=GLOBAL_DOMAIN,
network_middleware=RestMiddleware(),
known_nodes=[known_node],
federated_only=True,
)
monitor.start_learning_loop()
import time
import json
def send_states(subscriber):
message = {"states": monitor.known_nodes.abridged_states_dict()}
subscriber.sendMessage(json.dumps(message).encode())
websocket_service.register_followup("states", send_states)
@rest_app.route("/")
def status():
# for node in monitor.known_nodes:
# hey_joe.send(node.status_json(), topic="nodes")
return render_template('monitor.html')
deployer = HendrixDeploy(action="start", options={"wsgi": rest_app, "http_port": 9750})
deployer.add_non_tls_websocket_service(websocket_service)
deployer.run()

View File

@ -0,0 +1,173 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="icon" type="image/x-icon" href="https://www.nucypher.com/favicon-32x32.png"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.12/handlebars.js"></script>
<style type="text/css">
html {
font-family: sans-serif;
}
table, th, td {
border: 1px solid black;
}
.nucypher-nickname-icon {
border-width: 10px;
border-style: solid;
margin: 3px;
padding: 3px;
text-align: center;
box-shadow: 1px 1px black, -1px -1px black;
width: 100px;
}
.small {
float: left;
width: 100%;
text-shadow: none;
font-family: sans;
font-size: 10px;
}
.symbols {
float: left;
width: 100%;
}
.single-symbol {
font-size: 3em;
color: black;
text-shadow: 1px 1px black, -1px -1px black;
}
.address, .small-address {
font-family: monospace;
}
.small-address {
text-shadow: none;
}
.state {
float: left;
}
#previous-states {
float: left;
clear: left;
}
#states .state {
margin: left: 10px;
border-right: 3px solid black;
}
#states .nucypher-nickname-icon {
height: 75px;
width: 75px;
}
#states .single-symbol {
font-size: 2em;
}
#known-nodes {
float: left;
clear: left;
}
.small-address {
text-shadow: none;
}
.state {
float: left;
}
#states {
float: left;
clear: left;
}
#states .state {
margin: left: 10px;
border-right: 3px solid black;
}
#states .nucypher-nickname-icon {
height: 75px;
width: 75px;
}
#states .single-symbol {
font-size: 2em;
}
#known-nodes {
float: left;
clear: left;
}
</style>
{% raw %}
<script id="state-template" type="text/x-handlebars-template">
<h5>{{nickname}}</h5>
<div class="body">
<div class="single-symbol">{{metadata.[0].[1]}}</div>
{{updated}}
</div>
</script>
{% endraw %}
<script type="text/javascript">
window.onload = function () {
var stateTemplate = Handlebars.compile(document.getElementById("state-template").innerHTML);
const socket = new WebSocket("ws://localhost:9000");
socket.binaryType = "arraybuffer";
socket.onopen = function () {
socket.send(JSON.stringify({'hx_subscribe': 'states'}));
socket.send(JSON.stringify({'hx_subscribe': 'nodes'}));
isopen = true;
}
socket.addEventListener('message', function (event) {
console.log("Message from server ", event.data);
if (event.data.startsWith("{\"states\"")) {
message = JSON.parse(event.data);
Object.entries(message.states).forEach((state) => {
var stateDiv = document.getElementById(state[0]);
if (stateDiv == null) {
var stateDiv = document.createElement("div");
stateDiv.setAttribute("id", state[0]);
stateDiv.className = "state";
statesDiv = document.getElementById("states");
statesDiv.appendChild(stateDiv)
}
var stateHtml = stateTemplate(state[1]);
stateDiv.innerHTML = stateHtml;
console.log(state[1]);
})
}
;
});
socket.onerror = function (error) {
console.log(error.data);
}
}
</script>
</head>
<div id="states"></div>
</html>