blob: 451ee6d0a58a81c3f8fdcac4c97d658bb9130676 [file] [log] [blame]
Siobhan Tullycf04fb62014-01-11 11:25:57 -05001#views.py
Scott Bakerbd57a432014-04-14 16:12:15 -07002import functools
3import math
Scott Baker771819b2014-03-19 22:10:17 -07004import os
5import sys
Siobhan Tully06d2c032014-02-23 23:48:52 -05006from django.views.generic import TemplateView, View
7import datetime
Scott Baker771819b2014-03-19 22:10:17 -07008from pprint import pprint
9import json
Scott Baker168b7412014-05-16 14:25:41 -070010from syndicate.models import *
Scott Baker537d8232014-04-08 23:40:56 -070011from core.models import *
Scott Bakere9f4ec92014-04-29 11:29:45 -070012from hpc.models import ContentProvider
Scott Bakerbd57a432014-04-14 16:12:15 -070013from operator import attrgetter
Scott Baker186ae612014-05-19 10:42:51 -070014from django import template
Scott Bakerbd57a432014-04-14 16:12:15 -070015from django.views.decorators.csrf import csrf_exempt
16from django.http import HttpResponse, HttpResponseServerError
Scott Baker537d8232014-04-08 23:40:56 -070017from django.core import urlresolvers
Scott Bakerbd57a432014-04-14 16:12:15 -070018from django.contrib.gis.geoip import GeoIP
19from ipware.ip import get_ip
Scott Baker771819b2014-03-19 22:10:17 -070020import traceback
Scott Baker6253dfc2014-04-14 09:29:30 -070021import socket
Siobhan Tully06d2c032014-02-23 23:48:52 -050022
Scott Bakera880bb52014-04-14 23:52:48 -070023BLESSED_SITES = ["Stanford", "Washington", "Princeton", "GeorgiaTech", "MaxPlanck"]
24
Scott Baker771819b2014-03-19 22:10:17 -070025if os.path.exists("/home/smbaker/projects/vicci/cdn/bigquery"):
26 sys.path.append("/home/smbaker/projects/vicci/cdn/bigquery")
27else:
28 sys.path.append("/opt/planetstack/hpc_wizard")
29import hpc_wizard
Scott Baker75095b62014-04-21 17:32:09 -070030from planetstack_analytics import DoPlanetStackAnalytics, PlanetStackAnalytics, RED_LOAD, BLUE_LOAD
Siobhan Tullycf04fb62014-01-11 11:25:57 -050031
32class 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 Baker771819b2014-03-19 22:10:17 -070037 userDetails = getUserSliceInfo(request.user)
38 #context['site'] = userDetails['site']
Siobhan Tullycf04fb62014-01-11 11:25:57 -050039
Scott Baker771819b2014-03-19 22:10:17 -070040 context['userSliceInfo'] = userDetails['userSliceInfo']
41 context['cdnData'] = userDetails['cdnData']
Scott Bakere9f4ec92014-04-29 11:29:45 -070042 context['cdnContentProviders'] = userDetails['cdnContentProviders']
Siobhan Tullycf04fb62014-01-11 11:25:57 -050043 return self.render_to_response(context=context)
Siobhan Tullye18b3442014-02-23 14:23:34 -050044
Scott Baker186ae612014-05-19 10:42:51 -070045class 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 Bakerfc06e782014-05-19 10:52:10 -070056 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 Baker186ae612014-05-19 10:42:51 -070065
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 Baker771819b2014-03-19 22:10:17 -070081def getUserSliceInfo(user, tableFormat = False):
82 userDetails = {}
Scott Baker771819b2014-03-19 22:10:17 -070083
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 Bakere9f4ec92014-04-29 11:29:45 -070090 userDetails['cdnData'] = getCDNOperatorData(wait=False)
91 userDetails['cdnContentProviders'] = getCDNContentProviderData()
Scott Baker771819b2014-03-19 22:10:17 -070092 return userDetails
93
Scott Baker537d8232014-04-08 23:40:56 -070094class 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 Baker168b7412014-05-16 14:25:41 -0700100 #network = request.POST.get("network","0")
Scott Bakeredf2f672014-04-22 10:17:37 -0700101 mountDataSets = request.POST.get("mountDataSets","0")
Scott Baker537d8232014-04-08 23:40:56 -0700102 if (actionToDo == "add"):
103 serviceClass = ServiceClass.objects.get(name=serviceClass)
104 site = request.user.site
Scott Baker4b0cedd2014-04-22 21:01:21 -0700105 image = Image.objects.get(name=imageName)
Scott Baker168b7412014-05-16 14:25:41 -0700106 newSlice = Slice(name=sliceName,serviceClass=serviceClass,site=site,imagePreference=image,mountDataSets=mountDataSets)
Scott Baker537d8232014-04-08 23:40:56 -0700107 newSlice.save()
Scott Baker4b0cedd2014-04-22 21:01:21 -0700108 return HttpResponse("Slice created")
Scott Baker537d8232014-04-08 23:40:56 -0700109
Scott Bakeredf2f672014-04-22 10:17:37 -0700110class 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 Baker168b7412014-05-16 14:25:41 -0700116 #network = request.POST.get("network","0")
Scott Baker4b0cedd2014-04-22 21:01:21 -0700117 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 Baker168b7412014-05-16 14:25:41 -0700125 #setattr(entry,'network',network)
Scott Baker4b0cedd2014-04-22 21:01:21 -0700126 setattr(entry,'mountDataSets',dataSet)
127 entry.save()
128 break
129 return HttpResponse("Slice updated")
130
Scott Baker537d8232014-04-08 23:40:56 -0700131def 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 Baker168b7412014-05-16 14:25:41 -0700142 #tenantSliceDetails['network']=userSliceTableFormatter(getNetworkInfo(user))
Scott Baker537d8232014-04-08 23:40:56 -0700143 tenantSliceDetails['deploymentSites']=userSliceTableFormatter(getDeploymentSites())
Scott Bakerb85c26b2014-04-18 11:25:32 -0700144 tenantSliceDetails['sites'] = userSliceTableFormatter(getTenantSitesInfo())
145 tenantSliceDetails['mountDataSets'] = userSliceTableFormatter(getMountDataSets())
Scott Baker168b7412014-05-16 14:25:41 -0700146 tenantSliceDetails['publicKey'] = getPublicKey(user)
Scott Baker537d8232014-04-08 23:40:56 -0700147 return tenantSliceDetails
148
Scott Baker537d8232014-04-08 23:40:56 -0700149def 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 Bakeredf2f672014-04-22 10:17:37 -0700157 sliceDataSet = entry.mountDataSets
Scott Baker168b7412014-05-16 14:25:41 -0700158 #sliceNetwork = entry.network
Scott Baker537d8232014-04-08 23:40:56 -0700159 numSliver = 0
160 sliceImage=""
161 sliceSite = {}
Scott Bakeredf2f672014-04-22 10:17:37 -0700162 sliceNode = {}
163 sliceInstance= {}
Scott Baker537d8232014-04-08 23:40:56 -0700164 for sliver in slice.slivers.all():
Scott Bakera880bb52014-04-14 23:52:48 -0700165 if sliver.node.site.name in BLESSED_SITES:
Scott Bakera880bb52014-04-14 23:52:48 -0700166 sliceSite[sliver.node.site.name] = sliceSite.get(sliver.node.site.name,0) + 1
167 sliceImage = sliver.image.name
Scott Bakerb37ce332014-04-22 22:57:40 -0700168 sliceNode[str(sliver)] = sliver.node.name
Scott Bakeredf2f672014-04-22 10:17:37 -0700169 numSliver = sum(sliceSite.values())
Scott Bakerb85c26b2014-04-18 11:25:32 -0700170 numSites = len(sliceSite)
Scott Baker168b7412014-05-16 14:25:41 -0700171 userSliceInfo.append({'sliceName': sliceName,'sliceServiceClass': sliceServiceClass,'preferredImage':preferredImage,'numOfSites':numSites, 'sliceSite':sliceSite,'sliceImage':sliceImage,'numOfSlivers':numSliver,'sliceDataSet':sliceDataSet,'instanceNodePair':sliceNode})
Scott Baker537d8232014-04-08 23:40:56 -0700172 return userSliceInfo
173
174def getTenantSitesInfo():
175 tenantSiteInfo=[]
176 for entry in Site.objects.all():
Scott Bakera880bb52014-04-14 23:52:48 -0700177 if entry.name in BLESSED_SITES:
178 tenantSiteInfo.append({'siteName':entry.name})
Scott Baker537d8232014-04-08 23:40:56 -0700179 return tenantSiteInfo
180
Scott Baker771819b2014-03-19 22:10:17 -0700181def userSliceTableFormatter(data):
182# pprint(data)
183 formattedData = {
184 'rows' : data
185 }
186 return formattedData
187
Scott Baker168b7412014-05-16 14:25:41 -0700188def 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 Baker537d8232014-04-08 23:40:56 -0700195def getServiceClassInfo(user):
196 serviceClassList = ServiceClass.objects.all()
197 sliceInfo = []
198 for entry in serviceClassList:
199 sliceInfo.append({'serviceClass':entry.name})
200 return sliceInfo
201
202def getImageInfo(user):
Scott Baker4b0cedd2014-04-22 21:01:21 -0700203 imageList = Image.objects.all()
204 #imageList = ['Fedora 16 LXC rev 1.3','Hadoop','MPI']
Scott Baker537d8232014-04-08 23:40:56 -0700205 imageInfo = []
206 for imageEntry in imageList:
Scott Baker4b0cedd2014-04-22 21:01:21 -0700207 imageInfo.append({'Image':imageEntry.name})
208 #imageInfo.append({'Image':imageEntry})
Scott Baker537d8232014-04-08 23:40:56 -0700209 return imageInfo
210
Scott Baker168b7412014-05-16 14:25:41 -0700211def 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
219SYNDICATE_REPLICATE_PORTNUM = 1025
220
221def 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
240def 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
250def 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 Bakerb85c26b2014-04-18 11:25:32 -0700257def getMountDataSets():
Scott Baker168b7412014-05-16 14:25:41 -0700258 dataSetInfo=[]
259 for volume in Volume.objects.all():
260 if not volume.private:
261 dataSetInfo.append({'DataSet': volume.name})
262
Scott Bakerb85c26b2014-04-18 11:25:32 -0700263 return dataSetInfo
264
Scott Baker537d8232014-04-08 23:40:56 -0700265def 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
274def getDeploymentSites():
275 deploymentList = Deployment.objects.all()
276 deploymentInfo = []
277 for entry in deploymentList:
278 deploymentInfo.append({'DeploymentSite':entry.name})
279 return deploymentInfo
280
Scott Baker771819b2014-03-19 22:10:17 -0700281def getSliceInfo(user):
Siobhan Tully06d2c032014-02-23 23:48:52 -0500282 sliceList = Slice.objects.all()
Scott Baker771819b2014-03-19 22:10:17 -0700283 slicePrivs = SlicePrivilege.objects.filter(user=user)
Siobhan Tully06d2c032014-02-23 23:48:52 -0500284 userSliceInfo = []
285 for entry in slicePrivs:
Siobhan Tullye18b3442014-02-23 14:23:34 -0500286
Scott Baker771819b2014-03-19 22:10:17 -0700287 slicename = Slice.objects.get(id=entry.slice.id).name
Scott Baker537d8232014-04-08 23:40:56 -0700288 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 Baker771819b2014-03-19 22:10:17 -0700295 sliceid = Slice.objects.get(id=entry.slice.id).id
Siobhan Tully06d2c032014-02-23 23:48:52 -0500296 try:
Scott Baker771819b2014-03-19 22:10:17 -0700297 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 Tully06d2c032014-02-23 23:48:52 -0500304 except:
Scott Baker771819b2014-03-19 22:10:17 -0700305 traceback.print_exc()
306 slivercount = 0
307 sitecount = 0
Siobhan Tully06d2c032014-02-23 23:48:52 -0500308
Scott Baker771819b2014-03-19 22:10:17 -0700309 userSliceInfo.append({'slicename': slicename, 'sliceid':sliceid,
Scott Baker537d8232014-04-08 23:40:56 -0700310 'sitesUsed':sites_used,
311 'role': SliceRole.objects.get(id=entry.role.id).role,
312 'slivercount': slivercount,
313 'sitecount':sitecount})
Scott Baker771819b2014-03-19 22:10:17 -0700314
Siobhan Tully06d2c032014-02-23 23:48:52 -0500315 return userSliceInfo
316
Scott Bakere9f4ec92014-04-29 11:29:45 -0700317def 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 Baker75095b62014-04-21 17:32:09 -0700326def getCDNOperatorData(randomizeData = False, wait=True):
327 HPC_SLICE_NAME = "HyperCache"
328
Scott Baker0017c102014-04-18 10:48:31 -0700329 bq = PlanetStackAnalytics()
Siobhan Tully06d2c032014-02-23 23:48:52 -0500330
Scott Baker4a4472d2014-05-13 17:07:46 -0700331 rows = bq.get_cached_query_results(bq.compose_cached_query(), wait)
Siobhan Tully06d2c032014-02-23 23:48:52 -0500332
Scott Bakeredf2f672014-04-22 10:17:37 -0700333 # 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 Baker0017c102014-04-18 10:48:31 -0700336
Scott Baker75095b62014-04-21 17:32:09 -0700337 if rows:
Scott Baker696e2662014-04-24 17:03:29 -0700338 rows = bq.postprocess_results(rows, filter={"event": "hpc_heartbeat"}, maxi=["cpu"], count=["hostname"], computed=["bytes_sent/elapsed"], groupBy=["Time","site"], maxDeltaTime=80)
Scott Baker0017c102014-04-18 10:48:31 -0700339
Scott Baker75095b62014-04-21 17:32:09 -0700340 # dictionaryize the statistics rows by site name
341 stats_rows = {}
342 for row in rows:
343 stats_rows[row["site"]] = row
Scott Bakeredf2f672014-04-22 10:17:37 -0700344 else:
345 stats_rows = {}
Scott Baker0017c102014-04-18 10:48:31 -0700346
Scott Baker09fad022014-04-30 09:50:01 -0700347 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 Baker0017c102014-04-18 10:48:31 -0700352
353 new_rows = {}
Scott Baker75095b62014-04-21 17:32:09 -0700354 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 Baker0017c102014-04-18 10:48:31 -0700371 "health": 0,
Scott Baker75095b62014-04-21 17:32:09 -0700372 "numNodes": int(site.nodes.count()),
Scott Bakere9f4ec92014-04-29 11:29:45 -0700373 "activeHPCSlivers": int(stats_row.get("count_hostname", 0)), # measured number of slivers, from bigquery statistics
Scott Baker75095b62014-04-21 17:32:09 -0700374 "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 Baker0017c102014-04-18 10:48:31 -0700392
393 return new_rows
Siobhan Tully06d2c032014-02-23 23:48:52 -0500394
Scott Baker168b7412014-05-16 14:25:41 -0700395def 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 Baker771819b2014-03-19 22:10:17 -0700400class 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
417class DashboardUserSiteView(View):
418 def get(self, request, **kwargs):
419 return HttpResponse(json.dumps(getUserSliceInfo(request.user, True)), mimetype='application/javascript')
420
Scott Baker537d8232014-04-08 23:40:56 -0700421class TenantViewData(View):
422 def get(self, request, **kwargs):
423 return HttpResponse(json.dumps(getTenantSliceInfo(request.user, True)), mimetype='application/javascript')
424
Scott Baker75095b62014-04-21 17:32:09 -0700425def haversine(site_lat, site_lon, lat, lon):
Scott Baker75095b62014-04-21 17:32:09 -0700426 d=0
427 if lat and lon and site_lat and site_lon:
Scott Baker4b0cedd2014-04-22 21:01:21 -0700428 site_lat = float(site_lat)
429 site_lon = float(site_lon)
430 lat = float(lat)
431 lon = float(lon)
Scott Baker75095b62014-04-21 17:32:09 -0700432 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 Bakerbd57a432014-04-14 16:12:15 -0700439def 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 Baker75095b62014-04-21 17:32:09 -0700448 d = haversine(site.location.latitude, site.location.longitude, lat, lon)
Scott Bakerbd57a432014-04-14 16:12:15 -0700449
450 return (-has_slivers_here, d)
451
452def 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 Bakera880bb52014-04-14 23:52:48 -0700466 sites = [x for x in sites if x.name in BLESSED_SITES]
Scott Bakerbd57a432014-04-14 16:12:15 -0700467 sites = sorted(sites, key=functools.partial(siteSortKey, slice=slice, count=count, lat=lat, lon=lon))
468
469 return sites
470
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700471def slice_increase_slivers(user, user_ip, siteList, slice, count, noAct=False):
Scott Bakere4deddd2014-04-14 16:48:05 -0700472 sitesChanged = {}
473
Scott Bakerbd57a432014-04-14 16:12:15 -0700474 # 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 Baker6253dfc2014-04-14 09:29:30 -0700479 for sliver in node.slivers.all():
Scott Baker21511942014-04-15 17:44:27 -0700480 if sliver.slice.id == slice.id:
Scott Baker0017c102014-04-18 10:48:31 -0700481 node.sliverCount = node.sliverCount + 1
Scott Baker6253dfc2014-04-14 09:29:30 -0700482
Scott Bakerbd57a432014-04-14 16:12:15 -0700483 # 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 Baker6253dfc2014-04-14 09:29:30 -0700491
Scott Bakerbd57a432014-04-14 16:12:15 -0700492 print "adding sliver at node", node.name, "of site", node.site.name
Scott Baker6253dfc2014-04-14 09:29:30 -0700493
Scott Bakere4deddd2014-04-14 16:48:05 -0700494 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 Bakerbd57a432014-04-14 16:12:15 -0700503
504 node.sliverCount = node.sliverCount + 1
505
506 count = count - 1
507
Scott Bakere4deddd2014-04-14 16:48:05 -0700508 sitesChanged[node.site.name] = sitesChanged.get(node.site.name,0) + 1
509
510 return sitesChanged
511
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700512def slice_decrease_slivers(user, siteList, slice, count, noAct=False):
Scott Bakere4deddd2014-04-14 16:48:05 -0700513 sitesChanged = {}
Scott Bakerb85c26b2014-04-18 11:25:32 -0700514 sliverList ={}
Scott Bakerbd57a432014-04-14 16:12:15 -0700515 if siteList:
516 siteNames = [site.name for site in siteList]
517 else:
518 siteNames = None
519
520 for sliver in slice.slivers.all():
Scott Bakerb85c26b2014-04-18 11:25:32 -0700521 if(not siteNames) or (sliver.node.site.name in siteNames):
522 node = sliver.node
523 sliverList[sliver.name]=node.name
Scott Bakerbd57a432014-04-14 16:12:15 -0700524
Scott Bakerb85c26b2014-04-18 11:25:32 -0700525 for key in sliverList:
526 if count>0:
Scott Bakerf1b65c02014-04-21 01:03:17 -0700527 sliver = Sliver.objects.filter(name=key)[0]
Scott Bakerb85c26b2014-04-18 11:25:32 -0700528 sliver.delete()
529 print "deleting sliver",sliverList[key],"at node",sliver.node.name
530 count=count-1
Scott Bakerf1b65c02014-04-21 01:03:17 -0700531 sitesChanged[sliver.node.site.name] = sitesChanged.get(sliver.node.site.name,0) - 1
Scott Bakerb85c26b2014-04-18 11:25:32 -0700532
Scott Bakere4deddd2014-04-14 16:48:05 -0700533 return sitesChanged
534
Scott Bakerb85c26b2014-04-18 11:25:32 -0700535class TenantDeleteSliceView(View):
536 def post(self,request):
537 sliceName = request.POST.get("sliceName",None)
538 slice = Slice.objects.get(name=sliceName)
Scott Baker168b7412014-05-16 14:25:41 -0700539 #print slice, slice.id
Scott Bakerb85c26b2014-04-18 11:25:32 -0700540 sliceToDel=Slice(name=sliceName, id=slice.id)
541 sliceToDel.delete()
Scott Baker4b0cedd2014-04-22 21:01:21 -0700542 return HttpResponse("Slice deleted")
Scott Bakerb85c26b2014-04-18 11:25:32 -0700543
Scott Baker6253dfc2014-04-14 09:29:30 -0700544class TenantAddOrRemoveSliverView(View):
Scott Bakere4deddd2014-04-14 16:48:05 -0700545 """ 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 Bakera880bb52014-04-14 23:52:48 -0700553 noAct - if set, no changes will be made to db, but result will still
554 show which sites would have been modified.
Scott Bakere4deddd2014-04-14 16:48:05 -0700555
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 Baker6253dfc2014-04-14 09:29:30 -0700560 def post(self, request, *args, **kwargs):
Scott Bakerbd57a432014-04-14 16:12:15 -0700561 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 Bakere4deddd2014-04-14 16:48:05 -0700565 noAct = request.POST.get("noAct", False)
Scott Bakerbd57a432014-04-14 16:12:15 -0700566
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 Baker6253dfc2014-04-14 09:29:30 -0700576
577 if (actionToDo == "add"):
Scott Bakerbd57a432014-04-14 16:12:15 -0700578 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 Bakerfe6a7e12014-04-15 10:58:31 -0700582 sitesChanged = slice_increase_slivers(request.user, user_ip, siteList, slice, count, noAct)
Scott Baker6253dfc2014-04-14 09:29:30 -0700583 elif (actionToDo == "rem"):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700584 sitesChanged = slice_decrease_slivers(request.user, siteList, slice, count, noAct)
Scott Bakerbd57a432014-04-14 16:12:15 -0700585 else:
586 return HttpResponseServerError("Unknown actionToDo %s" % actionToDo)
587
Scott Bakere4deddd2014-04-14 16:48:05 -0700588 return HttpResponse(json.dumps(sitesChanged), mimetype='application/javascript')
Scott Bakerbd57a432014-04-14 16:12:15 -0700589
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
595class 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 Baker6253dfc2014-04-14 09:29:30 -0700606
Scott Baker771819b2014-03-19 22:10:17 -0700607class DashboardSummaryAjaxView(View):
608 def get(self, request, **kwargs):
Scott Baker0017c102014-04-18 10:48:31 -0700609 def avg(x):
Scott Baker111702b2014-05-15 16:17:16 -0700610 if len(x)==0:
611 return 0
Scott Baker0017c102014-04-18 10:48:31 -0700612 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 Baker771819b2014-03-19 22:10:17 -0700627
628class DashboardAddOrRemoveSliverView(View):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700629 # TODO: deprecate this view in favor of using TenantAddOrRemoveSliverView
630
Scott Baker771819b2014-03-19 22:10:17 -0700631 def post(self, request, *args, **kwargs):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700632 siteName = request.POST.get("site", None)
Scott Baker771819b2014-03-19 22:10:17 -0700633 actionToDo = request.POST.get("actionToDo", "0")
634
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700635 siteList = [Site.objects.get(name=siteName)]
636 slice = Slice.objects.get(name="HyperCache")
637
Scott Baker771819b2014-03-19 22:10:17 -0700638 if (actionToDo == "add"):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700639 user_ip = request.GET.get("ip", get_ip(request))
640 slice_increase_slivers(request.user, user_ip, siteList, slice, 1)
Scott Baker771819b2014-03-19 22:10:17 -0700641 elif (actionToDo == "rem"):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700642 slice_decrease_slivers(request.user, siteList, slice, 1)
Scott Baker771819b2014-03-19 22:10:17 -0700643
644 print '*' * 50
645 print 'Ask for site: ' + siteName + ' to ' + actionToDo + ' another HPC Sliver'
646 return HttpResponse('This is POST request ')
Siobhan Tully06d2c032014-02-23 23:48:52 -0500647
648class DashboardAjaxView(View):
649 def get(self, request, **kwargs):
650 return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript')
Scott Baker771819b2014-03-19 22:10:17 -0700651
652class 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