adding healthchecks

This commit is contained in:
lza_menace 2020-10-24 01:29:10 -07:00
parent 6a1ecc0b7d
commit bb22147dd0
4 changed files with 47 additions and 9 deletions

View File

@ -11,7 +11,7 @@ from flask import render_template, flash, url_for
from urllib.parse import urlparse
from xmrnodes.helpers import determine_crypto, is_onion, make_request
from xmrnodes.forms import SubmitNode
from xmrnodes.models import Node
from xmrnodes.models import Node, HealthCheck
from xmrnodes import config
@ -50,7 +50,7 @@ def index():
)
if onion:
nodes = nodes.where(Node.is_tor==True)
paginated = nodes.paginate(page, itp)
total_pages = nodes.count() / itp
return render_template(
@ -93,6 +93,7 @@ def check():
nodes = Node.select().where(Node.validated == True)
for node in nodes:
now = datetime.utcnow()
hc = HealthCheck(node=node)
logging.info(f"Attempting to check {node.url}")
try:
r = make_request(node.url)
@ -103,15 +104,18 @@ def check():
logging.info("success")
node.available = True
node.last_height = r.json()["height"]
hc.health = True
else:
raise
except:
logging.info("fail")
node.datetime_failed = now
node.available = False
hc.health = False
finally:
node.datetime_checked = now
node.save()
hc.save()
@app.cli.command("validate")
def validate():
@ -171,11 +175,14 @@ def import_():
export_dir = f"{config.DATA_DIR}/export.txt"
with open(export_dir, 'r') as f:
for url in f.readlines():
n = url.rstrip()
all_nodes.append(n)
logging.info(f"Adding {n}")
node = Node(url=n)
node.save()
try:
n = url.rstrip()
logging.info(f"Adding {n}")
node = Node(url=n)
node.save()
all_nodes.append(n)
except:
pass
logging.info(f"{len(all_nodes)} node urls imported and ready to be validated")
@app.template_filter("humanize")

View File

@ -7,7 +7,7 @@ db = SqliteDatabase(f"{config.DATA_DIR}/sqlite.db")
class Node(Model):
id = AutoField()
url = CharField()
url = CharField(unique=True)
is_tor = BooleanField(default=False)
available = BooleanField(default=False)
validated = BooleanField(default=False)
@ -22,4 +22,13 @@ class Node(Model):
class Meta:
database = db
db.create_tables([Node])
class HealthCheck(Model):
id = AutoField()
node = ForeignKeyField(Node, backref='healthchecks')
datetime = DateTimeField(default=datetime.utcnow)
health = BooleanField()
class Meta:
database = db
db.create_tables([Node, HealthCheck])

View File

@ -33,3 +33,18 @@ input[type="text"] {
.wownero {
background-color: rgb(235, 74, 206);
}
.dot {
height: 10px;
width: 10px;
border-radius: 50%;
display: inline-block;
}
.glowing-green {
background-color: #00cd00;
}
.glowing-red {
background-color: #cd0000;
}

View File

@ -38,6 +38,7 @@
<th>Network</th>
<th>Height</th>
<th>Last Checked</th>
<th>History</th>
</tr>
</thead>
<tbody>
@ -54,6 +55,12 @@
<td>{{ node.nettype }}</td>
<td>{{ node.last_height }}</td>
<td>{{ node.datetime_checked | humanize }}</td>
<td>{% for hc in node.healthchecks %}
{% if loop.index > loop.length - 11 %}
<span class="dot glowing-{% if hc.health %}green{% else %}red{% endif %}"></span>
{% endif %}
{% endfor %}
</td>
</tr>
{% endfor %}
</tbody>