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