blob: a50d9462dfd74a457e6cfe26bf5be7af9ea7ee6c [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
8from core.models import Slice,SliceRole,SlicePrivilege,Site,Reservation,Sliver
Siobhan Tully06d2c032014-02-23 23:48:52 -05009from django.http import HttpResponse
Scott Baker771819b2014-03-19 22:10:17 -070010import traceback
Siobhan Tully06d2c032014-02-23 23:48:52 -050011
Scott Baker771819b2014-03-19 22:10:17 -070012if os.path.exists("/home/smbaker/projects/vicci/cdn/bigquery"):
13 sys.path.append("/home/smbaker/projects/vicci/cdn/bigquery")
14else:
15 sys.path.append("/opt/planetstack/hpc_wizard")
16import hpc_wizard
17from planetstack_analytics import DoPlanetStackAnalytics
Siobhan Tullycf04fb62014-01-11 11:25:57 -050018
19class DashboardWelcomeView(TemplateView):
20 template_name = 'admin/dashboard/welcome.html'
21
22 def get(self, request, *args, **kwargs):
23 context = self.get_context_data(**kwargs)
Scott Baker771819b2014-03-19 22:10:17 -070024 userDetails = getUserSliceInfo(request.user)
25 #context['site'] = userDetails['site']
Siobhan Tullycf04fb62014-01-11 11:25:57 -050026
Scott Baker771819b2014-03-19 22:10:17 -070027 context['userSliceInfo'] = userDetails['userSliceInfo']
28 context['cdnData'] = userDetails['cdnData']
Siobhan Tullycf04fb62014-01-11 11:25:57 -050029 return self.render_to_response(context=context)
Siobhan Tullye18b3442014-02-23 14:23:34 -050030
Scott Baker771819b2014-03-19 22:10:17 -070031def getUserSliceInfo(user, tableFormat = False):
32 userDetails = {}
33# try:
34# // site = Site.objects.filter(id=user.site.id)
35# // except:
36# // site = Site.objects.filter(name="Princeton")
37# // userDetails['sitename'] = site[0].name
38# // userDetails['siteid'] = site[0].id
39
40 userSliceData = getSliceInfo(user)
41 if (tableFormat):
42# pprint("******* GET USER SLICE INFO")
43 userDetails['userSliceInfo'] = userSliceTableFormatter(userSliceData)
44 else:
45 userDetails['userSliceInfo'] = userSliceData
46 userDetails['cdnData'] = getCDNOperatorData();
47# pprint( userDetails)
48 return userDetails
49
50def userSliceTableFormatter(data):
51# pprint(data)
52 formattedData = {
53 'rows' : data
54 }
55 return formattedData
56
57def getSliceInfo(user):
Siobhan Tully06d2c032014-02-23 23:48:52 -050058 sliceList = Slice.objects.all()
Scott Baker771819b2014-03-19 22:10:17 -070059 slicePrivs = SlicePrivilege.objects.filter(user=user)
Siobhan Tully06d2c032014-02-23 23:48:52 -050060 userSliceInfo = []
61 for entry in slicePrivs:
Siobhan Tullye18b3442014-02-23 14:23:34 -050062
Scott Baker771819b2014-03-19 22:10:17 -070063 slicename = Slice.objects.get(id=entry.slice.id).name
64 sliceid = Slice.objects.get(id=entry.slice.id).id
Siobhan Tully06d2c032014-02-23 23:48:52 -050065 try:
Scott Baker771819b2014-03-19 22:10:17 -070066 sliverList = Sliver.objects.filter(slice=entry.slice.id)
67 siteList = {}
68 for x in sliverList:
69 if x.node.site not in siteList:
70 siteList[x.node.site] = 1
71 slivercount = len(sliverList)
72 sitecount = len(siteList)
Siobhan Tully06d2c032014-02-23 23:48:52 -050073 except:
Scott Baker771819b2014-03-19 22:10:17 -070074 traceback.print_exc()
75 slivercount = 0
76 sitecount = 0
Siobhan Tully06d2c032014-02-23 23:48:52 -050077
Scott Baker771819b2014-03-19 22:10:17 -070078 userSliceInfo.append({'slicename': slicename, 'sliceid':sliceid,
79 'role': SliceRole.objects.get(id=entry.role.id).role, 'slivercount': slivercount, 'sitecount':sitecount})
80
Siobhan Tully06d2c032014-02-23 23:48:52 -050081 return userSliceInfo
82
Siobhan Tully06d2c032014-02-23 23:48:52 -050083def getCDNOperatorData(randomizeData = False):
Scott Baker771819b2014-03-19 22:10:17 -070084 return hpc_wizard.get_hpc_wizard().get_sites_for_view()
Siobhan Tully06d2c032014-02-23 23:48:52 -050085
Scott Baker771819b2014-03-19 22:10:17 -070086def getPageSummary(request):
87 slice = request.GET.get('slice', None)
88 site = request.GET.get('site', None)
89 node = request.GET.get('node', None)
Siobhan Tully06d2c032014-02-23 23:48:52 -050090
91
Scott Baker771819b2014-03-19 22:10:17 -070092class SimulatorView(View):
93 def get(self, request, **kwargs):
94 sim = json.loads(file("/tmp/simulator.json","r").read())
95 text = "<html><head></head><body>"
96 text += "Iteration: %d<br>" % sim["iteration"]
97 text += "Elapsed since report %d<br><br>" % sim["elapsed_since_report"]
98 text += "<table border=1>"
99 text += "<tr><th>site</th><th>trend</th><th>weight</th><th>bytes_sent</th><th>hot</th></tr>"
100 for site in sim["site_load"].values():
101 text += "<tr>"
102 text += "<td>%s</td><td>%0.2f</td><td>%0.2f</td><td>%d</td><td>%0.2f</td>" % \
103 (site["name"], site["trend"], site["weight"], site["bytes_sent"], site["load_frac"])
104 text += "</tr>"
105 text += "</table>"
106 text += "</body></html>"
107 return HttpResponse(text)
108
109class DashboardUserSiteView(View):
110 def get(self, request, **kwargs):
111 return HttpResponse(json.dumps(getUserSliceInfo(request.user, True)), mimetype='application/javascript')
112
113class DashboardSummaryAjaxView(View):
114 def get(self, request, **kwargs):
115 return HttpResponse(json.dumps(hpc_wizard.get_hpc_wizard().get_summary_for_view()), mimetype='application/javascript')
116
117class DashboardAddOrRemoveSliverView(View):
118 def post(self, request, *args, **kwargs):
119 siteName = request.POST.get("site", "0")
120 actionToDo = request.POST.get("actionToDo", "0")
121
122 if (actionToDo == "add"):
123 hpc_wizard.get_hpc_wizard().increase_slivers(siteName, 1)
124 elif (actionToDo == "rem"):
125 hpc_wizard.get_hpc_wizard().decrease_slivers(siteName, 1)
126
127 print '*' * 50
128 print 'Ask for site: ' + siteName + ' to ' + actionToDo + ' another HPC Sliver'
129 return HttpResponse('This is POST request ')
Siobhan Tully06d2c032014-02-23 23:48:52 -0500130
131class DashboardAjaxView(View):
132 def get(self, request, **kwargs):
133 return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript')
Scott Baker771819b2014-03-19 22:10:17 -0700134
135class DashboardAnalyticsAjaxView(View):
136 def get(self, request, name="hello_world", **kwargs):
137 if (name == "hpcSummary"):
138 return HttpResponse(json.dumps(hpc_wizard.get_hpc_wizard().get_summary_for_view()), mimetype='application/javascript')
139 elif (name == "hpcUserSite"):
140 return HttpResponse(json.dumps(getUserSliceInfo(request.user, True)), mimetype='application/javascript')
141 elif (name == "hpcMap"):
142 return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript')
143 elif (name == "bigquery"):
144 (mimetype, data) = DoPlanetStackAnalytics(request)
145 return HttpResponse(data, mimetype=mimetype)
146 else:
147 return HttpResponse(json.dumps("Unknown"), mimetype='application/javascript')
148