Skip to content

Commit

Permalink
btc_addr nodeid migration
Browse files Browse the repository at this point in the history
  • Loading branch information
littleskunk committed Mar 18, 2016
1 parent e01d46b commit 9372408
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
18 changes: 17 additions & 1 deletion driveshare_graph/farmer_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand Down
22 changes: 19 additions & 3 deletions driveshare_graph/uptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -93,15 +98,26 @@ 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)

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),))
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ flask==0.10.1
pymongo==3.0.3
pygal==2.0.7
simplejson==3.8.1
pycoin==0.62

0 comments on commit 9372408

Please sign in to comment.