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,
159 googlemap_api_key = Config().googlemap_api_key(),
167 resources_as_3dmap = SensLabMap(
170 domid = 'senslabmap',
172 query_all = query_resource_all,
175 resources_as_list = QueryTable(
177 domid = 'resources-list',
179 # this is the query at the core of the slice list
181 query_all = query_resource_all,
183 datatables_options = {
184 'iDisplayLength': 25,
185 'bLengthChange' : True,
190 # with the new 'Filter' stuff on top, no need for anything but the querytable
191 resources_as_list_area = resources_as_list
193 resources_area = Tabs ( page=page,
197 outline_complete=True,
198 sons=[ resources_as_gmap,
200 resources_as_list_area, ],
201 active_domid = 'resources-map',
202 persistent_active=True,
204 main_stack.insert (resources_area)
206 # --------------------------------------------------------------------------
213 outline_complete = True,
216 active_domid = 'users-list',
218 main_stack.insert(tab_users)
220 tab_users.insert(QueryTable(
222 title = 'Users List',
223 domid = 'users-list',
224 # tab's sons preferably turn this off
226 # this is the query at the core of the slice list
228 query_all = query_user_all,
230 datatables_options = {
231 'iDisplayLength' : 25,
232 'bLengthChange' : True,
238 # --------------------------------------------------------------------------
240 measurements_stats_cpu = SliceStat(
242 domid = 'resources-stats-cpu',
247 slicename = slicename,
251 measurements_stats_mem = SliceStat(
252 title = "Memory Usage",
253 domid = 'resources-stats-mem',
258 slicename = slicename,
262 measurements_stats_asb = SliceStat(
263 title = "Traffic Sent",
264 domid = 'resources-stats-asb',
269 slicename = slicename,
273 measurements_stats_arb = SliceStat(
274 title = "Traffic Received",
275 domid = 'resources-stats-arb',
280 slicename = slicename,
284 tab_measurements = Tabs ( page=page,
285 domid="measurements",
288 title="Measurements",
289 outline_complete=True,
290 sons=[ measurements_stats_cpu, measurements_stats_mem, measurements_stats_asb, measurements_stats_arb ],
291 active_domid = 'measurements_stats_cpu',
293 main_stack.insert (tab_measurements)
295 # tab_measurements = Tabs (
297 # active_domid = 'measurements-list',
298 # outline_complete = True,
300 # title = 'Measurements',
301 # domid = 'measurements',
303 # main_stack.insert(tab_measurements)
305 # tab_measurements.insert(QueryTable(
307 # title = 'Measurements',
308 # domid = 'measurements-list',
309 # # tab's sons preferably turn this off
311 # # this is the query at the core of the slice list
312 # query = sq_measurement,
313 # # do NOT set checkboxes to False
314 # # this table being otherwise empty, it just does not fly with dataTables
316 # datatables_options = {
317 # 'iDisplayLength' : 25,
318 # 'bLengthChange' : True,
319 # 'bAutoWidth' : True,
323 # # --------------------------------------------------------------------------
324 # # MESSAGES (we use transient=False for now)
326 main_stack.insert(Messages(
328 title = "Runtime messages for slice %s"%slicename,
331 # plain messages are probably less nice for production but more reliable for development for now
333 # these make sense only in non-transient mode..
335 toggled = 'persistent',
336 outline_complete = True,
340 # variables that will get passed to the view-unfold1.html template
343 # define 'unfold_main' to the template engine - the main contents
344 template_env [ 'unfold_main' ] = main_stack.render(request)
346 # more general variables expected in the template
347 template_env [ 'title' ] = '%(slicename)s'%locals()
348 # the menu items on the top
349 template_env [ 'topmenu_items' ] = topmenu_items('Slice', request)
350 # so we can sho who is logged
351 template_env [ 'username' ] = the_user (request)
353 # don't forget to run the requests
354 page.expose_queries ()
356 # xxx create another plugin with the same query and a different layout (with_datatables)
357 # show that it worls as expected, one single api call to backend and 2 refreshed views
359 # the prelude object in page contains a summary of the requirements() for all plugins
360 # define {js,css}_{files,chunks}
361 prelude_env = page.prelude_env()
362 template_env.update(prelude_env)
363 result=render_to_response ('view-unfold1.html',template_env,
364 context_instance=RequestContext(request))