Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | from django.views.generic import TemplateView, View |
| 4 | import datetime |
| 5 | from pprint import pprint |
| 6 | import json |
jcnelson | 160012b | 2014-07-10 19:32:58 -0400 | [diff] [blame] | 7 | from syndicate_storage.models import * |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 8 | from core.models import * |
| 9 | from hpc.models import ContentProvider |
| 10 | from operator import attrgetter |
| 11 | from django import template |
| 12 | from django.views.decorators.csrf import csrf_exempt |
| 13 | from django.http import HttpResponse, HttpResponseServerError, HttpResponseForbidden |
| 14 | from django.core import urlresolvers |
| 15 | from django.contrib.gis.geoip import GeoIP |
| 16 | from django.db.models import Q |
| 17 | from ipware.ip import get_ip |
| 18 | from operator import itemgetter, attrgetter |
| 19 | import traceback |
| 20 | import math |
| 21 | |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 22 | def getDashboardContext(user, context={}, tableFormat = False): |
| 23 | context = {} |
| 24 | |
| 25 | userSliceData = getSliceInfo(user) |
| 26 | if (tableFormat): |
| 27 | context['userSliceInfo'] = userSliceTableFormatter(userSliceData) |
| 28 | else: |
| 29 | context['userSliceInfo'] = userSliceData |
Scott Baker | 0906612 | 2015-02-02 16:12:47 -0800 | [diff] [blame] | 30 | # context['cdnData'] = getCDNOperatorData(wait=False) |
| 31 | # context['cdnContentProviders'] = getCDNContentProviderData() |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 32 | |
| 33 | (dashboards, unusedDashboards)= getDashboards(user) |
| 34 | unusedDashboards=[x for x in unusedDashboards if x!="Customize"] |
| 35 | context['dashboards'] = dashboards |
| 36 | context['unusedDashboards'] = unusedDashboards |
| 37 | |
| 38 | return context |
| 39 | |
| 40 | def getDashboards(user): |
| 41 | dashboards = user.get_dashboards() |
| 42 | |
| 43 | dashboard_names = [d.name for d in dashboards] |
| 44 | |
| 45 | unused_dashboard_names = [] |
| 46 | for dashboardView in DashboardView.objects.all(): |
Scott Baker | 8c83e71 | 2015-01-18 21:30:10 -0800 | [diff] [blame] | 47 | # do not show disabled dashboard views |
| 48 | if not dashboardView.enabled: |
| 49 | continue |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 50 | if not dashboardView.name in dashboard_names: |
| 51 | unused_dashboard_names.append(dashboardView.name) |
| 52 | |
| 53 | return (dashboard_names, unused_dashboard_names) |
| 54 | |
| 55 | def getSliceInfo(user): |
| 56 | sliceList = Slice.objects.all() |
| 57 | slicePrivs = SlicePrivilege.objects.filter(user=user) |
| 58 | userSliceInfo = [] |
| 59 | for entry in slicePrivs: |
| 60 | |
Scott Baker | d7c3131 | 2014-11-05 11:05:45 -0800 | [diff] [blame] | 61 | slice = Slice.objects.filter(id=entry.slice.id) |
| 62 | if not slice: |
| 63 | # the privilege is to a slice that doesn't exist |
| 64 | print "data model consistency problem, slice %s doesn't exist" % entry.slice.id |
| 65 | continue |
| 66 | slice = slice[0] |
| 67 | slicename = slice.name |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 68 | sliverList=Sliver.objects.all() |
| 69 | sites_used = {} |
| 70 | for sliver in slice.slivers.all(): |
| 71 | #sites_used['deploymentSites'] = sliver.node.deployment.name |
| 72 | # sites_used[sliver.image.name] = sliver.image.name |
Tony Mack | 3c01ff9 | 2015-01-10 23:08:10 -0500 | [diff] [blame] | 73 | sites_used[sliver.node.site_deployment.site] = 1 #sliver.numberCores |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 74 | sliceid = Slice.objects.get(id=entry.slice.id).id |
| 75 | try: |
| 76 | sliverList = Sliver.objects.filter(slice=entry.slice.id) |
| 77 | siteList = {} |
| 78 | for x in sliverList: |
Scott Baker | e7035eb | 2015-01-09 14:13:32 -0800 | [diff] [blame] | 79 | if x.node.site_deployment.site not in siteList: |
| 80 | siteList[x.node.site_deployment.site] = 1 |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 81 | slivercount = len(sliverList) |
| 82 | sitecount = len(siteList) |
| 83 | except: |
| 84 | traceback.print_exc() |
| 85 | slivercount = 0 |
| 86 | sitecount = 0 |
| 87 | |
| 88 | userSliceInfo.append({'slicename': slicename, 'sliceid':sliceid, |
| 89 | 'sitesUsed':sites_used, |
| 90 | 'role': SliceRole.objects.get(id=entry.role.id).role, |
| 91 | 'slivercount': slivercount, |
| 92 | 'sitecount':sitecount}) |
| 93 | |
| 94 | return userSliceInfo |
| 95 | |
Scott Baker | 866c5b3 | 2014-08-29 11:34:00 -0700 | [diff] [blame] | 96 | def slice_increase_slivers(user, user_ip, siteList, slice, image, count, noAct=False): |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 97 | sitesChanged = {} |
| 98 | |
| 99 | # let's compute how many slivers are in use in each node of each site |
| 100 | for site in siteList: |
| 101 | site.nodeList = list(site.nodes.all()) |
| 102 | for node in site.nodeList: |
| 103 | node.sliverCount = 0 |
| 104 | for sliver in node.slivers.all(): |
| 105 | if sliver.slice.id == slice.id: |
| 106 | node.sliverCount = node.sliverCount + 1 |
| 107 | |
| 108 | # Allocate slivers to nodes |
| 109 | # for now, assume we want to allocate all slivers from the same site |
| 110 | nodes = siteList[0].nodeList |
| 111 | while (count>0): |
| 112 | # Sort the node list by number of slivers per node, then pick the |
| 113 | # node with the least number of slivers. |
| 114 | nodes = sorted(nodes, key=attrgetter("sliverCount")) |
| 115 | node = nodes[0] |
| 116 | |
| 117 | print "adding sliver at node", node.name, "of site", node.site.name |
| 118 | |
| 119 | if not noAct: |
| 120 | sliver = Sliver(name=node.name, |
| 121 | slice=slice, |
| 122 | node=node, |
Scott Baker | 866c5b3 | 2014-08-29 11:34:00 -0700 | [diff] [blame] | 123 | image = image, |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 124 | creator = User.objects.get(email=user), |
Scott Baker | 4f3c9d5 | 2014-09-02 17:38:40 -0700 | [diff] [blame] | 125 | deploymentNetwork=node.deployment) |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 126 | sliver.save() |
| 127 | |
| 128 | node.sliverCount = node.sliverCount + 1 |
| 129 | |
| 130 | count = count - 1 |
| 131 | |
| 132 | sitesChanged[node.site.name] = sitesChanged.get(node.site.name,0) + 1 |
| 133 | |
| 134 | return sitesChanged |
| 135 | |
| 136 | def slice_decrease_slivers(user, siteList, slice, count, noAct=False): |
| 137 | sitesChanged = {} |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 138 | if siteList: |
| 139 | siteNames = [site.name for site in siteList] |
| 140 | else: |
| 141 | siteNames = None |
| 142 | |
Scott Baker | 86baef6 | 2014-07-11 09:48:45 -0700 | [diff] [blame] | 143 | for sliver in list(slice.slivers.all()): |
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 144 | if count>0: |
Scott Baker | 86baef6 | 2014-07-11 09:48:45 -0700 | [diff] [blame] | 145 | if(not siteNames) or (sliver.node.site.name in siteNames): |
| 146 | sliver.delete()
|
| 147 | print "deleting sliver",sliver.name,"at node",sliver.node.name
|
| 148 | count=count-1
|
| 149 | sitesChanged[sliver.node.site.name] = sitesChanged.get(sliver.node.site.name,0) - 1
|
Scott Baker | c7325a4 | 2014-05-30 16:06:46 -0700 | [diff] [blame] | 150 |
|
| 151 | return sitesChanged |
| 152 | |
| 153 | def haversine(site_lat, site_lon, lat, lon): |
| 154 | d=0 |
| 155 | if lat and lon and site_lat and site_lon: |
| 156 | site_lat = float(site_lat) |
| 157 | site_lon = float(site_lon) |
| 158 | lat = float(lat) |
| 159 | lon = float(lon) |
| 160 | R = 6378.1 |
| 161 | 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) |
| 162 | c = 2 * math.atan2( math.sqrt(a), math.sqrt(1 - a) ) |
| 163 | d = R * c |
| 164 | |
| 165 | return d |
| 166 | |
| 167 | def userSliceTableFormatter(data): |
| 168 | formattedData = { |
| 169 | 'rows' : data |
| 170 | } |
| 171 | return formattedData |