From 9372408581e80ed486a65f25d79de97ffe990f08 Mon Sep 17 00:00:00 2001 From: littleskunk Date: Sat, 19 Mar 2016 06:18:12 +0800 Subject: [PATCH] btc_addr nodeid migration --- driveshare_graph/farmer_summary.py | 18 +++++++++++++++++- driveshare_graph/uptime.py | 22 +++++++++++++++++++--- requirements.txt | 1 + 3 files changed, 37 insertions(+), 4 deletions(-) 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/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