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

Commit

Permalink
pool/info: using grequests to get units data
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsmedina committed Jan 15, 2016
1 parent e96cc43 commit ff590ed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
9 changes: 2 additions & 7 deletions tsuru_dashboard/admin/tests/test_pool_info_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ def test_should_pass_addresses_to_the_template(self, token_is_valid):
"Metadata": {"LastSuccess": "2014-08-01T14:09:40-03:00",
"pool": "theonepool"},
"Status": "ready"},
{"Address": "http://myserver2.com:2375",
"Metadata": {"LastSuccess": "2014-08-01T14:09:40-03:00"},
"Status": "ready"},
],
}

Expand All @@ -63,7 +60,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 = PoolInfo.as_view()(self.request, pool="mypool")
Expand Down Expand Up @@ -100,9 +98,6 @@ def test_without_units_by_node(self, token_is_valid):
"Metadata": {"LastSuccess": "2014-08-01T14:09:40-03:00",
"pool": "theonepool"},
"Status": "ready"},
{"Address": "http://myserver2.com:2375",
"Metadata": {"LastSuccess": "2014-08-01T14:09:40-03:00"},
"Status": "ready"},
],
}

Expand Down
30 changes: 23 additions & 7 deletions tsuru_dashboard/admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,24 @@ def get_context_data(self, *args, **kwargs):
class PoolInfo(LoginRequiredView, TemplateView):
template_name = "docker/pool_info.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

node_units = response.json()
if not node_units:
continue

if 'HostAddr' not in node_units[0]:
continue

if response.status_code != 200:
return {}
if node_units[0]['HostAddr'] in address:
return node_units
return []

units = response.json() or []
def units_by_node(self, address, units):
units = self.get_node(address, units)
result = {}

for unit in units:
Expand Down Expand Up @@ -249,14 +259,20 @@ def nodes_by_pool(self, pool):
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:
if node["Metadata"].get("pool", "") != pool:
continue

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, [])
Expand Down

0 comments on commit ff590ed

Please sign in to comment.