From 00b8e4b19dd0d2825efd7365a79bbc9cb5164b98 Mon Sep 17 00:00:00 2001 From: Feilong Wang Date: Wed, 18 Jul 2018 00:26:19 +1200 Subject: [PATCH] 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 --- .../billing/templates/billing/index.html | 35 ++++++------ distil_ui/content/billing/views.py | 53 +++++++++++-------- 2 files changed, 49 insertions(+), 39 deletions(-) diff --git a/distil_ui/content/billing/templates/billing/index.html b/distil_ui/content/billing/templates/billing/index.html index 591aab5..97498ae 100644 --- a/distil_ui/content/billing/templates/billing/index.html +++ b/distil_ui/content/billing/templates/billing/index.html @@ -189,30 +189,31 @@ var link_mapping = {"c1": "/project/instances/", "b1": "/project/volumes/"} $('#month_details tbody').empty(); + if (MONTH_DETAILS.length == 0) {return} month_detail = MONTH_DETAILS[monthIndex] for(i = 0; i < month_detail.length; i++) { var resource_id = "" var resource_url = "#"; if (month_detail[i]['resource_id'] != null && month_detail[i]['resource_id'] != "") { resource_id = "(" + month_detail[i]['resource_id']+")" - var resource_type = month_detail[i]["product"].split(".")[1]; - var product_name = month_detail[i]["product"].split(".")[2]; - if (resource_type in link_mapping){ - resource_url = link_mapping[resource_type] + month_detail[i]['resource_id']; - } - if (resource_type == 'n1'){ - if (product_name == 'network'){ - resource_url = '/project/networks/'+ month_detail[i]['resource_id'] +'/detail'; - } - if (product_name == 'router'){ - resource_url = '/project/routers/'+ month_detail[i]['resource_id']; - } - if (product_name == 'vpn'){ - resource_url = '/project/vpn/vpnservice/'+ month_detail[i]['resource_id']; - } - } + var resource_type = month_detail[i]["product"].split(".")[1]; + var product_name = month_detail[i]["product"].split(".")[2]; + if (resource_type in link_mapping){ + resource_url = link_mapping[resource_type] + month_detail[i]['resource_id']; + } + if (resource_type == 'n1'){ + if (product_name == 'network'){ + resource_url = '/project/networks/'+ month_detail[i]['resource_id'] +'/detail'; + } + if (product_name == 'router'){ + resource_url = '/project/routers/'+ month_detail[i]['resource_id']; + } + if (product_name == 'vpn'){ + resource_url = '/project/vpn/vpnservice/'+ month_detail[i]['resource_id']; + } + } } - + resource = resource_id == ""? month_detail[i]['resource_name']+resource_id : "" + month_detail[i]['resource_name'] + resource_id + "" $('#month_details tbody').append('' + month_detail[i]['product'] + '' + resource +''+month_detail[i]['quantity']+''+month_detail[i]['unit']+''+month_detail[i]['rate']+'$'+month_detail[i]['cost']+''); } diff --git a/distil_ui/content/billing/views.py b/distil_ui/content/billing/views.py index e4040e7..f3013c9 100644 --- a/distil_ui/content/billing/views.py +++ b/distil_ui/content/billing/views.py @@ -13,9 +13,11 @@ # limitations under the License. import datetime +from django.utils.translation import ugettext_lazy as _ import json import logging +from horizon import exceptions from horizon import views from distil_ui.api import distil_v2 as distil @@ -31,30 +33,37 @@ class IndexView(views.HorizonTemplateView): def get_context_data(self, **kwargs): context = super(IndexView, self).get_context_data(**kwargs) - distil_client = distil.distilclient(self.request) - self.cost = distil.get_cost(self.request, distil_client) - self.credits = distil.get_credits(self.request, distil_client) - pie_data = [] - for i in range(len(self.cost)): - pie_data.append([{"value": value, "key": key} for (key, value) - in self.cost[i]["breakdown"].items()]) - # NOTE(flwang): The average cost is removed for now until we can get - # a better performance of the API. - # avg_cost = round(sum([m["total_cost"] - # for m in self.cost[:11]]) / 11.0, 2) - line_data = [{"values": [{"y": round(m["total_cost"], 2), "x": i, - "p": m.get("status")} for i, m - in enumerate(self.cost)], "key": "Cost"}] - # {"values": [{"y": avg_cost, "x": i} - # for i in range(12)], - # "key": "Avg Cost", "color": "#fdd0a2"}] + try: + distil_client = distil.distilclient(self.request) + self.cost = distil.get_cost(self.request, distil_client) + self.credits = distil.get_credits(self.request, distil_client) + pie_data = [] + for i in range(len(self.cost)): + pie_data.append([{"value": value, "key": key} for (key, value) + in self.cost[i]["breakdown"].items()]) + line_data = [{"values": [{"y": round(m["total_cost"], 2), "x": i, + "p": m.get("status")} for i, m + in enumerate(self.cost)], "key": "Cost"}] + 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['credits'] = json.dumps(self.credits) + 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['credits'] = json.dumps(self.credits) return context def _get_x_axis_for_line_chart(self):