blob: 6ff6d9efdaf0cc80d414d9b11a680d25d1a47e62 [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 Bakerbd57a432014-04-14 16:12:15 -070011from operator import attrgetter
12from django.views.decorators.csrf import csrf_exempt
13from django.http import HttpResponse, HttpResponseServerError
Scott Baker537d8232014-04-08 23:40:56 -070014from django.core import urlresolvers
Scott Bakerbd57a432014-04-14 16:12:15 -070015from django.contrib.gis.geoip import GeoIP
16from ipware.ip import get_ip
Scott Baker771819b2014-03-19 22:10:17 -070017import traceback
Scott Baker6253dfc2014-04-14 09:29:30 -070018import socket
Siobhan Tully06d2c032014-02-23 23:48:52 -050019
Scott Bakera880bb52014-04-14 23:52:48 -070020BLESSED_SITES = ["Stanford", "Washington", "Princeton", "GeorgiaTech", "MaxPlanck"]
21
Scott Baker771819b2014-03-19 22:10:17 -070022if os.path.exists("/home/smbaker/projects/vicci/cdn/bigquery"):
23 sys.path.append("/home/smbaker/projects/vicci/cdn/bigquery")
24else:
25 sys.path.append("/opt/planetstack/hpc_wizard")
26import hpc_wizard
Scott Baker0017c102014-04-18 10:48:31 -070027from planetstack_analytics import DoPlanetStackAnalytics, PlanetStackAnalytics
Siobhan Tullycf04fb62014-01-11 11:25:57 -050028
29class DashboardWelcomeView(TemplateView):
30 template_name = 'admin/dashboard/welcome.html'
31
32 def get(self, request, *args, **kwargs):
33 context = self.get_context_data(**kwargs)
Scott Baker771819b2014-03-19 22:10:17 -070034 userDetails = getUserSliceInfo(request.user)
35 #context['site'] = userDetails['site']
Siobhan Tullycf04fb62014-01-11 11:25:57 -050036
Scott Baker771819b2014-03-19 22:10:17 -070037 context['userSliceInfo'] = userDetails['userSliceInfo']
38 context['cdnData'] = userDetails['cdnData']
Siobhan Tullycf04fb62014-01-11 11:25:57 -050039 return self.render_to_response(context=context)
Siobhan Tullye18b3442014-02-23 14:23:34 -050040
Scott Baker771819b2014-03-19 22:10:17 -070041def getUserSliceInfo(user, tableFormat = False):
42 userDetails = {}
43# try:
44# // site = Site.objects.filter(id=user.site.id)
45# // except:
46# // site = Site.objects.filter(name="Princeton")
47# // userDetails['sitename'] = site[0].name
48# // userDetails['siteid'] = site[0].id
49
50 userSliceData = getSliceInfo(user)
51 if (tableFormat):
52# pprint("******* GET USER SLICE INFO")
53 userDetails['userSliceInfo'] = userSliceTableFormatter(userSliceData)
54 else:
55 userDetails['userSliceInfo'] = userSliceData
56 userDetails['cdnData'] = getCDNOperatorData();
57# pprint( userDetails)
58 return userDetails
59
Scott Baker537d8232014-04-08 23:40:56 -070060class TenantCreateSlice(View):
61 def post(self, request, *args, **kwargs):
62 sliceName = request.POST.get("sliceName", "0")
63 serviceClass = request.POST.get("serviceClass", "0")
64 imageName = request.POST.get("imageName", "0")
65 actionToDo = request.POST.get("actionToDo", "0")
66 print sliceName
67 if (actionToDo == "add"):
68 serviceClass = ServiceClass.objects.get(name=serviceClass)
69 site = request.user.site
70 #image = Image.objects.get(name=imageName)
71 newSlice = Slice(name=sliceName,serviceClass=serviceClass,site=site,imagePreference=imageName)
72 newSlice.save()
73 return newSlice
74
75
76def getTenantSliceInfo(user, tableFormat = False):
77 tenantSliceDetails = {}
78 tenantSliceData = getTenantInfo(user)
79 tenantServiceClassData = getServiceClassInfo(user)
80 if (tableFormat):
81 tenantSliceDetails['userSliceInfo'] = userSliceTableFormatter(tenantSliceData)
82 tenantSliceDetails['sliceServiceClass']=userSliceTableFormatter(tenantServiceClassData)
83 else:
84 tenantSliceDetails['userSliceInfo'] = tenantSliceData
85 tenantSliceDetails['sliceServiceClass']=userSliceTableFormatter(tenantServiceClassData)
86 tenantSliceDetails['image']=userSliceTableFormatter(getImageInfo(user))
87 tenantSliceDetails['network']=userSliceTableFormatter(getNetworkInfo(user))
88 tenantSliceDetails['deploymentSites']=userSliceTableFormatter(getDeploymentSites())
Scott Bakerb85c26b2014-04-18 11:25:32 -070089 tenantSliceDetails['sites'] = userSliceTableFormatter(getTenantSitesInfo())
90 tenantSliceDetails['mountDataSets'] = userSliceTableFormatter(getMountDataSets())
Scott Baker537d8232014-04-08 23:40:56 -070091 return tenantSliceDetails
92
93
94def getTenantInfo(user):
95 slices =Slice.objects.all()
96 userSliceInfo = []
97 for entry in slices:
98 sliceName = Slice.objects.get(id=entry.id).name
99 slice = Slice.objects.get(name=Slice.objects.get(id=entry.id).name)
100 sliceServiceClass = entry.serviceClass.name
101 preferredImage = entry.imagePreference
102 numSliver = 0
103 sliceImage=""
104 sliceSite = {}
105 for sliver in slice.slivers.all():
106 numSliver +=sliver.numberCores
107 # sliceSite[sliver.deploymentNetwork.name] =sliceSite.get(sliver.deploymentNetwork.name,0) + 1
Scott Bakera880bb52014-04-14 23:52:48 -0700108 if sliver.node.site.name in BLESSED_SITES:
Scott Bakera880bb52014-04-14 23:52:48 -0700109 sliceSite[sliver.node.site.name] = sliceSite.get(sliver.node.site.name,0) + 1
110 sliceImage = sliver.image.name
Scott Bakerb85c26b2014-04-18 11:25:32 -0700111 numSites = len(sliceSite)
112 userSliceInfo.append({'sliceName': sliceName,'sliceServiceClass': sliceServiceClass,'preferredImage':preferredImage,'numOfSites':numSites, 'sliceSite':sliceSite,'sliceImage':sliceImage,'numOfSlivers':numSliver})
Scott Baker537d8232014-04-08 23:40:56 -0700113 return userSliceInfo
114
115def getTenantSitesInfo():
116 tenantSiteInfo=[]
117 for entry in Site.objects.all():
Scott Bakera880bb52014-04-14 23:52:48 -0700118 if entry.name in BLESSED_SITES:
119 tenantSiteInfo.append({'siteName':entry.name})
Scott Baker537d8232014-04-08 23:40:56 -0700120 return tenantSiteInfo
121
Scott Baker771819b2014-03-19 22:10:17 -0700122def userSliceTableFormatter(data):
123# pprint(data)
124 formattedData = {
125 'rows' : data
126 }
127 return formattedData
128
Scott Baker537d8232014-04-08 23:40:56 -0700129def getServiceClassInfo(user):
130 serviceClassList = ServiceClass.objects.all()
131 sliceInfo = []
132 for entry in serviceClassList:
133 sliceInfo.append({'serviceClass':entry.name})
134 return sliceInfo
135
136def getImageInfo(user):
Scott Bakerb85c26b2014-04-18 11:25:32 -0700137 #imageList = Image.objects.all()
138 imageList = ['Fedora 16 LXC rev 1.3','Hadoop','MPI']
Scott Baker537d8232014-04-08 23:40:56 -0700139 imageInfo = []
140 for imageEntry in imageList:
Scott Bakerb85c26b2014-04-18 11:25:32 -0700141 #imageInfo.append({'Image':imageEntry.name})
142 imageInfo.append({'Image':imageEntry})
Scott Baker537d8232014-04-08 23:40:56 -0700143 return imageInfo
144
Scott Bakerb85c26b2014-04-18 11:25:32 -0700145def getMountDataSets():
146 dataSetList = ['GenBank','LSST','LHC','NOAA','Measurement Lab','Common Crawl']
147 dataSetInfo = []
148 for entry in dataSetList:
149 dataSetInfo.append({'DataSet':entry})
150 return dataSetInfo
151
Scott Baker537d8232014-04-08 23:40:56 -0700152def getNetworkInfo(user):
153 #networkList = Network.objects.all()
154 networkList = ['Private Only','Private and Publicly Routable']
155 networkInfo = []
156 for networkEntry in networkList:
157 #networkInfo.append({'Network':networkEntry.name})
158 networkInfo.append({'Network':networkEntry})
159 return networkInfo
160
161def getDeploymentSites():
162 deploymentList = Deployment.objects.all()
163 deploymentInfo = []
164 for entry in deploymentList:
165 deploymentInfo.append({'DeploymentSite':entry.name})
166 return deploymentInfo
167
Scott Baker771819b2014-03-19 22:10:17 -0700168def getSliceInfo(user):
Siobhan Tully06d2c032014-02-23 23:48:52 -0500169 sliceList = Slice.objects.all()
Scott Baker771819b2014-03-19 22:10:17 -0700170 slicePrivs = SlicePrivilege.objects.filter(user=user)
Siobhan Tully06d2c032014-02-23 23:48:52 -0500171 userSliceInfo = []
172 for entry in slicePrivs:
Siobhan Tullye18b3442014-02-23 14:23:34 -0500173
Scott Baker771819b2014-03-19 22:10:17 -0700174 slicename = Slice.objects.get(id=entry.slice.id).name
Scott Baker537d8232014-04-08 23:40:56 -0700175 slice = Slice.objects.get(name=Slice.objects.get(id=entry.slice.id).name)
176 sliverList=Sliver.objects.all()
177 sites_used = {}
178 for sliver in slice.slivers.all():
179 #sites_used['deploymentSites'] = sliver.node.deployment.name
180 # sites_used[sliver.image.name] = sliver.image.name
181 sites_used[sliver.node.site.name] = sliver.numberCores
Scott Baker771819b2014-03-19 22:10:17 -0700182 sliceid = Slice.objects.get(id=entry.slice.id).id
Siobhan Tully06d2c032014-02-23 23:48:52 -0500183 try:
Scott Baker771819b2014-03-19 22:10:17 -0700184 sliverList = Sliver.objects.filter(slice=entry.slice.id)
185 siteList = {}
186 for x in sliverList:
187 if x.node.site not in siteList:
188 siteList[x.node.site] = 1
189 slivercount = len(sliverList)
190 sitecount = len(siteList)
Siobhan Tully06d2c032014-02-23 23:48:52 -0500191 except:
Scott Baker771819b2014-03-19 22:10:17 -0700192 traceback.print_exc()
193 slivercount = 0
194 sitecount = 0
Siobhan Tully06d2c032014-02-23 23:48:52 -0500195
Scott Baker771819b2014-03-19 22:10:17 -0700196 userSliceInfo.append({'slicename': slicename, 'sliceid':sliceid,
Scott Baker537d8232014-04-08 23:40:56 -0700197 'sitesUsed':sites_used,
198 'role': SliceRole.objects.get(id=entry.role.id).role,
199 'slivercount': slivercount,
200 'sitecount':sitecount})
Scott Baker771819b2014-03-19 22:10:17 -0700201
Siobhan Tully06d2c032014-02-23 23:48:52 -0500202 return userSliceInfo
203
Siobhan Tully06d2c032014-02-23 23:48:52 -0500204def getCDNOperatorData(randomizeData = False):
Scott Baker0017c102014-04-18 10:48:31 -0700205 bq = PlanetStackAnalytics()
Siobhan Tully06d2c032014-02-23 23:48:52 -0500206
Scott Baker0017c102014-04-18 10:48:31 -0700207 #hpc_sliceNames = bq.service_to_sliceNames("HPC Service")
Siobhan Tully06d2c032014-02-23 23:48:52 -0500208
Scott Baker0017c102014-04-18 10:48:31 -0700209 rows = bq.get_cached_query_results(bq.compose_latest_query())
210
211 #rows = [x for x in rows if x.get("slice","") in hpc_sliceNames]
212
213 rows = bq.postprocess_results(rows, filter={"slice": "HyperCache"}, maxi=["cpu"], count=["hostname"], computed=["bytes_sent/elapsed"], groupBy=["Time","site"])
214
215 bq.merge_datamodel_sites(rows)
216
217 new_rows = {}
218 for row in rows:
219 new_row = {"lat": float(row.get("lat", 0)),
220 "long": float(row.get("long", 0)),
221 "health": 0,
222 "numNodes": int(row.get("numNodes",0)),
223 "numHPCSlivers": int(row.get("count_hostname", 0)),
224 "siteUrl": str(row.get("url", "")),
225 "hot": float(row.get("hotness", 0.0)),
226 "bandwidth": row.get("sum_computed_bytes_sent_div_elapsed",0),
227 "load": int(float(row.get("max_avg_cpu", row.get("max_cpu",0))))}
228 new_rows[str(row["site"])] = new_row
229
230 return new_rows
Siobhan Tully06d2c032014-02-23 23:48:52 -0500231
Scott Baker771819b2014-03-19 22:10:17 -0700232class SimulatorView(View):
233 def get(self, request, **kwargs):
234 sim = json.loads(file("/tmp/simulator.json","r").read())
235 text = "<html><head></head><body>"
236 text += "Iteration: %d<br>" % sim["iteration"]
237 text += "Elapsed since report %d<br><br>" % sim["elapsed_since_report"]
238 text += "<table border=1>"
239 text += "<tr><th>site</th><th>trend</th><th>weight</th><th>bytes_sent</th><th>hot</th></tr>"
240 for site in sim["site_load"].values():
241 text += "<tr>"
242 text += "<td>%s</td><td>%0.2f</td><td>%0.2f</td><td>%d</td><td>%0.2f</td>" % \
243 (site["name"], site["trend"], site["weight"], site["bytes_sent"], site["load_frac"])
244 text += "</tr>"
245 text += "</table>"
246 text += "</body></html>"
247 return HttpResponse(text)
248
249class DashboardUserSiteView(View):
250 def get(self, request, **kwargs):
251 return HttpResponse(json.dumps(getUserSliceInfo(request.user, True)), mimetype='application/javascript')
252
Scott Baker537d8232014-04-08 23:40:56 -0700253class TenantViewData(View):
254 def get(self, request, **kwargs):
255 return HttpResponse(json.dumps(getTenantSliceInfo(request.user, True)), mimetype='application/javascript')
256
Scott Bakerbd57a432014-04-14 16:12:15 -0700257def siteSortKey(site, slice=None, count=None, lat=None, lon=None):
258 # try to pick a site we're already using
259 has_slivers_here=False
260 if slice:
261 for sliver in slice.slivers.all():
262 if sliver.node.site.name == site.name:
263 has_slivers_here=True
264
265 # Haversine method
266 d = 0
267 site_lat = site.location.latitude
268 site_lon = site.location.longitude
269 if lat and lon and site_lat and site_lon:
270 site_lat = float(site_lat)
271 site_lon = float(site_lon)
272 R = 6378.1
273 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)
274 c = 2 * math.atan2( math.sqrt(a), math.sqrt(1 - a) )
275 d = R * c
276
277 return (-has_slivers_here, d)
278
279def tenant_pick_sites(user, user_ip=None, slice=None, count=None):
280 """ Returns list of sites, sorted from most favorable to least favorable """
281 lat=None
282 lon=None
283 try:
284 client_geo = GeoIP().city(user_ip)
285 if client_geo:
286 lat=float(client_geo["latitude"])
287 lon=float(client_geo["longitude"])
288 except:
289 print "exception in geo code"
290 traceback.print_exc()
291
292 sites = Site.objects.all()
Scott Bakera880bb52014-04-14 23:52:48 -0700293 sites = [x for x in sites if x.name in BLESSED_SITES]
Scott Bakerbd57a432014-04-14 16:12:15 -0700294 sites = sorted(sites, key=functools.partial(siteSortKey, slice=slice, count=count, lat=lat, lon=lon))
295
296 return sites
297
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700298def slice_increase_slivers(user, user_ip, siteList, slice, count, noAct=False):
Scott Bakere4deddd2014-04-14 16:48:05 -0700299 sitesChanged = {}
300
Scott Bakerbd57a432014-04-14 16:12:15 -0700301 # let's compute how many slivers are in use in each node of each site
302 for site in siteList:
303 site.nodeList = list(site.nodes.all())
304 for node in site.nodeList:
305 node.sliverCount = 0
Scott Baker6253dfc2014-04-14 09:29:30 -0700306 for sliver in node.slivers.all():
Scott Baker21511942014-04-15 17:44:27 -0700307 if sliver.slice.id == slice.id:
Scott Baker0017c102014-04-18 10:48:31 -0700308 node.sliverCount = node.sliverCount + 1
Scott Baker6253dfc2014-04-14 09:29:30 -0700309
Scott Bakerbd57a432014-04-14 16:12:15 -0700310 # Allocate slivers to nodes
311 # for now, assume we want to allocate all slivers from the same site
312 nodes = siteList[0].nodeList
313 while (count>0):
314 # Sort the node list by number of slivers per node, then pick the
315 # node with the least number of slivers.
316 nodes = sorted(nodes, key=attrgetter("sliverCount"))
317 node = nodes[0]
Scott Baker6253dfc2014-04-14 09:29:30 -0700318
Scott Bakerbd57a432014-04-14 16:12:15 -0700319 print "adding sliver at node", node.name, "of site", node.site.name
Scott Baker6253dfc2014-04-14 09:29:30 -0700320
Scott Bakere4deddd2014-04-14 16:48:05 -0700321 if not noAct:
322 sliver = Sliver(name=node.name,
323 slice=slice,
324 node=node,
325 image = Image.objects.all()[0],
326 creator = User.objects.get(email=user),
327 deploymentNetwork=node.deployment,
328 numberCores =1 )
329 sliver.save()
Scott Bakerbd57a432014-04-14 16:12:15 -0700330
331 node.sliverCount = node.sliverCount + 1
332
333 count = count - 1
334
Scott Bakere4deddd2014-04-14 16:48:05 -0700335 sitesChanged[node.site.name] = sitesChanged.get(node.site.name,0) + 1
336
337 return sitesChanged
338
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700339def slice_decrease_slivers(user, siteList, slice, count, noAct=False):
Scott Bakere4deddd2014-04-14 16:48:05 -0700340 sitesChanged = {}
Scott Bakerb85c26b2014-04-18 11:25:32 -0700341 sliverList ={}
Scott Bakerbd57a432014-04-14 16:12:15 -0700342 if siteList:
343 siteNames = [site.name for site in siteList]
344 else:
345 siteNames = None
346
347 for sliver in slice.slivers.all():
Scott Bakerb85c26b2014-04-18 11:25:32 -0700348 if(not siteNames) or (sliver.node.site.name in siteNames):
349 node = sliver.node
350 sliverList[sliver.name]=node.name
Scott Bakerbd57a432014-04-14 16:12:15 -0700351
Scott Bakerb85c26b2014-04-18 11:25:32 -0700352 for key in sliverList:
353 if count>0:
354 sliver = Sliver.objects.filter(name=key)
355 sliver.delete()
356 print "deleting sliver",sliverList[key],"at node",sliver.node.name
357 count=count-1
358 sitesChanged[node.site.name] = sitesChanged.get(node.site.name,0) - 1
359
Scott Bakere4deddd2014-04-14 16:48:05 -0700360 return sitesChanged
361
Scott Bakerb85c26b2014-04-18 11:25:32 -0700362class TenantDeleteSliceView(View):
363 def post(self,request):
364 sliceName = request.POST.get("sliceName",None)
365 slice = Slice.objects.get(name=sliceName)
366 print slice, slice.id
367 sliceToDel=Slice(name=sliceName, id=slice.id)
368 sliceToDel.delete()
369
Scott Baker6253dfc2014-04-14 09:29:30 -0700370class TenantAddOrRemoveSliverView(View):
Scott Bakere4deddd2014-04-14 16:48:05 -0700371 """ Add or remove slivers from a Slice
372
373 Arguments:
374 siteName - name of site. If not specified, PlanetStack will pick the
375 best site.,
376 actionToDo - [add | rem]
377 count - number of slivers to add or remove
378 sliceName - name of slice
Scott Bakera880bb52014-04-14 23:52:48 -0700379 noAct - if set, no changes will be made to db, but result will still
380 show which sites would have been modified.
Scott Bakere4deddd2014-04-14 16:48:05 -0700381
382 Returns:
383 Dictionary of sites that were modified, and the count of nodes
384 that were added or removed at each site.
385 """
Scott Baker6253dfc2014-04-14 09:29:30 -0700386 def post(self, request, *args, **kwargs):
Scott Bakerbd57a432014-04-14 16:12:15 -0700387 siteName = request.POST.get("siteName", None)
388 actionToDo = request.POST.get("actionToDo", None)
389 count = int(request.POST.get("count","0"))
390 sliceName = request.POST.get("slice", None)
Scott Bakere4deddd2014-04-14 16:48:05 -0700391 noAct = request.POST.get("noAct", False)
Scott Bakerbd57a432014-04-14 16:12:15 -0700392
393 if not sliceName:
394 return HttpResponseServerError("No slice name given")
395
396 slice = Slice.objects.get(name=sliceName)
397
398 if siteName:
399 siteList = [Site.objects.get(name=siteName)]
400 else:
401 siteList = None
Scott Baker6253dfc2014-04-14 09:29:30 -0700402
403 if (actionToDo == "add"):
Scott Bakerbd57a432014-04-14 16:12:15 -0700404 user_ip = request.GET.get("ip", get_ip(request))
405 if (siteList is None):
406 siteList = tenant_pick_sites(user, user_ip, slice, count)
407
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700408 sitesChanged = slice_increase_slivers(request.user, user_ip, siteList, slice, count, noAct)
Scott Baker6253dfc2014-04-14 09:29:30 -0700409 elif (actionToDo == "rem"):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700410 sitesChanged = slice_decrease_slivers(request.user, siteList, slice, count, noAct)
Scott Bakerbd57a432014-04-14 16:12:15 -0700411 else:
412 return HttpResponseServerError("Unknown actionToDo %s" % actionToDo)
413
Scott Bakere4deddd2014-04-14 16:48:05 -0700414 return HttpResponse(json.dumps(sitesChanged), mimetype='application/javascript')
Scott Bakerbd57a432014-04-14 16:12:15 -0700415
416 def get(self, request, *args, **kwargs):
417 request.POST = request.GET
418 return self.post(request, *args, **kwargs) # for testing REST in browser
419 #return HttpResponseServerError("GET is not supported")
420
421class TenantPickSitesView(View):
422 """ primarily just for testing purposes """
423 def get(self, request, *args, **kwargs):
424 count = request.GET.get("count","0")
425 slice = request.GET.get("slice",None)
426 if slice:
427 slice = Slice.objects.get(name=slice)
428 ip = request.GET.get("ip", get_ip(request))
429 sites = tenant_pick_sites(request.user, user_ip=ip, count=0, slice=slice)
430 sites = [x.name for x in sites]
431 return HttpResponse(json.dumps(sites), mimetype='application/javascript')
Scott Baker6253dfc2014-04-14 09:29:30 -0700432
Scott Baker771819b2014-03-19 22:10:17 -0700433class DashboardSummaryAjaxView(View):
434 def get(self, request, **kwargs):
Scott Baker0017c102014-04-18 10:48:31 -0700435 def avg(x):
436 return float(sum(x))/len(x)
437
438 sites = getCDNOperatorData().values()
439
440 sites = [site for site in sites if site["numHPCSlivers"]>0]
441
442 total_slivers = sum( [site["numHPCSlivers"] for site in sites] )
443 total_bandwidth = sum( [site["bandwidth"] for site in sites] )
444 average_cpu = int(avg( [site["load"] for site in sites] ))
445
446 result= {"total_slivers": total_slivers,
447 "total_bandwidth": total_bandwidth,
448 "average_cpu": average_cpu}
449
450 return HttpResponse(json.dumps(result), mimetype='application/javascript')
Scott Baker771819b2014-03-19 22:10:17 -0700451
452class DashboardAddOrRemoveSliverView(View):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700453 # TODO: deprecate this view in favor of using TenantAddOrRemoveSliverView
454
Scott Baker771819b2014-03-19 22:10:17 -0700455 def post(self, request, *args, **kwargs):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700456 siteName = request.POST.get("site", None)
Scott Baker771819b2014-03-19 22:10:17 -0700457 actionToDo = request.POST.get("actionToDo", "0")
458
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700459 siteList = [Site.objects.get(name=siteName)]
460 slice = Slice.objects.get(name="HyperCache")
461
Scott Baker771819b2014-03-19 22:10:17 -0700462 if (actionToDo == "add"):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700463 user_ip = request.GET.get("ip", get_ip(request))
464 slice_increase_slivers(request.user, user_ip, siteList, slice, 1)
Scott Baker771819b2014-03-19 22:10:17 -0700465 elif (actionToDo == "rem"):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700466 slice_decrease_slivers(request.user, siteList, slice, 1)
Scott Baker771819b2014-03-19 22:10:17 -0700467
468 print '*' * 50
469 print 'Ask for site: ' + siteName + ' to ' + actionToDo + ' another HPC Sliver'
470 return HttpResponse('This is POST request ')
Siobhan Tully06d2c032014-02-23 23:48:52 -0500471
472class DashboardAjaxView(View):
473 def get(self, request, **kwargs):
474 return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript')
Scott Baker771819b2014-03-19 22:10:17 -0700475
476class DashboardAnalyticsAjaxView(View):
477 def get(self, request, name="hello_world", **kwargs):
478 if (name == "hpcSummary"):
479 return HttpResponse(json.dumps(hpc_wizard.get_hpc_wizard().get_summary_for_view()), mimetype='application/javascript')
480 elif (name == "hpcUserSite"):
481 return HttpResponse(json.dumps(getUserSliceInfo(request.user, True)), mimetype='application/javascript')
482 elif (name == "hpcMap"):
483 return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript')
484 elif (name == "bigquery"):
485 (mimetype, data) = DoPlanetStackAnalytics(request)
486 return HttpResponse(data, mimetype=mimetype)
487 else:
488 return HttpResponse(json.dumps("Unknown"), mimetype='application/javascript')
489