Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
pool/list: using grequests to get units data
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsmedina committed Jan 14, 2016
1 parent 037c7ce commit e96cc43
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
"pytz>=2015.4",
"Pygments>=2.0.2",
"tsuruclient>=0.2.1",
"grequests>=0.2.0",
],
)
3 changes: 2 additions & 1 deletion tsuru_dashboard/admin/tests/test_pool_list_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def test_should_pass_addresses_to_the_template(self, token_is_valid):

for addr in ["http://128.0.0.1:4243", "http://myserver.com:2375", "http://127.0.0.1:2375"]:
url = "{}/docker/node/{}/containers".format(settings.TSURU_HOST, addr)
body = json.dumps([{"Status": "started"}, {"Status": "stopped"}])
body = json.dumps(
[{"Status": "started", "HostAddr": addr}, {"Status": "stopped", "HostAddr": addr}])
httpretty.register_uri(httpretty.GET, url, body=body, status=200)

response = PoolList.as_view()(self.request)
Expand Down
32 changes: 23 additions & 9 deletions tsuru_dashboard/admin/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
import grequests
import json
import re
from dateutil import parser
Expand All @@ -23,15 +24,24 @@
class PoolList(LoginRequiredView, TemplateView):
template_name = "admin/pool_list.html"

def units_by_node(self, address):
url = "{}/docker/node/{}/containers".format(settings.TSURU_HOST, address)
response = requests.get(url, headers=self.authorization)
def get_node(self, address, data):
for response in data:
if response.status_code != 200:
continue

if response.status_code != 200:
return {}
node_units = response.json()
if not node_units:
continue

units = response.json() or []
if 'HostAddr' not in node_units[0]:
continue

if node_units[0]['HostAddr'] in address:
return node_units
return []

def units_by_node(self, address, units):
units = self.get_node(address, units)
result = {}

for unit in units:
Expand Down Expand Up @@ -61,12 +71,16 @@ def nodes_by_pool(self):
data = response.json()
nodes = data.get("nodes", [])

url = "{}/docker/node/{}/containers"
urls = [url.format(settings.TSURU_HOST, node["Address"]) for node in nodes]

rs = (grequests.get(u, headers=self.authorization) for u in urls)
units = grequests.map(rs)

for node in nodes:
dt = node["Metadata"].get("LastSuccess")
node["Metadata"]["LastSuccess"] = self.node_last_success(dt)

node["Units"] = self.units_by_node(node["Address"])

node["Units"] = self.units_by_node(node["Address"], units)
pool = node["Metadata"].get("pool")
nodes_by_pool = pools.get(pool, [])
nodes_by_pool.append(node)
Expand Down

0 comments on commit e96cc43

Please sign in to comment.