blob: 886db208041cb6217efc65956633ca61dd1246fe [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
Scott Baker60fd8cd2014-05-21 18:02:44 -070016from django.http import HttpResponse, HttpResponseServerError, HttpResponseForbidden
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
Scott Baker6b654202014-05-27 16:55:00 -070019from django.db.models import Q
Scott Bakerbd57a432014-04-14 16:12:15 -070020from ipware.ip import get_ip
Scott Baker2c3cb642014-05-19 17:55:56 -070021from operator import itemgetter, attrgetter
Scott Baker771819b2014-03-19 22:10:17 -070022import traceback
Scott Baker6253dfc2014-04-14 09:29:30 -070023import socket
Siobhan Tully06d2c032014-02-23 23:48:52 -050024
Scott Bakera880bb52014-04-14 23:52:48 -070025BLESSED_SITES = ["Stanford", "Washington", "Princeton", "GeorgiaTech", "MaxPlanck"]
26
Scott Baker771819b2014-03-19 22:10:17 -070027if os.path.exists("/home/smbaker/projects/vicci/cdn/bigquery"):
28 sys.path.append("/home/smbaker/projects/vicci/cdn/bigquery")
29else:
30 sys.path.append("/opt/planetstack/hpc_wizard")
31import hpc_wizard
Scott Baker75095b62014-04-21 17:32:09 -070032from planetstack_analytics import DoPlanetStackAnalytics, PlanetStackAnalytics, RED_LOAD, BLUE_LOAD
Siobhan Tullycf04fb62014-01-11 11:25:57 -050033
34class DashboardWelcomeView(TemplateView):
35 template_name = 'admin/dashboard/welcome.html'
36
37 def get(self, request, *args, **kwargs):
38 context = self.get_context_data(**kwargs)
Scott Baker2c3cb642014-05-19 17:55:56 -070039 context = getDashboardContext(request.user, context)
Siobhan Tullycf04fb62014-01-11 11:25:57 -050040 return self.render_to_response(context=context)
Siobhan Tullye18b3442014-02-23 14:23:34 -050041
Scott Baker2c3cb642014-05-19 17:55:56 -070042class DashboardDynamicView(TemplateView):
Scott Baker186ae612014-05-19 10:42:51 -070043 head_template = r"""{% extends "admin/dashboard/dashboard_base.html" %}
44 {% load admin_static %}
45 {% block content %}
46 """
47
48 tail_template = r"{% endblock %}"
49
Scott Baker2c3cb642014-05-19 17:55:56 -070050 def get(self, request, name="root", *args, **kwargs):
Scott Baker186ae612014-05-19 10:42:51 -070051 context = self.get_context_data(**kwargs)
Scott Baker2c3cb642014-05-19 17:55:56 -070052 context = getDashboardContext(request.user, context)
Scott Baker186ae612014-05-19 10:42:51 -070053
Scott Baker2c3cb642014-05-19 17:55:56 -070054 if name=="root":
55 return self.multiDashboardView(request, context)
56 else:
57 return self.singleDashboardView(request, name, context)
58
59 def readDashboard(self, fn):
60 try:
61 template= open("/opt/planetstack/templates/admin/dashboard/%s.html" % fn, "r").read()
62 if (fn=="tenant"):
Scott Baker7237cdc2014-05-21 16:55:18 -070063 # fix for tenant view - it writes html to a div called tabs-5
Scott Baker2c3cb642014-05-19 17:55:56 -070064 template = '<div id="tabs-5"></div>' + template
65 return template
66 except:
67 return "failed to open %s" % fn
68
69 def multiDashboardView(self, request, context):
Scott Bakerfc06e782014-05-19 10:52:10 -070070 head_template = self.head_template
71 tail_template = self.tail_template
72
Scott Baker2c3cb642014-05-19 17:55:56 -070073 body = """
74 <div id="hometabs" >
75 <ul id="suit_form_tabs" class="nav nav-tabs nav-tabs-suit" data-tab-prefix="suit-tab">
76 """
Scott Bakerfc06e782014-05-19 10:52:10 -070077
Scott Baker2c3cb642014-05-19 17:55:56 -070078 dashboards = request.user.get_dashboards()
Scott Bakerfc06e782014-05-19 10:52:10 -070079
Scott Baker2c3cb642014-05-19 17:55:56 -070080 # customize is a special dashboard they always get
81 customize = DashboardView.objects.filter(name="Customize")
82 if customize:
83 dashboards.append(customize[0])
Scott Baker186ae612014-05-19 10:42:51 -070084
Scott Baker2c3cb642014-05-19 17:55:56 -070085 for i,view in enumerate(dashboards):
86 body = body + '<li><a href="#dashtab-%d">%s</a></li>\n' % (i, view.name)
Scott Baker186ae612014-05-19 10:42:51 -070087
Scott Baker2c3cb642014-05-19 17:55:56 -070088 body = body + "</ul>\n"
89
90 for i,view in enumerate(dashboards):
91 url = view.url
92 body = body + '<div id="dashtab-%d">\n' % i
93 if url.startswith("template:"):
94 fn = url[9:]
95 body = body + self.readDashboard(fn)
96 body = body + '</div>\n'
97
98 body=body+"</div>\n"
99
100 t = template.Template(head_template + body + self.tail_template)
Scott Baker186ae612014-05-19 10:42:51 -0700101
102 response_kwargs = {}
103 response_kwargs.setdefault('content_type', self.content_type)
104 return self.response_class(
105 request = request,
106 template = t,
107 context = context,
108 **response_kwargs)
109
Scott Baker2c3cb642014-05-19 17:55:56 -0700110 def singleDashboardView(self, request, name, context):
111 head_template = self.head_template
112 tail_template = self.tail_template
113
Scott Baker6b654202014-05-27 16:55:00 -0700114 t = template.Template(head_template + self.readDashboard(name) + self.tail_template)
Scott Baker2c3cb642014-05-19 17:55:56 -0700115
116 response_kwargs = {}
117 response_kwargs.setdefault('content_type', self.content_type)
118 return self.response_class(
119 request = request,
120 template = t,
121 context = context,
122 **response_kwargs)
123
124def getDashboardContext(user, context={}, tableFormat = False):
125 context = {}
Scott Baker771819b2014-03-19 22:10:17 -0700126
127 userSliceData = getSliceInfo(user)
128 if (tableFormat):
Scott Baker2c3cb642014-05-19 17:55:56 -0700129 context['userSliceInfo'] = userSliceTableFormatter(userSliceData)
Scott Baker771819b2014-03-19 22:10:17 -0700130 else:
Scott Baker2c3cb642014-05-19 17:55:56 -0700131 context['userSliceInfo'] = userSliceData
132 context['cdnData'] = getCDNOperatorData(wait=False)
133 context['cdnContentProviders'] = getCDNContentProviderData()
134
135 (dashboards, unusedDashboards)= getDashboards(user)
136 unusedDashboards=[x for x in unusedDashboards if x!="Customize"]
137 context['dashboards'] = dashboards
138 context['unusedDashboards'] = unusedDashboards
139
140 return context
141
142def getDashboards(user):
143 #dashboards = sorted(list(user.dashboardViews.all()), key=attrgetter('order'))
144 dashboards = user.get_dashboards()
145
146 dashboard_names = [d.name for d in dashboards]
147
148 unused_dashboard_names = []
149 for dashboardView in DashboardView.objects.all():
150 if not dashboardView.name in dashboard_names:
151 unused_dashboard_names.append(dashboardView.name)
152
153 return (dashboard_names, unused_dashboard_names)
Scott Baker771819b2014-03-19 22:10:17 -0700154
Scott Baker537d8232014-04-08 23:40:56 -0700155class TenantCreateSlice(View):
156 def post(self, request, *args, **kwargs):
Scott Baker94319c52014-05-26 11:45:29 -0700157 if request.user.isReadOnlyUser():
158 return HttpResponseForbidden("User is in read-only mode")
159
Scott Baker537d8232014-04-08 23:40:56 -0700160 sliceName = request.POST.get("sliceName", "0")
161 serviceClass = request.POST.get("serviceClass", "0")
162 imageName = request.POST.get("imageName", "0")
163 actionToDo = request.POST.get("actionToDo", "0")
Scott Baker9d9650d2014-05-23 11:50:54 -0700164 networkPorts = request.POST.get("network","0")
Scott Bakeredf2f672014-04-22 10:17:37 -0700165 mountDataSets = request.POST.get("mountDataSets","0")
Scott Baker9d9650d2014-05-23 11:50:54 -0700166 privateVolume = request.POST.get("privateVolume","0")
Scott Baker537d8232014-04-08 23:40:56 -0700167 if (actionToDo == "add"):
168 serviceClass = ServiceClass.objects.get(name=serviceClass)
169 site = request.user.site
Scott Baker4b0cedd2014-04-22 21:01:21 -0700170 image = Image.objects.get(name=imageName)
Scott Baker168b7412014-05-16 14:25:41 -0700171 newSlice = Slice(name=sliceName,serviceClass=serviceClass,site=site,imagePreference=image,mountDataSets=mountDataSets)
Scott Baker537d8232014-04-08 23:40:56 -0700172 newSlice.save()
Scott Baker9d9650d2014-05-23 11:50:54 -0700173 privateTemplate="Private"
174 publicTemplate="Public shared IPv4"
175 privateNetworkName = sliceName+"-"+privateTemplate
176 publicNetworkName = sliceName+"-"+publicTemplate
177 slice=Slice.objects.get(name=sliceName)
178 addNetwork(privateNetworkName,privateTemplate,slice)
179 addNetwork(publicNetworkName,publicTemplate,slice)
180 addOrModifyPorts(networkPorts,sliceName)
181 if privateVolume=="true":
182 privateVolForSlice(request.user,sliceName)
Scott Baker4b0cedd2014-04-22 21:01:21 -0700183 return HttpResponse("Slice created")
Scott Baker537d8232014-04-08 23:40:56 -0700184
Scott Baker9d9650d2014-05-23 11:50:54 -0700185def privateVolForSlice(user,sliceName):
186 if not hasPrivateVolume(sliceName):
187 volumeName=createPrivateVolume(user,sliceName)
188 readWrite="true"
189 mountVolume(sliceName,volumeName,readWrite)
190
Scott Bakeredf2f672014-04-22 10:17:37 -0700191class TenantUpdateSlice(View):
192 def post(self, request, *args, **kwargs):
Scott Baker94319c52014-05-26 11:45:29 -0700193 if request.user.isReadOnlyUser():
194 return HttpResponseForbidden("User is in read-only mode")
195
Scott Bakeredf2f672014-04-22 10:17:37 -0700196 sliceName = request.POST.get("sliceName", "0")
197 serviceClass = request.POST.get("serviceClass", "0")
198 imageName = request.POST.get("imageName", "0")
199 actionToDo = request.POST.get("actionToDo", "0")
Scott Baker9d9650d2014-05-23 11:50:54 -0700200 networkPorts = request.POST.get("networkPorts","0")
Scott Baker4b0cedd2014-04-22 21:01:21 -0700201 dataSet = request.POST.get("dataSet","0")
Scott Baker9d9650d2014-05-23 11:50:54 -0700202 privateVolume = request.POST.get("privateVolume","0")
Scott Baker4b0cedd2014-04-22 21:01:21 -0700203 slice = Slice.objects.all()
204 for entry in slice:
205 serviceClass = ServiceClass.objects.get(name=serviceClass)
206 if(entry.name==sliceName):
207 if (actionToDo == "update"):
208 setattr(entry,'serviceClass',serviceClass)
209 setattr(entry,'imagePreference',imageName)
Scott Baker4b0cedd2014-04-22 21:01:21 -0700210 setattr(entry,'mountDataSets',dataSet)
211 entry.save()
212 break
Scott Baker9d9650d2014-05-23 11:50:54 -0700213 addOrModifyPorts(networkPorts,sliceName)
214 if privateVolume=="true":
215 privateVolForSlice(request.user,sliceName)
Scott Baker4b0cedd2014-04-22 21:01:21 -0700216 return HttpResponse("Slice updated")
217
Scott Baker9d9650d2014-05-23 11:50:54 -0700218def addNetwork(name,template,sliceName):
219 networkTemplate=NetworkTemplate.objects.get(name=template)
220 newNetwork = Network(name = name,
221 template = networkTemplate,
222 owner = sliceName)
223 newNetwork.save()
224 addNetworkSlice(newNetwork,sliceName)
225
226def addNetworkSlice(networkSlice,sliceName):
227 newNetworkSlice=NetworkSlice(network =networkSlice,
228 slice=sliceName)
229 newNetworkSlice.save()
230
231def addOrModifyPorts(networkPorts,sliceName):
232 networkList = Network.objects.all()
233 networkInfo = []
234 if networkPorts:
235 for networkEntry in networkList:
236 networkSlices = networkEntry.slices.all()
237 for slice in networkSlices:
238 if slice.name==sliceName:
239 if networkEntry.template.name=="Public shared IPv4":
240 setattr(networkEntry,'ports',networkPorts)
241 networkEntry.save()
242
Scott Baker537d8232014-04-08 23:40:56 -0700243def getTenantSliceInfo(user, tableFormat = False):
244 tenantSliceDetails = {}
245 tenantSliceData = getTenantInfo(user)
246 tenantServiceClassData = getServiceClassInfo(user)
247 if (tableFormat):
248 tenantSliceDetails['userSliceInfo'] = userSliceTableFormatter(tenantSliceData)
249 tenantSliceDetails['sliceServiceClass']=userSliceTableFormatter(tenantServiceClassData)
250 else:
251 tenantSliceDetails['userSliceInfo'] = tenantSliceData
252 tenantSliceDetails['sliceServiceClass']=userSliceTableFormatter(tenantServiceClassData)
253 tenantSliceDetails['image']=userSliceTableFormatter(getImageInfo(user))
Scott Baker537d8232014-04-08 23:40:56 -0700254 tenantSliceDetails['deploymentSites']=userSliceTableFormatter(getDeploymentSites())
Scott Bakerb85c26b2014-04-18 11:25:32 -0700255 tenantSliceDetails['sites'] = userSliceTableFormatter(getTenantSitesInfo())
256 tenantSliceDetails['mountDataSets'] = userSliceTableFormatter(getMountDataSets())
Scott Baker168b7412014-05-16 14:25:41 -0700257 tenantSliceDetails['publicKey'] = getPublicKey(user)
Scott Baker537d8232014-04-08 23:40:56 -0700258 return tenantSliceDetails
259
Scott Baker537d8232014-04-08 23:40:56 -0700260def getTenantInfo(user):
261 slices =Slice.objects.all()
262 userSliceInfo = []
263 for entry in slices:
264 sliceName = Slice.objects.get(id=entry.id).name
265 slice = Slice.objects.get(name=Slice.objects.get(id=entry.id).name)
266 sliceServiceClass = entry.serviceClass.name
267 preferredImage = entry.imagePreference
Scott Baker9d9650d2014-05-23 11:50:54 -0700268 #sliceDataSet = entry.mountDataSets
269 sliceNetwork = {}
Scott Baker537d8232014-04-08 23:40:56 -0700270 numSliver = 0
271 sliceImage=""
272 sliceSite = {}
Scott Bakeredf2f672014-04-22 10:17:37 -0700273 sliceNode = {}
274 sliceInstance= {}
Scott Baker9d9650d2014-05-23 11:50:54 -0700275 #createPrivateVolume(user,sliceName)
Scott Baker537d8232014-04-08 23:40:56 -0700276 for sliver in slice.slivers.all():
Scott Bakera880bb52014-04-14 23:52:48 -0700277 if sliver.node.site.name in BLESSED_SITES:
Scott Bakera880bb52014-04-14 23:52:48 -0700278 sliceSite[sliver.node.site.name] = sliceSite.get(sliver.node.site.name,0) + 1
279 sliceImage = sliver.image.name
Scott Bakerb37ce332014-04-22 22:57:40 -0700280 sliceNode[str(sliver)] = sliver.node.name
Scott Bakeredf2f672014-04-22 10:17:37 -0700281 numSliver = sum(sliceSite.values())
Scott Bakerb85c26b2014-04-18 11:25:32 -0700282 numSites = len(sliceSite)
Scott Baker9d9650d2014-05-23 11:50:54 -0700283 userSliceInfo.append({'sliceName': sliceName,'sliceServiceClass': sliceServiceClass,'preferredImage':preferredImage,'numOfSites':numSites, 'sliceSite':sliceSite,'sliceImage':sliceImage,'numOfSlivers':numSliver,'instanceNodePair':sliceNode})
Scott Baker537d8232014-04-08 23:40:56 -0700284 return userSliceInfo
285
286def getTenantSitesInfo():
287 tenantSiteInfo=[]
288 for entry in Site.objects.all():
Scott Bakera880bb52014-04-14 23:52:48 -0700289 if entry.name in BLESSED_SITES:
290 tenantSiteInfo.append({'siteName':entry.name})
Scott Baker537d8232014-04-08 23:40:56 -0700291 return tenantSiteInfo
292
Scott Baker771819b2014-03-19 22:10:17 -0700293def userSliceTableFormatter(data):
294# pprint(data)
295 formattedData = {
296 'rows' : data
297 }
298 return formattedData
299
Scott Baker168b7412014-05-16 14:25:41 -0700300def getPublicKey(user):
301 users=User.objects.all()
302 for key in users:
303 if (str(key.email)==str(user)):
304 sshKey = key.public_key
305 return sshKey
306
Scott Baker537d8232014-04-08 23:40:56 -0700307def getServiceClassInfo(user):
308 serviceClassList = ServiceClass.objects.all()
309 sliceInfo = []
310 for entry in serviceClassList:
311 sliceInfo.append({'serviceClass':entry.name})
312 return sliceInfo
313
314def getImageInfo(user):
Scott Baker4b0cedd2014-04-22 21:01:21 -0700315 imageList = Image.objects.all()
316 #imageList = ['Fedora 16 LXC rev 1.3','Hadoop','MPI']
Scott Baker537d8232014-04-08 23:40:56 -0700317 imageInfo = []
318 for imageEntry in imageList:
Scott Baker4b0cedd2014-04-22 21:01:21 -0700319 imageInfo.append({'Image':imageEntry.name})
320 #imageInfo.append({'Image':imageEntry})
Scott Baker537d8232014-04-08 23:40:56 -0700321 return imageInfo
322
Scott Baker168b7412014-05-16 14:25:41 -0700323def createPrivateVolume(user, sliceName):
324 caps = Volume.CAP_READ_DATA | Volume.CAP_WRITE_DATA | Volume.CAP_HOST_DATA
325 getattr(Volume.default_gateway_caps,"read data") | \
326 getattr(Volume.default_gateway_caps,"write data") | \
327 getattr(Volume.default_gateway_caps,"host files")
328 v = Volume(name="private_" + sliceName, owner_id=user, description="private volume for %s" % sliceName, blocksize=61440, private=True, archive=False, default_gateway_caps = caps)
329 v.save()
Scott Baker9d9650d2014-05-23 11:50:54 -0700330 return v
Scott Baker168b7412014-05-16 14:25:41 -0700331
332SYNDICATE_REPLICATE_PORTNUM = 1025
333
334def get_free_port():
335 inuse={}
336 inuse[SYNDICATE_REPLICATE_PORTNUM] = True
337 for vs in VolumeSlice.objects.all():
338 inuse[vs.peer_portnum]=True
339 inuse[vs.replicate_portnum]=True
340 for network in Network.objects.all():
Scott Baker6b654202014-05-27 16:55:00 -0700341 if not network.ports:
Scott Baker9d9650d2014-05-23 11:50:54 -0700342 continue
Scott Baker168b7412014-05-16 14:25:41 -0700343 network_ports = [x.strip() for x in network.ports.split(",")]
344 for network_port in network_ports:
345 try:
346 inuse[int(network_port)] = True
347 except:
348 # in case someone has put a malformed port number in the list
349 pass
350 for i in range(1025, 65535):
351 if not inuse.get(i,False):
352 return i
353 return False
354
355def mountVolume(sliceName, volumeName, readWrite):
356 slice = Slice.objects.get(name=sliceName)
357 volume = Volume.objects.get(name=volumeName)
358 # choose some unused port numbers
359 flags = Volume.CAP_READ_DATA
360 if readWrite:
361 flags = flags | Volume.CAP_WRITE_DATA
362 vs = VolumeSlice(volume_id = volume, slice_id = slice, gateway_caps=flags, peer_portnum = get_free_port(), replicate_portnum = SYNDICATE_REPLICATE_PORTNUM)
363 vs.save()
364
365def hasPrivateVolume(sliceName):
366 slice = Slice.objects.get(name=sliceName)
367 for vs in VolumeSlice.objects.filter(slice_id=slice):
368 if vs.volume_id.private:
369 return True
370 return False
371
Scott Bakerb85c26b2014-04-18 11:25:32 -0700372def getMountDataSets():
Scott Baker168b7412014-05-16 14:25:41 -0700373 dataSetInfo=[]
374 for volume in Volume.objects.all():
375 if not volume.private:
376 dataSetInfo.append({'DataSet': volume.name})
377
Scott Bakerb85c26b2014-04-18 11:25:32 -0700378 return dataSetInfo
379
Scott Baker537d8232014-04-08 23:40:56 -0700380def getDeploymentSites():
381 deploymentList = Deployment.objects.all()
382 deploymentInfo = []
383 for entry in deploymentList:
384 deploymentInfo.append({'DeploymentSite':entry.name})
385 return deploymentInfo
386
Scott Baker771819b2014-03-19 22:10:17 -0700387def getSliceInfo(user):
Siobhan Tully06d2c032014-02-23 23:48:52 -0500388 sliceList = Slice.objects.all()
Scott Baker771819b2014-03-19 22:10:17 -0700389 slicePrivs = SlicePrivilege.objects.filter(user=user)
Siobhan Tully06d2c032014-02-23 23:48:52 -0500390 userSliceInfo = []
391 for entry in slicePrivs:
Siobhan Tullye18b3442014-02-23 14:23:34 -0500392
Scott Baker771819b2014-03-19 22:10:17 -0700393 slicename = Slice.objects.get(id=entry.slice.id).name
Scott Baker537d8232014-04-08 23:40:56 -0700394 slice = Slice.objects.get(name=Slice.objects.get(id=entry.slice.id).name)
395 sliverList=Sliver.objects.all()
396 sites_used = {}
397 for sliver in slice.slivers.all():
398 #sites_used['deploymentSites'] = sliver.node.deployment.name
399 # sites_used[sliver.image.name] = sliver.image.name
400 sites_used[sliver.node.site.name] = sliver.numberCores
Scott Baker771819b2014-03-19 22:10:17 -0700401 sliceid = Slice.objects.get(id=entry.slice.id).id
Siobhan Tully06d2c032014-02-23 23:48:52 -0500402 try:
Scott Baker771819b2014-03-19 22:10:17 -0700403 sliverList = Sliver.objects.filter(slice=entry.slice.id)
404 siteList = {}
405 for x in sliverList:
406 if x.node.site not in siteList:
407 siteList[x.node.site] = 1
408 slivercount = len(sliverList)
409 sitecount = len(siteList)
Siobhan Tully06d2c032014-02-23 23:48:52 -0500410 except:
Scott Baker771819b2014-03-19 22:10:17 -0700411 traceback.print_exc()
412 slivercount = 0
413 sitecount = 0
Siobhan Tully06d2c032014-02-23 23:48:52 -0500414
Scott Baker771819b2014-03-19 22:10:17 -0700415 userSliceInfo.append({'slicename': slicename, 'sliceid':sliceid,
Scott Baker537d8232014-04-08 23:40:56 -0700416 'sitesUsed':sites_used,
417 'role': SliceRole.objects.get(id=entry.role.id).role,
418 'slivercount': slivercount,
419 'sitecount':sitecount})
Scott Baker771819b2014-03-19 22:10:17 -0700420
Siobhan Tully06d2c032014-02-23 23:48:52 -0500421 return userSliceInfo
422
Scott Bakere9f4ec92014-04-29 11:29:45 -0700423def getCDNContentProviderData():
424 cps = []
425 for dm_cp in ContentProvider.objects.all():
426 cp = {"name": dm_cp.name,
427 "account": dm_cp.account}
428 cps.append(cp)
429
430 return cps
431
Scott Baker75095b62014-04-21 17:32:09 -0700432def getCDNOperatorData(randomizeData = False, wait=True):
433 HPC_SLICE_NAME = "HyperCache"
434
Scott Baker0017c102014-04-18 10:48:31 -0700435 bq = PlanetStackAnalytics()
Siobhan Tully06d2c032014-02-23 23:48:52 -0500436
Scott Baker4a4472d2014-05-13 17:07:46 -0700437 rows = bq.get_cached_query_results(bq.compose_cached_query(), wait)
Siobhan Tully06d2c032014-02-23 23:48:52 -0500438
Scott Bakeredf2f672014-04-22 10:17:37 -0700439 # wait=False on the first time the Dashboard is opened. This means we might
440 # not have any rows yet. The dashboard code polls every 30 seconds, so it
441 # will eventually pick them up.
Scott Baker0017c102014-04-18 10:48:31 -0700442
Scott Baker75095b62014-04-21 17:32:09 -0700443 if rows:
Scott Baker696e2662014-04-24 17:03:29 -0700444 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 -0700445
Scott Baker75095b62014-04-21 17:32:09 -0700446 # dictionaryize the statistics rows by site name
447 stats_rows = {}
448 for row in rows:
449 stats_rows[row["site"]] = row
Scott Bakeredf2f672014-04-22 10:17:37 -0700450 else:
451 stats_rows = {}
Scott Baker0017c102014-04-18 10:48:31 -0700452
Scott Baker09fad022014-04-30 09:50:01 -0700453 slice = Slice.objects.filter(name=HPC_SLICE_NAME)
454 if slice:
455 slice_slivers = list(slice[0].slivers.all())
456 else:
457 slice_slivers = []
Scott Baker0017c102014-04-18 10:48:31 -0700458
459 new_rows = {}
Scott Baker75095b62014-04-21 17:32:09 -0700460 for site in Site.objects.all():
461 # compute number of slivers allocated in the data model
462 allocated_slivers = 0
463 for sliver in slice_slivers:
464 if sliver.node.site == site:
465 allocated_slivers = allocated_slivers + 1
466
467 stats_row = stats_rows.get(site.name,{})
468
469 max_cpu = stats_row.get("max_avg_cpu", stats_row.get("max_cpu",0))
470 cpu=float(max_cpu)/100.0
471 hotness = max(0.0, ((cpu*RED_LOAD) - BLUE_LOAD)/(RED_LOAD-BLUE_LOAD))
472
473 # format it to what that CDN Operations View is expecting
474 new_row = {"lat": float(site.location.longitude),
475 "long": float(site.location.longitude),
476 "lat": float(site.location.latitude),
Scott Baker0017c102014-04-18 10:48:31 -0700477 "health": 0,
Scott Baker75095b62014-04-21 17:32:09 -0700478 "numNodes": int(site.nodes.count()),
Scott Bakere9f4ec92014-04-29 11:29:45 -0700479 "activeHPCSlivers": int(stats_row.get("count_hostname", 0)), # measured number of slivers, from bigquery statistics
Scott Baker75095b62014-04-21 17:32:09 -0700480 "numHPCSlivers": allocated_slivers, # allocated number of slivers, from data model
481 "siteUrl": str(site.site_url),
482 "bandwidth": stats_row.get("sum_computed_bytes_sent_div_elapsed",0),
483 "load": max_cpu,
484 "hot": float(hotness)}
485 new_rows[str(site.name)] = new_row
486
487 # get rid of sites with 0 slivers that overlap other sites with >0 slivers
488 for (k,v) in new_rows.items():
489 bad=False
490 if v["numHPCSlivers"]==0:
491 for v2 in new_rows.values():
492 if (v!=v2) and (v2["numHPCSlivers"]>=0):
493 d = haversine(v["lat"],v["long"],v2["lat"],v2["long"])
494 if d<100:
495 bad=True
496 if bad:
497 del new_rows[k]
Scott Baker0017c102014-04-18 10:48:31 -0700498
499 return new_rows
Siobhan Tully06d2c032014-02-23 23:48:52 -0500500
Scott Baker168b7412014-05-16 14:25:41 -0700501def getPageSummary(request):
502 slice = request.GET.get('slice', None)
503 site = request.GET.get('site', None)
504 node = request.GET.get('node', None)
505
Scott Baker771819b2014-03-19 22:10:17 -0700506class SimulatorView(View):
507 def get(self, request, **kwargs):
508 sim = json.loads(file("/tmp/simulator.json","r").read())
509 text = "<html><head></head><body>"
510 text += "Iteration: %d<br>" % sim["iteration"]
511 text += "Elapsed since report %d<br><br>" % sim["elapsed_since_report"]
512 text += "<table border=1>"
513 text += "<tr><th>site</th><th>trend</th><th>weight</th><th>bytes_sent</th><th>hot</th></tr>"
514 for site in sim["site_load"].values():
515 text += "<tr>"
516 text += "<td>%s</td><td>%0.2f</td><td>%0.2f</td><td>%d</td><td>%0.2f</td>" % \
517 (site["name"], site["trend"], site["weight"], site["bytes_sent"], site["load_frac"])
518 text += "</tr>"
519 text += "</table>"
520 text += "</body></html>"
521 return HttpResponse(text)
522
523class DashboardUserSiteView(View):
524 def get(self, request, **kwargs):
Scott Baker2c3cb642014-05-19 17:55:56 -0700525 return HttpResponse(json.dumps(getDashboardContext(request.user, tableFormat=True)), mimetype='application/javascript')
Scott Baker771819b2014-03-19 22:10:17 -0700526
Scott Baker537d8232014-04-08 23:40:56 -0700527class TenantViewData(View):
528 def get(self, request, **kwargs):
529 return HttpResponse(json.dumps(getTenantSliceInfo(request.user, True)), mimetype='application/javascript')
530
Scott Baker75095b62014-04-21 17:32:09 -0700531def haversine(site_lat, site_lon, lat, lon):
Scott Baker75095b62014-04-21 17:32:09 -0700532 d=0
533 if lat and lon and site_lat and site_lon:
Scott Baker4b0cedd2014-04-22 21:01:21 -0700534 site_lat = float(site_lat)
535 site_lon = float(site_lon)
536 lat = float(lat)
537 lon = float(lon)
Scott Baker75095b62014-04-21 17:32:09 -0700538 R = 6378.1
539 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)
540 c = 2 * math.atan2( math.sqrt(a), math.sqrt(1 - a) )
541 d = R * c
542
543 return d
544
Scott Bakerbd57a432014-04-14 16:12:15 -0700545def siteSortKey(site, slice=None, count=None, lat=None, lon=None):
546 # try to pick a site we're already using
547 has_slivers_here=False
548 if slice:
549 for sliver in slice.slivers.all():
550 if sliver.node.site.name == site.name:
551 has_slivers_here=True
552
553 # Haversine method
Scott Baker75095b62014-04-21 17:32:09 -0700554 d = haversine(site.location.latitude, site.location.longitude, lat, lon)
Scott Bakerbd57a432014-04-14 16:12:15 -0700555
556 return (-has_slivers_here, d)
557
558def tenant_pick_sites(user, user_ip=None, slice=None, count=None):
559 """ Returns list of sites, sorted from most favorable to least favorable """
560 lat=None
561 lon=None
562 try:
563 client_geo = GeoIP().city(user_ip)
564 if client_geo:
565 lat=float(client_geo["latitude"])
566 lon=float(client_geo["longitude"])
567 except:
568 print "exception in geo code"
569 traceback.print_exc()
570
571 sites = Site.objects.all()
Scott Bakera880bb52014-04-14 23:52:48 -0700572 sites = [x for x in sites if x.name in BLESSED_SITES]
Scott Bakerbd57a432014-04-14 16:12:15 -0700573 sites = sorted(sites, key=functools.partial(siteSortKey, slice=slice, count=count, lat=lat, lon=lon))
574
575 return sites
576
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700577def slice_increase_slivers(user, user_ip, siteList, slice, count, noAct=False):
Scott Bakere4deddd2014-04-14 16:48:05 -0700578 sitesChanged = {}
579
Scott Bakerbd57a432014-04-14 16:12:15 -0700580 # let's compute how many slivers are in use in each node of each site
581 for site in siteList:
582 site.nodeList = list(site.nodes.all())
583 for node in site.nodeList:
584 node.sliverCount = 0
Scott Baker6253dfc2014-04-14 09:29:30 -0700585 for sliver in node.slivers.all():
Scott Baker21511942014-04-15 17:44:27 -0700586 if sliver.slice.id == slice.id:
Scott Baker0017c102014-04-18 10:48:31 -0700587 node.sliverCount = node.sliverCount + 1
Scott Baker6253dfc2014-04-14 09:29:30 -0700588
Scott Bakerbd57a432014-04-14 16:12:15 -0700589 # Allocate slivers to nodes
590 # for now, assume we want to allocate all slivers from the same site
591 nodes = siteList[0].nodeList
592 while (count>0):
593 # Sort the node list by number of slivers per node, then pick the
594 # node with the least number of slivers.
595 nodes = sorted(nodes, key=attrgetter("sliverCount"))
596 node = nodes[0]
Scott Baker6253dfc2014-04-14 09:29:30 -0700597
Scott Bakerbd57a432014-04-14 16:12:15 -0700598 print "adding sliver at node", node.name, "of site", node.site.name
Scott Baker6253dfc2014-04-14 09:29:30 -0700599
Scott Bakere4deddd2014-04-14 16:48:05 -0700600 if not noAct:
601 sliver = Sliver(name=node.name,
602 slice=slice,
603 node=node,
604 image = Image.objects.all()[0],
605 creator = User.objects.get(email=user),
606 deploymentNetwork=node.deployment,
607 numberCores =1 )
608 sliver.save()
Scott Bakerbd57a432014-04-14 16:12:15 -0700609
610 node.sliverCount = node.sliverCount + 1
611
612 count = count - 1
613
Scott Bakere4deddd2014-04-14 16:48:05 -0700614 sitesChanged[node.site.name] = sitesChanged.get(node.site.name,0) + 1
615
616 return sitesChanged
617
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700618def slice_decrease_slivers(user, siteList, slice, count, noAct=False):
Scott Bakere4deddd2014-04-14 16:48:05 -0700619 sitesChanged = {}
Scott Bakerb85c26b2014-04-18 11:25:32 -0700620 sliverList ={}
Scott Bakerbd57a432014-04-14 16:12:15 -0700621 if siteList:
622 siteNames = [site.name for site in siteList]
623 else:
624 siteNames = None
625
626 for sliver in slice.slivers.all():
Scott Bakerb85c26b2014-04-18 11:25:32 -0700627 if(not siteNames) or (sliver.node.site.name in siteNames):
628 node = sliver.node
629 sliverList[sliver.name]=node.name
Scott Bakerbd57a432014-04-14 16:12:15 -0700630
Scott Bakerb85c26b2014-04-18 11:25:32 -0700631 for key in sliverList:
632 if count>0:
Scott Bakerf1b65c02014-04-21 01:03:17 -0700633 sliver = Sliver.objects.filter(name=key)[0]
Scott Bakerb85c26b2014-04-18 11:25:32 -0700634 sliver.delete()
635 print "deleting sliver",sliverList[key],"at node",sliver.node.name
636 count=count-1
Scott Bakerf1b65c02014-04-21 01:03:17 -0700637 sitesChanged[sliver.node.site.name] = sitesChanged.get(sliver.node.site.name,0) - 1
Scott Bakerb85c26b2014-04-18 11:25:32 -0700638
Scott Bakere4deddd2014-04-14 16:48:05 -0700639 return sitesChanged
640
Scott Bakerb85c26b2014-04-18 11:25:32 -0700641class TenantDeleteSliceView(View):
642 def post(self,request):
Scott Baker94319c52014-05-26 11:45:29 -0700643 if request.user.isReadOnlyUser():
644 return HttpResponseForbidden("User is in read-only mode")
Scott Bakerb85c26b2014-04-18 11:25:32 -0700645 sliceName = request.POST.get("sliceName",None)
646 slice = Slice.objects.get(name=sliceName)
Scott Baker168b7412014-05-16 14:25:41 -0700647 #print slice, slice.id
Scott Bakerb85c26b2014-04-18 11:25:32 -0700648 sliceToDel=Slice(name=sliceName, id=slice.id)
649 sliceToDel.delete()
Scott Baker4b0cedd2014-04-22 21:01:21 -0700650 return HttpResponse("Slice deleted")
Scott Bakerb85c26b2014-04-18 11:25:32 -0700651
Scott Baker6253dfc2014-04-14 09:29:30 -0700652class TenantAddOrRemoveSliverView(View):
Scott Bakere4deddd2014-04-14 16:48:05 -0700653 """ Add or remove slivers from a Slice
654
655 Arguments:
656 siteName - name of site. If not specified, PlanetStack will pick the
657 best site.,
658 actionToDo - [add | rem]
659 count - number of slivers to add or remove
660 sliceName - name of slice
Scott Bakera880bb52014-04-14 23:52:48 -0700661 noAct - if set, no changes will be made to db, but result will still
662 show which sites would have been modified.
Scott Bakere4deddd2014-04-14 16:48:05 -0700663
664 Returns:
665 Dictionary of sites that were modified, and the count of nodes
666 that were added or removed at each site.
667 """
Scott Baker6253dfc2014-04-14 09:29:30 -0700668 def post(self, request, *args, **kwargs):
Scott Bakerbd57a432014-04-14 16:12:15 -0700669 siteName = request.POST.get("siteName", None)
670 actionToDo = request.POST.get("actionToDo", None)
671 count = int(request.POST.get("count","0"))
672 sliceName = request.POST.get("slice", None)
Scott Bakere4deddd2014-04-14 16:48:05 -0700673 noAct = request.POST.get("noAct", False)
Scott Bakerbd57a432014-04-14 16:12:15 -0700674
675 if not sliceName:
676 return HttpResponseServerError("No slice name given")
677
678 slice = Slice.objects.get(name=sliceName)
679
680 if siteName:
681 siteList = [Site.objects.get(name=siteName)]
682 else:
683 siteList = None
Scott Baker6253dfc2014-04-14 09:29:30 -0700684
685 if (actionToDo == "add"):
Scott Bakerbd57a432014-04-14 16:12:15 -0700686 user_ip = request.GET.get("ip", get_ip(request))
687 if (siteList is None):
688 siteList = tenant_pick_sites(user, user_ip, slice, count)
689
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700690 sitesChanged = slice_increase_slivers(request.user, user_ip, siteList, slice, count, noAct)
Scott Baker6253dfc2014-04-14 09:29:30 -0700691 elif (actionToDo == "rem"):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700692 sitesChanged = slice_decrease_slivers(request.user, siteList, slice, count, noAct)
Scott Bakerbd57a432014-04-14 16:12:15 -0700693 else:
694 return HttpResponseServerError("Unknown actionToDo %s" % actionToDo)
695
Scott Bakere4deddd2014-04-14 16:48:05 -0700696 return HttpResponse(json.dumps(sitesChanged), mimetype='application/javascript')
Scott Bakerbd57a432014-04-14 16:12:15 -0700697
698 def get(self, request, *args, **kwargs):
699 request.POST = request.GET
700 return self.post(request, *args, **kwargs) # for testing REST in browser
701 #return HttpResponseServerError("GET is not supported")
702
703class TenantPickSitesView(View):
704 """ primarily just for testing purposes """
705 def get(self, request, *args, **kwargs):
706 count = request.GET.get("count","0")
707 slice = request.GET.get("slice",None)
708 if slice:
709 slice = Slice.objects.get(name=slice)
710 ip = request.GET.get("ip", get_ip(request))
711 sites = tenant_pick_sites(request.user, user_ip=ip, count=0, slice=slice)
712 sites = [x.name for x in sites]
713 return HttpResponse(json.dumps(sites), mimetype='application/javascript')
Scott Baker6253dfc2014-04-14 09:29:30 -0700714
Scott Baker771819b2014-03-19 22:10:17 -0700715class DashboardSummaryAjaxView(View):
716 def get(self, request, **kwargs):
Scott Baker0017c102014-04-18 10:48:31 -0700717 def avg(x):
Scott Baker111702b2014-05-15 16:17:16 -0700718 if len(x)==0:
719 return 0
Scott Baker0017c102014-04-18 10:48:31 -0700720 return float(sum(x))/len(x)
721
722 sites = getCDNOperatorData().values()
723
724 sites = [site for site in sites if site["numHPCSlivers"]>0]
725
726 total_slivers = sum( [site["numHPCSlivers"] for site in sites] )
727 total_bandwidth = sum( [site["bandwidth"] for site in sites] )
728 average_cpu = int(avg( [site["load"] for site in sites] ))
729
730 result= {"total_slivers": total_slivers,
731 "total_bandwidth": total_bandwidth,
732 "average_cpu": average_cpu}
733
734 return HttpResponse(json.dumps(result), mimetype='application/javascript')
Scott Baker771819b2014-03-19 22:10:17 -0700735
736class DashboardAddOrRemoveSliverView(View):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700737 # TODO: deprecate this view in favor of using TenantAddOrRemoveSliverView
738
Scott Baker771819b2014-03-19 22:10:17 -0700739 def post(self, request, *args, **kwargs):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700740 siteName = request.POST.get("site", None)
Scott Baker771819b2014-03-19 22:10:17 -0700741 actionToDo = request.POST.get("actionToDo", "0")
742
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700743 siteList = [Site.objects.get(name=siteName)]
744 slice = Slice.objects.get(name="HyperCache")
745
Scott Bakerd927d662014-05-22 09:29:06 -0700746 if request.user.isReadOnlyUser():
747 return HttpResponseForbidden("User is in read-only mode")
748
Scott Baker771819b2014-03-19 22:10:17 -0700749 if (actionToDo == "add"):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700750 user_ip = request.GET.get("ip", get_ip(request))
751 slice_increase_slivers(request.user, user_ip, siteList, slice, 1)
Scott Baker771819b2014-03-19 22:10:17 -0700752 elif (actionToDo == "rem"):
Scott Bakerfe6a7e12014-04-15 10:58:31 -0700753 slice_decrease_slivers(request.user, siteList, slice, 1)
Scott Baker771819b2014-03-19 22:10:17 -0700754
755 print '*' * 50
756 print 'Ask for site: ' + siteName + ' to ' + actionToDo + ' another HPC Sliver'
Scott Bakerd927d662014-05-22 09:29:06 -0700757 return HttpResponse(json.dumps("Success"), mimetype='application/javascript')
Siobhan Tully06d2c032014-02-23 23:48:52 -0500758
759class DashboardAjaxView(View):
760 def get(self, request, **kwargs):
761 return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript')
Scott Baker771819b2014-03-19 22:10:17 -0700762
763class DashboardAnalyticsAjaxView(View):
764 def get(self, request, name="hello_world", **kwargs):
765 if (name == "hpcSummary"):
766 return HttpResponse(json.dumps(hpc_wizard.get_hpc_wizard().get_summary_for_view()), mimetype='application/javascript')
767 elif (name == "hpcUserSite"):
Scott Baker2c3cb642014-05-19 17:55:56 -0700768 return HttpResponse(json.dumps(getDashboardContext(request.user, tableFormat=True)), mimetype='application/javascript')
Scott Baker771819b2014-03-19 22:10:17 -0700769 elif (name == "hpcMap"):
770 return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript')
771 elif (name == "bigquery"):
772 (mimetype, data) = DoPlanetStackAnalytics(request)
773 return HttpResponse(data, mimetype=mimetype)
774 else:
775 return HttpResponse(json.dumps("Unknown"), mimetype='application/javascript')
776
Scott Baker2c3cb642014-05-19 17:55:56 -0700777class DashboardCustomize(View):
778 def post(self, request, *args, **kwargs):
Scott Baker60fd8cd2014-05-21 18:02:44 -0700779 if request.user.isReadOnlyUser():
780 return HttpResponseForbidden("User is in read-only mode")
781
Scott Baker2c3cb642014-05-19 17:55:56 -0700782 dashboards = request.POST.get("dashboards", None)
783 if not dashboards:
Scott Baker2870e272014-05-19 18:14:54 -0700784 dashboards=[]
785 else:
786 dashboards = [x.strip() for x in dashboards.split(",")]
787 dashboards = [DashboardView.objects.get(name=x) for x in dashboards]
Scott Baker2c3cb642014-05-19 17:55:56 -0700788
789 request.user.dashboardViews.all().delete()
790
791 for i,dashboard in enumerate(dashboards):
792 udbv = UserDashboardView(user=request.user, dashboardView=dashboard, order=i)
793 udbv.save()
794
Scott Baker60fd8cd2014-05-21 18:02:44 -0700795 return HttpResponse(json.dumps("Success"), mimetype='application/javascript')
Scott Baker2c3cb642014-05-19 17:55:56 -0700796
Scott Baker6b654202014-05-27 16:55:00 -0700797class DashboardSliceInteractions(View):
798 def get(self, request, name="users", **kwargs):
799 colors = ["#005586", "#6ebe49", "orange", "#707170", "#00c4b3", "#077767", "dodgerblue", "#a79b94", "#c4e76a", "red"]
800
801 groups = []
802 matrix = []
803 slices = list(Slice.objects.all())
804
805 slices = [x for x in slices if not self.isEmpty(x,name)]
806
807 for i,slice in enumerate(slices):
808 groups.append({"name": slice.name, "color": colors[i%len(colors)]})
809 matrix.append(self.buildMatrix(slice, slices, name))
810
811 result = {"groups": groups, "matrix": matrix}
812
813 if name=="users":
814 result["title"] = "Slice interactions by user privilege"
815 result["objectName"] = "users"
816 elif name=="networks":
817 result["title"] = "Slice interactions by network membership"
818 result["objectName"] = "networks"
819 elif name=="sites":
820 result["title"] = "Slice interactions by site ownership"
821 result["objectName"] = "sites"
822 elif name=="sliver_sites":
823 result["title"] = "Slice interactions by sliver sites"
824 result["objectName"] = "sites"
825 elif name=="sliver_nodes":
826 result["title"] = "Slice interactions by sliver nodes"
827 result["objectName"] = "nodes"
828
829 return HttpResponse(json.dumps(result), mimetype='application/javascript')
830
831 def buildMatrix(self, slice, slices, name):
832 row = []
833 for otherSlice in slices:
834 if (otherSlice == slice):
835 row.append(self.getCount(slice, name))
836 else:
837 row.append(self.getInCommon(slice, otherSlice, name))
838 return row
839
840 def other_slice_sites(self, slice):
841 ids=[]
842 for sliver in Sliver.objects.all():
843 if sliver.slice!=slice:
844 if sliver.node.site.id not in ids:
845 ids.append(sliver.node.site.id)
846 return ids
847
848 def other_slice_nodes(self, slice):
849 ids=[]
850 for sliver in Sliver.objects.all():
851 if sliver.slice!=slice:
852 if sliver.node.id not in ids:
853 ids.append(sliver.node.id)
854 return ids
855
856 def getIds(self, slice, name, onlySelf=False):
857 ids=[]
858 if name=="users":
859 for sp in slice.slice_privileges.all():
860 if (not onlySelf) or (len(sp.user.slice_privileges.all())==1):
861 if sp.user.id not in ids:
862 ids.append(sp.user.id)
863 elif name=="networks":
864 for sp in slice.networkslice_set.all():
865 if (not onlySelf) or (len(sp.network.networkslice_set.all())==1):
866 if sp.network.id not in ids:
867 ids.append(sp.network.id)
868 elif name=="sites":
869 ids = [slice.site.id]
870 elif name=="sliver_sites":
871 for sp in slice.slivers.all():
872 if sp.node.site.id not in ids:
873 ids.append(sp.node.site.id)
874 if onlySelf:
875 other_slice_sites = self.other_slice_sites(slice)
876 ids = [x for x in ids if x not in other_slice_sites]
877 elif name=="sliver_nodes":
878 for sp in slice.slivers.all():
879 if sp.node.id not in ids:
880 ids.append(sp.node.id)
881 if onlySelf:
882 other_slice_nodes = self.other_slice_nodes(slice)
883 ids = [x for x in ids if x not in other_slice_nodes]
884
885 return ids
886
887 def inCommonIds(self, ids1, ids2):
888 count = 0
889 for id in ids1:
890 if id in ids2:
891 count+=1
892 return count
893
894 def getCount(self, slice, name):
895 if (name in ["users", "networks", "sites", "sliver_nodes", "sliver_sites"]):
896 return len(self.getIds(slice,name,onlySelf=True))
897
898 def getInCommon(self, slice, otherSlice, name):
899 if (name in ["users", "networks", "sites", "sliver_nodes", "sliver_sites"]):
900 slice_ids = self.getIds(slice,name)
901 otherSlice_ids = self.getIds(otherSlice,name)
902 return self.inCommonIds(slice_ids, otherSlice_ids)
903
904 def isEmpty(self, slice, name):
905 if (name in ["users", "networks", "sites", "sliver_nodes", "sliver_sites"]):
906 return (len(self.getIds(slice,name)) == 0)
907