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/",
"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 : "<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>');
}

View File

@ -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):