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 |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 8 | from core.models import * |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 9 | from django.http import HttpResponse |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 10 | from django.core import urlresolvers |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 11 | import traceback |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 12 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 13 | if os.path.exists("/home/smbaker/projects/vicci/cdn/bigquery"): |
| 14 | sys.path.append("/home/smbaker/projects/vicci/cdn/bigquery") |
| 15 | else: |
| 16 | sys.path.append("/opt/planetstack/hpc_wizard") |
| 17 | import hpc_wizard |
| 18 | from planetstack_analytics import DoPlanetStackAnalytics |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 19 | |
| 20 | class DashboardWelcomeView(TemplateView): |
| 21 | template_name = 'admin/dashboard/welcome.html' |
| 22 | |
| 23 | def get(self, request, *args, **kwargs): |
| 24 | context = self.get_context_data(**kwargs) |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 25 | userDetails = getUserSliceInfo(request.user) |
| 26 | #context['site'] = userDetails['site'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 27 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 28 | context['userSliceInfo'] = userDetails['userSliceInfo'] |
| 29 | context['cdnData'] = userDetails['cdnData'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 30 | return self.render_to_response(context=context) |
Siobhan Tully | e18b344 | 2014-02-23 14:23:34 -0500 | [diff] [blame] | 31 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 32 | def getUserSliceInfo(user, tableFormat = False): |
| 33 | userDetails = {} |
| 34 | # try: |
| 35 | # // site = Site.objects.filter(id=user.site.id) |
| 36 | # // except: |
| 37 | # // site = Site.objects.filter(name="Princeton") |
| 38 | # // userDetails['sitename'] = site[0].name |
| 39 | # // userDetails['siteid'] = site[0].id |
| 40 | |
| 41 | userSliceData = getSliceInfo(user) |
| 42 | if (tableFormat): |
| 43 | # pprint("******* GET USER SLICE INFO") |
| 44 | userDetails['userSliceInfo'] = userSliceTableFormatter(userSliceData) |
| 45 | else: |
| 46 | userDetails['userSliceInfo'] = userSliceData |
| 47 | userDetails['cdnData'] = getCDNOperatorData(); |
| 48 | # pprint( userDetails) |
| 49 | return userDetails |
| 50 | |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 51 | class TenantCreateSlice(View): |
| 52 | def post(self, request, *args, **kwargs): |
| 53 | sliceName = request.POST.get("sliceName", "0") |
| 54 | serviceClass = request.POST.get("serviceClass", "0") |
| 55 | imageName = request.POST.get("imageName", "0") |
| 56 | actionToDo = request.POST.get("actionToDo", "0") |
| 57 | print sliceName |
| 58 | if (actionToDo == "add"): |
| 59 | serviceClass = ServiceClass.objects.get(name=serviceClass) |
| 60 | site = request.user.site |
| 61 | #image = Image.objects.get(name=imageName) |
| 62 | newSlice = Slice(name=sliceName,serviceClass=serviceClass,site=site,imagePreference=imageName) |
| 63 | newSlice.save() |
| 64 | return newSlice |
| 65 | |
| 66 | |
| 67 | def getTenantSliceInfo(user, tableFormat = False): |
| 68 | tenantSliceDetails = {} |
| 69 | tenantSliceData = getTenantInfo(user) |
| 70 | tenantServiceClassData = getServiceClassInfo(user) |
| 71 | if (tableFormat): |
| 72 | tenantSliceDetails['userSliceInfo'] = userSliceTableFormatter(tenantSliceData) |
| 73 | tenantSliceDetails['sliceServiceClass']=userSliceTableFormatter(tenantServiceClassData) |
| 74 | else: |
| 75 | tenantSliceDetails['userSliceInfo'] = tenantSliceData |
| 76 | tenantSliceDetails['sliceServiceClass']=userSliceTableFormatter(tenantServiceClassData) |
| 77 | tenantSliceDetails['image']=userSliceTableFormatter(getImageInfo(user)) |
| 78 | tenantSliceDetails['network']=userSliceTableFormatter(getNetworkInfo(user)) |
| 79 | tenantSliceDetails['deploymentSites']=userSliceTableFormatter(getDeploymentSites()) |
| 80 | tenantSliceDetails['sites'] = userSliceTableFormatter(getTenantSitesInfo()); |
| 81 | return tenantSliceDetails |
| 82 | |
| 83 | |
| 84 | def getTenantInfo(user): |
| 85 | slices =Slice.objects.all() |
| 86 | userSliceInfo = [] |
| 87 | for entry in slices: |
| 88 | sliceName = Slice.objects.get(id=entry.id).name |
| 89 | slice = Slice.objects.get(name=Slice.objects.get(id=entry.id).name) |
| 90 | sliceServiceClass = entry.serviceClass.name |
| 91 | preferredImage = entry.imagePreference |
| 92 | numSliver = 0 |
| 93 | sliceImage="" |
| 94 | sliceSite = {} |
| 95 | for sliver in slice.slivers.all(): |
| 96 | numSliver +=sliver.numberCores |
| 97 | # sliceSite[sliver.deploymentNetwork.name] =sliceSite.get(sliver.deploymentNetwork.name,0) + 1 |
| 98 | sliceSite[sliver.node.site.name] = sliceSite.get(sliver.node.site.name,0) + 1 |
| 99 | sliceImage = sliver.image.name |
| 100 | userSliceInfo.append({'sliceName': sliceName,'sliceServiceClass': sliceServiceClass,'preferredImage':preferredImage, 'sliceSite':sliceSite,'sliceImage':sliceImage,'numOfSlivers':numSliver}) |
| 101 | return userSliceInfo |
| 102 | |
| 103 | def getTenantSitesInfo(): |
| 104 | tenantSiteInfo=[] |
| 105 | for entry in Site.objects.all(): |
| 106 | tenantSiteInfo.append({'siteName':entry.name}) |
| 107 | return tenantSiteInfo |
| 108 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 109 | def userSliceTableFormatter(data): |
| 110 | # pprint(data) |
| 111 | formattedData = { |
| 112 | 'rows' : data |
| 113 | } |
| 114 | return formattedData |
| 115 | |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 116 | def getServiceClassInfo(user): |
| 117 | serviceClassList = ServiceClass.objects.all() |
| 118 | sliceInfo = [] |
| 119 | for entry in serviceClassList: |
| 120 | sliceInfo.append({'serviceClass':entry.name}) |
| 121 | return sliceInfo |
| 122 | |
| 123 | def getImageInfo(user): |
| 124 | imageList = Image.objects.all() |
| 125 | imageInfo = [] |
| 126 | for imageEntry in imageList: |
| 127 | imageInfo.append({'Image':imageEntry.name}) |
| 128 | return imageInfo |
| 129 | |
| 130 | def getNetworkInfo(user): |
| 131 | #networkList = Network.objects.all() |
| 132 | networkList = ['Private Only','Private and Publicly Routable'] |
| 133 | networkInfo = [] |
| 134 | for networkEntry in networkList: |
| 135 | #networkInfo.append({'Network':networkEntry.name}) |
| 136 | networkInfo.append({'Network':networkEntry}) |
| 137 | return networkInfo |
| 138 | |
| 139 | def getDeploymentSites(): |
| 140 | deploymentList = Deployment.objects.all() |
| 141 | deploymentInfo = [] |
| 142 | for entry in deploymentList: |
| 143 | deploymentInfo.append({'DeploymentSite':entry.name}) |
| 144 | return deploymentInfo |
| 145 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 146 | def getSliceInfo(user): |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 147 | sliceList = Slice.objects.all() |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 148 | slicePrivs = SlicePrivilege.objects.filter(user=user) |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 149 | userSliceInfo = [] |
| 150 | for entry in slicePrivs: |
Siobhan Tully | e18b344 | 2014-02-23 14:23:34 -0500 | [diff] [blame] | 151 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 152 | slicename = Slice.objects.get(id=entry.slice.id).name |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 153 | slice = Slice.objects.get(name=Slice.objects.get(id=entry.slice.id).name) |
| 154 | sliverList=Sliver.objects.all() |
| 155 | sites_used = {} |
| 156 | for sliver in slice.slivers.all(): |
| 157 | #sites_used['deploymentSites'] = sliver.node.deployment.name |
| 158 | # sites_used[sliver.image.name] = sliver.image.name |
| 159 | sites_used[sliver.node.site.name] = sliver.numberCores |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 160 | sliceid = Slice.objects.get(id=entry.slice.id).id |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 161 | try: |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 162 | sliverList = Sliver.objects.filter(slice=entry.slice.id) |
| 163 | siteList = {} |
| 164 | for x in sliverList: |
| 165 | if x.node.site not in siteList: |
| 166 | siteList[x.node.site] = 1 |
| 167 | slivercount = len(sliverList) |
| 168 | sitecount = len(siteList) |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 169 | except: |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 170 | traceback.print_exc() |
| 171 | slivercount = 0 |
| 172 | sitecount = 0 |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 173 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 174 | userSliceInfo.append({'slicename': slicename, 'sliceid':sliceid, |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 175 | 'sitesUsed':sites_used, |
| 176 | 'role': SliceRole.objects.get(id=entry.role.id).role, |
| 177 | 'slivercount': slivercount, |
| 178 | 'sitecount':sitecount}) |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 179 | |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 180 | return userSliceInfo |
| 181 | |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 182 | def getCDNOperatorData(randomizeData = False): |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 183 | return hpc_wizard.get_hpc_wizard().get_sites_for_view() |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 184 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 185 | def getPageSummary(request): |
| 186 | slice = request.GET.get('slice', None) |
| 187 | site = request.GET.get('site', None) |
| 188 | node = request.GET.get('node', None) |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 189 | |
| 190 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 191 | class SimulatorView(View): |
| 192 | def get(self, request, **kwargs): |
| 193 | sim = json.loads(file("/tmp/simulator.json","r").read()) |
| 194 | text = "<html><head></head><body>" |
| 195 | text += "Iteration: %d<br>" % sim["iteration"] |
| 196 | text += "Elapsed since report %d<br><br>" % sim["elapsed_since_report"] |
| 197 | text += "<table border=1>" |
| 198 | text += "<tr><th>site</th><th>trend</th><th>weight</th><th>bytes_sent</th><th>hot</th></tr>" |
| 199 | for site in sim["site_load"].values(): |
| 200 | text += "<tr>" |
| 201 | text += "<td>%s</td><td>%0.2f</td><td>%0.2f</td><td>%d</td><td>%0.2f</td>" % \ |
| 202 | (site["name"], site["trend"], site["weight"], site["bytes_sent"], site["load_frac"]) |
| 203 | text += "</tr>" |
| 204 | text += "</table>" |
| 205 | text += "</body></html>" |
| 206 | return HttpResponse(text) |
| 207 | |
| 208 | class DashboardUserSiteView(View): |
| 209 | def get(self, request, **kwargs): |
| 210 | return HttpResponse(json.dumps(getUserSliceInfo(request.user, True)), mimetype='application/javascript') |
| 211 | |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 212 | class TenantViewData(View): |
| 213 | def get(self, request, **kwargs): |
| 214 | return HttpResponse(json.dumps(getTenantSliceInfo(request.user, True)), mimetype='application/javascript') |
| 215 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 216 | class DashboardSummaryAjaxView(View): |
| 217 | def get(self, request, **kwargs): |
| 218 | return HttpResponse(json.dumps(hpc_wizard.get_hpc_wizard().get_summary_for_view()), mimetype='application/javascript') |
| 219 | |
| 220 | class DashboardAddOrRemoveSliverView(View): |
| 221 | def post(self, request, *args, **kwargs): |
| 222 | siteName = request.POST.get("site", "0") |
| 223 | actionToDo = request.POST.get("actionToDo", "0") |
| 224 | |
| 225 | if (actionToDo == "add"): |
| 226 | hpc_wizard.get_hpc_wizard().increase_slivers(siteName, 1) |
| 227 | elif (actionToDo == "rem"): |
| 228 | hpc_wizard.get_hpc_wizard().decrease_slivers(siteName, 1) |
| 229 | |
| 230 | print '*' * 50 |
| 231 | print 'Ask for site: ' + siteName + ' to ' + actionToDo + ' another HPC Sliver' |
| 232 | return HttpResponse('This is POST request ') |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 233 | |
| 234 | class DashboardAjaxView(View): |
| 235 | def get(self, request, **kwargs): |
| 236 | return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript') |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 237 | |
| 238 | class DashboardAnalyticsAjaxView(View): |
| 239 | def get(self, request, name="hello_world", **kwargs): |
| 240 | if (name == "hpcSummary"): |
| 241 | return HttpResponse(json.dumps(hpc_wizard.get_hpc_wizard().get_summary_for_view()), mimetype='application/javascript') |
| 242 | elif (name == "hpcUserSite"): |
| 243 | return HttpResponse(json.dumps(getUserSliceInfo(request.user, True)), mimetype='application/javascript') |
| 244 | elif (name == "hpcMap"): |
| 245 | return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript') |
| 246 | elif (name == "bigquery"): |
| 247 | (mimetype, data) = DoPlanetStackAnalytics(request) |
| 248 | return HttpResponse(data, mimetype=mimetype) |
| 249 | else: |
| 250 | return HttpResponse(json.dumps("Unknown"), mimetype='application/javascript') |
| 251 | |