Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1 | #views.py |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 2 | import os |
| 3 | import sys |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 4 | from django.views.generic import TemplateView, View |
| 5 | import datetime |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 6 | from pprint import pprint |
| 7 | import json |
| 8 | from core.models import Slice,SliceRole,SlicePrivilege,Site,Reservation,Sliver |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 9 | from django.http import HttpResponse |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 10 | import traceback |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 11 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 12 | if os.path.exists("/home/smbaker/projects/vicci/cdn/bigquery"): |
| 13 | sys.path.append("/home/smbaker/projects/vicci/cdn/bigquery") |
| 14 | else: |
| 15 | sys.path.append("/opt/planetstack/hpc_wizard") |
| 16 | import hpc_wizard |
| 17 | from planetstack_analytics import DoPlanetStackAnalytics |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 18 | |
| 19 | class DashboardWelcomeView(TemplateView): |
| 20 | template_name = 'admin/dashboard/welcome.html' |
| 21 | |
| 22 | def get(self, request, *args, **kwargs): |
| 23 | context = self.get_context_data(**kwargs) |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 24 | userDetails = getUserSliceInfo(request.user) |
| 25 | #context['site'] = userDetails['site'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 26 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 27 | context['userSliceInfo'] = userDetails['userSliceInfo'] |
| 28 | context['cdnData'] = userDetails['cdnData'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 29 | return self.render_to_response(context=context) |
Siobhan Tully | e18b344 | 2014-02-23 14:23:34 -0500 | [diff] [blame] | 30 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 31 | def getUserSliceInfo(user, tableFormat = False): |
| 32 | userDetails = {} |
| 33 | # try: |
| 34 | # // site = Site.objects.filter(id=user.site.id) |
| 35 | # // except: |
| 36 | # // site = Site.objects.filter(name="Princeton") |
| 37 | # // userDetails['sitename'] = site[0].name |
| 38 | # // userDetails['siteid'] = site[0].id |
| 39 | |
| 40 | userSliceData = getSliceInfo(user) |
| 41 | if (tableFormat): |
| 42 | # pprint("******* GET USER SLICE INFO") |
| 43 | userDetails['userSliceInfo'] = userSliceTableFormatter(userSliceData) |
| 44 | else: |
| 45 | userDetails['userSliceInfo'] = userSliceData |
| 46 | userDetails['cdnData'] = getCDNOperatorData(); |
| 47 | # pprint( userDetails) |
| 48 | return userDetails |
| 49 | |
| 50 | def userSliceTableFormatter(data): |
| 51 | # pprint(data) |
| 52 | formattedData = { |
| 53 | 'rows' : data |
| 54 | } |
| 55 | return formattedData |
| 56 | |
| 57 | def getSliceInfo(user): |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 58 | sliceList = Slice.objects.all() |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 59 | slicePrivs = SlicePrivilege.objects.filter(user=user) |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 60 | userSliceInfo = [] |
| 61 | for entry in slicePrivs: |
Siobhan Tully | e18b344 | 2014-02-23 14:23:34 -0500 | [diff] [blame] | 62 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 63 | slicename = Slice.objects.get(id=entry.slice.id).name |
| 64 | sliceid = Slice.objects.get(id=entry.slice.id).id |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 65 | try: |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 66 | sliverList = Sliver.objects.filter(slice=entry.slice.id) |
| 67 | siteList = {} |
| 68 | for x in sliverList: |
| 69 | if x.node.site not in siteList: |
| 70 | siteList[x.node.site] = 1 |
| 71 | slivercount = len(sliverList) |
| 72 | sitecount = len(siteList) |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 73 | except: |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 74 | traceback.print_exc() |
| 75 | slivercount = 0 |
| 76 | sitecount = 0 |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 77 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 78 | userSliceInfo.append({'slicename': slicename, 'sliceid':sliceid, |
| 79 | 'role': SliceRole.objects.get(id=entry.role.id).role, 'slivercount': slivercount, 'sitecount':sitecount}) |
| 80 | |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 81 | return userSliceInfo |
| 82 | |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 83 | def getCDNOperatorData(randomizeData = False): |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 84 | return hpc_wizard.get_hpc_wizard().get_sites_for_view() |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 85 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 86 | def getPageSummary(request): |
| 87 | slice = request.GET.get('slice', None) |
| 88 | site = request.GET.get('site', None) |
| 89 | node = request.GET.get('node', None) |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 90 | |
| 91 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 92 | class SimulatorView(View): |
| 93 | def get(self, request, **kwargs): |
| 94 | sim = json.loads(file("/tmp/simulator.json","r").read()) |
| 95 | text = "<html><head></head><body>" |
| 96 | text += "Iteration: %d<br>" % sim["iteration"] |
| 97 | text += "Elapsed since report %d<br><br>" % sim["elapsed_since_report"] |
| 98 | text += "<table border=1>" |
| 99 | text += "<tr><th>site</th><th>trend</th><th>weight</th><th>bytes_sent</th><th>hot</th></tr>" |
| 100 | for site in sim["site_load"].values(): |
| 101 | text += "<tr>" |
| 102 | text += "<td>%s</td><td>%0.2f</td><td>%0.2f</td><td>%d</td><td>%0.2f</td>" % \ |
| 103 | (site["name"], site["trend"], site["weight"], site["bytes_sent"], site["load_frac"]) |
| 104 | text += "</tr>" |
| 105 | text += "</table>" |
| 106 | text += "</body></html>" |
| 107 | return HttpResponse(text) |
| 108 | |
| 109 | class DashboardUserSiteView(View): |
| 110 | def get(self, request, **kwargs): |
| 111 | return HttpResponse(json.dumps(getUserSliceInfo(request.user, True)), mimetype='application/javascript') |
| 112 | |
| 113 | class DashboardSummaryAjaxView(View): |
| 114 | def get(self, request, **kwargs): |
| 115 | return HttpResponse(json.dumps(hpc_wizard.get_hpc_wizard().get_summary_for_view()), mimetype='application/javascript') |
| 116 | |
| 117 | class DashboardAddOrRemoveSliverView(View): |
| 118 | def post(self, request, *args, **kwargs): |
| 119 | siteName = request.POST.get("site", "0") |
| 120 | actionToDo = request.POST.get("actionToDo", "0") |
| 121 | |
| 122 | if (actionToDo == "add"): |
| 123 | hpc_wizard.get_hpc_wizard().increase_slivers(siteName, 1) |
| 124 | elif (actionToDo == "rem"): |
| 125 | hpc_wizard.get_hpc_wizard().decrease_slivers(siteName, 1) |
| 126 | |
| 127 | print '*' * 50 |
| 128 | print 'Ask for site: ' + siteName + ' to ' + actionToDo + ' another HPC Sliver' |
| 129 | return HttpResponse('This is POST request ') |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 130 | |
| 131 | class DashboardAjaxView(View): |
| 132 | def get(self, request, **kwargs): |
| 133 | return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript') |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 134 | |
| 135 | class DashboardAnalyticsAjaxView(View): |
| 136 | def get(self, request, name="hello_world", **kwargs): |
| 137 | if (name == "hpcSummary"): |
| 138 | return HttpResponse(json.dumps(hpc_wizard.get_hpc_wizard().get_summary_for_view()), mimetype='application/javascript') |
| 139 | elif (name == "hpcUserSite"): |
| 140 | return HttpResponse(json.dumps(getUserSliceInfo(request.user, True)), mimetype='application/javascript') |
| 141 | elif (name == "hpcMap"): |
| 142 | return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript') |
| 143 | elif (name == "bigquery"): |
| 144 | (mimetype, data) = DoPlanetStackAnalytics(request) |
| 145 | return HttpResponse(data, mimetype=mimetype) |
| 146 | else: |
| 147 | return HttpResponse(json.dumps("Unknown"), mimetype='application/javascript') |
| 148 | |