mirror of https://github.com/r4sas/Niflheim-api
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.
184 lines
4.2 KiB
184 lines
4.2 KiB
4 years ago
|
#!/usr/bin/env python
|
||
|
|
||
4 years ago
|
import signal, sys, time
|
||
1 year ago
|
from functools import wraps
|
||
|
from flask import Flask, Response, render_template
|
||
6 years ago
|
from flask_restful import Resource, Api
|
||
6 years ago
|
import requests
|
||
6 years ago
|
import psycopg2
|
||
4 years ago
|
import json
|
||
1 year ago
|
from config import DB_PASS, DB_USER, DB_NAME, DB_HOST, DB_RETRIES, DB_RECONNIDLE, ALIVE_SECONDS
|
||
6 years ago
|
|
||
4 years ago
|
######
|
||
6 years ago
|
|
||
1 year ago
|
app = Flask(__name__)
|
||
|
api = Api(app)
|
||
6 years ago
|
|
||
1 year ago
|
def pg_connect():
|
||
|
return psycopg2.connect(host=DB_HOST,\
|
||
|
database=DB_NAME,\
|
||
|
user=DB_USER,\
|
||
|
password=DB_PASS)
|
||
6 years ago
|
|
||
4 years ago
|
|
||
1 year ago
|
# dbconn = pg_connect() # initialize connection
|
||
|
|
||
|
|
||
|
def retry(fn):
|
||
|
@wraps(fn)
|
||
|
def wrapper(*args, **kw):
|
||
|
for x in range(DB_RETRIES):
|
||
|
try:
|
||
|
return fn(*args, **kw)
|
||
|
except (psycopg2.InterfaceError, psycopg2.OperationalError) as e:
|
||
|
print ("\nDatabase Connection [InterfaceError or OperationalError]")
|
||
|
print ("Idle for %s seconds" % (cls._reconnectIdle))
|
||
|
time.sleep(DB_RECONNIDLE)
|
||
|
dbconn = pg_connect()
|
||
|
return wrapper
|
||
4 years ago
|
|
||
|
|
||
|
def signal_handler(sig, frame):
|
||
|
dbconn.close()
|
||
|
sys.exit(0)
|
||
6 years ago
|
|
||
1 year ago
|
|
||
6 years ago
|
def age_calc(ustamp):
|
||
4 years ago
|
if (time.time() - ustamp) <= ALIVE_SECONDS:
|
||
6 years ago
|
return True
|
||
|
else:
|
||
|
return False
|
||
|
|
||
1 year ago
|
|
||
4 years ago
|
# active nodes
|
||
6 years ago
|
class nodesCurrent(Resource):
|
||
1 year ago
|
@retry
|
||
6 years ago
|
def get(self):
|
||
1 year ago
|
dbconn = pg_connect()
|
||
6 years ago
|
cur = dbconn.cursor()
|
||
1 year ago
|
|
||
6 years ago
|
nodes = {}
|
||
6 years ago
|
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])]
|
||
6 years ago
|
|
||
6 years ago
|
dbconn.commit()
|
||
|
cur.close()
|
||
6 years ago
|
|
||
1 year ago
|
nodeinfo = {}
|
||
|
nodeinfo['yggnodes'] = nodes
|
||
6 years ago
|
|
||
1 year ago
|
dbconn.close()
|
||
|
return nodeinfo
|
||
6 years ago
|
|
||
6 years ago
|
|
||
4 years ago
|
# nodes info
|
||
|
class nodesInfo(Resource):
|
||
1 year ago
|
@retry
|
||
4 years ago
|
def get(self):
|
||
1 year ago
|
dbconn = pg_connect()
|
||
4 years ago
|
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])
|
||
6 years ago
|
|
||
4 years ago
|
dbconn.commit()
|
||
|
cur.close()
|
||
6 years ago
|
|
||
4 years ago
|
nodeinfo = {}
|
||
|
nodeinfo['yggnodeinfo'] = nodes
|
||
6 years ago
|
|
||
1 year ago
|
dbconn.close()
|
||
4 years ago
|
return nodeinfo
|
||
6 years ago
|
|
||
|
|
||
4 years ago
|
# alive nodes count for latest 24 hours
|
||
|
class nodes24h(Resource):
|
||
1 year ago
|
@retry
|
||
4 years ago
|
def get(self):
|
||
1 year ago
|
dbconn = pg_connect()
|
||
4 years ago
|
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()
|
||
|
|
||
|
nodeinfo = {}
|
||
|
nodeinfo['nodes24h'] = nodes
|
||
|
|
||
1 year ago
|
dbconn.close()
|
||
4 years ago
|
return nodeinfo
|
||
|
|
||
4 years ago
|
|
||
|
# alive nodes count for latest 30 days
|
||
|
class nodes30d(Resource):
|
||
1 year ago
|
@retry
|
||
4 years ago
|
def get(self):
|
||
1 year ago
|
dbconn = pg_connect()
|
||
4 years ago
|
cur = dbconn.cursor()
|
||
|
nodes = {}
|
||
|
cur.execute("SELECT * FROM timeseries ORDER BY unixtstamp DESC LIMIT 24 * 30")
|
||
|
for i in cur.fetchall():
|
||
|
nodes[i[1]] = i[0]
|
||
|
|
||
|
dbconn.commit()
|
||
|
cur.close()
|
||
|
|
||
|
nodeinfo = {}
|
||
|
nodeinfo['nodes30d'] = nodes
|
||
|
|
||
1 year ago
|
dbconn.close()
|
||
4 years ago
|
return nodeinfo
|
||
|
|
||
|
|
||
4 years ago
|
@app.route("/")
|
||
1 year ago
|
@retry
|
||
4 years ago
|
def fpage():
|
||
1 year ago
|
dbconn = pg_connect()
|
||
6 years ago
|
cur = dbconn.cursor()
|
||
4 years ago
|
nodes = 0
|
||
|
cur.execute("select * from yggindex")
|
||
6 years ago
|
|
||
6 years ago
|
for i in cur.fetchall():
|
||
4 years ago
|
if age_calc(int(i[2])):
|
||
|
nodes += 1
|
||
6 years ago
|
|
||
6 years ago
|
dbconn.commit()
|
||
|
cur.close()
|
||
|
|
||
1 year ago
|
dbconn.close()
|
||
4 years ago
|
return render_template('index.html', nodes=nodes)
|
||
6 years ago
|
|
||
1 year ago
|
@app.route('/map')
|
||
|
@app.route('/map/network')
|
||
|
def page_network():
|
||
|
return render_template('map/network.html', page='network')
|
||
|
|
||
|
@app.route('/map/about')
|
||
|
def page_about():
|
||
|
return render_template('map/about.html', page='about')
|
||
|
|
||
|
@app.after_request
|
||
|
def add_header(response):
|
||
|
response.cache_control.max_age = 300
|
||
|
return response
|
||
6 years ago
|
|
||
4 years ago
|
# sort out the api request here for the url
|
||
6 years ago
|
api.add_resource(nodesCurrent, '/current')
|
||
4 years ago
|
api.add_resource(nodesInfo, '/nodeinfo')
|
||
4 years ago
|
api.add_resource(nodes24h, '/nodes24h')
|
||
4 years ago
|
api.add_resource(nodes30d, '/nodes30d')
|
||
4 years ago
|
|
||
|
# regirster signal handler
|
||
|
signal.signal(signal.SIGINT, signal_handler)
|
||
|
signal.signal(signal.SIGTERM, signal_handler)
|
||
6 years ago
|
|
||
|
if __name__ == '__main__':
|
||
1 year ago
|
app.run(host='127.0.0.1', port=3001)
|