blob: 705f4f6ea423f2ac90a39404acc68f1bd05e74c0 [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 Baker537d8232014-04-08 23:40:56 -070010from core.models import *
Scott Bakere9f4ec92014-04-29 11:29:45 -070011from hpc.models import ContentProvider
Scott Bakerbd57a432014-04-14 16:12:15 -070012from operator import attrgetter
13from django.views.decorators.csrf import csrf_exempt
14from django.http import HttpResponse, HttpResponseServerError
Scott Baker537d8232014-04-08 23:40:56 -070015from django.core import urlresolvers
Scott Bakerbd57a432014-04-14 16:12:15 -070016from django.contrib.gis.geoip import GeoIP
17from ipware.ip import get_ip
Scott Baker771819b2014-03-19 22:10:17 -070018import traceback
Scott Baker6253dfc2014-04-14 09:29:30 -070019import socket
Siobhan Tully06d2c032014-02-23 23:48:52 -050020
Scott Bakera880bb52014-04-14 23:52:48 -070021BLESSED_SITES = ["Stanford", "Washington", "Princeton", "GeorgiaTech", "MaxPlanck"]
22
Scott Baker771819b2014-03-19 22:10:17 -070023if os.path.exists("/home/smbaker/projects/vicci/cdn/bigquery"):
24 sys.path.append("/home/smbaker/projects/vicci/cdn/bigquery")
25else:
26 sys.path.append("/opt/planetstack/hpc_wizard")
27import hpc_wizard
Scott Baker75095b62014-04-21 17:32:09 -070028from planetstack_analytics import DoPlanetStackAnalytics, PlanetStackAnalytics, RED_LOAD, BLUE_LOAD
Siobhan Tullycf04fb62014-01-11 11:25:57 -050029
30class DashboardWelcomeView(TemplateView):
31 template_name = 'admin/dashboard/welcome.html'
32
33 def get(self, request, *args, **kwargs):
34 context = self.get_context_data(**kwargs)
Scott Baker771819b2014-03-19 22:10:17 -070035 userDetails = getUserSliceInfo(request.user)
36 #context['site'] = userDetails['site']
Siobhan Tullycf04fb62014-01-11 11:25:57 -050037
Scott Baker771819b2014-03-19 22:10:17 -070038 context['userSliceInfo'] = userDetails['userSliceInfo']
39 context['cdnData'] = userDetails['cdnData']
Scott Bakere9f4ec92014-04-29 11:29:45 -070040 context['cdnContentProviders'] = userDetails['cdnContentProviders']
Siobhan Tullycf04fb62014-01-11 11:25:57 -050041 return self.render_to_response(context=context)
Siobhan Tullye18b3442014-02-23 14:23:34 -050042
Scott Baker771819b2014-03-19 22:10:17 -070043def getUserSliceInfo(user, tableFormat = False):
44 userDetails = {}
Scott Baker771819b2014-03-19 22:10:17 -070045
46 userSliceData = getSliceInfo(user)
47 if (tableFormat):
48# pprint("******* GET USER SLICE INFO")
49 userDetails['userSliceInfo'] = userSliceTableFormatter(userSliceData)
50 else:
51 userDetails['userSliceInfo'] = userSliceData
Scott Bakere9f4ec92014-04-29 11:29:45 -070052 userDetails['cdnData'] = getCDNOperatorData(wait=False)
53 userDetails['cdnContentProviders'] = getCDNContentProviderData()
Scott Baker771819b2014-03-19 22:10:17 -070054 return userDetails
55
Scott Baker537d8232014-04-08 23:40:56 -070056class TenantCreateSlice(View):
57 def post(self, request, *args, **kwargs):
58 sliceName = request.POST.get("sliceName", "0")
59 serviceClass = request.POST.get("serviceClass", "0")
60 imageName = request.POST.get("imageName", "0")
61 actionToDo = request.POST.get("actionToDo", "0")
Scott Bakeredf2f672014-04-22 10:17:37 -070062 network = request.POST.get("network","0")
63 mountDataSets = request.POST.get("mountDataSets","0")
Scott Baker537d8232014-04-08 23:40:56 -070064 if (actionToDo == "add"):
65 serviceClass = ServiceClass.objects.get(name=serviceClass)
66 site = request.user.site
Scott Baker4b0cedd2014-04-22 21:01:21 -070067 image = Image.objects.get(name=imageName)
68 newSlice = Slice(name=sliceName,serviceClass=serviceClass,site=site,imagePreference=image,mountDataSets=mountDataSets,network=network)
Scott Baker537d8232014-04-08 23:40:56 -070069 newSlice.save()
Scott Baker4b0cedd2014-04-22 21:01:21 -070070 return HttpResponse("Slice created")
Scott Baker537d8232014-04-08 23:40:56 -070071
Scott Bakeredf2f672014-04-22 10:17:37 -070072class TenantUpdateSlice(View):
73 def post(self, request, *args, **kwargs):
74 sliceName = request.POST.get("sliceName", "0")
75 serviceClass = request.POST.get("serviceClass", "0")
76 imageName = request.POST.get("imageName", "0")
77 actionToDo = request.POST.get("actionToDo", "0")
78 network = request.POST.get("network","0")
Scott Baker4b0cedd2014-04-22 21:01:21 -070079 dataSet = request.POST.get("dataSet","0")
80 slice = Slice.objects.all()
81 for entry in slice:
82 serviceClass = ServiceClass.objects.get(name=serviceClass)
83 if(entry.name==sliceName):
84 if (actionToDo == "update"):
85 setattr(entry,'serviceClass',serviceClass)
86 setattr(entry,'imagePreference',imageName)
87 setattr(entry,'network',network)
88 setattr(entry,'mountDataSets',dataSet)
89 entry.save()
90 break
91 return HttpResponse("Slice updated")
92
Scott Bakeredf2f672014-04-22 10:17:37 -070093def update_slice(sliceName,**fields):
94 slice = Slice.objects.filter(name = sliceName)
95 for (k,v) in fields.items():
96 setattr(slice, k, v)
97 slice.save()
98 return slice
Scott Baker537d8232014-04-08 23:40:56 -070099
100def getTenantSliceInfo(user, tableFormat = False):
101 tenantSliceDetails = {}
102 tenantSliceData = getTenantInfo(user)
103 tenantServiceClassData = getServiceClassInfo(user)
104 if (tableFormat):
105 tenantSliceDetails['userSliceInfo'] = userSliceTableFormatter(tenantSliceData)
106 tenantSliceDetails['sliceServiceClass']=userSliceTableFormatter(tenantServiceClassData)
107 else:
108 tenantSliceDetails['userSliceInfo'] = tenantSliceData
109 tenantSliceDetails['sliceServiceClass']=userSliceTableFormatter(tenantServiceClassData)
110 tenantSliceDetails['image']=userSliceTableFormatter(getImageInfo(user))
111 tenantSliceDetails['network']=userSliceTableFormatter(getNetworkInfo(user))
112 tenantSliceDetails['deploymentSites']=userSliceTableFormatter(getDeploymentSites())
Scott Bakerb85c26b2014-04-18 11:25:32 -0700113 tenantSliceDetails['sites'] = userSliceTableFormatter(getTenantSitesInfo())
114 tenantSliceDetails['mountDataSets'] = userSliceTableFormatter(getMountDataSets())
Scott Baker537d8232014-04-08 23:40:56 -0700115 return tenantSliceDetails
116
117
118def getTenantInfo(user):
119 slices =Slice.objects.all()
120 userSliceInfo = []
121 for entry in slices:
122 sliceName = Slice.objects.get(id=entry.id).name
123 slice = Slice.objects.get(name=Slice.objects.get(id=entry.id).name)
124 sliceServiceClass = entry.serviceClass.name
125 preferredImage = entry.imagePreference
Scott Bakeredf2f672014-04-22 10:17:37 -0700126 sliceDataSet = entry.mountDataSets
127 sliceNetwork = entry.network
Scott Baker537d8232014-04-08 23:40:56 -0700128 numSliver = 0
129 sliceImage=""
130 sliceSite = {}
Scott Bakeredf2f672014-04-22 10:17:37 -0700131 sliceNode = {}
132 sliceInstance= {}
Scott Baker537d8232014-04-08 23:40:56 -0700133 for sliver in slice.slivers.all():
Scott Bakera880bb52014-04-14 23:52:48 -0700134 if sliver.node.site.name in BLESSED_SITES:
Scott Bakera880bb52014-04-14 23:52:48 -0700135 sliceSite[sliver.node.site.name] = sliceSite.get(sliver.node.site.name,0) + 1
136 sliceImage = sliver.image.name
Scott Bakerb37ce332014-04-22 22:57:40 -0700137 sliceNode[str(sliver)] = sliver.node.name
Scott Bakeredf2f672014-04-22 10:17:37 -0700138 numSliver = sum(sliceSite.values())
Scott Bakerb85c26b2014-04-18 11:25:32 -0700139 numSites = len(sliceSite)
Scott Bakeredf2f672014-04-22 10:17:37 -0700140 userSliceInfo.append({'sliceName': sliceName,'sliceServiceClass': sliceServiceClass,'preferredImage':preferredImage,'numOfSites':numSites, 'sliceSite':sliceSite,'sliceImage':sliceImage,'numOfSlivers':numSliver,'sliceDataSet':sliceDataSet,'sliceNetwork':sliceNetwork, 'instanceNodePair':sliceNode})
Scott Baker537d8232014-04-08 23:40:56 -0700141 return userSliceInfo
142
143def getTenantSitesInfo():
144 tenantSiteInfo=[]
145 for entry in Site.objects.all():
Scott Bakera880bb52014-04-14 23:52:48 -0700146 if entry.name in BLESSED_SITES:
147 tenantSiteInfo.append({'siteName':entry.name})
Scott Baker537d8232014-04-08 23:40:56 -0700148 return tenantSiteInfo
149
Scott Baker771819b2014-03-19 22:10:17 -0700150def userSliceTableFormatter(data):
151# pprint(data)
152 formattedData = {
153 'rows' : data
154 }
155 return formattedData
156
Scott Baker537d8232014-04-08 23:40:56 -0700157def getServiceClassInfo(user):
158 serviceClassList = ServiceClass.objects.all()
159 sliceInfo = []
160 for entry in serviceClassList:
161 sliceInfo.append({'serviceClass':entry.name})
162 return sliceInfo
163
164def getImageInfo(user):
Scott Baker4b0cedd2014-04-22 21:01:21 -0700165 imageList = Image.objects.all()
166 #imageList = ['Fedora 16 LXC rev 1.3','Hadoop','MPI']
Scott Baker537d8232014-04-08 23:40:56 -0700167 imageInfo = []
168 for imageEntry in imageList:
Scott Baker4b0cedd2014-04-22 21:01:21 -0700169 imageInfo.append({'Image':imageEntry.name})
170 #imageInfo.append({'Image':imageEntry})
Scott Baker537d8232014-04-08 23:40:56 -0700171 return imageInfo
172
Scott Bakerb85c26b2014-04-18 11:25:32 -0700173def getMountDataSets():
Scott Bakeredf2f672014-04-22 10:17:37 -0700174 dataSetList = ['------','GenBank','LSST','LHC','NOAA','Measurement Lab','Common Crawl']
Scott Bakerb85c26b2014-04-18 11:25:32 -0700175 dataSetInfo = []
176 for entry in dataSetList:
177 dataSetInfo.append({'DataSet':entry})
178 return dataSetInfo
179
Scott Baker537d8232014-04-08 23:40:56 -0700180def getNetworkInfo(user):
181 #networkList = Network.objects.all()
182 networkList = ['Private Only','Private and Publicly Routable']
183 networkInfo = []
184 for networkEntry in networkList:
185 #networkInfo.append({'Network':networkEntry.name})
186 networkInfo.append({'Network':networkEntry})
187 return networkInfo
188
189def getDeploymentSites():
190 deploymentList = Deployment.objects.all()
191 deploymentInfo = []
192 for entry in deploymentList:
193 deploymentInfo.append({'DeploymentSite':entry.name})
194 return deploymentInfo
195
Scott Baker771819b2014-03-19 22:10:17 -0700196def getSliceInfo(user):
Siobhan Tully06d2c032014-02-23 23:48:52 -0500197 sliceList = Slice.objects.all()
Scott Baker771819b2014-03-19 22:10:17 -0700198 slicePrivs = SlicePrivilege.objects.filter(user=user)
Siobhan Tully06d2c032014-02-23 23:48:52 -0500199 userSliceInfo = []
200 for entry in slicePrivs:
Siobhan Tullye18b3442014-02-23 14:23:34 -0500201
Scott Baker771819b2014-03-19 22:10:17 -0700202 slicename = Slice.objects.get(id=entry.slice.id).name
Scott Baker537d8232014-04-08 23:40:56 -0700203 slice = Slice.objects.get(name=Slice.objects.get(id=entry.slice.id).name)
204 sliverList=Sliver.objects.all()
205 sites_used = {}
206 for sliver in slice.slivers.all():
207 #sites_used['deploymentSites'] = sliver.node.deployment.name
208 # sites_used[sliver.image.name] = sliver.image.name
209 sites_used[sliver.node.site.name] = sliver.numberCores
Scott Baker771819b2014-03-19 22:10:17 -0700210 sliceid = Slice.objects.get(id=entry.slice.id).id
Siobhan Tully06d2c032014-02-23 23:48:52 -0500211 try:
Scott Baker771819b2014-03-19 22:10:17 -0700212 sliverList = Sliver.objects.filter(slice=entry.slice.id)
213 siteList = {}
214 for x in sliverList:
215 if x.node.site not in siteList:
216 siteList[x.node.site] = 1
217 slivercount = len(sliverList)
218 sitecount = len(siteList)
Siobhan Tully06d2c032014-02-23 23:48:52 -0500219 except:
Scott Baker771819b2014-03-19 22:10:17 -0700220 traceback.print_exc()
221 slivercount = 0
222 sitecount = 0
Siobhan Tully06d2c032014-02-23 23:48:52 -0500223
Scott Baker771819b2014-03-19 22:10:17 -0700224 userSliceInfo.append({'slicename': slicename, 'sliceid':sliceid,
Scott Baker537d8232014-04-08 23:40:56 -0700225 'sitesUsed':sites_used,
226 'role': SliceRole.objects.get(id=entry.role.id).role,
227 'slivercount': slivercount,
228 'sitecount':sitecount})
Scott Baker771819b2014-03-19 22:10:17 -0700229
Siobhan Tully06d2c032014-02-23 23:48:52 -0500230 return userSliceInfo
231
Scott Bakere9f4ec92014-04-29 11:29:45 -0700232def getCDNContentProviderData():
233 cps = []
234 for dm_cp in ContentProvider.objects.all():
235 cp = {"name": dm_cp.name,
236 "account": dm_cp.account}
237 cps.append(cp)
238
239 return cps
240
Scott Baker75095b62014-04-21 17:32:09 -0700241def getCDNOperatorData(randomizeData = False, wait=True):
242 HPC_SLICE_NAME = "HyperCache"
243
Scott Baker0017c102014-04-18 10:48:31 -0700244 bq = PlanetStackAnalytics()
Siobhan Tully06d2c032014-02-23 23:48:52 -0500245
Scott Baker4a4472d2014-05-13 17:07:46 -0700246 rows = bq.get_cached_query_results(bq.compose_cached_query(), wait)
Siobhan Tully06d2c032014-02-23 23:48:52 -0500247
Scott Bakeredf2f672014-04-22 10:17:37 -0700248 # wait=False on the first time the Dashboard is opened. This means we might
249 # not have any rows yet. The dashboard code polls every 30 seconds, so it
250 # will eventually pick them up.
Scott Baker0017c102014-04-18 10:48:31 -0700251
Scott Baker75095b62014-04-21 17:32:09 -0700252 if rows:
Scott Baker696e2662014-04-24 17:03:29 -0700253 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 -0700254
Scott Baker75095b62014-04-21 17:32:09 -0700255 # dictionaryize the statistics rows by site name
256 stats_rows = {}
257 for row in rows:
258 stats_rows[row["site"]] = row
Scott Bakeredf2f672014-04-22 10:17:37 -0700259 else:
260 stats_rows = {}
Scott Baker0017c102014-04-18 10:48:31 -0700261
Scott Baker09fad022014-04-30 09:50:01 -0700262 slice = Slice.objects.filter(name=HPC_SLICE_NAME)
263 if slice:
264 slice_slivers = list(slice[0].slivers.all())
265 else:
266 slice_slivers = []
Scott Baker0017c102014-04-18 10:48:31 -0700267
268 new_rows = {}
Scott Baker75095b62014-04-21 17:32:09 -0700269 for site in Site.objects.all():
270 # compute number of slivers allocated in the data model
271 allocated_slivers = 0
272 for sliver in slice_slivers:
273 if sliver.node.site == site:
274 allocated_slivers = allocated_slivers + 1
275
276 stats_row = stats_rows.get(site.name,{})
277
278 max_cpu = stats_row.get("max_avg_cpu", stats_row.get("max_cpu",0))
279 cpu=float(max_cpu)/100.0
280 hotness = max(0.0, ((cpu*RED_LOAD) - BLUE_LOAD)/(RED_LOAD-BLUE_LOAD))
281
282 # format it to what that CDN Operations View is expecting
283 new_row = {"lat": float(site.location.longitude),
284 "long": float(site.location.longitude),
285 "lat": float(site.location.latitude),
Scott Baker0017c102014-04-18 10:48:31 -0700286 "health": 0,
Scott Baker75095b62014-04-21 17:32:09 -0700287 "numNodes": int(site.nodes.count()),
Scott Bakere9f4ec92014-04-29 11:29:45 -0700288 "activeHPCSlivers": int(stats_row.get("count_hostname", 0)), # measured number of slivers, from bigquery statistics
Scott Baker75095b62014-04-21 17:32:09 -0700289 "numHPCSlivers": allocated_slivers, # allocated number of slivers, from data model
290 "siteUrl": str(site.site_url),
291 "bandwidth": stats_row.get("sum_computed_bytes_sent_div_elapsed",0),
292 "load": max_cpu,
293 "hot": float(hotness)}
294 new_rows[str(site.name)] = new_row
295
296 # get rid of sites with 0 slivers that overlap other sites with >0 slivers
297 for (k,v) in new_rows.items():
298 bad=False
299 if v["numHPCSlivers"]==0:
300 for v2 in new_rows.values():
301 if (v!=v2) and (v2["numHPCSlivers"]>=0):
302 d = haversine(v["lat"],v["long"],v2["lat"],v2["long"])
303 if d<100:
304 bad=True
305 if bad:
306 del new_rows[k]
Scott Baker0017c102014-04-18 10:48:31 -0700307
308 return new_rows
Siobhan Tully06d2c032014-02-23 23:48:52 -0500309
Scott Baker771819b2014-03-19 22:10:17 -0700310class SimulatorView(View):
311 def get(self, request, **kwargs):
312 sim = json.loads(file("/tmp/simulator.json","r").read())
313 text = "<html><head></head><body>"
314 text += "Iteration: %d<br>" % sim["iteration"]
315 text += "Elapsed since report %d<br><br>" % sim["elapsed_since_report"]
316 text += "<table border=1>"
317 text += "<tr><th>site</th><th>trend</th><th>weight</th><th>bytes_sent</th><th>hot</th></tr>"
318 for site in sim["site_load"].values():
319 text += "<tr>"
320 text += "<td>%s</td><td>%0.2f</td><td>%0.2f</td><td>%d</td><td>%0.2f</td>" % \
321 (site["name"], site["trend"], site["weight"], site["bytes_sent"], site["load_frac"])
322 text += "</tr>"
323 text += "</table>"
324 text += "</body></html>"
325 return HttpResponse(text)
326
327class DashboardUserSiteView(View):
328 def get(self, request, **kwargs):
329 return HttpResponse(json.dumps(getUserSliceInfo(request.user, True)), mimetype='application/javascript')
330
Scott Baker537d8232014-04-08 23:40:56 -0700331class TenantViewData(View):
332 def get(self, request, **kwargs):
333 return HttpResponse(json.dumps(getTenantSliceInfo(request.user, True)), mimetype='application/javascript')
334
Scott Baker75095b62014-04-21 17:32:09 -0700335def haversine(site_lat, site_lon, lat, lon):
Scott Baker75095b62014-04-21 17:32:09 -0700336 d=0
337 if lat and lon and site_lat and site_lon:
Scott Baker4b0cedd2014-04-22 21:01:21 -0700338 site_lat = float(site_lat)
339 site_lon = float(site_lon)
340 lat = float(lat)
341 lon = float(lon)
Scott Baker75095b62014-04-21 17:32:09 -0700342 R = 6378.1
343 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)
344 c = 2 * math.atan2( math.sqrt(a), math.sqrt(1 - a) )
345 d = R * c
346
347 return d
348
Scott Bakerbd57a432014-04-14 16:12:15 -0700349def siteSortKey(site, slice=None, count=None, lat=None, lon=None):
350 # try to pick a site we're already using
351 has_slivers_here=False
352 if slice:
353 for sliver in slice.slivers.all():
354 if sliver.node.site.name == site.name:
355 has_slivers_here=True
356
357 # Haversine method
Scott Baker75095b62014-04-21 17:32:09 -0700358 d = haversine(site.location.latitude, site.location.longitude, lat, lon)
Scott Bakerbd57a432014-04-14 16:12:15 -0700359
360 return (-has_slivers_here, d)
361
362def tenant_pick_sites(user, user_ip=None, slice=None, count=None):
363 """ Returns list of sites, sorted from most favorable to least favorable """
364 lat=None
365 lon=None
366 try:
367 client_geo = GeoIP().city(user_ip)
368 if client_geo:
369 lat=float(client_geo["latitude"])
370 lon=float(client_geo["longitude"])
371 except:
372 print "exception in geo code"
373 traceback.print_exc()
374
375 sites = Site.objects.all()
Scott Bakera880bb52014-04-14 23:52:48 -0700376 sites = [x for x in sites if x.name in BLESSED_SITES]
Scott Bakerbd57a432014-04-14 16:12:15 -0700377 sites = sorted(sites, key=functools.partial(siteSortKey, slice=slice, count=count, lat=lat, lon=lon))
378
379 return sites
380
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700381def slice_increase_slivers(user, user_ip, siteList, slice, count, noAct=False):
Scott Bakere4deddd2014-04-14 16:48:05 -0700382 sitesChanged = {}
383
Scott Bakerbd57a432014-04-14 16:12:15 -0700384 # let's compute how many slivers are in use in each node of each site
385 for site in siteList:
386 site.nodeList = list(site.nodes.all())
387 for node in site.nodeList:
388 node.sliverCount = 0
Scott Baker6253dfc2014-04-14 09:29:30 -0700389 for sliver in node.slivers.all():
Scott Baker21511942014-04-15 17:44:27 -0700390 if sliver.slice.id == slice.id:
Scott Baker0017c102014-04-18 10:48:31 -0700391 node.sliverCount = node.sliverCount + 1
Scott Baker6253dfc2014-04-14 09:29:30 -0700392
Scott Bakerbd57a432014-04-14 16:12:15 -0700393 # Allocate slivers to nodes
394 # for now, assume we want to allocate all slivers from the same site
395 nodes = siteList[0].nodeList
396 while (count>0):
397 # Sort the node list by number of slivers per node, then pick the
398 # node with the least number of slivers.
399 nodes = sorted(nodes, key=attrgetter("sliverCount"))
400 node = nodes[0]
Scott Baker6253dfc2014-04-14 09:29:30 -0700401
Scott Bakerbd57a432014-04-14 16:12:15 -0700402 print "adding sliver at node", node.name, "of site", node.site.name
Scott Baker6253dfc2014-04-14 09:29:30 -0700403
Scott Bakere4deddd2014-04-14 16:48:05 -0700404 if not noAct:
405 sliver = Sliver(name=node.name,
406 slice=slice,
407 node=node,
408 image = Image.objects.all()[0],
409 creator = User.objects.get(email=user),
410 deploymentNetwork=node.deployment,
411 numberCores =1 )
412 sliver.save()
Scott Bakerbd57a432014-04-14 16:12:15 -0700413
414 node.sliverCount = node.sliverCount + 1
415
416 count = count - 1
417
Scott Bakere4deddd2014-04-14 16:48:05 -0700418 sitesChanged[node.site.name] = sitesChanged.get(node.site.name,0) + 1
419
420 return sitesChanged
421
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700422def slice_decrease_slivers(user, siteList, slice, count, noAct=False):
Scott Bakere4deddd2014-04-14 16:48:05 -0700423 sitesChanged = {}
Scott Bakerb85c26b2014-04-18 11:25:32 -0700424 sliverList ={}
Scott Bakerbd57a432014-04-14 16:12:15 -0700425 if siteList:
426 siteNames = [site.name for site in siteList]
427 else:
428 siteNames = None
429
430 for sliver in slice.slivers.all():
Scott Bakerb85c26b2014-04-18 11:25:32 -0700431 if(not siteNames) or (sliver.node.site.name in siteNames):
432 node = sliver.node
433 sliverList[sliver.name]=node.name
Scott Bakerbd57a432014-04-14 16:12:15 -0700434
Scott Bakerb85c26b2014-04-18 11:25:32 -0700435 for key in sliverList:
436 if count>0:
Scott Bakerf1b65c02014-04-21 01:03:17 -0700437 sliver = Sliver.objects.filter(name=key)[0]
Scott Bakerb85c26b2014-04-18 11:25:32 -0700438 sliver.delete()
439 print "deleting sliver",sliverList[key],"at node",sliver.node.name
440 count=count-1
Scott Bakerf1b65c02014-04-21 01:03:17 -0700441 sitesChanged[sliver.node.site.name] = sitesChanged.get(sliver.node.site.name,0) - 1
Scott Bakerb85c26b2014-04-18 11:25:32 -0700442
Scott Bakere4deddd2014-04-14 16:48:05 -0700443 return sitesChanged
444
Scott Bakerb85c26b2014-04-18 11:25:32 -0700445class TenantDeleteSliceView(View):
446 def post(self,request):
447 sliceName = request.POST.get("sliceName",None)
448 slice = Slice.objects.get(name=sliceName)
449 print slice, slice.id
450 sliceToDel=Slice(name=sliceName, id=slice.id)
451 sliceToDel.delete()
Scott Baker4b0cedd2014-04-22 21:01:21 -0700452 return HttpResponse("Slice deleted")
Scott Bakerb85c26b2014-04-18 11:25:32 -0700453
Scott Baker6253dfc2014-04-14 09:29:30 -0700454class TenantAddOrRemoveSliverView(View):
Scott Bakere4deddd2014-04-14 16:48:05 -0700455 """ Add or remove slivers from a Slice
456
457 Arguments:
458 siteName - name of site. If not specified, PlanetStack will pick the
459 best site.,
460 actionToDo - [add | rem]
461 count - number of slivers to add or remove
462 sliceName - name of slice
Scott Bakera880bb52014-04-14 23:52:48 -0700463 noAct - if set, no changes will be made to db, but result will still
464 show which sites would have been modified.
Scott Bakere4deddd2014-04-14 16:48:05 -0700465
466 Returns:
467 Dictionary of sites that were modified, and the count of nodes
468 that were added or removed at each site.
469 """
Scott Baker6253dfc2014-04-14 09:29:30 -0700470 def post(self, request, *args, **kwargs):
Scott Bakerbd57a432014-04-14 16:12:15 -0700471 siteName = request.POST.get("siteName", None)
472 actionToDo = request.POST.get("actionToDo", None)
473 count = int(request.POST.get("count","0"))
474 sliceName = request.POST.get("slice", None)
Scott Bakere4deddd2014-04-14 16:48:05 -0700475 noAct = request.POST.get("noAct", False)
Scott Bakerbd57a432014-04-14 16:12:15 -0700476
477 if not sliceName:
478 return HttpResponseServerError("No slice name given")
479
480 slice = Slice.objects.get(name=sliceName)
481
482 if siteName:
483 siteList = [Site.objects.get(name=siteName)]
484 else:
485 siteList = None
Scott Baker6253dfc2014-04-14 09:29:30 -0700486
487 if (actionToDo == "add"):
Scott Bakerbd57a432014-04-14 16:12:15 -0700488 user_ip = request.GET.get("ip", get_ip(request))
489 if (siteList is None):
490 siteList = tenant_pick_sites(user, user_ip, slice, count)
491
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700492 sitesChanged = slice_increase_slivers(request.user, user_ip, siteList, slice, count, noAct)
Scott Baker6253dfc2014-04-14 09:29:30 -0700493 elif (actionToDo == "rem"):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700494 sitesChanged = slice_decrease_slivers(request.user, siteList, slice, count, noAct)
Scott Bakerbd57a432014-04-14 16:12:15 -0700495 else:
496 return HttpResponseServerError("Unknown actionToDo %s" % actionToDo)
497
Scott Bakere4deddd2014-04-14 16:48:05 -0700498 return HttpResponse(json.dumps(sitesChanged), mimetype='application/javascript')
Scott Bakerbd57a432014-04-14 16:12:15 -0700499
500 def get(self, request, *args, **kwargs):
501 request.POST = request.GET
502 return self.post(request, *args, **kwargs) # for testing REST in browser
503 #return HttpResponseServerError("GET is not supported")
504
505class TenantPickSitesView(View):
506 """ primarily just for testing purposes """
507 def get(self, request, *args, **kwargs):
508 count = request.GET.get("count","0")
509 slice = request.GET.get("slice",None)
510 if slice:
511 slice = Slice.objects.get(name=slice)
512 ip = request.GET.get("ip", get_ip(request))
513 sites = tenant_pick_sites(request.user, user_ip=ip, count=0, slice=slice)
514 sites = [x.name for x in sites]
515 return HttpResponse(json.dumps(sites), mimetype='application/javascript')
Scott Baker6253dfc2014-04-14 09:29:30 -0700516
Scott Baker771819b2014-03-19 22:10:17 -0700517class DashboardSummaryAjaxView(View):
518 def get(self, request, **kwargs):
Scott Baker0017c102014-04-18 10:48:31 -0700519 def avg(x):
520 return float(sum(x))/len(x)
521
522 sites = getCDNOperatorData().values()
523
524 sites = [site for site in sites if site["numHPCSlivers"]>0]
525
526 total_slivers = sum( [site["numHPCSlivers"] for site in sites] )
527 total_bandwidth = sum( [site["bandwidth"] for site in sites] )
528 average_cpu = int(avg( [site["load"] for site in sites] ))
529
530 result= {"total_slivers": total_slivers,
531 "total_bandwidth": total_bandwidth,
532 "average_cpu": average_cpu}
533
534 return HttpResponse(json.dumps(result), mimetype='application/javascript')
Scott Baker771819b2014-03-19 22:10:17 -0700535
536class DashboardAddOrRemoveSliverView(View):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700537 # TODO: deprecate this view in favor of using TenantAddOrRemoveSliverView
538
Scott Baker771819b2014-03-19 22:10:17 -0700539 def post(self, request, *args, **kwargs):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700540 siteName = request.POST.get("site", None)
Scott Baker771819b2014-03-19 22:10:17 -0700541 actionToDo = request.POST.get("actionToDo", "0")
542
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700543 siteList = [Site.objects.get(name=siteName)]
544 slice = Slice.objects.get(name="HyperCache")
545
Scott Baker771819b2014-03-19 22:10:17 -0700546 if (actionToDo == "add"):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700547 user_ip = request.GET.get("ip", get_ip(request))
548 slice_increase_slivers(request.user, user_ip, siteList, slice, 1)
Scott Baker771819b2014-03-19 22:10:17 -0700549 elif (actionToDo == "rem"):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700550 slice_decrease_slivers(request.user, siteList, slice, 1)
Scott Baker771819b2014-03-19 22:10:17 -0700551
552 print '*' * 50
553 print 'Ask for site: ' + siteName + ' to ' + actionToDo + ' another HPC Sliver'
554 return HttpResponse('This is POST request ')
Siobhan Tully06d2c032014-02-23 23:48:52 -0500555
556class DashboardAjaxView(View):
557 def get(self, request, **kwargs):
558 return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript')
Scott Baker771819b2014-03-19 22:10:17 -0700559
560class DashboardAnalyticsAjaxView(View):
561 def get(self, request, name="hello_world", **kwargs):
562 if (name == "hpcSummary"):
563 return HttpResponse(json.dumps(hpc_wizard.get_hpc_wizard().get_summary_for_view()), mimetype='application/javascript')
564 elif (name == "hpcUserSite"):
565 return HttpResponse(json.dumps(getUserSliceInfo(request.user, True)), mimetype='application/javascript')
566 elif (name == "hpcMap"):
567 return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript')
568 elif (name == "bigquery"):
569 (mimetype, data) = DoPlanetStackAnalytics(request)
570 return HttpResponse(data, mimetype=mimetype)
571 else:
572 return HttpResponse(json.dumps("Unknown"), mimetype='application/javascript')
573