diff --git a/driveshare_graph/farmer_summary.py b/driveshare_graph/farmer_summary.py index c52c886..cc7eb71 100644 --- a/driveshare_graph/farmer_summary.py +++ b/driveshare_graph/farmer_summary.py @@ -10,6 +10,9 @@ import os import simplejson sys.path.append(os.path.join(os.path.dirname(__file__), '..')) +from pycoin.encoding import a2b_hashed_base58 +from binascii import hexlify + INDIVIDUAL_MAX_HEIGHT = 199999 SECONDS_IN_DAY = 86400 @@ -43,6 +46,14 @@ def init_table(conn, cursor, collection): def update_table(conn, cursor, collection): # pragma: no cover """Updates the summaries table if there is new data in collection.""" + cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='summaries'") + if len(cursor.fetchall()) == 0: + create_summary_table(conn, cursor) + + cursor.execute("SELECT count(date) FROM summaries") + if cursor.fetchone()[0] == 0: + init_table(conn, cursor, collection) + cursor.execute('SELECT MAX(date) FROM summaries') date = cursor.fetchone()[0] max_date = dt.datetime.strptime(date, '%Y-%m-%d %H:%M:%S') @@ -73,7 +84,10 @@ def create_daily_summary(conn, cursor, collection, date): for doc in collection.find({'time': {'$gte': date, '$lt': next_date}}): doc_time = time.mktime(doc['time'].timetuple()) for farmer in doc['farmers']: - auth_address = farmer['nodeid'] + if 'nodeid' in farmer: + auth_address = farmer['nodeid'] + else: + auth_address = hexlify(a2b_hashed_base58(farmer['btc_addr'])[1:]) if (auth_address in first_dates): if last_dates[auth_address] == previous_time: uptimes[auth_address] += doc_time - previous_time @@ -150,6 +164,8 @@ def average_height(nodeid, first_date, last_date, collection): height_array = [] for doc in collection.aggregate(pipeline): height_array.append(doc['farmers']['height']) + if len(height_array) == 0: + return 0 return sum(height_array)/len(height_array) diff --git a/driveshare_graph/update_summary.py b/driveshare_graph/update_summary.py index e03b1cb..a408e97 100644 --- a/driveshare_graph/update_summary.py +++ b/driveshare_graph/update_summary.py @@ -1,6 +1,5 @@ import farmer_summary import sqlite3 -import datetime from pymongo import MongoClient from time import sleep @@ -10,8 +9,6 @@ cursor = conn.cursor() client = MongoClient('localhost', 27017) collection = client['GroupB']['farmers'] - # farmer_summary.create_summary_table(conn, cursor) - # farmer_summary.init_table(conn, cursor, collection) farmer_summary.update_table(conn, cursor, collection) sleep(86400) conn.close() diff --git a/driveshare_graph/uptime.py b/driveshare_graph/uptime.py index 1706b56..4e77e13 100644 --- a/driveshare_graph/uptime.py +++ b/driveshare_graph/uptime.py @@ -5,6 +5,8 @@ import time import pygal from pygal.style import BlueStyle +from pycoin.encoding import a2b_hashed_base58 +from binascii import hexlify connection = MongoClient('localhost', 27017) collection = connection['GroupB']['farmers'] @@ -41,7 +43,10 @@ def init_farmers_table(conn, cursor, collection): for doc in collection.find({}).sort('time', 1): doc_time = time.mktime(doc['time'].timetuple()) for farmer in doc['farmers']: - nodeid = farmer['nodeid'] + if 'nodeid' in farmer: + nodeid = farmer['nodeid'] + else: + nodeid = hexlify(a2b_hashed_base58(farmer['btc_addr'])[1:]) if (nodeid in first_dates): if last_dates[nodeid] == previous_time: uptimes[nodeid] += doc_time - previous_time @@ -93,7 +98,15 @@ def update_farmers_table(conn, cursor, collection): conn: connection to sqlite3 database containing the table, farmers cursor: conn's cursor collection: MongoDB collection of farmers - """ + """ + cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='farmers'") + if len(cursor.fetchall()) == 0: + create_farmers_table(conn, cursor) + + cursor.execute("SELECT count(last_date) FROM farmers") + if cursor.fetchone()[0] == 0: + init_farmers_table(conn, cursor, collection) + cursor.execute('SELECT MAX(last_date) FROM farmers') last_time = int(cursor.fetchone()[0]) last_date = dt_from_timestamp(last_time) @@ -101,7 +114,10 @@ def update_farmers_table(conn, cursor, collection): for doc in collection.find({'time': {'$gt': last_date}}).sort('time', 1): doc_time = timestamp_from_dt(doc['time']) for farmer in doc['farmers']: - address = farmer['nodeid'] + if 'nodeid' in farmer: + address = farmer['nodeid'] + else: + address = hexlify(a2b_hashed_base58(farmer['btc_addr'])[1:]) if address_in_db(cursor, address): cursor.execute('''SELECT MAX(last_date) FROM farmers WHERE address=?''', (str(address),)) diff --git a/requirements.txt b/requirements.txt index 189390d..63cb7bf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ flask==0.10.1 pymongo==3.0.3 pygal==2.0.7 simplejson==3.8.1 +pycoin==0.62