2 from django.template import RequestContext
3 from django.shortcuts import render_to_response
5 from unfold.loginrequired import LoginRequiredAutoLogoutView
7 from unfold.page import Page
8 from manifold.core.query import Query, AnalyzedQuery
9 from manifold.manifoldapi import execute_query
11 from ui.topmenu import topmenu_items_live, the_user
13 from plugins.raw import Raw
14 from plugins.stack import Stack
15 from plugins.tabs import Tabs
16 from plugins.querytable import QueryTable
17 from plugins.querygrid import QueryGrid
18 from plugins.queryupdater import QueryUpdater
19 from plugins.googlemap import GoogleMap
20 from plugins.senslabmap import SensLabMap
21 from plugins.scheduler import Scheduler
22 from plugins.querycode import QueryCode
24 # stay away from query editor for now as it seems to make things go very slow
25 # see https://lists.myslice.info/pipermail/devel-myslice/2013-December/000221.html
26 #from plugins.query_editor import QueryEditor
27 from plugins.active_filters import ActiveFilters
28 from plugins.quickfilter import QuickFilter
29 from plugins.messages import Messages
30 from plugins.slicestat import SliceStat
32 from myslice.config import Config
34 tmp_default_slice='ple.upmc.myslicedemo'
36 # temporary : turn off the users part to speed things up
49 class SliceView (LoginRequiredAutoLogoutView):
51 def get (self,request, slicename=tmp_default_slice):
54 page.add_css_files ('css/slice-view.css')
55 page.add_js_files ( [ "js/common.functions.js" ] )
56 page.add_js_chunks ('$(function() { messages.debug("sliceview: jQuery version " + $.fn.jquery); });')
57 page.add_js_chunks ('$(function() { messages.debug("sliceview: users turned %s"); });'%("on" if do_query_users else "off"))
58 page.add_js_chunks ('$(function() { messages.debug("sliceview: leases turned %s"); });'%("on" if do_query_leases else "off"))
60 page.add_js_chunks ('$(function() { messages.debug("manifold URL %s"); });'%(config.manifold_url()))
62 metadata = page.get_metadata()
63 resource_md = metadata.details_by_object('resource')
64 resource_fields = [column['name'] for column in resource_md['column']]
66 user_md = metadata.details_by_object('user')
67 user_fields = ['user_hrn'] # [column['name'] for column in user_md['column']]
69 # TODO The query to run is embedded in the URL
70 main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
73 #'resource.hrn', 'resource.urn',
74 'resource.hostname', 'resource.type',
75 'resource.network_hrn',
78 #'application.measurement_point.counter'
80 # for internal use in the querytable plugin;
81 # needs to be a unique column present for each returned record
82 main_query_init_key = 'hostname'
84 query_resource_all = Query.get('resource').select(resource_fields)
86 aq = AnalyzedQuery(main_query, metadata=metadata)
87 page.enqueue_query(main_query, analyzed_query=aq)
88 page.enqueue_query(query_resource_all)
90 # Required: the user must have an authority in its user.config
91 # XXX Temporary solution
92 user_query = Query().get('local:user').select('config','email')
93 user_details = execute_query(self.request, user_query)
95 # not always found in user_details...
97 # for user_detail in user_details:
98 # #email = user_detail['email']
99 # if user_detail['config']:
100 # config = json.loads(user_detail['config'])
101 # user_detail['authority'] = config.get('authority',"Unknown Authority")
103 # if user_detail['authority'] is not None:
104 # sub_authority = user_detail['authority'].split('.')
105 # root_authority = sub_authority[0]
106 # query_user_all = Query.get(root_authority+':user').select(user_fields)
108 # # XXX TODO this filter doesn't work - to be improved in Manifold
109 # #.filter_by('authority.authority_hrn', '=', user_detail['authority'])
111 # page.enqueue_query(query_user_all)
113 # print "authority of the user is not in local:user db"
114 query_user_all = Query.get('user').select(user_fields)
115 # query_user_all = None
117 # ... and for the relations
118 # XXX Let's hardcode resources for now
119 sq_resource = aq.subquery('resource')
120 sq_user = aq.subquery('user')
121 sq_lease = aq.subquery('lease')
122 sq_measurement = aq.subquery('measurement')
125 # Prepare the display according to all metadata
126 # (some parts will be pending, others can be triggered by users).
128 # For example slice measurements will not be requested by default...
130 # Create the base layout (Stack)...
133 title="Slice %s"%slicename,
137 # ... responsible for the slice properties...
144 html="<h2 class='well well-lg'> Slice %s</h2>"%slicename)
147 # --------------------------------------------------------------------------
148 # QueryUpdater (Pending Operations)
150 main_stack.insert(QueryUpdater(
152 title = 'Pending operations',
155 # start turned off, it will open up itself when stuff comes in
158 outline_complete = True,
161 # --------------------------------------------------------------------------
164 # turn off for now -- see above
165 # filter_query_editor = QueryEditor(
167 # query = sq_resource,
168 # query_all = query_resource_all,
169 # title = "Select Columns",
170 # domid = 'select-columns',
172 filter_active_filters = ActiveFilters(
175 title = "Active Filters",
177 filters_area = Stack(
179 title = 'Filter Resources',
181 sons = [# filter_query_editor,
182 filter_active_filters],
184 toggled = 'persistent',
185 outline_complete = True,
187 main_stack.insert (filters_area)
189 # --------------------------------------------------------------------------
191 # the resources part is made of a Tabs (Geographic, List),
193 resources_as_gmap = GoogleMap(
195 title = 'Geographic view',
196 domid = 'resources-map',
197 # tab's sons preferably turn this off
200 query_all = query_resource_all,
201 # this key is the one issued by google
202 googlemap_api_key = Config().googlemap_api_key(),
203 # the key to use at init-time
204 init_key = main_query_init_key,
212 resources_as_3dmap = SensLabMap(
215 domid = 'senslabmap',
217 query_all = query_resource_all,
220 resources_as_list = QueryTable(
222 domid = 'resources-list',
224 # this is the query at the core of the slice list
226 query_all = query_resource_all,
227 init_key = main_query_init_key,
229 datatables_options = {
230 'iDisplayLength': 25,
231 'bLengthChange' : True,
237 resources_as_grid = QueryGrid(
239 domid = 'resources-grid',
241 # this is the query at the core of the slice list
243 query_all = query_resource_all,
244 init_key = main_query_init_key,
249 resources_as_scheduler = Scheduler(
254 query_all_resources = query_resource_all,
255 query_lease = sq_lease,
258 # with the new 'Filter' stuff on top, no need for anything but the querytable
259 resources_as_list_area = resources_as_list
264 resources_as_scheduler,
265 resources_as_list_area,
266 ] if do_query_leases else [
269 resources_as_list_area,
272 resources_sons.append(resources_as_grid)
274 resources_area = Tabs ( page=page,
278 outline_complete=True,
279 sons= resources_sons,
280 active_domid = 'resources-map',
281 persistent_active=True,
283 main_stack.insert (resources_area)
285 # --------------------------------------------------------------------------
288 if do_query_users and query_user_all is not None:
292 outline_complete = True,
295 active_domid = 'users-list',
297 main_stack.insert(tab_users)
299 tab_users.insert(QueryTable(
301 title = 'Users List',
302 domid = 'users-list',
303 # tab's sons preferably turn this off
305 # this is the query at the core of the slice list
307 query_all = query_user_all,
309 datatables_options = {
310 'iDisplayLength' : 25,
311 'bLengthChange' : True,
317 # --------------------------------------------------------------------------
319 measurements_stats_cpu = SliceStat(
321 domid = 'resources-stats-cpu',
326 slicename = slicename,
330 measurements_stats_mem = SliceStat(
331 title = "Memory Usage",
332 domid = 'resources-stats-mem',
337 slicename = slicename,
341 measurements_stats_asb = SliceStat(
342 title = "Traffic Sent",
343 domid = 'resources-stats-asb',
348 slicename = slicename,
352 measurements_stats_arb = SliceStat(
353 title = "Traffic Received",
354 domid = 'resources-stats-arb',
359 slicename = slicename,
363 tab_measurements = Tabs ( page=page,
364 domid="measurements",
367 title="Measurements",
368 outline_complete=True,
369 sons=[ measurements_stats_cpu, measurements_stats_mem, measurements_stats_asb, measurements_stats_arb ],
370 active_domid = 'measurements_stats_cpu',
372 main_stack.insert (tab_measurements)
374 # tab_measurements = Tabs (
376 # active_domid = 'measurements-list',
377 # outline_complete = True,
379 # title = 'Measurements',
380 # domid = 'measurements',
382 # main_stack.insert(tab_measurements)
384 # tab_measurements.insert(QueryTable(
386 # title = 'Measurements',
387 # domid = 'measurements-list',
388 # # tab's sons preferably turn this off
390 # # this is the query at the core of the slice list
391 # query = sq_measurement,
392 # # do NOT set checkboxes to False
393 # # this table being otherwise empty, it just does not fly with dataTables
395 # datatables_options = {
396 # 'iDisplayLength' : 25,
397 # 'bLengthChange' : True,
398 # 'bAutoWidth' : True,
402 # # --------------------------------------------------------------------------
403 # # MESSAGES (we use transient=False for now)
405 main_stack.insert(Messages(
407 title = "Runtime messages for slice %s"%slicename,
410 # plain messages are probably less nice for production but more reliable for development for now
412 # these make sense only in non-transient mode..
414 toggled = 'persistent',
415 outline_complete = True,
418 # variables that will get passed to the view-unfold1.html template
421 # define 'unfold_main' to the template engine - the main contents
422 template_env [ 'unfold_main' ] = main_stack.render(request)
424 # more general variables expected in the template
425 template_env [ 'title' ] = '%(slicename)s'%locals()
426 # the menu items on the top
427 template_env [ 'topmenu_items' ] = topmenu_items_live('Slice', page)
428 # so we can sho who is logged
429 template_env [ 'username' ] = the_user (request)
431 # don't forget to run the requests
432 page.expose_js_metadata()
433 # the prelude object in page contains a summary of the requirements() for all plugins
434 # define {js,css}_{files,chunks}
435 template_env.update(page.prelude_env())
437 return render_to_response ('view-unfold1.html',template_env,
438 context_instance=RequestContext(request))