+class DashboardSliceInteractions(View):
+ def get(self, request, name="users", **kwargs):
+ colors = ["#005586", "#6ebe49", "orange", "#707170", "#00c4b3", "#077767", "dodgerblue", "#a79b94", "#c4e76a", "red"]
+
+ groups = []
+ matrix = []
+ slices = list(Slice.objects.all())
+
+ slices = [x for x in slices if not self.isEmpty(x,name)]
+
+ for i,slice in enumerate(slices):
+ groups.append({"name": slice.name, "color": colors[i%len(colors)]})
+ matrix.append(self.buildMatrix(slice, slices, name))
+
+ result = {"groups": groups, "matrix": matrix}
+
+ if name=="users":
+ result["title"] = "Slice interactions by user privilege"
+ result["objectName"] = "users"
+ elif name=="networks":
+ result["title"] = "Slice interactions by network membership"
+ result["objectName"] = "networks"
+ elif name=="sites":
+ result["title"] = "Slice interactions by site ownership"
+ result["objectName"] = "sites"
+ elif name=="sliver_sites":
+ result["title"] = "Slice interactions by sliver sites"
+ result["objectName"] = "sites"
+ elif name=="sliver_nodes":
+ result["title"] = "Slice interactions by sliver nodes"
+ result["objectName"] = "nodes"
+
+ return HttpResponse(json.dumps(result), mimetype='application/javascript')
+
+ def buildMatrix(self, slice, slices, name):
+ row = []
+ for otherSlice in slices:
+ if (otherSlice == slice):
+ row.append(self.getCount(slice, name))
+ else:
+ row.append(self.getInCommon(slice, otherSlice, name))
+ return row
+
+ def other_slice_sites(self, slice):
+ ids=[]
+ for sliver in Sliver.objects.all():
+ if sliver.slice!=slice:
+ if sliver.node.site.id not in ids:
+ ids.append(sliver.node.site.id)
+ return ids
+
+ def other_slice_nodes(self, slice):
+ ids=[]
+ for sliver in Sliver.objects.all():
+ if sliver.slice!=slice:
+ if sliver.node.id not in ids:
+ ids.append(sliver.node.id)
+ return ids
+
+ def getIds(self, slice, name, onlySelf=False):
+ ids=[]
+ if name=="users":
+ for sp in slice.slice_privileges.all():
+ if (not onlySelf) or (len(sp.user.slice_privileges.all())==1):
+ if sp.user.id not in ids:
+ ids.append(sp.user.id)
+ elif name=="networks":
+ for sp in slice.networkslice_set.all():
+ if (not onlySelf) or (len(sp.network.networkslice_set.all())==1):
+ if sp.network.id not in ids:
+ ids.append(sp.network.id)
+ elif name=="sites":
+ ids = [slice.site.id]
+ elif name=="sliver_sites":
+ for sp in slice.slivers.all():
+ if sp.node.site.id not in ids:
+ ids.append(sp.node.site.id)
+ if onlySelf:
+ other_slice_sites = self.other_slice_sites(slice)
+ ids = [x for x in ids if x not in other_slice_sites]
+ elif name=="sliver_nodes":
+ for sp in slice.slivers.all():
+ if sp.node.id not in ids:
+ ids.append(sp.node.id)
+ if onlySelf:
+ other_slice_nodes = self.other_slice_nodes(slice)
+ ids = [x for x in ids if x not in other_slice_nodes]
+
+ return ids
+
+ def inCommonIds(self, ids1, ids2):
+ count = 0
+ for id in ids1:
+ if id in ids2:
+ count+=1
+ return count
+
+ def getCount(self, slice, name):
+ if (name in ["users", "networks", "sites", "sliver_nodes", "sliver_sites"]):
+ return len(self.getIds(slice,name,onlySelf=True))
+
+ def getInCommon(self, slice, otherSlice, name):
+ if (name in ["users", "networks", "sites", "sliver_nodes", "sliver_sites"]):
+ slice_ids = self.getIds(slice,name)
+ otherSlice_ids = self.getIds(otherSlice,name)
+ return self.inCommonIds(slice_ids, otherSlice_ids)
+
+ def isEmpty(self, slice, name):
+ if (name in ["users", "networks", "sites", "sliver_nodes", "sliver_sites"]):
+ return (len(self.getIds(slice,name)) == 0)
+