1 from django.template import RequestContext
2 from django.shortcuts import render_to_response
4 from unfold.loginrequired import LoginRequiredAutoLogoutView
6 from unfold.page import Page
7 from manifold.core.query import Query, AnalyzedQuery
9 from ui.topmenu import topmenu_items, the_user
11 from plugins.raw import Raw
12 from plugins.stack import Stack
13 from plugins.tabs import Tabs
14 from plugins.querytable import QueryTable
15 from plugins.queryupdater import QueryUpdater
16 from plugins.googlemap import GoogleMap
17 from plugins.senslabmap import SensLabMap
18 from plugins.querycode import QueryCode
19 from plugins.query_editor import QueryEditor
20 from plugins.active_filters import ActiveFilters
21 from plugins.quickfilter import QuickFilter
22 from plugins.messages import Messages
23 from plugins.slicestat import SliceStat
25 from myslice.config import Config
27 tmp_default_slice='ple.upmc.myslicedemo'
29 # temporary : turn off the users part to speed things up
36 class SliceView (LoginRequiredAutoLogoutView):
38 def get (self,request, slicename=tmp_default_slice):
41 page.add_css_files ('css/slice-view.css')
42 page.add_js_files ( [ "js/common.functions.js" ] )
43 page.add_js_chunks ('$(function() { messages.debug("sliceview: jQuery version " + $.fn.jquery); });')
44 page.add_js_chunks ('$(function() { messages.debug("sliceview: users turned %s"); });'%("on" if do_query_users else "off"))
46 page.add_js_chunks ('$(function() { messages.debug("manifold URL %s"); });'%(config.manifold_url()))
47 page.expose_js_metadata()
49 metadata = page.get_metadata()
50 resource_md = metadata.details_by_object('resource')
51 resource_fields = [column['name'] for column in resource_md['column']]
53 user_md = metadata.details_by_object('user')
54 user_fields = ['user_hrn'] # [column['name'] for column in user_md['column']]
56 # TODO The query to run is embedded in the URL
57 main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
60 'resource.hrn', 'resource.hostname', 'resource.type',
61 'resource.network_hrn',
64 #'application.measurement_point.counter'
67 query_resource_all = Query.get('resource').select(resource_fields)
69 query_user_all = Query.get('user').select(user_fields)
71 aq = AnalyzedQuery(main_query, metadata=metadata)
72 page.enqueue_query(main_query, analyzed_query=aq)
73 page.enqueue_query(query_resource_all)
75 page.enqueue_query(query_user_all)
77 # ... and for the relations
78 # XXX Let's hardcode resources for now
79 sq_resource = aq.subquery('resource')
80 sq_user = aq.subquery('user')
81 sq_lease = aq.subquery('lease')
82 sq_measurement = aq.subquery('measurement')
85 # Prepare the display according to all metadata
86 # (some parts will be pending, others can be triggered by users).
88 # For example slice measurements will not be requested by default...
90 # Create the base layout (Stack)...
93 title="Slice %s"%slicename,
97 # ... responsible for the slice properties...
104 html="<h2 class='well well-lg'> Slice %s</h2>"%slicename)
107 # --------------------------------------------------------------------------
108 # QueryUpdater (Pending Operations)
110 main_stack.insert(QueryUpdater(
112 title = 'Pending operations',
115 # start turned off, it will open up itself when stuff comes in
118 outline_complete = True,
121 # --------------------------------------------------------------------------
124 filter_query_editor = QueryEditor(
127 query_all = query_resource_all,
128 title = "Select Columns",
129 domid = 'select-columns',
131 filter_active_filters = ActiveFilters(
134 title = "Active Filters",
136 filters_area = Stack(
138 title = 'Filter Resources',
140 sons = [filter_query_editor, filter_active_filters],
142 toggled = 'persistent',
143 outline_complete = True,
145 main_stack.insert (filters_area)
147 # --------------------------------------------------------------------------
149 # the resources part is made of a Tabs (Geographic, List),
151 resources_as_gmap = GoogleMap(
153 title = 'Geographic view',
154 domid = 'resources-map',
155 # tab's sons preferably turn this off
158 query_all = query_resource_all,
166 # because (senslab)map.css changes stuff for *all* canvas elements
167 # it destroys the googlemap behaviour
168 # turning this off for now until Anthony can fix that
169 # resources_as_3dmap = SensLabMap(
172 # domid = 'senslabmap',
173 # query = sq_resource,
174 # query_all = query_resource_all,
177 resources_as_list = QueryTable(
179 domid = 'resources-list',
181 # this is the query at the core of the slice list
183 query_all = query_resource_all,
185 datatables_options = {
186 'iDisplayLength': 25,
187 'bLengthChange' : True,
192 # with the new 'Filter' stuff on top, no need for anything but the querytable
193 resources_as_list_area = resources_as_list
195 resources_area = Tabs ( page=page,
199 outline_complete=True,
200 sons=[ resources_as_gmap,
202 # resources_as_3dmap,
203 resources_as_list_area, ],
204 active_domid = 'resources-map',
206 main_stack.insert (resources_area)
208 # --------------------------------------------------------------------------
215 outline_complete = True,
218 active_domid = 'users-list',
220 main_stack.insert(tab_users)
222 tab_users.insert(QueryTable(
224 title = 'Users List',
225 domid = 'users-list',
226 # tab's sons preferably turn this off
228 # this is the query at the core of the slice list
230 query_all = query_user_all,
232 datatables_options = {
233 'iDisplayLength' : 25,
234 'bLengthChange' : True,
240 # --------------------------------------------------------------------------
242 measurements_stats_cpu = SliceStat(
244 domid = 'resources-stats-cpu',
249 slicename = slicename,
253 measurements_stats_mem = SliceStat(
254 title = "Memory Usage",
255 domid = 'resources-stats-mem',
260 slicename = slicename,
264 measurements_stats_asb = SliceStat(
265 title = "Traffic Sent",
266 domid = 'resources-stats-asb',
271 slicename = slicename,
275 measurements_stats_arb = SliceStat(
276 title = "Traffic Received",
277 domid = 'resources-stats-arb',
282 slicename = slicename,
286 tab_measurements = Tabs ( page=page,
287 domid="measurements",
290 title="Measurements",
291 outline_complete=True,
292 sons=[ measurements_stats_cpu, measurements_stats_mem, measurements_stats_asb, measurements_stats_arb ],
293 active_domid = 'measurements_stats_cpu',
295 main_stack.insert (tab_measurements)
297 # tab_measurements = Tabs (
299 # active_domid = 'measurements-list',
300 # outline_complete = True,
302 # title = 'Measurements',
303 # domid = 'measurements',
305 # main_stack.insert(tab_measurements)
307 # tab_measurements.insert(QueryTable(
309 # title = 'Measurements',
310 # domid = 'measurements-list',
311 # # tab's sons preferably turn this off
313 # # this is the query at the core of the slice list
314 # query = sq_measurement,
315 # # do NOT set checkboxes to False
316 # # this table being otherwise empty, it just does not fly with dataTables
318 # datatables_options = {
319 # 'iDisplayLength' : 25,
320 # 'bLengthChange' : True,
321 # 'bAutoWidth' : True,
325 # # --------------------------------------------------------------------------
326 # # MESSAGES (we use transient=False for now)
328 main_stack.insert(Messages(
330 title = "Runtime messages for slice %s"%slicename,
333 # plain messages are probably less nice for production but more reliable for development for now
335 # these make sense only in non-transient mode..
337 toggled = 'persistent',
338 outline_complete = True,
342 # variables that will get passed to the view-unfold1.html template
345 # define 'unfold_main' to the template engine - the main contents
346 template_env [ 'unfold_main' ] = main_stack.render(request)
348 # more general variables expected in the template
349 template_env [ 'title' ] = '%(slicename)s'%locals()
350 # the menu items on the top
351 template_env [ 'topmenu_items' ] = topmenu_items('Slice', request)
352 # so we can sho who is logged
353 template_env [ 'username' ] = the_user (request)
355 # don't forget to run the requests
356 page.expose_queries ()
358 # xxx create another plugin with the same query and a different layout (with_datatables)
359 # show that it worls as expected, one single api call to backend and 2 refreshed views
361 # the prelude object in page contains a summary of the requirements() for all plugins
362 # define {js,css}_{files,chunks}
363 prelude_env = page.prelude_env()
364 template_env.update(prelude_env)
365 result=render_to_response ('view-unfold1.html',template_env,
366 context_instance=RequestContext(request))