blob: 0078d91a12de6063ae9d1c714a4a57adbddc30b7 [file] [log] [blame]
Siobhan Tullycf04fb62014-01-11 11:25:57 -05001#views.py
Scott Baker771819b2014-03-19 22:10:17 -07002import os
3import sys
Siobhan Tully06d2c032014-02-23 23:48:52 -05004from django.views.generic import TemplateView, View
5import datetime
Scott Baker771819b2014-03-19 22:10:17 -07006from pprint import pprint
7import json
Scott Baker537d8232014-04-08 23:40:56 -07008from core.models import *
Siobhan Tully06d2c032014-02-23 23:48:52 -05009from django.http import HttpResponse
Scott Baker537d8232014-04-08 23:40:56 -070010from django.core import urlresolvers
Scott Baker771819b2014-03-19 22:10:17 -070011import traceback
Siobhan Tully06d2c032014-02-23 23:48:52 -050012
Scott Baker771819b2014-03-19 22:10:17 -070013if os.path.exists("/home/smbaker/projects/vicci/cdn/bigquery"):
14 sys.path.append("/home/smbaker/projects/vicci/cdn/bigquery")
15else:
16 sys.path.append("/opt/planetstack/hpc_wizard")
17import hpc_wizard
18from planetstack_analytics import DoPlanetStackAnalytics
Siobhan Tullycf04fb62014-01-11 11:25:57 -050019
20class DashboardWelcomeView(TemplateView):
21 template_name = 'admin/dashboard/welcome.html'
22
23 def get(self, request, *args, **kwargs):
24 context = self.get_context_data(**kwargs)
Scott Baker771819b2014-03-19 22:10:17 -070025 userDetails = getUserSliceInfo(request.user)
26 #context['site'] = userDetails['site']
Siobhan Tullycf04fb62014-01-11 11:25:57 -050027
Scott Baker771819b2014-03-19 22:10:17 -070028 context['userSliceInfo'] = userDetails['userSliceInfo']
29 context['cdnData'] = userDetails['cdnData']
Siobhan Tullycf04fb62014-01-11 11:25:57 -050030 return self.render_to_response(context=context)
Siobhan Tullye18b3442014-02-23 14:23:34 -050031
Scott Baker771819b2014-03-19 22:10:17 -070032def getUserSliceInfo(user, tableFormat = False):
33 userDetails = {}
34# try:
35# // site = Site.objects.filter(id=user.site.id)
36# // except:
37# // site = Site.objects.filter(name="Princeton")
38# // userDetails['sitename'] = site[0].name
39# // userDetails['siteid'] = site[0].id
40
41 userSliceData = getSliceInfo(user)
42 if (tableFormat):
43# pprint("******* GET USER SLICE INFO")
44 userDetails['userSliceInfo'] = userSliceTableFormatter(userSliceData)
45 else:
46 userDetails['userSliceInfo'] = userSliceData
47 userDetails['cdnData'] = getCDNOperatorData();
48# pprint( userDetails)
49 return userDetails
50
Scott Baker537d8232014-04-08 23:40:56 -070051class TenantCreateSlice(View):
52 def post(self, request, *args, **kwargs):
53 sliceName = request.POST.get("sliceName", "0")
54 serviceClass = request.POST.get("serviceClass", "0")
55 imageName = request.POST.get("imageName", "0")
56 actionToDo = request.POST.get("actionToDo", "0")
57 print sliceName
58 if (actionToDo == "add"):
59 serviceClass = ServiceClass.objects.get(name=serviceClass)
60 site = request.user.site
61 #image = Image.objects.get(name=imageName)
62 newSlice = Slice(name=sliceName,serviceClass=serviceClass,site=site,imagePreference=imageName)
63 newSlice.save()
64 return newSlice
65
66
67def getTenantSliceInfo(user, tableFormat = False):
68 tenantSliceDetails = {}
69 tenantSliceData = getTenantInfo(user)
70 tenantServiceClassData = getServiceClassInfo(user)
71 if (tableFormat):
72 tenantSliceDetails['userSliceInfo'] = userSliceTableFormatter(tenantSliceData)
73 tenantSliceDetails['sliceServiceClass']=userSliceTableFormatter(tenantServiceClassData)
74 else:
75 tenantSliceDetails['userSliceInfo'] = tenantSliceData
76 tenantSliceDetails['sliceServiceClass']=userSliceTableFormatter(tenantServiceClassData)
77 tenantSliceDetails['image']=userSliceTableFormatter(getImageInfo(user))
78 tenantSliceDetails['network']=userSliceTableFormatter(getNetworkInfo(user))
79 tenantSliceDetails['deploymentSites']=userSliceTableFormatter(getDeploymentSites())
80 tenantSliceDetails['sites'] = userSliceTableFormatter(getTenantSitesInfo());
81 return tenantSliceDetails
82
83
84def getTenantInfo(user):
85 slices =Slice.objects.all()
86 userSliceInfo = []
87 for entry in slices:
88 sliceName = Slice.objects.get(id=entry.id).name
89 slice = Slice.objects.get(name=Slice.objects.get(id=entry.id).name)
90 sliceServiceClass = entry.serviceClass.name
91 preferredImage = entry.imagePreference
92 numSliver = 0
93 sliceImage=""
94 sliceSite = {}
95 for sliver in slice.slivers.all():
96 numSliver +=sliver.numberCores
97 # sliceSite[sliver.deploymentNetwork.name] =sliceSite.get(sliver.deploymentNetwork.name,0) + 1
98 sliceSite[sliver.node.site.name] = sliceSite.get(sliver.node.site.name,0) + 1
99 sliceImage = sliver.image.name
100 userSliceInfo.append({'sliceName': sliceName,'sliceServiceClass': sliceServiceClass,'preferredImage':preferredImage, 'sliceSite':sliceSite,'sliceImage':sliceImage,'numOfSlivers':numSliver})
101 return userSliceInfo
102
103def getTenantSitesInfo():
104 tenantSiteInfo=[]
105 for entry in Site.objects.all():
106 tenantSiteInfo.append({'siteName':entry.name})
107 return tenantSiteInfo
108
Scott Baker771819b2014-03-19 22:10:17 -0700109def userSliceTableFormatter(data):
110# pprint(data)
111 formattedData = {
112 'rows' : data
113 }
114 return formattedData
115
Scott Baker537d8232014-04-08 23:40:56 -0700116def getServiceClassInfo(user):
117 serviceClassList = ServiceClass.objects.all()
118 sliceInfo = []
119 for entry in serviceClassList:
120 sliceInfo.append({'serviceClass':entry.name})
121 return sliceInfo
122
123def getImageInfo(user):
124 imageList = Image.objects.all()
125 imageInfo = []
126 for imageEntry in imageList:
127 imageInfo.append({'Image':imageEntry.name})
128 return imageInfo
129
130def getNetworkInfo(user):
131 #networkList = Network.objects.all()
132 networkList = ['Private Only','Private and Publicly Routable']
133 networkInfo = []
134 for networkEntry in networkList:
135 #networkInfo.append({'Network':networkEntry.name})
136 networkInfo.append({'Network':networkEntry})
137 return networkInfo
138
139def getDeploymentSites():
140 deploymentList = Deployment.objects.all()
141 deploymentInfo = []
142 for entry in deploymentList:
143 deploymentInfo.append({'DeploymentSite':entry.name})
144 return deploymentInfo
145
Scott Baker771819b2014-03-19 22:10:17 -0700146def getSliceInfo(user):
Siobhan Tully06d2c032014-02-23 23:48:52 -0500147 sliceList = Slice.objects.all()
Scott Baker771819b2014-03-19 22:10:17 -0700148 slicePrivs = SlicePrivilege.objects.filter(user=user)
Siobhan Tully06d2c032014-02-23 23:48:52 -0500149 userSliceInfo = []
150 for entry in slicePrivs:
Siobhan Tullye18b3442014-02-23 14:23:34 -0500151
Scott Baker771819b2014-03-19 22:10:17 -0700152 slicename = Slice.objects.get(id=entry.slice.id).name
Scott Baker537d8232014-04-08 23:40:56 -0700153 slice = Slice.objects.get(name=Slice.objects.get(id=entry.slice.id).name)
154 sliverList=Sliver.objects.all()
155 sites_used = {}
156 for sliver in slice.slivers.all():
157 #sites_used['deploymentSites'] = sliver.node.deployment.name
158 # sites_used[sliver.image.name] = sliver.image.name
159 sites_used[sliver.node.site.name] = sliver.numberCores
Scott Baker771819b2014-03-19 22:10:17 -0700160 sliceid = Slice.objects.get(id=entry.slice.id).id
Siobhan Tully06d2c032014-02-23 23:48:52 -0500161 try:
Scott Baker771819b2014-03-19 22:10:17 -0700162 sliverList = Sliver.objects.filter(slice=entry.slice.id)
163 siteList = {}
164 for x in sliverList:
165 if x.node.site not in siteList:
166 siteList[x.node.site] = 1
167 slivercount = len(sliverList)
168 sitecount = len(siteList)
Siobhan Tully06d2c032014-02-23 23:48:52 -0500169 except:
Scott Baker771819b2014-03-19 22:10:17 -0700170 traceback.print_exc()
171 slivercount = 0
172 sitecount = 0
Siobhan Tully06d2c032014-02-23 23:48:52 -0500173
Scott Baker771819b2014-03-19 22:10:17 -0700174 userSliceInfo.append({'slicename': slicename, 'sliceid':sliceid,
Scott Baker537d8232014-04-08 23:40:56 -0700175 'sitesUsed':sites_used,
176 'role': SliceRole.objects.get(id=entry.role.id).role,
177 'slivercount': slivercount,
178 'sitecount':sitecount})
Scott Baker771819b2014-03-19 22:10:17 -0700179
Siobhan Tully06d2c032014-02-23 23:48:52 -0500180 return userSliceInfo
181
Siobhan Tully06d2c032014-02-23 23:48:52 -0500182def getCDNOperatorData(randomizeData = False):
Scott Baker771819b2014-03-19 22:10:17 -0700183 return hpc_wizard.get_hpc_wizard().get_sites_for_view()
Siobhan Tully06d2c032014-02-23 23:48:52 -0500184
Scott Baker771819b2014-03-19 22:10:17 -0700185def getPageSummary(request):
186 slice = request.GET.get('slice', None)
187 site = request.GET.get('site', None)
188 node = request.GET.get('node', None)
Siobhan Tully06d2c032014-02-23 23:48:52 -0500189
190
Scott Baker771819b2014-03-19 22:10:17 -0700191class SimulatorView(View):
192 def get(self, request, **kwargs):
193 sim = json.loads(file("/tmp/simulator.json","r").read())
194 text = "<html><head></head><body>"
195 text += "Iteration: %d<br>" % sim["iteration"]
196 text += "Elapsed since report %d<br><br>" % sim["elapsed_since_report"]
197 text += "<table border=1>"
198 text += "<tr><th>site</th><th>trend</th><th>weight</th><th>bytes_sent</th><th>hot</th></tr>"
199 for site in sim["site_load"].values():
200 text += "<tr>"
201 text += "<td>%s</td><td>%0.2f</td><td>%0.2f</td><td>%d</td><td>%0.2f</td>" % \
202 (site["name"], site["trend"], site["weight"], site["bytes_sent"], site["load_frac"])
203 text += "</tr>"
204 text += "</table>"
205 text += "</body></html>"
206 return HttpResponse(text)
207
208class DashboardUserSiteView(View):
209 def get(self, request, **kwargs):
210 return HttpResponse(json.dumps(getUserSliceInfo(request.user, True)), mimetype='application/javascript')
211
Scott Baker537d8232014-04-08 23:40:56 -0700212class TenantViewData(View):
213 def get(self, request, **kwargs):
214 return HttpResponse(json.dumps(getTenantSliceInfo(request.user, True)), mimetype='application/javascript')
215
Scott Baker771819b2014-03-19 22:10:17 -0700216class DashboardSummaryAjaxView(View):
217 def get(self, request, **kwargs):
218 return HttpResponse(json.dumps(hpc_wizard.get_hpc_wizard().get_summary_for_view()), mimetype='application/javascript')
219
220class DashboardAddOrRemoveSliverView(View):
221 def post(self, request, *args, **kwargs):
222 siteName = request.POST.get("site", "0")
223 actionToDo = request.POST.get("actionToDo", "0")
224
225 if (actionToDo == "add"):
226 hpc_wizard.get_hpc_wizard().increase_slivers(siteName, 1)
227 elif (actionToDo == "rem"):
228 hpc_wizard.get_hpc_wizard().decrease_slivers(siteName, 1)
229
230 print '*' * 50
231 print 'Ask for site: ' + siteName + ' to ' + actionToDo + ' another HPC Sliver'
232 return HttpResponse('This is POST request ')
Siobhan Tully06d2c032014-02-23 23:48:52 -0500233
234class DashboardAjaxView(View):
235 def get(self, request, **kwargs):
236 return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript')
Scott Baker771819b2014-03-19 22:10:17 -0700237
238class DashboardAnalyticsAjaxView(View):
239 def get(self, request, name="hello_world", **kwargs):
240 if (name == "hpcSummary"):
241 return HttpResponse(json.dumps(hpc_wizard.get_hpc_wizard().get_summary_for_view()), mimetype='application/javascript')
242 elif (name == "hpcUserSite"):
243 return HttpResponse(json.dumps(getUserSliceInfo(request.user, True)), mimetype='application/javascript')
244 elif (name == "hpcMap"):
245 return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript')
246 elif (name == "bigquery"):
247 (mimetype, data) = DoPlanetStackAnalytics(request)
248 return HttpResponse(data, mimetype=mimetype)
249 else:
250 return HttpResponse(json.dumps("Unknown"), mimetype='application/javascript')
251