Browse Source

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
Feilong Wang 9 months ago
parent
commit
00b8e4b19d

+ 18
- 17
distil_ui/content/billing/templates/billing/index.html View File

@@ -189,30 +189,31 @@
189 189
         var link_mapping = {"c1": "/project/instances/",
190 190
                             "b1": "/project/volumes/"}
191 191
         $('#month_details tbody').empty();
192
+        if (MONTH_DETAILS.length == 0) {return}
192 193
         month_detail = MONTH_DETAILS[monthIndex]
193 194
         for(i = 0; i < month_detail.length; i++) {
194 195
             var resource_id = ""
195 196
             var resource_url = "#";
196 197
             if (month_detail[i]['resource_id'] != null && month_detail[i]['resource_id'] != "") {
197 198
                 resource_id = "(" + month_detail[i]['resource_id']+")"
198
-	            var resource_type = month_detail[i]["product"].split(".")[1];
199
-	            var product_name = month_detail[i]["product"].split(".")[2];
200
-	            if (resource_type in link_mapping){
201
-	                resource_url = link_mapping[resource_type] + month_detail[i]['resource_id'];
202
-	            }
203
-	            if (resource_type == 'n1'){
204
-	                if (product_name == 'network'){
205
-	                    resource_url = '/project/networks/'+ month_detail[i]['resource_id'] +'/detail';
206
-	                }
207
-	                if (product_name == 'router'){
208
-	                    resource_url = '/project/routers/'+ month_detail[i]['resource_id'];
209
-	                }
210
-	                if (product_name == 'vpn'){
211
-	                    resource_url = '/project/vpn/vpnservice/'+ month_detail[i]['resource_id'];
212
-	                }
213
-	            }
199
+                var resource_type = month_detail[i]["product"].split(".")[1];
200
+                var product_name = month_detail[i]["product"].split(".")[2];
201
+                if (resource_type in link_mapping){
202
+                    resource_url = link_mapping[resource_type] + month_detail[i]['resource_id'];
203
+                }
204
+                if (resource_type == 'n1'){
205
+                    if (product_name == 'network'){
206
+                        resource_url = '/project/networks/'+ month_detail[i]['resource_id'] +'/detail';
207
+                    }
208
+                    if (product_name == 'router'){
209
+                        resource_url = '/project/routers/'+ month_detail[i]['resource_id'];
210
+                    }
211
+                    if (product_name == 'vpn'){
212
+                        resource_url = '/project/vpn/vpnservice/'+ month_detail[i]['resource_id'];
213
+                    }
214
+                }
214 215
             }
215
-            
216
+
216 217
             resource = resource_id == ""? month_detail[i]['resource_name']+resource_id : "<a href="+ resource_url +">" + month_detail[i]['resource_name'] + resource_id + "</a>"
217 218
             $('#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>');
218 219
         }

+ 31
- 22
distil_ui/content/billing/views.py View File

@@ -13,9 +13,11 @@
13 13
 # limitations under the License.
14 14
 
15 15
 import datetime
16
+from django.utils.translation import ugettext_lazy as _
16 17
 import json
17 18
 import logging
18 19
 
20
+from horizon import exceptions
19 21
 from horizon import views
20 22
 
21 23
 from distil_ui.api import distil_v2 as distil
@@ -31,30 +33,37 @@ class IndexView(views.HorizonTemplateView):
31 33
 
32 34
     def get_context_data(self, **kwargs):
33 35
         context = super(IndexView, self).get_context_data(**kwargs)
34
-        distil_client = distil.distilclient(self.request)
35
-        self.cost = distil.get_cost(self.request, distil_client)
36
-        self.credits = distil.get_credits(self.request, distil_client)
37
-        pie_data = []
38
-        for i in range(len(self.cost)):
39
-            pie_data.append([{"value": value, "key": key} for (key, value)
40
-                             in self.cost[i]["breakdown"].items()])
41
-        # NOTE(flwang): The average cost is removed for now until we can get
42
-        # a better performance of the API.
43
-        # avg_cost = round(sum([m["total_cost"]
44
-        #                      for m in self.cost[:11]]) / 11.0, 2)
45
-        line_data = [{"values": [{"y": round(m["total_cost"], 2), "x": i,
46
-                                  "p": m.get("status")} for i, m
47
-                                 in enumerate(self.cost)], "key": "Cost"}]
48
-        #             {"values": [{"y": avg_cost, "x": i}
49
-        #                         for i in range(12)],
50
-        #              "key": "Avg Cost", "color": "#fdd0a2"}]
36
+        try:
37
+            distil_client = distil.distilclient(self.request)
38
+            self.cost = distil.get_cost(self.request, distil_client)
39
+            self.credits = distil.get_credits(self.request, distil_client)
40
+            pie_data = []
41
+            for i in range(len(self.cost)):
42
+                pie_data.append([{"value": value, "key": key} for (key, value)
43
+                                in self.cost[i]["breakdown"].items()])
44
+            line_data = [{"values": [{"y": round(m["total_cost"], 2), "x": i,
45
+                                      "p": m.get("status")} for i, m
46
+                          in enumerate(self.cost)], "key": "Cost"}]
47
+            context['line_chart_data'] = json.dumps(line_data)
48
+            context['pie_chart_data'] = json.dumps(pie_data)
49
+            context['month_details'] = json.dumps([d["details"] for d
50
+                                                   in self.cost])
51
+            context['credits'] = json.dumps(self.credits)
52
+        except Exception as e:
53
+            LOG.exception(e)
54
+            msg = _("Failed to load usage data, please try again. If it is "
55
+                    "still not working, please open a support ticket.")
56
+            exceptions.handle(self.request, msg)
57
+            # data for place holder
58
+            context['line_chart_data'] = json.dumps([{"values": [{"y": 0,
59
+                                                                  "x": i}
60
+                                                      for i in range(12)]}])
61
+            context['pie_chart_data'] = json.dumps([{"value": 0,
62
+                                                     "key": "N/A"}])
63
+            context['month_details'] = json.dumps([])
64
+            context['credits'] = json.dumps({"credits": []})
51 65
 
52
-        context['line_chart_data'] = json.dumps(line_data)
53
-        context['pie_chart_data'] = json.dumps(pie_data)
54
-        context['month_details'] = json.dumps([d["details"] for d
55
-                                               in self.cost])
56 66
         context['x_axis_line_chart'] = self._get_x_axis_for_line_chart()
57
-        context['credits'] = json.dumps(self.credits)
58 67
         return context
59 68
 
60 69
     def _get_x_axis_for_line_chart(self):

Loading…
Cancel
Save