Skip to content

Commit

Permalink
use nodeid
Browse files Browse the repository at this point in the history
  • Loading branch information
littleskunk committed Mar 18, 2016
1 parent d0f575d commit e01d46b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Go to http://graph.driveshare.org to see network statistics. Built using the Fla

**API**

Go to http://graph.driveshare.org/api/summary/<btc_addr> to see daily summaries for the build with the specified btc address over the past 30 days.
Go to http://graph.driveshare.org/api/summary/<nodeid> to see daily summaries for the build with the specified btc address over the past 30 days.


Ubuntu Digital Ocean Node
Expand Down
6 changes: 3 additions & 3 deletions driveshare_graph/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ def daily_data():
return json_totals


@app.route("/api/summary/<btc_addr>")
def api_summary(btc_addr):
@app.route("/api/summary/<nodeid>")
def api_summary(nodeid):
conn = sqlite3.connect('summary.db')
cursor = conn.cursor()
json_summary = farmer_summary.json_month_summary(cursor, btc_addr)
json_summary = farmer_summary.json_month_summary(cursor, nodeid)
return json_summary


Expand Down
20 changes: 10 additions & 10 deletions driveshare_graph/farmer_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ 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['btc_addr']
auth_address = farmer['nodeid']
if (auth_address in first_dates):
if last_dates[auth_address] == previous_time:
uptimes[auth_address] += doc_time - previous_time
Expand Down Expand Up @@ -126,26 +126,26 @@ def assign_points(conn, cursor, date):
conn.commit()


def average_height(btc_address, first_date, last_date, collection):
def average_height(nodeid, first_date, last_date, collection):
"""Returns the average height of the specified btc address
between the first_date and last_date.
Args:
btc_address: btc_address (authentication address) for farmer
nodeid: nodeid (authentication address) for farmer
first_date: first datetime in date range
last_date: last datetime in date range
collection: MongoDB collection of data on farmers
Returns:
avg_height: average height of the build with btc_address between
avg_height: average height of the build with nodeid between
first_date and last_date
"""
pipeline = [
{'$match': {'farmers.btc_addr': btc_address,
{'$match': {'farmers.nodeid': nodeid,
'time': {'$gte': first_date, '$lt': last_date}}},
{'$project': {'_id': 0, 'farmers.btc_addr': 1, 'farmers.height': 1}},
{'$project': {'_id': 0, 'farmers.nodeid': 1, 'farmers.height': 1}},
{'$unwind': '$farmers'},
{'$match': {'farmers.btc_addr': btc_address}}
{'$match': {'farmers.nodeid': nodeid}}
]
height_array = []
for doc in collection.aggregate(pipeline):
Expand Down Expand Up @@ -208,16 +208,16 @@ def end_date(farmers_collection): # pragma: no cover
return last_date


def json_month_summary(cursor, btc_addr):
"""Return json summary for btc_addr in the past month"""
def json_month_summary(cursor, nodeid):
"""Return json summary for nodeid in the past month"""
summaries = []
current_date = dt.datetime.now() - timedelta(days = 1)
last_date = dt.datetime(current_date.year, current_date.month, current_date.day, 0, 0, 0)
first_date = last_date - timedelta(days = 30)
day_count = (last_date - first_date).days + 1
for single_date in (first_date + timedelta(days=n) for n in range(day_count)):
cursor.execute('SELECT date, uptime, duration, height, points FROM summaries '
'WHERE auth_address = ? AND date = ?', (str(btc_addr), str(single_date),))
'WHERE auth_address = ? AND date = ?', (str(nodeid), str(single_date),))
data = cursor.fetchone()
if (data is not None):
date = data[0]
Expand Down
3 changes: 3 additions & 0 deletions driveshare_graph/update_summary.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import farmer_summary
import sqlite3
import datetime
from pymongo import MongoClient
from time import sleep

Expand All @@ -9,6 +10,8 @@
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()
Expand Down
22 changes: 11 additions & 11 deletions driveshare_graph/uptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ 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']:
btc_address = farmer['btc_addr']
if (btc_address in first_dates):
if last_dates[btc_address] == previous_time:
uptimes[btc_address] += doc_time - previous_time
last_dates[btc_address] = doc_time
nodeid = farmer['nodeid']
if (nodeid in first_dates):
if last_dates[nodeid] == previous_time:
uptimes[nodeid] += doc_time - previous_time
last_dates[nodeid] = doc_time
else:
first_dates[btc_address] = doc_time
last_dates[btc_address] = doc_time
uptimes[btc_address] = 0
first_dates[nodeid] = doc_time
last_dates[nodeid] = doc_time
uptimes[nodeid] = 0
previous_time = doc_time

for key, value in first_dates.iteritems():
Expand Down Expand Up @@ -101,7 +101,7 @@ 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['btc_addr']
address = farmer['nodeid']
if address_in_db(cursor, address):
cursor.execute('''SELECT MAX(last_date) FROM farmers WHERE
address=?''', (str(address),))
Expand Down Expand Up @@ -185,7 +185,7 @@ def uptime_distribution(cursor, collection): # pragma: no cover
"""
begin_date = dt.datetime.now() - timedelta(days = 7)
farmers = collection.find({'time': {'$gte': begin_date}}
).distinct('farmers.btc_addr')
).distinct('farmers.nodeid')
distribution = {0: 0, 5: 0, 10: 0, 15: 0, 20: 0, 25: 0, 30: 0, 35: 0, 40: 0,
45: 0, 50: 0, 55: 0, 60: 0, 65: 0, 70: 0, 75: 0, 80: 0,
85: 0, 90: 0, 95: 0}
Expand All @@ -211,7 +211,7 @@ def active_average_uptime(cursor, collection): # pragma: no cover
"""
begin_date = dt.datetime.now() - timedelta(days=7)
farmers = collection.find({'time': {'$gte':begin_date}}
).distinct('farmers.btc_addr')
).distinct('farmers.nodeid')
uptime_percentages = []
for farmer in farmers:
cursor.execute('''SELECT (uptime / (last_date - first_date)) * 100
Expand Down

0 comments on commit e01d46b

Please sign in to comment.