Server to collect DHT views from yggdrasil nodes and store in a data base
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.2 KiB

#!/usr/bin/env python
import time
6 years ago
from flask import Flask, render_template
from flask_restful import Resource, Api
import requests
import psycopg2
import json
6 years ago
app = Flask(__name__)
api = Api(app)
DB_PASSWORD = "password"
DB_USER = "yggindex"
DB_NAME = "yggindex"
DB_HOST = "localhost"
# count peer alive if it was available not more that amount of seconds ago
# I'm using 1 hour beause of running crawler every 15 minutes
ALIVE_SECONDS = 3600 # 1 hour
6 years ago
def age_calc(ustamp):
if (time.time() - ustamp) <= ALIVE_SECONDS:
6 years ago
return True
else:
return False
# active nodes
6 years ago
class nodesCurrent(Resource):
6 years ago
def get(self):
6 years ago
dbconn = psycopg2.connect(host=DB_HOST,\
database=DB_NAME,\
user=DB_USER,\
password=DB_PASSWORD)
cur = dbconn.cursor()
6 years ago
nodes = {}
cur.execute("select * from yggindex")
for i in cur.fetchall():
if age_calc(int(i[2])):
6 years ago
nodes[i[0]] = [i[1], int(i[2])]
dbconn.commit()
cur.close()
dbconn.close()
6 years ago
nodelist = {}
nodelist['yggnodes'] = nodes
6 years ago
return nodelist
# nodes info
class nodesInfo(Resource):
def get(self):
dbconn = psycopg2.connect(host=DB_HOST,\
database=DB_NAME,\
user=DB_USER,\
password=DB_PASSWORD)
cur = dbconn.cursor()
nodes = {}
cur.execute("select * from yggnodeinfo")
for i in cur.fetchall():
if age_calc(int(i[2])):
nodes[i[0]] = json.loads(i[1])
dbconn.commit()
cur.close()
dbconn.close()
nodeinfo = {}
nodeinfo['yggnodeinfo'] = nodes
return nodeinfo
# alive nodes count for latest 24 hours
class nodes24h(Resource):
def get(self):
dbconn = psycopg2.connect(host=DB_HOST,\
database=DB_NAME,\
user=DB_USER,\
password=DB_PASSWORD)
cur = dbconn.cursor()
nodes = {}
cur.execute("SELECT * FROM timeseries ORDER BY unixtstamp DESC LIMIT 24")
for i in cur.fetchall():
nodes[i[1]] = i[0]
dbconn.commit()
cur.close()
dbconn.close()
nodeinfo = {}
nodeinfo['nodes24h'] = nodes
return nodeinfo
@app.route("/")
def fpage():
6 years ago
dbconn = psycopg2.connect(host=DB_HOST,\
database=DB_NAME,\
user=DB_USER,\
password=DB_PASSWORD)
cur = dbconn.cursor()
nodes = 0
cur.execute("select * from yggindex")
for i in cur.fetchall():
if age_calc(int(i[2])):
nodes += 1
dbconn.commit()
cur.close()
dbconn.close()
return render_template('index.html', nodes=nodes)
6 years ago
#sort out the api request here for the url
6 years ago
api.add_resource(nodesCurrent, '/current')
api.add_resource(nodesInfo, '/nodeinfo')
api.add_resource(nodes24h, '/nodes24h')
6 years ago
if __name__ == '__main__':
6 years ago
app.run(host='::', port=3000)