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.hazelnut import Hazelnut
15 from plugins.resources_selected import ResourcesSelected
16 from plugins.googlemap import GoogleMap
17 from plugins.senslabmap.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
24 from myslice.config import Config
26 tmp_default_slice='ple.upmc.myslicedemo'
28 # temporary : turn off the users part to speed things up
31 class SliceView (LoginRequiredAutoLogoutView):
33 def get (self,request, slicename=tmp_default_slice):
36 page.add_css_files ('css/slice-view.css')
37 page.add_js_files ( [ "js/common.functions.js" ] )
38 page.add_js_chunks ('$(function() { messages.debug("sliceview: jQuery version " + $.fn.jquery); });')
39 page.add_js_chunks ('$(function() { messages.debug("sliceview: users turned %s"); });'%("on" if do_query_users else "off"))
40 page.add_js_chunks ('$(function() { messages.debug("manifold URL %s"); });'%(Config.manifold_url()))
41 page.expose_js_metadata()
43 metadata = page.get_metadata()
44 resource_md = metadata.details_by_object('resource')
45 resource_fields = [column['name'] for column in resource_md['column']]
47 user_md = metadata.details_by_object('user')
48 user_fields = ['user_hrn'] # [column['name'] for column in user_md['column']]
50 # TODO The query to run is embedded in the URL
51 main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
54 'resource.resource_hrn', 'resource.hostname', 'resource.type', 'resource.network_hrn',
57 #'application.measurement_point.counter'
60 query_resource_all = Query.get('resource').select(resource_fields)
62 query_user_all = Query.get('user').select(user_fields)
64 aq = AnalyzedQuery(main_query, metadata=metadata)
65 page.enqueue_query(main_query, analyzed_query=aq)
66 page.enqueue_query(query_resource_all)
68 page.enqueue_query(query_user_all)
70 # ... and for the relations
71 # XXX Let's hardcode resources for now
72 sq_resource = aq.subquery('resource')
73 sq_user = aq.subquery('user')
74 sq_lease = aq.subquery('lease')
75 sq_measurement = aq.subquery('measurement')
78 # Prepare the display according to all metadata
79 # (some parts will be pending, others can be triggered by users).
81 # For example slice measurements will not be requested by default...
83 # Create the base layout (Stack)...
86 title="Slice %s"%slicename,
90 # ... responsible for the slice properties...
97 html="<h2 class='well well-lg'> Slice %s</h2>"%slicename)
100 # --------------------------------------------------------------------------
101 # ResourcesSelected (Pending Operations)
103 main_stack.insert(ResourcesSelected(
105 title = 'Pending operations',
109 outline_complete = True,
112 # --------------------------------------------------------------------------
114 # the resources part is made of a Tabs (Geographic, List),
116 resources_as_map = GoogleMap(
118 title = 'Geographic view',
119 domid = 'resources-map',
120 # tab's sons preferably turn this off
123 query_all = query_resource_all,
131 resources_as_list = Hazelnut(
133 domid = 'resources-list',
134 # this is the query at the core of the slice list
136 query_all = query_resource_all,
138 datatables_options = {
139 # for now we turn off sorting on the checkboxes columns this way
140 # this of course should be automatic in hazelnut
141 'aoColumns' : [None, None, None, None, {'bSortable': False}],
142 'iDisplayLength': 25,
143 'bLengthChange' : True,
148 resources_query_editor = QueryEditor(
150 query = query_resource_all,
151 title = "Select Columns",
153 resources_active_filters = ActiveFilters(
156 title = "Active Filters ?",
159 # List area itself is a Stack with hazelnut on top,
160 # and a togglable tabs for customization plugins
161 resources_as_list_area = Stack(
163 title = 'Resources as a List',
164 domid = 'resources-list-area',
165 sons= [ resources_as_list,
167 title="Customize Resources layout",
169 toggled='persistent',
170 domid="customize-resources",
171 outline_complete=True,
172 sons = [ resources_query_editor, resources_active_filters, ],
176 resources_area = Tabs ( page=page,
180 outline_complete=True,
181 sons=[ resources_as_map, resources_as_list_area, ],
182 active_domid = 'resources-map',
184 main_stack.insert (resources_area)
187 # --------------------------------------------------------------------------
194 outline_complete = True,
197 active_domid = 'checkboxes2',
199 main_stack.insert(tab_users)
201 tab_users.insert(Hazelnut(
203 title = 'Users List',
204 domid = 'checkboxes2',
205 # tab's sons preferably turn this off
207 # this is the query at the core of the slice list
209 query_all = query_user_all,
211 datatables_options = {
212 'iDisplayLength' : 25,
213 'bLengthChange' : True,
218 # --------------------------------------------------------------------------
220 tab_measurements = Tabs (
222 active_domid = 'checkboxes3',
223 outline_complete = True,
225 title = 'Measurements',
226 domid = 'measurements',
228 main_stack.insert(tab_measurements)
230 tab_measurements.insert(Hazelnut(
232 title = 'Measurements',
233 domid = 'checkboxes3',
234 # tab's sons preferably turn this off
236 # this is the query at the core of the slice list
237 query = sq_measurement,
239 datatables_options = {
240 'iDisplayLength' : 25,
241 'bLengthChange' : True,
246 # --------------------------------------------------------------------------
247 # MESSAGES (we use transient=False for now)
248 main_stack.insert(Messages(
250 title = "Runtime messages for slice %s"%slicename,
253 # plain messages are probably less nice for production but more reliable for development for now
255 # these make sense only in non-transient mode..
257 toggled = 'persistent',
258 outline_complete = True,
262 # variables that will get passed to the view-unfold1.html template
265 # define 'unfold1_main' to the template engine - the main contents
266 template_env [ 'unfold1_main' ] = main_stack.render(request)
268 # more general variables expected in the template
269 template_env [ 'title' ] = '%(slicename)s'%locals()
270 # the menu items on the top
271 template_env [ 'topmenu_items' ] = topmenu_items('Slice', request)
272 # so we can sho who is logged
273 template_env [ 'username' ] = the_user (request)
275 # don't forget to run the requests
276 page.expose_queries ()
278 # xxx create another plugin with the same query and a different layout (with_datatables)
279 # show that it worls as expected, one single api call to backend and 2 refreshed views
281 # the prelude object in page contains a summary of the requirements() for all plugins
282 # define {js,css}_{files,chunks}
283 prelude_env = page.prelude_env()
284 template_env.update(prelude_env)
285 result=render_to_response ('view-unfold1.html',template_env,
286 context_instance=RequestContext(request))