Improve error catching

Currently the error catching for billing panel is not good, which
may lead user see the general error page. The UX is bad. This patch
is adding a try/except to cover all API calls to Distil to avoid
above issue.

Change-Id: I4abe065d41675933c6ee6be7b82111ce1c149a2c
This commit is contained in:
Feilong Wang 2018-07-18 00:26:19 +12:00
parent 30fc7654c2
commit 00b8e4b19d
2 changed files with 49 additions and 39 deletions

View File

@ -189,30 +189,31 @@
var link_mapping = {"c1": "/project/instances/", var link_mapping = {"c1": "/project/instances/",
"b1": "/project/volumes/"} "b1": "/project/volumes/"}
$('#month_details tbody').empty(); $('#month_details tbody').empty();
if (MONTH_DETAILS.length == 0) {return}
month_detail = MONTH_DETAILS[monthIndex] month_detail = MONTH_DETAILS[monthIndex]
for(i = 0; i < month_detail.length; i++) { for(i = 0; i < month_detail.length; i++) {
var resource_id = "" var resource_id = ""
var resource_url = "#"; var resource_url = "#";
if (month_detail[i]['resource_id'] != null && month_detail[i]['resource_id'] != "") { if (month_detail[i]['resource_id'] != null && month_detail[i]['resource_id'] != "") {
resource_id = "(" + month_detail[i]['resource_id']+")" resource_id = "(" + month_detail[i]['resource_id']+")"
var resource_type = month_detail[i]["product"].split(".")[1]; var resource_type = month_detail[i]["product"].split(".")[1];
var product_name = month_detail[i]["product"].split(".")[2]; var product_name = month_detail[i]["product"].split(".")[2];
if (resource_type in link_mapping){ if (resource_type in link_mapping){
resource_url = link_mapping[resource_type] + month_detail[i]['resource_id']; resource_url = link_mapping[resource_type] + month_detail[i]['resource_id'];
} }
if (resource_type == 'n1'){ if (resource_type == 'n1'){
if (product_name == 'network'){ if (product_name == 'network'){
resource_url = '/project/networks/'+ month_detail[i]['resource_id'] +'/detail'; resource_url = '/project/networks/'+ month_detail[i]['resource_id'] +'/detail';
} }
if (product_name == 'router'){ if (product_name == 'router'){
resource_url = '/project/routers/'+ month_detail[i]['resource_id']; resource_url = '/project/routers/'+ month_detail[i]['resource_id'];
} }
if (product_name == 'vpn'){ if (product_name == 'vpn'){
resource_url = '/project/vpn/vpnservice/'+ month_detail[i]['resource_id']; resource_url = '/project/vpn/vpnservice/'+ month_detail[i]['resource_id'];
} }
} }
} }
resource = resource_id == ""? month_detail[i]['resource_name']+resource_id : "<a href="+ resource_url +">" + month_detail[i]['resource_name'] + resource_id + "</a>" resource = resource_id == ""? month_detail[i]['resource_name']+resource_id : "<a href="+ resource_url +">" + month_detail[i]['resource_name'] + resource_id + "</a>"
$('#month_details tbody').append('<tr><td>' + month_detail[i]['product'] + '</td><td>' + resource +'</td><td>'+month_detail[i]['quantity']+'</td><td>'+month_detail[i]['unit']+'</td><td>'+month_detail[i]['rate']+'</td><td>$'+month_detail[i]['cost']+'</td></tr>'); $('#month_details tbody').append('<tr><td>' + month_detail[i]['product'] + '</td><td>' + resource +'</td><td>'+month_detail[i]['quantity']+'</td><td>'+month_detail[i]['unit']+'</td><td>'+month_detail[i]['rate']+'</td><td>$'+month_detail[i]['cost']+'</td></tr>');
} }

View File

@ -13,9 +13,11 @@
# limitations under the License. # limitations under the License.
import datetime import datetime
from django.utils.translation import ugettext_lazy as _
import json import json
import logging import logging
from horizon import exceptions
from horizon import views from horizon import views
from distil_ui.api import distil_v2 as distil from distil_ui.api import distil_v2 as distil
@ -31,30 +33,37 @@ class IndexView(views.HorizonTemplateView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs) context = super(IndexView, self).get_context_data(**kwargs)
distil_client = distil.distilclient(self.request) try:
self.cost = distil.get_cost(self.request, distil_client) distil_client = distil.distilclient(self.request)
self.credits = distil.get_credits(self.request, distil_client) self.cost = distil.get_cost(self.request, distil_client)
pie_data = [] self.credits = distil.get_credits(self.request, distil_client)
for i in range(len(self.cost)): pie_data = []
pie_data.append([{"value": value, "key": key} for (key, value) for i in range(len(self.cost)):
in self.cost[i]["breakdown"].items()]) pie_data.append([{"value": value, "key": key} for (key, value)
# NOTE(flwang): The average cost is removed for now until we can get in self.cost[i]["breakdown"].items()])
# a better performance of the API. line_data = [{"values": [{"y": round(m["total_cost"], 2), "x": i,
# avg_cost = round(sum([m["total_cost"] "p": m.get("status")} for i, m
# for m in self.cost[:11]]) / 11.0, 2) in enumerate(self.cost)], "key": "Cost"}]
line_data = [{"values": [{"y": round(m["total_cost"], 2), "x": i, context['line_chart_data'] = json.dumps(line_data)
"p": m.get("status")} for i, m context['pie_chart_data'] = json.dumps(pie_data)
in enumerate(self.cost)], "key": "Cost"}] context['month_details'] = json.dumps([d["details"] for d
# {"values": [{"y": avg_cost, "x": i} in self.cost])
# for i in range(12)], context['credits'] = json.dumps(self.credits)
# "key": "Avg Cost", "color": "#fdd0a2"}] except Exception as e:
LOG.exception(e)
msg = _("Failed to load usage data, please try again. If it is "
"still not working, please open a support ticket.")
exceptions.handle(self.request, msg)
# data for place holder
context['line_chart_data'] = json.dumps([{"values": [{"y": 0,
"x": i}
for i in range(12)]}])
context['pie_chart_data'] = json.dumps([{"value": 0,
"key": "N/A"}])
context['month_details'] = json.dumps([])
context['credits'] = json.dumps({"credits": []})
context['line_chart_data'] = json.dumps(line_data)
context['pie_chart_data'] = json.dumps(pie_data)
context['month_details'] = json.dumps([d["details"] for d
in self.cost])
context['x_axis_line_chart'] = self._get_x_axis_for_line_chart() context['x_axis_line_chart'] = self._get_x_axis_for_line_chart()
context['credits'] = json.dumps(self.credits)
return context return context
def _get_x_axis_for_line_chart(self): def _get_x_axis_for_line_chart(self):