Skip to content

Commit

Permalink
Code and Tests for Total Bytes API Call
Browse files Browse the repository at this point in the history
  • Loading branch information
super3 committed Aug 10, 2015
1 parent 40436de commit 35a0e79
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 24 deletions.
15 changes: 3 additions & 12 deletions README.rst
Expand Up @@ -147,10 +147,7 @@ Success Example:
Total Bytes
***********

This API call was build to be human readable rather than machine readable. We get a simple
list of the all the farmers, their addresses, their advertised height. We only
display farmers that have done a ping in the last `online_time` minutes, which by default
is 15 minutes.
Get the total number of terabytes currently being managed by the node.

::

Expand All @@ -160,16 +157,10 @@ Success Example:

::

GET /api/online
GET /api/total
RESPONSE:
Status Code: 200
Text:
1NeV1z5BMmFpCXgotwVeZjuN5k124W76MA | Last Seen: 14 second(s) | Height: 10
137x69jwmcyy4mYCBtQUVoxa21p9Fxyss5 | Last Seen: 7 second(s) | Height: 6234
14wLMb2A9APqrdXJhTQArYLyivmEAf7Y1r | Last Seen: 10 second(s) | Height: 431
18RZNu2nxTdeNyuDCwAMq8aBpgC3FFERPp | Last Seen: 3 second(s) | Height: 7634
1CgLoZT1ZuSHPBp3H4rLTXJvEUDV3kK7QK | Last Seen: 13 second(s) | Height: 245
1QACy1Tx5JFzGDyPd8J3oU8SrjhkZkru4H | Last Seen: 14 second(s) | Height: 88
Text: 35 TB

Advertise Height
****************
Expand Down
41 changes: 29 additions & 12 deletions dataserv/app.py
Expand Up @@ -19,6 +19,18 @@ def secs_to_mins(seconds):
else:
return "{0} hour(s)".format(int(seconds/3600))

def online_farmers():
# maximum number of minutes since the last check in for
# the farmer to be considered an online farmer
online_time = app.config["ONLINE_TIME"]

# find the time object online_time minutes in the past
current_time = datetime.datetime.utcnow()
time_ago = current_time - datetime.timedelta(minutes=online_time)

# give us all farmers that have been around for the past online_time
return db.session.query(Farmer).filter(Farmer.last_seen > time_ago).all()


# Routes
@app.route('/')
Expand Down Expand Up @@ -68,28 +80,33 @@ def ping(btc_addr):

@app.route('/api/online', methods=["GET"])
def online():
# maximum number of minutes since the last check in for
# the farmer to be considered an online farmer
online_time = app.config["ONLINE_TIME"]

# find the time object online_time minutes in the past
current_time = datetime.datetime.utcnow()
time_ago = current_time - datetime.timedelta(minutes=online_time)

# give us all farmers that have been around for the past online_time
online_farmers = db.session.query(Farmer).filter(Farmer.last_seen > time_ago).all()

# this could be formatted a bit better, but we just want to publicly display
# that status of the farmers connected to the node
output = ""
for farmer in online_farmers:
current_time = datetime.datetime.utcnow()

for farmer in online_farmers():
last_seen = secs_to_mins((current_time - farmer.last_seen).seconds)
text = "{0} | Last Seen: {1} | Height: {2}<br/>"
output += text.format(farmer.btc_addr, last_seen, farmer.height)

return output


@app.route('/api/total', methods=["GET"])
def total():
total_shards = 0

# add up number of shards
for farmer in online_farmers():
total_shards += farmer.height

# return in TB the number
app.config["BYTE_SIZE"] = 1024*1024*128
result = total_shards * (app.config["BYTE_SIZE"] / (1024*1024*1024*1024)) # 1 TB
return "{0} TB".format(round(result,2))


@app.route('/api/height/<btc_addr>/<int:height>', methods=["GET"])
def set_height(btc_addr, height):
# create Farmer object to represent user
Expand Down
26 changes: 26 additions & 0 deletions tests/test_App.py
Expand Up @@ -126,5 +126,31 @@ def test_farmer_set_height(self):
rv = self.app.get('/api/height/{0}/1'.format(addr2))
self.assertEqual(rv.status_code, 400)

def test_farmer_total_bytes(self):
addr1 = '191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc'
addr2 = '18c2qnUAfgF3UnJCjAz2rpWQph5xugEfkr'
addr3 = '1NqtfdHe3X6rqHRQjsGq5CT9LYYjTFJ1qD'
addr4 = '1JnaPB29Un3FBSf3e4Jzabwi1ekeKoh1Gr'

# register farmers
self.app.get('/api/register/{0}'.format(addr1))
self.app.get('/api/register/{0}'.format(addr2))
self.app.get('/api/register/{0}'.format(addr3))
self.app.get('/api/register/{0}'.format(addr4))

# set height
self.app.get('/api/height/{0}/{1}'.format(addr1, 0))
self.app.get('/api/height/{0}/{1}'.format(addr2, 2475))
self.app.get('/api/height/{0}/{1}'.format(addr3, 2525))
self.app.get('/api/height/{0}/{1}'.format(addr4, 5000))

# check online
rv = self.app.get('/api/online')
self.assertTrue(b"Height: 0" in rv.data)
self.assertTrue(b"Height: 2475" in rv.data)
self.assertTrue(b"Height: 2525" in rv.data)
self.assertTrue(b"Height: 5000" in rv.data)

# check total bytes
rv = self.app.get('/api/total')
self.assertEqual(b"1.22 TB", rv.data)

0 comments on commit 35a0e79

Please sign in to comment.