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, 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()))
61 page.expose_js_metadata()
63 metadata = page.get_metadata()
64 resource_md = metadata.details_by_object('resource')
65 resource_fields = [column['name'] for column in resource_md['column']]
67 user_md = metadata.details_by_object('user')
68 user_fields = ['user_hrn'] # [column['name'] for column in user_md['column']]
70 # TODO The query to run is embedded in the URL
71 main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
74 'resource.hrn', 'resource.urn', '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_key = 'hrn'
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 googlemap_api_key = Config().googlemap_api_key(),
209 resources_as_3dmap = SensLabMap(
212 domid = 'senslabmap',
214 query_all = query_resource_all,
217 resources_as_list = QueryTable(
219 domid = 'resources-list',
221 # this is the query at the core of the slice list
223 query_all = query_resource_all,
224 # safer to use 'hrn' as the internal unique key for this plugin
225 #id_key = main_query_key,
227 datatables_options = {
228 'iDisplayLength': 25,
229 'bLengthChange' : True,
235 resources_as_grid = QueryGrid(
237 domid = 'resources-grid',
239 # this is the query at the core of the slice list
241 query_all = query_resource_all,
242 # safer to use 'hrn' as the internal unique key for this plugin
243 id_key = main_query_key,
248 resources_as_scheduler = Scheduler(
253 query_all_resources = query_resource_all,
254 query_lease = sq_lease,
257 # with the new 'Filter' stuff on top, no need for anything but the querytable
258 resources_as_list_area = resources_as_list
263 resources_as_scheduler,
264 resources_as_list_area,
265 ] if do_query_leases else [
268 resources_as_list_area,
271 resources_sons.append(resources_as_grid)
273 print 40*'+-',"resources_sons has",len(resources_sons),"son"
275 resources_area = Tabs ( page=page,
279 outline_complete=True,
280 sons= resources_sons,
281 active_domid = 'resources-map',
282 persistent_active=True,
284 main_stack.insert (resources_area)
286 # --------------------------------------------------------------------------
289 if do_query_users and query_user_all is not None:
293 outline_complete = True,
296 active_domid = 'users-list',
298 main_stack.insert(tab_users)
300 tab_users.insert(QueryTable(
302 title = 'Users List',
303 domid = 'users-list',
304 # tab's sons preferably turn this off
306 # this is the query at the core of the slice list
308 query_all = query_user_all,
310 datatables_options = {
311 'iDisplayLength' : 25,
312 'bLengthChange' : True,
318 # --------------------------------------------------------------------------
320 measurements_stats_cpu = SliceStat(
322 domid = 'resources-stats-cpu',
327 slicename = slicename,
331 measurements_stats_mem = SliceStat(
332 title = "Memory Usage",
333 domid = 'resources-stats-mem',
338 slicename = slicename,
342 measurements_stats_asb = SliceStat(
343 title = "Traffic Sent",
344 domid = 'resources-stats-asb',
349 slicename = slicename,
353 measurements_stats_arb = SliceStat(
354 title = "Traffic Received",
355 domid = 'resources-stats-arb',
360 slicename = slicename,
364 tab_measurements = Tabs ( page=page,
365 domid="measurements",
368 title="Measurements",
369 outline_complete=True,
370 sons=[ measurements_stats_cpu, measurements_stats_mem, measurements_stats_asb, measurements_stats_arb ],
371 active_domid = 'measurements_stats_cpu',
373 main_stack.insert (tab_measurements)
375 # tab_measurements = Tabs (
377 # active_domid = 'measurements-list',
378 # outline_complete = True,
380 # title = 'Measurements',
381 # domid = 'measurements',
383 # main_stack.insert(tab_measurements)
385 # tab_measurements.insert(QueryTable(
387 # title = 'Measurements',
388 # domid = 'measurements-list',
389 # # tab's sons preferably turn this off
391 # # this is the query at the core of the slice list
392 # query = sq_measurement,
393 # # do NOT set checkboxes to False
394 # # this table being otherwise empty, it just does not fly with dataTables
396 # datatables_options = {
397 # 'iDisplayLength' : 25,
398 # 'bLengthChange' : True,
399 # 'bAutoWidth' : True,
403 # # --------------------------------------------------------------------------
404 # # MESSAGES (we use transient=False for now)
406 main_stack.insert(Messages(
408 title = "Runtime messages for slice %s"%slicename,
411 # plain messages are probably less nice for production but more reliable for development for now
413 # these make sense only in non-transient mode..
415 toggled = 'persistent',
416 outline_complete = True,
420 # variables that will get passed to the view-unfold1.html template
423 # define 'unfold_main' to the template engine - the main contents
424 template_env [ 'unfold_main' ] = main_stack.render(request)
426 # more general variables expected in the template
427 template_env [ 'title' ] = '%(slicename)s'%locals()
428 # the menu items on the top
429 template_env [ 'topmenu_items' ] = topmenu_items('Slice', request)
430 # so we can sho who is logged
431 template_env [ 'username' ] = the_user (request)
433 # don't forget to run the requests
434 page.expose_queries ()
436 # xxx create another plugin with the same query and a different layout (with_datatables)
437 # show that it worls as expected, one single api call to backend and 2 refreshed views
439 # the prelude object in page contains a summary of the requirements() for all plugins
440 # define {js,css}_{files,chunks}
441 prelude_env = page.prelude_env()
442 template_env.update(prelude_env)
443 result=render_to_response ('view-unfold1.html',template_env,
444 context_instance=RequestContext(request))