Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 1 | from view_common import * |
| 2 | |
| 3 | class DashboardSummaryAjaxView(View): |
| 4 | def get(self, request, **kwargs): |
| 5 | def avg(x): |
| 6 | if len(x)==0: |
| 7 | return 0 |
| 8 | return float(sum(x))/len(x) |
| 9 | |
| 10 | sites = getCDNOperatorData().values() |
| 11 | |
| 12 | sites = [site for site in sites if site["numHPCSlivers"]>0] |
| 13 | |
| 14 | total_slivers = sum( [site["numHPCSlivers"] for site in sites] ) |
| 15 | total_bandwidth = sum( [site["bandwidth"] for site in sites] ) |
| 16 | average_cpu = int(avg( [site["load"] for site in sites] )) |
| 17 | |
| 18 | result= {"total_slivers": total_slivers, |
| 19 | "total_bandwidth": total_bandwidth, |
| 20 | "average_cpu": average_cpu} |
| 21 | |
Scott Baker | 823b721 | 2014-06-16 10:25:39 -0700 | [diff] [blame] | 22 | return HttpResponse(json.dumps(result), content_type='application/javascript') |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 23 | |
| 24 | class DashboardAddOrRemoveSliverView(View): |
| 25 | # TODO: deprecate this view in favor of using TenantAddOrRemoveSliverView |
| 26 | |
| 27 | def post(self, request, *args, **kwargs): |
| 28 | siteName = request.POST.get("site", None) |
| 29 | actionToDo = request.POST.get("actionToDo", "0") |
| 30 | |
| 31 | siteList = [Site.objects.get(name=siteName)] |
| 32 | slice = Slice.objects.get(name="HyperCache") |
| 33 | |
| 34 | if request.user.isReadOnlyUser(): |
| 35 | return HttpResponseForbidden("User is in read-only mode") |
| 36 | |
| 37 | if (actionToDo == "add"): |
| 38 | user_ip = request.GET.get("ip", get_ip(request)) |
| 39 | slice_increase_slivers(request.user, user_ip, siteList, slice, 1) |
| 40 | elif (actionToDo == "rem"): |
| 41 | slice_decrease_slivers(request.user, siteList, slice, 1) |
| 42 | |
| 43 | print '*' * 50 |
| 44 | print 'Ask for site: ' + siteName + ' to ' + actionToDo + ' another HPC Sliver' |
Scott Baker | 823b721 | 2014-06-16 10:25:39 -0700 | [diff] [blame] | 45 | return HttpResponse(json.dumps("Success"), content_type='application/javascript') |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 46 | |
| 47 | class DashboardAjaxView(View): |
| 48 | def get(self, request, **kwargs): |
Scott Baker | 823b721 | 2014-06-16 10:25:39 -0700 | [diff] [blame] | 49 | return HttpResponse(json.dumps(getCDNOperatorData(True)), content_type='application/javascript') |