Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1 | #views.py |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 2 | import functools |
| 3 | import math |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 4 | import os |
| 5 | import sys |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 6 | from django.views.generic import TemplateView, View |
| 7 | import datetime |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 8 | from pprint import pprint |
| 9 | import json |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 10 | from syndicate.models import * |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 11 | from core.models import * |
Scott Baker | e9f4ec9 | 2014-04-29 11:29:45 -0700 | [diff] [blame] | 12 | from hpc.models import ContentProvider |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 13 | from operator import attrgetter |
Scott Baker | 186ae61 | 2014-05-19 10:42:51 -0700 | [diff] [blame] | 14 | from django import template |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 15 | from django.views.decorators.csrf import csrf_exempt |
| 16 | from django.http import HttpResponse, HttpResponseServerError |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 17 | from django.core import urlresolvers |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 18 | from django.contrib.gis.geoip import GeoIP |
| 19 | from ipware.ip import get_ip |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 20 | from operator import itemgetter, attrgetter |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 21 | import traceback |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 22 | import socket |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 23 | |
Scott Baker | a880bb5 | 2014-04-14 23:52:48 -0700 | [diff] [blame] | 24 | BLESSED_SITES = ["Stanford", "Washington", "Princeton", "GeorgiaTech", "MaxPlanck"] |
| 25 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 26 | if os.path.exists("/home/smbaker/projects/vicci/cdn/bigquery"): |
| 27 | sys.path.append("/home/smbaker/projects/vicci/cdn/bigquery") |
| 28 | else: |
| 29 | sys.path.append("/opt/planetstack/hpc_wizard") |
| 30 | import hpc_wizard |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 31 | from planetstack_analytics import DoPlanetStackAnalytics, PlanetStackAnalytics, RED_LOAD, BLUE_LOAD |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 32 | |
| 33 | class DashboardWelcomeView(TemplateView): |
| 34 | template_name = 'admin/dashboard/welcome.html' |
| 35 | |
| 36 | def get(self, request, *args, **kwargs): |
| 37 | context = self.get_context_data(**kwargs) |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 38 | context = getDashboardContext(request.user, context) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 39 | return self.render_to_response(context=context) |
Siobhan Tully | e18b344 | 2014-02-23 14:23:34 -0500 | [diff] [blame] | 40 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 41 | class DashboardDynamicView(TemplateView): |
Scott Baker | 186ae61 | 2014-05-19 10:42:51 -0700 | [diff] [blame] | 42 | head_template = r"""{% extends "admin/dashboard/dashboard_base.html" %} |
| 43 | {% load admin_static %} |
| 44 | {% block content %} |
| 45 | """ |
| 46 | |
| 47 | tail_template = r"{% endblock %}" |
| 48 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 49 | def get(self, request, name="root", *args, **kwargs): |
Scott Baker | 186ae61 | 2014-05-19 10:42:51 -0700 | [diff] [blame] | 50 | context = self.get_context_data(**kwargs) |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 51 | context = getDashboardContext(request.user, context) |
Scott Baker | 186ae61 | 2014-05-19 10:42:51 -0700 | [diff] [blame] | 52 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 53 | if name=="root": |
| 54 | return self.multiDashboardView(request, context) |
| 55 | else: |
| 56 | return self.singleDashboardView(request, name, context) |
| 57 | |
| 58 | def readDashboard(self, fn): |
| 59 | try: |
| 60 | template= open("/opt/planetstack/templates/admin/dashboard/%s.html" % fn, "r").read() |
| 61 | if (fn=="tenant"): |
| 62 | template = '<div id="tabs-5"></div>' + template |
| 63 | return template |
| 64 | except: |
| 65 | return "failed to open %s" % fn |
| 66 | |
| 67 | def multiDashboardView(self, request, context): |
Scott Baker | fc06e78 | 2014-05-19 10:52:10 -0700 | [diff] [blame] | 68 | head_template = self.head_template |
| 69 | tail_template = self.tail_template |
| 70 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 71 | body = """ |
| 72 | <div id="hometabs" > |
| 73 | <ul id="suit_form_tabs" class="nav nav-tabs nav-tabs-suit" data-tab-prefix="suit-tab"> |
| 74 | """ |
Scott Baker | fc06e78 | 2014-05-19 10:52:10 -0700 | [diff] [blame] | 75 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 76 | dashboards = request.user.get_dashboards() |
Scott Baker | fc06e78 | 2014-05-19 10:52:10 -0700 | [diff] [blame] | 77 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 78 | # customize is a special dashboard they always get |
| 79 | customize = DashboardView.objects.filter(name="Customize") |
| 80 | if customize: |
| 81 | dashboards.append(customize[0]) |
Scott Baker | 186ae61 | 2014-05-19 10:42:51 -0700 | [diff] [blame] | 82 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 83 | for i,view in enumerate(dashboards): |
| 84 | body = body + '<li><a href="#dashtab-%d">%s</a></li>\n' % (i, view.name) |
Scott Baker | 186ae61 | 2014-05-19 10:42:51 -0700 | [diff] [blame] | 85 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 86 | body = body + "</ul>\n" |
| 87 | |
| 88 | for i,view in enumerate(dashboards): |
| 89 | url = view.url |
| 90 | body = body + '<div id="dashtab-%d">\n' % i |
| 91 | if url.startswith("template:"): |
| 92 | fn = url[9:] |
| 93 | body = body + self.readDashboard(fn) |
| 94 | body = body + '</div>\n' |
| 95 | |
| 96 | body=body+"</div>\n" |
| 97 | |
| 98 | t = template.Template(head_template + body + self.tail_template) |
Scott Baker | 186ae61 | 2014-05-19 10:42:51 -0700 | [diff] [blame] | 99 | |
| 100 | response_kwargs = {} |
| 101 | response_kwargs.setdefault('content_type', self.content_type) |
| 102 | return self.response_class(
|
| 103 | request = request,
|
| 104 | template = t,
|
| 105 | context = context,
|
| 106 | **response_kwargs) |
| 107 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 108 | def singleDashboardView(self, request, name, context): |
| 109 | head_template = self.head_template |
| 110 | tail_template = self.tail_template |
| 111 | |
| 112 | t = template.Template(head_template + self.readDashboard(fn) + self.tail_template) |
| 113 | |
| 114 | response_kwargs = {} |
| 115 | response_kwargs.setdefault('content_type', self.content_type) |
| 116 | return self.response_class(
|
| 117 | request = request,
|
| 118 | template = t,
|
| 119 | context = context,
|
| 120 | **response_kwargs) |
| 121 | |
| 122 | def getDashboardContext(user, context={}, tableFormat = False): |
| 123 | context = {} |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 124 | |
| 125 | userSliceData = getSliceInfo(user) |
| 126 | if (tableFormat): |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 127 | context['userSliceInfo'] = userSliceTableFormatter(userSliceData) |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 128 | else: |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 129 | context['userSliceInfo'] = userSliceData |
| 130 | context['cdnData'] = getCDNOperatorData(wait=False) |
| 131 | context['cdnContentProviders'] = getCDNContentProviderData() |
| 132 | |
| 133 | (dashboards, unusedDashboards)= getDashboards(user) |
| 134 | unusedDashboards=[x for x in unusedDashboards if x!="Customize"] |
| 135 | context['dashboards'] = dashboards |
| 136 | context['unusedDashboards'] = unusedDashboards |
| 137 | |
| 138 | return context |
| 139 | |
| 140 | def getDashboards(user): |
| 141 | #dashboards = sorted(list(user.dashboardViews.all()), key=attrgetter('order')) |
| 142 | dashboards = user.get_dashboards() |
| 143 | |
| 144 | dashboard_names = [d.name for d in dashboards] |
| 145 | |
| 146 | unused_dashboard_names = [] |
| 147 | for dashboardView in DashboardView.objects.all(): |
| 148 | if not dashboardView.name in dashboard_names: |
| 149 | unused_dashboard_names.append(dashboardView.name) |
| 150 | |
| 151 | return (dashboard_names, unused_dashboard_names) |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 152 | |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 153 | class TenantCreateSlice(View): |
| 154 | def post(self, request, *args, **kwargs): |
| 155 | sliceName = request.POST.get("sliceName", "0") |
| 156 | serviceClass = request.POST.get("serviceClass", "0") |
| 157 | imageName = request.POST.get("imageName", "0") |
| 158 | actionToDo = request.POST.get("actionToDo", "0") |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 159 | #network = request.POST.get("network","0") |
Scott Baker | edf2f67 | 2014-04-22 10:17:37 -0700 | [diff] [blame] | 160 | mountDataSets = request.POST.get("mountDataSets","0") |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 161 | if (actionToDo == "add"): |
| 162 | serviceClass = ServiceClass.objects.get(name=serviceClass) |
| 163 | site = request.user.site |
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 164 | image = Image.objects.get(name=imageName) |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 165 | newSlice = Slice(name=sliceName,serviceClass=serviceClass,site=site,imagePreference=image,mountDataSets=mountDataSets) |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 166 | newSlice.save() |
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 167 | return HttpResponse("Slice created") |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 168 | |
Scott Baker | edf2f67 | 2014-04-22 10:17:37 -0700 | [diff] [blame] | 169 | class TenantUpdateSlice(View): |
| 170 | def post(self, request, *args, **kwargs):
|
| 171 | sliceName = request.POST.get("sliceName", "0")
|
| 172 | serviceClass = request.POST.get("serviceClass", "0")
|
| 173 | imageName = request.POST.get("imageName", "0")
|
| 174 | actionToDo = request.POST.get("actionToDo", "0")
|
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 175 | #network = request.POST.get("network","0")
|
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 176 | dataSet = request.POST.get("dataSet","0")
|
| 177 | slice = Slice.objects.all()
|
| 178 | for entry in slice:
|
| 179 | serviceClass = ServiceClass.objects.get(name=serviceClass)
|
| 180 | if(entry.name==sliceName):
|
| 181 | if (actionToDo == "update"):
|
| 182 | setattr(entry,'serviceClass',serviceClass)
|
| 183 | setattr(entry,'imagePreference',imageName)
|
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 184 | #setattr(entry,'network',network)
|
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 185 | setattr(entry,'mountDataSets',dataSet)
|
| 186 | entry.save()
|
| 187 | break
|
| 188 | return HttpResponse("Slice updated")
|
| 189 |
|
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 190 | def getTenantSliceInfo(user, tableFormat = False): |
| 191 | tenantSliceDetails = {} |
| 192 | tenantSliceData = getTenantInfo(user) |
| 193 | tenantServiceClassData = getServiceClassInfo(user) |
| 194 | if (tableFormat): |
| 195 | tenantSliceDetails['userSliceInfo'] = userSliceTableFormatter(tenantSliceData) |
| 196 | tenantSliceDetails['sliceServiceClass']=userSliceTableFormatter(tenantServiceClassData) |
| 197 | else: |
| 198 | tenantSliceDetails['userSliceInfo'] = tenantSliceData |
| 199 | tenantSliceDetails['sliceServiceClass']=userSliceTableFormatter(tenantServiceClassData) |
| 200 | tenantSliceDetails['image']=userSliceTableFormatter(getImageInfo(user)) |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 201 | #tenantSliceDetails['network']=userSliceTableFormatter(getNetworkInfo(user)) |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 202 | tenantSliceDetails['deploymentSites']=userSliceTableFormatter(getDeploymentSites()) |
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 203 | tenantSliceDetails['sites'] = userSliceTableFormatter(getTenantSitesInfo()) |
| 204 | tenantSliceDetails['mountDataSets'] = userSliceTableFormatter(getMountDataSets()) |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 205 | tenantSliceDetails['publicKey'] = getPublicKey(user) |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 206 | return tenantSliceDetails |
| 207 | |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 208 | def getTenantInfo(user): |
| 209 | slices =Slice.objects.all() |
| 210 | userSliceInfo = [] |
| 211 | for entry in slices: |
| 212 | sliceName = Slice.objects.get(id=entry.id).name |
| 213 | slice = Slice.objects.get(name=Slice.objects.get(id=entry.id).name) |
| 214 | sliceServiceClass = entry.serviceClass.name |
| 215 | preferredImage = entry.imagePreference |
Scott Baker | edf2f67 | 2014-04-22 10:17:37 -0700 | [diff] [blame] | 216 | sliceDataSet = entry.mountDataSets |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 217 | #sliceNetwork = entry.network |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 218 | numSliver = 0 |
| 219 | sliceImage="" |
| 220 | sliceSite = {} |
Scott Baker | edf2f67 | 2014-04-22 10:17:37 -0700 | [diff] [blame] | 221 | sliceNode = {} |
| 222 | sliceInstance= {} |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 223 | for sliver in slice.slivers.all(): |
Scott Baker | a880bb5 | 2014-04-14 23:52:48 -0700 | [diff] [blame] | 224 | if sliver.node.site.name in BLESSED_SITES: |
Scott Baker | a880bb5 | 2014-04-14 23:52:48 -0700 | [diff] [blame] | 225 | sliceSite[sliver.node.site.name] = sliceSite.get(sliver.node.site.name,0) + 1 |
| 226 | sliceImage = sliver.image.name |
Scott Baker | b37ce33 | 2014-04-22 22:57:40 -0700 | [diff] [blame] | 227 | sliceNode[str(sliver)] = sliver.node.name |
Scott Baker | edf2f67 | 2014-04-22 10:17:37 -0700 | [diff] [blame] | 228 | numSliver = sum(sliceSite.values()) |
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 229 | numSites = len(sliceSite) |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 230 | userSliceInfo.append({'sliceName': sliceName,'sliceServiceClass': sliceServiceClass,'preferredImage':preferredImage,'numOfSites':numSites, 'sliceSite':sliceSite,'sliceImage':sliceImage,'numOfSlivers':numSliver,'sliceDataSet':sliceDataSet,'instanceNodePair':sliceNode}) |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 231 | return userSliceInfo |
| 232 | |
| 233 | def getTenantSitesInfo(): |
| 234 | tenantSiteInfo=[] |
| 235 | for entry in Site.objects.all(): |
Scott Baker | a880bb5 | 2014-04-14 23:52:48 -0700 | [diff] [blame] | 236 | if entry.name in BLESSED_SITES: |
| 237 | tenantSiteInfo.append({'siteName':entry.name}) |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 238 | return tenantSiteInfo |
| 239 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 240 | def userSliceTableFormatter(data): |
| 241 | # pprint(data) |
| 242 | formattedData = { |
| 243 | 'rows' : data |
| 244 | } |
| 245 | return formattedData |
| 246 | |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 247 | def getPublicKey(user): |
| 248 | users=User.objects.all()
|
| 249 | for key in users:
|
| 250 | if (str(key.email)==str(user)):
|
| 251 | sshKey = key.public_key
|
| 252 | return sshKey |
| 253 | |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 254 | def getServiceClassInfo(user): |
| 255 | serviceClassList = ServiceClass.objects.all() |
| 256 | sliceInfo = [] |
| 257 | for entry in serviceClassList: |
| 258 | sliceInfo.append({'serviceClass':entry.name}) |
| 259 | return sliceInfo |
| 260 | |
| 261 | def getImageInfo(user): |
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 262 | imageList = Image.objects.all() |
| 263 | #imageList = ['Fedora 16 LXC rev 1.3','Hadoop','MPI'] |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 264 | imageInfo = [] |
| 265 | for imageEntry in imageList: |
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 266 | imageInfo.append({'Image':imageEntry.name}) |
| 267 | #imageInfo.append({'Image':imageEntry}) |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 268 | return imageInfo |
| 269 | |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 270 | def createPrivateVolume(user, sliceName): |
| 271 | caps = Volume.CAP_READ_DATA | Volume.CAP_WRITE_DATA | Volume.CAP_HOST_DATA |
| 272 | getattr(Volume.default_gateway_caps,"read data") | \ |
| 273 | getattr(Volume.default_gateway_caps,"write data") | \ |
| 274 | getattr(Volume.default_gateway_caps,"host files") |
| 275 | v = Volume(name="private_" + sliceName, owner_id=user, description="private volume for %s" % sliceName, blocksize=61440, private=True, archive=False, default_gateway_caps = caps) |
| 276 | v.save() |
| 277 | |
| 278 | SYNDICATE_REPLICATE_PORTNUM = 1025 |
| 279 | |
| 280 | def get_free_port(): |
| 281 | inuse={} |
| 282 | inuse[SYNDICATE_REPLICATE_PORTNUM] = True |
| 283 | for vs in VolumeSlice.objects.all(): |
| 284 | inuse[vs.peer_portnum]=True |
| 285 | inuse[vs.replicate_portnum]=True |
| 286 | for network in Network.objects.all(): |
| 287 | network_ports = [x.strip() for x in network.ports.split(",")] |
| 288 | for network_port in network_ports: |
| 289 | try: |
| 290 | inuse[int(network_port)] = True |
| 291 | except: |
| 292 | # in case someone has put a malformed port number in the list |
| 293 | pass |
| 294 | for i in range(1025, 65535): |
| 295 | if not inuse.get(i,False): |
| 296 | return i |
| 297 | return False |
| 298 | |
| 299 | def mountVolume(sliceName, volumeName, readWrite): |
| 300 | slice = Slice.objects.get(name=sliceName) |
| 301 | volume = Volume.objects.get(name=volumeName) |
| 302 | # choose some unused port numbers |
| 303 | flags = Volume.CAP_READ_DATA |
| 304 | if readWrite: |
| 305 | flags = flags | Volume.CAP_WRITE_DATA |
| 306 | vs = VolumeSlice(volume_id = volume, slice_id = slice, gateway_caps=flags, peer_portnum = get_free_port(), replicate_portnum = SYNDICATE_REPLICATE_PORTNUM) |
| 307 | vs.save() |
| 308 | |
| 309 | def hasPrivateVolume(sliceName): |
| 310 | slice = Slice.objects.get(name=sliceName) |
| 311 | for vs in VolumeSlice.objects.filter(slice_id=slice): |
| 312 | if vs.volume_id.private: |
| 313 | return True |
| 314 | return False |
| 315 | |
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 316 | def getMountDataSets(): |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 317 | dataSetInfo=[]
|
| 318 | for volume in Volume.objects.all():
|
| 319 | if not volume.private:
|
| 320 | dataSetInfo.append({'DataSet': volume.name})
|
| 321 |
|
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 322 | return dataSetInfo |
| 323 | |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 324 | def getNetworkInfo(user): |
| 325 | #networkList = Network.objects.all() |
| 326 | networkList = ['Private Only','Private and Publicly Routable'] |
| 327 | networkInfo = [] |
| 328 | for networkEntry in networkList: |
| 329 | #networkInfo.append({'Network':networkEntry.name}) |
| 330 | networkInfo.append({'Network':networkEntry}) |
| 331 | return networkInfo |
| 332 | |
| 333 | def getDeploymentSites(): |
| 334 | deploymentList = Deployment.objects.all() |
| 335 | deploymentInfo = [] |
| 336 | for entry in deploymentList: |
| 337 | deploymentInfo.append({'DeploymentSite':entry.name}) |
| 338 | return deploymentInfo |
| 339 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 340 | def getSliceInfo(user): |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 341 | sliceList = Slice.objects.all() |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 342 | slicePrivs = SlicePrivilege.objects.filter(user=user) |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 343 | userSliceInfo = [] |
| 344 | for entry in slicePrivs: |
Siobhan Tully | e18b344 | 2014-02-23 14:23:34 -0500 | [diff] [blame] | 345 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 346 | slicename = Slice.objects.get(id=entry.slice.id).name |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 347 | slice = Slice.objects.get(name=Slice.objects.get(id=entry.slice.id).name) |
| 348 | sliverList=Sliver.objects.all() |
| 349 | sites_used = {} |
| 350 | for sliver in slice.slivers.all(): |
| 351 | #sites_used['deploymentSites'] = sliver.node.deployment.name |
| 352 | # sites_used[sliver.image.name] = sliver.image.name |
| 353 | sites_used[sliver.node.site.name] = sliver.numberCores |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 354 | sliceid = Slice.objects.get(id=entry.slice.id).id |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 355 | try: |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 356 | sliverList = Sliver.objects.filter(slice=entry.slice.id) |
| 357 | siteList = {} |
| 358 | for x in sliverList: |
| 359 | if x.node.site not in siteList: |
| 360 | siteList[x.node.site] = 1 |
| 361 | slivercount = len(sliverList) |
| 362 | sitecount = len(siteList) |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 363 | except: |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 364 | traceback.print_exc() |
| 365 | slivercount = 0 |
| 366 | sitecount = 0 |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 367 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 368 | userSliceInfo.append({'slicename': slicename, 'sliceid':sliceid, |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 369 | 'sitesUsed':sites_used, |
| 370 | 'role': SliceRole.objects.get(id=entry.role.id).role, |
| 371 | 'slivercount': slivercount, |
| 372 | 'sitecount':sitecount}) |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 373 | |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 374 | return userSliceInfo |
| 375 | |
Scott Baker | e9f4ec9 | 2014-04-29 11:29:45 -0700 | [diff] [blame] | 376 | def getCDNContentProviderData(): |
| 377 | cps = [] |
| 378 | for dm_cp in ContentProvider.objects.all(): |
| 379 | cp = {"name": dm_cp.name, |
| 380 | "account": dm_cp.account} |
| 381 | cps.append(cp) |
| 382 | |
| 383 | return cps |
| 384 | |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 385 | def getCDNOperatorData(randomizeData = False, wait=True): |
| 386 | HPC_SLICE_NAME = "HyperCache" |
| 387 | |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 388 | bq = PlanetStackAnalytics() |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 389 | |
Scott Baker | 4a4472d | 2014-05-13 17:07:46 -0700 | [diff] [blame] | 390 | rows = bq.get_cached_query_results(bq.compose_cached_query(), wait) |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 391 | |
Scott Baker | edf2f67 | 2014-04-22 10:17:37 -0700 | [diff] [blame] | 392 | # wait=False on the first time the Dashboard is opened. This means we might |
| 393 | # not have any rows yet. The dashboard code polls every 30 seconds, so it |
| 394 | # will eventually pick them up. |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 395 | |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 396 | if rows: |
Scott Baker | 696e266 | 2014-04-24 17:03:29 -0700 | [diff] [blame] | 397 | rows = bq.postprocess_results(rows, filter={"event": "hpc_heartbeat"}, maxi=["cpu"], count=["hostname"], computed=["bytes_sent/elapsed"], groupBy=["Time","site"], maxDeltaTime=80) |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 398 | |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 399 | # dictionaryize the statistics rows by site name |
| 400 | stats_rows = {} |
| 401 | for row in rows: |
| 402 | stats_rows[row["site"]] = row |
Scott Baker | edf2f67 | 2014-04-22 10:17:37 -0700 | [diff] [blame] | 403 | else: |
| 404 | stats_rows = {} |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 405 | |
Scott Baker | 09fad02 | 2014-04-30 09:50:01 -0700 | [diff] [blame] | 406 | slice = Slice.objects.filter(name=HPC_SLICE_NAME) |
| 407 | if slice: |
| 408 | slice_slivers = list(slice[0].slivers.all()) |
| 409 | else: |
| 410 | slice_slivers = [] |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 411 | |
| 412 | new_rows = {} |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 413 | for site in Site.objects.all(): |
| 414 | # compute number of slivers allocated in the data model |
| 415 | allocated_slivers = 0 |
| 416 | for sliver in slice_slivers: |
| 417 | if sliver.node.site == site: |
| 418 | allocated_slivers = allocated_slivers + 1 |
| 419 | |
| 420 | stats_row = stats_rows.get(site.name,{}) |
| 421 | |
| 422 | max_cpu = stats_row.get("max_avg_cpu", stats_row.get("max_cpu",0)) |
| 423 | cpu=float(max_cpu)/100.0 |
| 424 | hotness = max(0.0, ((cpu*RED_LOAD) - BLUE_LOAD)/(RED_LOAD-BLUE_LOAD)) |
| 425 | |
| 426 | # format it to what that CDN Operations View is expecting |
| 427 | new_row = {"lat": float(site.location.longitude), |
| 428 | "long": float(site.location.longitude), |
| 429 | "lat": float(site.location.latitude), |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 430 | "health": 0, |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 431 | "numNodes": int(site.nodes.count()), |
Scott Baker | e9f4ec9 | 2014-04-29 11:29:45 -0700 | [diff] [blame] | 432 | "activeHPCSlivers": int(stats_row.get("count_hostname", 0)), # measured number of slivers, from bigquery statistics |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 433 | "numHPCSlivers": allocated_slivers, # allocated number of slivers, from data model |
| 434 | "siteUrl": str(site.site_url), |
| 435 | "bandwidth": stats_row.get("sum_computed_bytes_sent_div_elapsed",0), |
| 436 | "load": max_cpu, |
| 437 | "hot": float(hotness)} |
| 438 | new_rows[str(site.name)] = new_row |
| 439 | |
| 440 | # get rid of sites with 0 slivers that overlap other sites with >0 slivers |
| 441 | for (k,v) in new_rows.items(): |
| 442 | bad=False |
| 443 | if v["numHPCSlivers"]==0: |
| 444 | for v2 in new_rows.values(): |
| 445 | if (v!=v2) and (v2["numHPCSlivers"]>=0): |
| 446 | d = haversine(v["lat"],v["long"],v2["lat"],v2["long"]) |
| 447 | if d<100: |
| 448 | bad=True |
| 449 | if bad: |
| 450 | del new_rows[k] |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 451 | |
| 452 | return new_rows |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 453 | |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 454 | def getPageSummary(request): |
| 455 | slice = request.GET.get('slice', None) |
| 456 | site = request.GET.get('site', None) |
| 457 | node = request.GET.get('node', None) |
| 458 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 459 | class SimulatorView(View): |
| 460 | def get(self, request, **kwargs): |
| 461 | sim = json.loads(file("/tmp/simulator.json","r").read()) |
| 462 | text = "<html><head></head><body>" |
| 463 | text += "Iteration: %d<br>" % sim["iteration"] |
| 464 | text += "Elapsed since report %d<br><br>" % sim["elapsed_since_report"] |
| 465 | text += "<table border=1>" |
| 466 | text += "<tr><th>site</th><th>trend</th><th>weight</th><th>bytes_sent</th><th>hot</th></tr>" |
| 467 | for site in sim["site_load"].values(): |
| 468 | text += "<tr>" |
| 469 | text += "<td>%s</td><td>%0.2f</td><td>%0.2f</td><td>%d</td><td>%0.2f</td>" % \ |
| 470 | (site["name"], site["trend"], site["weight"], site["bytes_sent"], site["load_frac"]) |
| 471 | text += "</tr>" |
| 472 | text += "</table>" |
| 473 | text += "</body></html>" |
| 474 | return HttpResponse(text) |
| 475 | |
| 476 | class DashboardUserSiteView(View): |
| 477 | def get(self, request, **kwargs): |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 478 | return HttpResponse(json.dumps(getDashboardContext(request.user, tableFormat=True)), mimetype='application/javascript') |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 479 | |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 480 | class TenantViewData(View): |
| 481 | def get(self, request, **kwargs): |
| 482 | return HttpResponse(json.dumps(getTenantSliceInfo(request.user, True)), mimetype='application/javascript') |
| 483 | |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 484 | def haversine(site_lat, site_lon, lat, lon): |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 485 | d=0 |
| 486 | if lat and lon and site_lat and site_lon: |
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 487 | site_lat = float(site_lat) |
| 488 | site_lon = float(site_lon) |
| 489 | lat = float(lat) |
| 490 | lon = float(lon) |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 491 | R = 6378.1 |
| 492 | a = math.sin( math.radians((lat - site_lat)/2.0) )**2 + math.cos( math.radians(lat) )*math.cos( math.radians(site_lat) )*(math.sin( math.radians((lon - site_lon)/2.0 ) )**2) |
| 493 | c = 2 * math.atan2( math.sqrt(a), math.sqrt(1 - a) ) |
| 494 | d = R * c |
| 495 | |
| 496 | return d |
| 497 | |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 498 | def siteSortKey(site, slice=None, count=None, lat=None, lon=None): |
| 499 | # try to pick a site we're already using |
| 500 | has_slivers_here=False |
| 501 | if slice: |
| 502 | for sliver in slice.slivers.all(): |
| 503 | if sliver.node.site.name == site.name: |
| 504 | has_slivers_here=True |
| 505 | |
| 506 | # Haversine method |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 507 | d = haversine(site.location.latitude, site.location.longitude, lat, lon) |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 508 | |
| 509 | return (-has_slivers_here, d) |
| 510 | |
| 511 | def tenant_pick_sites(user, user_ip=None, slice=None, count=None): |
| 512 | """ Returns list of sites, sorted from most favorable to least favorable """ |
| 513 | lat=None |
| 514 | lon=None |
| 515 | try: |
| 516 | client_geo = GeoIP().city(user_ip) |
| 517 | if client_geo: |
| 518 | lat=float(client_geo["latitude"]) |
| 519 | lon=float(client_geo["longitude"]) |
| 520 | except: |
| 521 | print "exception in geo code" |
| 522 | traceback.print_exc() |
| 523 | |
| 524 | sites = Site.objects.all() |
Scott Baker | a880bb5 | 2014-04-14 23:52:48 -0700 | [diff] [blame] | 525 | sites = [x for x in sites if x.name in BLESSED_SITES] |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 526 | sites = sorted(sites, key=functools.partial(siteSortKey, slice=slice, count=count, lat=lat, lon=lon)) |
| 527 | |
| 528 | return sites |
| 529 | |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 530 | def slice_increase_slivers(user, user_ip, siteList, slice, count, noAct=False): |
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 531 | sitesChanged = {} |
| 532 | |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 533 | # let's compute how many slivers are in use in each node of each site |
| 534 | for site in siteList: |
| 535 | site.nodeList = list(site.nodes.all()) |
| 536 | for node in site.nodeList: |
| 537 | node.sliverCount = 0 |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 538 | for sliver in node.slivers.all(): |
Scott Baker | 2151194 | 2014-04-15 17:44:27 -0700 | [diff] [blame] | 539 | if sliver.slice.id == slice.id: |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 540 | node.sliverCount = node.sliverCount + 1 |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 541 | |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 542 | # Allocate slivers to nodes |
| 543 | # for now, assume we want to allocate all slivers from the same site |
| 544 | nodes = siteList[0].nodeList |
| 545 | while (count>0): |
| 546 | # Sort the node list by number of slivers per node, then pick the |
| 547 | # node with the least number of slivers. |
| 548 | nodes = sorted(nodes, key=attrgetter("sliverCount")) |
| 549 | node = nodes[0] |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 550 | |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 551 | print "adding sliver at node", node.name, "of site", node.site.name |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 552 | |
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 553 | if not noAct: |
| 554 | sliver = Sliver(name=node.name, |
| 555 | slice=slice, |
| 556 | node=node, |
| 557 | image = Image.objects.all()[0], |
| 558 | creator = User.objects.get(email=user), |
| 559 | deploymentNetwork=node.deployment, |
| 560 | numberCores =1 ) |
| 561 | sliver.save() |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 562 | |
| 563 | node.sliverCount = node.sliverCount + 1 |
| 564 | |
| 565 | count = count - 1 |
| 566 | |
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 567 | sitesChanged[node.site.name] = sitesChanged.get(node.site.name,0) + 1 |
| 568 | |
| 569 | return sitesChanged |
| 570 | |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 571 | def slice_decrease_slivers(user, siteList, slice, count, noAct=False): |
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 572 | sitesChanged = {} |
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 573 | sliverList ={} |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 574 | if siteList: |
| 575 | siteNames = [site.name for site in siteList] |
| 576 | else: |
| 577 | siteNames = None |
| 578 | |
| 579 | for sliver in slice.slivers.all(): |
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 580 | if(not siteNames) or (sliver.node.site.name in siteNames):
|
| 581 | node = sliver.node
|
| 582 | sliverList[sliver.name]=node.name |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 583 | |
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 584 | for key in sliverList: |
| 585 | if count>0: |
Scott Baker | f1b65c0 | 2014-04-21 01:03:17 -0700 | [diff] [blame] | 586 | sliver = Sliver.objects.filter(name=key)[0]
|
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 587 | sliver.delete()
|
| 588 | print "deleting sliver",sliverList[key],"at node",sliver.node.name
|
| 589 | count=count-1
|
Scott Baker | f1b65c0 | 2014-04-21 01:03:17 -0700 | [diff] [blame] | 590 | sitesChanged[sliver.node.site.name] = sitesChanged.get(sliver.node.site.name,0) - 1
|
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 591 |
|
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 592 | return sitesChanged |
| 593 | |
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 594 | class TenantDeleteSliceView(View): |
| 595 | def post(self,request):
|
| 596 | sliceName = request.POST.get("sliceName",None)
|
| 597 | slice = Slice.objects.get(name=sliceName)
|
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 598 | #print slice, slice.id
|
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 599 | sliceToDel=Slice(name=sliceName, id=slice.id)
|
| 600 | sliceToDel.delete() |
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 601 | return HttpResponse("Slice deleted") |
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 602 | |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 603 | class TenantAddOrRemoveSliverView(View): |
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 604 | """ Add or remove slivers from a Slice |
| 605 | |
| 606 | Arguments: |
| 607 | siteName - name of site. If not specified, PlanetStack will pick the |
| 608 | best site., |
| 609 | actionToDo - [add | rem] |
| 610 | count - number of slivers to add or remove |
| 611 | sliceName - name of slice |
Scott Baker | a880bb5 | 2014-04-14 23:52:48 -0700 | [diff] [blame] | 612 | noAct - if set, no changes will be made to db, but result will still |
| 613 | show which sites would have been modified. |
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 614 | |
| 615 | Returns: |
| 616 | Dictionary of sites that were modified, and the count of nodes |
| 617 | that were added or removed at each site. |
| 618 | """ |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 619 | def post(self, request, *args, **kwargs): |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 620 | siteName = request.POST.get("siteName", None) |
| 621 | actionToDo = request.POST.get("actionToDo", None) |
| 622 | count = int(request.POST.get("count","0")) |
| 623 | sliceName = request.POST.get("slice", None) |
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 624 | noAct = request.POST.get("noAct", False) |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 625 | |
| 626 | if not sliceName: |
| 627 | return HttpResponseServerError("No slice name given") |
| 628 | |
| 629 | slice = Slice.objects.get(name=sliceName) |
| 630 | |
| 631 | if siteName: |
| 632 | siteList = [Site.objects.get(name=siteName)] |
| 633 | else: |
| 634 | siteList = None |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 635 | |
| 636 | if (actionToDo == "add"): |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 637 | user_ip = request.GET.get("ip", get_ip(request)) |
| 638 | if (siteList is None): |
| 639 | siteList = tenant_pick_sites(user, user_ip, slice, count) |
| 640 | |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 641 | sitesChanged = slice_increase_slivers(request.user, user_ip, siteList, slice, count, noAct) |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 642 | elif (actionToDo == "rem"): |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 643 | sitesChanged = slice_decrease_slivers(request.user, siteList, slice, count, noAct) |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 644 | else: |
| 645 | return HttpResponseServerError("Unknown actionToDo %s" % actionToDo) |
| 646 | |
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 647 | return HttpResponse(json.dumps(sitesChanged), mimetype='application/javascript') |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 648 | |
| 649 | def get(self, request, *args, **kwargs): |
| 650 | request.POST = request.GET |
| 651 | return self.post(request, *args, **kwargs) # for testing REST in browser |
| 652 | #return HttpResponseServerError("GET is not supported") |
| 653 | |
| 654 | class TenantPickSitesView(View): |
| 655 | """ primarily just for testing purposes """ |
| 656 | def get(self, request, *args, **kwargs): |
| 657 | count = request.GET.get("count","0") |
| 658 | slice = request.GET.get("slice",None) |
| 659 | if slice: |
| 660 | slice = Slice.objects.get(name=slice) |
| 661 | ip = request.GET.get("ip", get_ip(request)) |
| 662 | sites = tenant_pick_sites(request.user, user_ip=ip, count=0, slice=slice) |
| 663 | sites = [x.name for x in sites] |
| 664 | return HttpResponse(json.dumps(sites), mimetype='application/javascript') |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 665 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 666 | class DashboardSummaryAjaxView(View): |
| 667 | def get(self, request, **kwargs): |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 668 | def avg(x): |
Scott Baker | 111702b | 2014-05-15 16:17:16 -0700 | [diff] [blame] | 669 | if len(x)==0: |
| 670 | return 0 |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 671 | return float(sum(x))/len(x) |
| 672 | |
| 673 | sites = getCDNOperatorData().values() |
| 674 | |
| 675 | sites = [site for site in sites if site["numHPCSlivers"]>0] |
| 676 | |
| 677 | total_slivers = sum( [site["numHPCSlivers"] for site in sites] ) |
| 678 | total_bandwidth = sum( [site["bandwidth"] for site in sites] ) |
| 679 | average_cpu = int(avg( [site["load"] for site in sites] )) |
| 680 | |
| 681 | result= {"total_slivers": total_slivers, |
| 682 | "total_bandwidth": total_bandwidth, |
| 683 | "average_cpu": average_cpu} |
| 684 | |
| 685 | return HttpResponse(json.dumps(result), mimetype='application/javascript') |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 686 | |
| 687 | class DashboardAddOrRemoveSliverView(View): |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 688 | # TODO: deprecate this view in favor of using TenantAddOrRemoveSliverView |
| 689 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 690 | def post(self, request, *args, **kwargs): |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 691 | siteName = request.POST.get("site", None) |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 692 | actionToDo = request.POST.get("actionToDo", "0") |
| 693 | |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 694 | siteList = [Site.objects.get(name=siteName)] |
| 695 | slice = Slice.objects.get(name="HyperCache") |
| 696 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 697 | if (actionToDo == "add"): |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 698 | user_ip = request.GET.get("ip", get_ip(request)) |
| 699 | slice_increase_slivers(request.user, user_ip, siteList, slice, 1) |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 700 | elif (actionToDo == "rem"): |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 701 | slice_decrease_slivers(request.user, siteList, slice, 1) |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 702 | |
| 703 | print '*' * 50 |
| 704 | print 'Ask for site: ' + siteName + ' to ' + actionToDo + ' another HPC Sliver' |
| 705 | return HttpResponse('This is POST request ') |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 706 | |
| 707 | class DashboardAjaxView(View): |
| 708 | def get(self, request, **kwargs): |
| 709 | return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript') |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 710 | |
| 711 | class DashboardAnalyticsAjaxView(View): |
| 712 | def get(self, request, name="hello_world", **kwargs): |
| 713 | if (name == "hpcSummary"): |
| 714 | return HttpResponse(json.dumps(hpc_wizard.get_hpc_wizard().get_summary_for_view()), mimetype='application/javascript') |
| 715 | elif (name == "hpcUserSite"): |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 716 | return HttpResponse(json.dumps(getDashboardContext(request.user, tableFormat=True)), mimetype='application/javascript') |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 717 | elif (name == "hpcMap"): |
| 718 | return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript') |
| 719 | elif (name == "bigquery"): |
| 720 | (mimetype, data) = DoPlanetStackAnalytics(request) |
| 721 | return HttpResponse(data, mimetype=mimetype) |
| 722 | else: |
| 723 | return HttpResponse(json.dumps("Unknown"), mimetype='application/javascript') |
| 724 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 725 | class DashboardCustomize(View): |
| 726 | def post(self, request, *args, **kwargs):
|
| 727 | dashboards = request.POST.get("dashboards", None)
|
| 728 | if not dashboards:
|
Scott Baker | 2870e27 | 2014-05-19 18:14:54 -0700 | [diff] [blame] | 729 | dashboards=[]
|
| 730 | else:
|
| 731 | dashboards = [x.strip() for x in dashboards.split(",")]
|
| 732 | dashboards = [DashboardView.objects.get(name=x) for x in dashboards]
|
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 733 |
|
| 734 | request.user.dashboardViews.all().delete()
|
| 735 |
|
| 736 | for i,dashboard in enumerate(dashboards):
|
| 737 | udbv = UserDashboardView(user=request.user, dashboardView=dashboard, order=i)
|
| 738 | udbv.save()
|
| 739 |
|
| 740 | return HttpResponse("updated")
|
| 741 | |