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