blob: 7d592783f30fcd26da9a6056d6c974373365c61f [file] [log] [blame]
Scott Bakerc7325a42014-05-30 16:06:46 -07001import os
2import sys
3from django.views.generic import TemplateView, View
4import datetime
5from pprint import pprint
6import json
jcnelson160012b2014-07-10 19:32:58 -04007from syndicate_storage.models import *
Scott Bakerc7325a42014-05-30 16:06:46 -07008from core.models import *
9from hpc.models import ContentProvider
10from operator import attrgetter
11from django import template
12from django.views.decorators.csrf import csrf_exempt
13from django.http import HttpResponse, HttpResponseServerError, HttpResponseForbidden
14from django.core import urlresolvers
15from django.contrib.gis.geoip import GeoIP
16from django.db.models import Q
17from ipware.ip import get_ip
18from operator import itemgetter, attrgetter
19import traceback
20import math
Scott Baker76a840e2015-02-11 21:38:09 -080021from xos.config import Config, XOS_DIR
Scott Bakerc7325a42014-05-30 16:06:46 -070022
Scott Bakerc7325a42014-05-30 16:06:46 -070023def 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 Baker09066122015-02-02 16:12:47 -080031# context['cdnData'] = getCDNOperatorData(wait=False)
32# context['cdnContentProviders'] = getCDNContentProviderData()
Scott Bakerc7325a42014-05-30 16:06:46 -070033
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
41def 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 Baker8c83e712015-01-18 21:30:10 -080048 # do not show disabled dashboard views
49 if not dashboardView.enabled:
50 continue
Scott Bakerc7325a42014-05-30 16:06:46 -070051 if not dashboardView.name in dashboard_names:
52 unused_dashboard_names.append(dashboardView.name)
53
54 return (dashboard_names, unused_dashboard_names)
55
56def getSliceInfo(user):
57 sliceList = Slice.objects.all()
58 slicePrivs = SlicePrivilege.objects.filter(user=user)
59 userSliceInfo = []
60 for entry in slicePrivs:
61
Scott Bakerd7c31312014-11-05 11:05:45 -080062 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 Bakerc7325a42014-05-30 16:06:46 -070069 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 Mack3c01ff92015-01-10 23:08:10 -050074 sites_used[sliver.node.site_deployment.site] = 1 #sliver.numberCores
Scott Bakerc7325a42014-05-30 16:06:46 -070075 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 Bakere7035eb2015-01-09 14:13:32 -080080 if x.node.site_deployment.site not in siteList:
81 siteList[x.node.site_deployment.site] = 1
Scott Bakerc7325a42014-05-30 16:06:46 -070082 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 Baker866c5b32014-08-29 11:34:00 -070097def slice_increase_slivers(user, user_ip, siteList, slice, image, count, noAct=False):
Scott Bakerc7325a42014-05-30 16:06:46 -070098 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 Baker866c5b32014-08-29 11:34:00 -0700124 image = image,
Scott Bakerc7325a42014-05-30 16:06:46 -0700125 creator = User.objects.get(email=user),
Scott Baker4f3c9d52014-09-02 17:38:40 -0700126 deploymentNetwork=node.deployment)
Scott Bakerc7325a42014-05-30 16:06:46 -0700127 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
137def slice_decrease_slivers(user, siteList, slice, count, noAct=False):
138 sitesChanged = {}
Scott Bakerc7325a42014-05-30 16:06:46 -0700139 if siteList:
140 siteNames = [site.name for site in siteList]
141 else:
142 siteNames = None
143
Scott Baker86baef62014-07-11 09:48:45 -0700144 for sliver in list(slice.slivers.all()):
Scott Bakerc7325a42014-05-30 16:06:46 -0700145 if count>0:
Scott Baker86baef62014-07-11 09:48:45 -0700146 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 Bakerc7325a42014-05-30 16:06:46 -0700151
152 return sitesChanged
153
154def 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
168def userSliceTableFormatter(data):
169 formattedData = {
170 'rows' : data
171 }
172 return formattedData