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',
75 'resource.hostname', 'resource.type',
76 'resource.network_hrn',
79 #'application.measurement_point.counter'
81 # for internal use in the querytable plugin;
82 # needs to be a unique column present for each returned record
83 main_query_init_key = 'hostname'
85 query_resource_all = Query.get('resource').select(resource_fields)
87 aq = AnalyzedQuery(main_query, metadata=metadata)
88 page.enqueue_query(main_query, analyzed_query=aq)
89 page.enqueue_query(query_resource_all)
91 # Required: the user must have an authority in its user.config
92 # XXX Temporary solution
93 user_query = Query().get('local:user').select('config','email')
94 user_details = execute_query(self.request, user_query)
96 # not always found in user_details...
98 for user_detail in user_details:
99 #email = user_detail['email']
100 if user_detail['config']:
101 config = json.loads(user_detail['config'])
102 user_detail['authority'] = config.get('authority',"Unknown Authority")
104 if user_detail['authority'] is not None:
105 sub_authority = user_detail['authority'].split('.')
106 root_authority = sub_authority[0]
107 query_user_all = Query.get(root_authority+':user').select(user_fields)
109 # XXX TODO this filter doesn't work - to be improved in Manifold
110 #.filter_by('authority.authority_hrn', '=', user_detail['authority'])
112 page.enqueue_query(query_user_all)
114 print "authority of the user is not in local:user db"
115 query_user_all = Query.get('user').select(user_fields)
116 # query_user_all = None
118 # ... and for the relations
119 # XXX Let's hardcode resources for now
120 sq_resource = aq.subquery('resource')
121 sq_user = aq.subquery('user')
122 sq_lease = aq.subquery('lease')
123 sq_measurement = aq.subquery('measurement')
126 # Prepare the display according to all metadata
127 # (some parts will be pending, others can be triggered by users).
129 # For example slice measurements will not be requested by default...
131 # Create the base layout (Stack)...
134 title="Slice %s"%slicename,
138 # ... responsible for the slice properties...
145 html="<h2 class='well well-lg'> Slice %s</h2>"%slicename)
148 # --------------------------------------------------------------------------
149 # QueryUpdater (Pending Operations)
151 main_stack.insert(QueryUpdater(
153 title = 'Pending operations',
156 # start turned off, it will open up itself when stuff comes in
159 outline_complete = True,
162 # --------------------------------------------------------------------------
165 # turn off for now -- see above
166 # filter_query_editor = QueryEditor(
168 # query = sq_resource,
169 # query_all = query_resource_all,
170 # title = "Select Columns",
171 # domid = 'select-columns',
173 filter_active_filters = ActiveFilters(
176 title = "Active Filters",
178 filters_area = Stack(
180 title = 'Filter Resources',
182 sons = [# filter_query_editor,
183 filter_active_filters],
185 toggled = 'persistent',
186 outline_complete = True,
188 main_stack.insert (filters_area)
190 # --------------------------------------------------------------------------
192 # the resources part is made of a Tabs (Geographic, List),
194 resources_as_gmap = GoogleMap(
196 title = 'Geographic view',
197 domid = 'resources-map',
198 # tab's sons preferably turn this off
201 query_all = query_resource_all,
202 # this key is the one issued by google
203 googlemap_api_key = Config().googlemap_api_key(),
204 # the key to use at init-time
205 init_key = main_query_init_key,
213 resources_as_3dmap = SensLabMap(
216 domid = 'senslabmap',
218 query_all = query_resource_all,
221 resources_as_list = QueryTable(
223 domid = 'resources-list',
225 # this is the query at the core of the slice list
227 query_all = query_resource_all,
228 # use 'hrn' as the internal unique key for this plugin
229 init_key = main_query_init_key,
231 datatables_options = {
232 'iDisplayLength': 25,
233 'bLengthChange' : True,
239 resources_as_grid = QueryGrid(
241 domid = 'resources-grid',
243 # this is the query at the core of the slice list
245 query_all = query_resource_all,
246 # use 'hrn' as the internal unique key for this plugin
247 # xxx todo on querygrid as well
248 # init_key = main_query_init_key,
253 resources_as_scheduler = Scheduler(
258 query_all_resources = query_resource_all,
259 query_lease = sq_lease,
262 # with the new 'Filter' stuff on top, no need for anything but the querytable
263 resources_as_list_area = resources_as_list
268 resources_as_scheduler,
269 resources_as_list_area,
270 ] if do_query_leases else [
273 resources_as_list_area,
276 resources_sons.append(resources_as_grid)
278 resources_area = Tabs ( page=page,
282 outline_complete=True,
283 sons= resources_sons,
284 active_domid = 'resources-map',
285 persistent_active=True,
287 main_stack.insert (resources_area)
289 # --------------------------------------------------------------------------
292 if do_query_users and query_user_all is not None:
296 outline_complete = True,
299 active_domid = 'users-list',
301 main_stack.insert(tab_users)
303 tab_users.insert(QueryTable(
305 title = 'Users List',
306 domid = 'users-list',
307 # tab's sons preferably turn this off
309 # this is the query at the core of the slice list
311 query_all = query_user_all,
313 datatables_options = {
314 'iDisplayLength' : 25,
315 'bLengthChange' : True,
321 # --------------------------------------------------------------------------
323 measurements_stats_cpu = SliceStat(
325 domid = 'resources-stats-cpu',
330 slicename = slicename,
334 measurements_stats_mem = SliceStat(
335 title = "Memory Usage",
336 domid = 'resources-stats-mem',
341 slicename = slicename,
345 measurements_stats_asb = SliceStat(
346 title = "Traffic Sent",
347 domid = 'resources-stats-asb',
352 slicename = slicename,
356 measurements_stats_arb = SliceStat(
357 title = "Traffic Received",
358 domid = 'resources-stats-arb',
363 slicename = slicename,
367 tab_measurements = Tabs ( page=page,
368 domid="measurements",
371 title="Measurements",
372 outline_complete=True,
373 sons=[ measurements_stats_cpu, measurements_stats_mem, measurements_stats_asb, measurements_stats_arb ],
374 active_domid = 'measurements_stats_cpu',
376 main_stack.insert (tab_measurements)
378 # tab_measurements = Tabs (
380 # active_domid = 'measurements-list',
381 # outline_complete = True,
383 # title = 'Measurements',
384 # domid = 'measurements',
386 # main_stack.insert(tab_measurements)
388 # tab_measurements.insert(QueryTable(
390 # title = 'Measurements',
391 # domid = 'measurements-list',
392 # # tab's sons preferably turn this off
394 # # this is the query at the core of the slice list
395 # query = sq_measurement,
396 # # do NOT set checkboxes to False
397 # # this table being otherwise empty, it just does not fly with dataTables
399 # datatables_options = {
400 # 'iDisplayLength' : 25,
401 # 'bLengthChange' : True,
402 # 'bAutoWidth' : True,
406 # # --------------------------------------------------------------------------
407 # # MESSAGES (we use transient=False for now)
409 main_stack.insert(Messages(
411 title = "Runtime messages for slice %s"%slicename,
414 # plain messages are probably less nice for production but more reliable for development for now
416 # these make sense only in non-transient mode..
418 toggled = 'persistent',
419 outline_complete = True,
423 # variables that will get passed to the view-unfold1.html template
426 # define 'unfold_main' to the template engine - the main contents
427 template_env [ 'unfold_main' ] = main_stack.render(request)
429 # more general variables expected in the template
430 template_env [ 'title' ] = '%(slicename)s'%locals()
431 # the menu items on the top
432 template_env [ 'topmenu_items' ] = topmenu_items('Slice', request)
433 # so we can sho who is logged
434 template_env [ 'username' ] = the_user (request)
436 # don't forget to run the requests
437 page.expose_queries ()
439 # xxx create another plugin with the same query and a different layout (with_datatables)
440 # show that it worls as expected, one single api call to backend and 2 refreshed views
442 # the prelude object in page contains a summary of the requirements() for all plugins
443 # define {js,css}_{files,chunks}
444 prelude_env = page.prelude_env()
445 template_env.update(prelude_env)
446 result=render_to_response ('view-unfold1.html',template_env,
447 context_instance=RequestContext(request))