1
0
mirror of https://github.com/r4sas/Niflheim-api synced 2025-01-26 06:34:14 +00:00
yggdrasil-api/api/max-min.py

44 lines
937 B
Python
Raw Normal View History

2018-11-17 21:17:39 +01:00
#max/min for the day with nodes
import psycopg2
import time
2018-11-17 21:17:39 +01:00
#run every hour
2018-11-17 21:17:39 +01:00
DB_PASSWORD = "password"
DB_USER = "yggindex"
DB_NAME = "yggindex"
DB_HOST = "localhost"
def age_calc(ustamp):
2018-11-17 21:17:39 +01:00
if (time.time() - ustamp) <= 14400 :
return True
else:
return False
def get_nodes_for_count():
dbconn = psycopg2.connect(host=DB_HOST,database=DB_NAME, user=DB_USER, password=DB_PASSWORD)
cur = dbconn.cursor()
nodes = {}
cur.execute("select * from yggindex")
for i in cur.fetchall():
if age_calc(int(i[2])):
nodes[i[0]] = [i[1],int(i[2])]
cur.close()
dbconn.close()
return str(len(nodes))
2018-11-17 21:17:39 +01:00
def add_to_db():
dbconn = psycopg2.connect(host=DB_HOST,database=DB_NAME, user=DB_USER, password=DB_PASSWORD)
cur = dbconn.cursor()
2018-11-17 21:17:39 +01:00
cur.execute('''INSERT INTO timeseries(max, unixtstamp) VALUES(''' + "'" + get_nodes_for_count() + "'," + str(int(time.time())) + ''')''')
2018-11-17 21:17:39 +01:00
dbconn.commit()
cur.close()
dbconn.close()
2018-11-17 21:17:39 +01:00
add_to_db()