Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 1 | from view_common import * |
| 2 | |
| 3 | class DashboardSliceInteractions(View): |
| 4 | def get(self, request, name="users", **kwargs): |
| 5 | colors = ["#005586", "#6ebe49", "orange", "#707170", "#00c4b3", "#077767", "dodgerblue", "#a79b94", "#c4e76a", "red"] |
| 6 | |
| 7 | groups = [] |
| 8 | matrix = [] |
| 9 | slices = list(Slice.objects.all()) |
| 10 | |
| 11 | ids_by_slice = self.build_id_list(slices, name) |
| 12 | |
| 13 | slices = [x for x in slices if (len(ids_by_slice[x])>0)] |
| 14 | |
| 15 | for i,slice in enumerate(slices): |
| 16 | groups.append({"name": slice.name, "color": colors[i%len(colors)]}) |
| 17 | row=self.buildMatrix(slice, slices, name, ids_by_slice) |
| 18 | matrix.append(row) |
| 19 | |
| 20 | result = {"groups": groups, "matrix": matrix} |
| 21 | |
| 22 | if name=="users": |
| 23 | result["title"] = "Slice interactions by user privilege" |
| 24 | result["objectName"] = "users" |
| 25 | elif name=="networks": |
| 26 | result["title"] = "Slice interactions by network membership" |
| 27 | result["objectName"] = "networks" |
| 28 | elif name=="sites": |
| 29 | result["title"] = "Slice interactions by site ownership" |
| 30 | result["objectName"] = "sites" |
| 31 | elif name=="sliver_sites": |
| 32 | result["title"] = "Slice interactions by sliver sites" |
| 33 | result["objectName"] = "sites" |
| 34 | elif name=="sliver_nodes": |
| 35 | result["title"] = "Slice interactions by sliver nodes" |
| 36 | result["objectName"] = "nodes" |
| 37 | |
| 38 | return HttpResponse(json.dumps(result), mimetype='application/javascript') |
| 39 | |
| 40 | def build_id_list(self, slices, name): |
| 41 | ids_by_slice = {} |
| 42 | for slice in slices: |
| 43 | # build up a list of object ids that are used by each slice |
| 44 | ids_by_slice[slice] = self.getIds(slice, name) |
| 45 | |
| 46 | return ids_by_slice |
| 47 | |
| 48 | def buildMatrix(self, slice, slices, name, ids_by_slice): |
| 49 | not_only_my_ids = [] |
| 50 | |
| 51 | # build up a list of object ids that are used by other slices |
| 52 | for otherSlice in ids_by_slice.keys(): |
| 53 | if (slice != otherSlice): |
| 54 | for id in ids_by_slice[otherSlice]: |
| 55 | if not id in not_only_my_ids: |
| 56 | not_only_my_ids.append(id) |
| 57 | |
| 58 | # build up a list of ids that are used only by the slice, and not |
| 59 | # shared with any other slice |
| 60 | only_my_ids = [] |
| 61 | for id in ids_by_slice[slice]: |
| 62 | if id not in not_only_my_ids: |
| 63 | only_my_ids.append(id) |
| 64 | |
| 65 | row = [] |
| 66 | for otherSlice in ids_by_slice.keys(): |
| 67 | if (otherSlice == slice): |
| 68 | row.append(len(only_my_ids)) |
| 69 | else: |
| 70 | row.append(self.inCommonIds(ids_by_slice[slice], ids_by_slice[otherSlice])) |
| 71 | |
| 72 | return row |
| 73 | |
| 74 | def getIds(self, slice, name): |
| 75 | ids=[] |
| 76 | if name=="users": |
| 77 | for sp in slice.slice_privileges.all(): |
| 78 | if sp.user.id not in ids: |
| 79 | ids.append(sp.user.id) |
| 80 | elif name=="networks": |
| 81 | for sp in slice.networkslice_set.all(): |
| 82 | if sp.network.id not in ids: |
| 83 | ids.append(sp.network.id) |
| 84 | elif name=="sites": |
| 85 | ids = [slice.site.id] |
| 86 | elif name=="sliver_sites": |
| 87 | for sp in slice.slivers.all(): |
| 88 | if sp.node.site.id not in ids: |
| 89 | ids.append(sp.node.site.id) |
| 90 | elif name=="sliver_nodes": |
| 91 | for sp in slice.slivers.all(): |
| 92 | if sp.node.id not in ids: |
| 93 | ids.append(sp.node.id) |
| 94 | return ids |
| 95 | |
| 96 | def inCommonIds(self, ids1, ids2): |
| 97 | count = 0 |
| 98 | for id in ids1: |
| 99 | if id in ids2: |
| 100 | count+=1 |
| 101 | return count |
| 102 | |
| 103 | |