Slicestat -> SliceStat
[myslice.git] / portal / sliceview.py
1 from django.template                 import RequestContext
2 from django.shortcuts                import render_to_response
3
4 from unfold.loginrequired            import LoginRequiredAutoLogoutView
5
6 from unfold.page                     import Page
7 from manifold.core.query             import Query, AnalyzedQuery
8
9 from ui.topmenu                      import topmenu_items, the_user
10
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              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
24
25 from myslice.config                  import Config
26
27 tmp_default_slice='ple.upmc.myslicedemo'
28
29 # temporary : turn off the users part to speed things up
30 #do_query_users=True
31 do_query_users=False
32
33 class SliceView (LoginRequiredAutoLogoutView):
34
35     def get (self,request, slicename=tmp_default_slice):
36     
37         page = Page(request)
38         page.add_css_files ('css/slice-view.css')
39         page.add_js_files  ( [ "js/common.functions.js" ] )
40         page.add_js_chunks ('$(function() { messages.debug("sliceview: jQuery version " + $.fn.jquery); });')
41         page.add_js_chunks ('$(function() { messages.debug("sliceview: users turned %s"); });'%("on" if do_query_users else "off"))
42         config=Config()
43         page.add_js_chunks ('$(function() { messages.debug("manifold URL %s"); });'%(config.manifold_url()))
44         page.expose_js_metadata()
45     
46         metadata = page.get_metadata()
47         resource_md = metadata.details_by_object('resource')
48         resource_fields = [column['name'] for column in resource_md['column']]
49     
50         user_md = metadata.details_by_object('user')
51         user_fields = ['user_hrn'] # [column['name'] for column in user_md['column']]
52     
53         # TODO The query to run is embedded in the URL
54         main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
55         main_query.select(
56                 'slice_hrn',
57                 'resource.hrn', 'resource.hostname', 'resource.type', 
58                 'resource.network_hrn',
59                 #'lease.urn',
60                 'user.user_hrn',
61                 #'application.measurement_point.counter'
62         )
63     
64         query_resource_all = Query.get('resource').select(resource_fields)
65         if do_query_users:
66             query_user_all = Query.get('user').select(user_fields)
67     
68         aq = AnalyzedQuery(main_query, metadata=metadata)
69         page.enqueue_query(main_query, analyzed_query=aq)
70         page.enqueue_query(query_resource_all)
71         if do_query_users:
72             page.enqueue_query(query_user_all)
73     
74         # ... and for the relations
75         # XXX Let's hardcode resources for now
76         sq_resource    = aq.subquery('resource')
77         sq_user        = aq.subquery('user')
78         sq_lease       = aq.subquery('lease')
79         sq_measurement = aq.subquery('measurement')
80         
81     
82         # Prepare the display according to all metadata
83         # (some parts will be pending, others can be triggered by users).
84         # 
85         # For example slice measurements will not be requested by default...
86     
87         # Create the base layout (Stack)...
88         main_stack = Stack (
89             page=page,
90             title="Slice %s"%slicename,
91             sons=[],
92         )
93     
94         # ... responsible for the slice properties...
95     
96         # a nice header
97         main_stack.insert (
98             Raw (page=page,
99                  togglable=False, 
100                  toggled=True,
101                  html="<h2 class='well well-lg'> Slice %s</h2>"%slicename)
102         )
103     
104         # --------------------------------------------------------------------------
105         # ResourcesSelected (Pending Operations)
106
107         main_stack.insert(ResourcesSelected(
108             page                = page,
109             title               = 'Pending operations',
110             query               = main_query,
111             togglable           = True,
112             # start turned off, it will open up itself when stuff comes in
113             toggled             = False, 
114             domid               = 'pending',
115             outline_complete    = True,
116         ))
117
118         # --------------------------------------------------------------------------
119         # Filter Resources
120        
121         filter_query_editor = QueryEditor(
122             page  = page,
123             query = sq_resource, 
124             query_all = query_resource_all,
125             title = "Select Columns",
126             domid = 'select-columns',
127             )
128         filter_active_filters = ActiveFilters(
129             page  = page,
130             query = sq_resource,
131             title = "Active Filters",
132             )
133         filters_area = Stack(
134             page                = page,
135             title               = 'Filter Resources',
136             domid               = 'filters',
137             sons                = [filter_query_editor, filter_active_filters],
138             togglable           = True,
139             toggled             = 'persistent',
140             outline_complete    = True, 
141         )
142         main_stack.insert (filters_area)
143
144         # --------------------------------------------------------------------------
145         # RESOURCES
146         # the resources part is made of a Tabs (Geographic, List), 
147
148         resources_as_map = GoogleMap(
149             page       = page,
150             title      = 'Geographic view',
151             domid      = 'resources-map',
152             # tab's sons preferably turn this off
153             togglable  = False,
154             query      = sq_resource,
155             query_all  = query_resource_all,
156             checkboxes = True,
157             # center on Paris
158             latitude   = 49.,
159             longitude  = 9,
160             zoom       = 4,
161         )
162
163         resources_as_list = Hazelnut( 
164             page       = page,
165             domid      = 'resources-list',
166             title      = 'List view',
167             # this is the query at the core of the slice list
168             query      = sq_resource,
169             query_all  = query_resource_all,
170             checkboxes = True,
171             datatables_options = { 
172                 'iDisplayLength': 25,
173                 'bLengthChange' : True,
174                 'bAutoWidth'    : True,
175                 },
176             )
177
178         resources_stats_cpu = SliceStat(
179             title = "CPU Usage",
180             domid = 'resources-stats-cpu',
181             page  = page,
182             stats = 'slice',
183             key   = 'hrn',
184             query = 'none',
185             slicename = slicename,
186             o = 'cpu'
187         )
188         
189         resources_stats_mem = SliceStat(
190             title = "Memory Usage",
191             domid = 'resources-stats-mem',
192             page  = page,
193             stats = 'slice',
194             key   = 'hrn',
195             query = 'none',
196             slicename = slicename,
197             o = 'mem'
198         )
199         
200         resources_stats_asb = SliceStat(
201             title = "Traffic Sent",
202             domid = 'resources-stats-asb',
203             page  = page,
204             stats = 'slice',
205             key   = 'hrn',
206             query = 'none',
207             slicename = slicename,
208             o = 'asb'
209         )
210         
211         resources_stats_arb = SliceStat(
212             title = "Traffic Received",
213             domid = 'resources-stats-arb',
214             page  = page,
215             stats = 'slice',
216             key   = 'hrn',
217             query = 'none',
218             slicename = slicename,
219             o = 'arb'
220         )
221         
222         # with the new 'Filter' stuff on top, no need for anything but the hazelnut
223         resources_as_list_area = resources_as_list 
224
225         resources_area = Tabs ( page=page, 
226                                 domid="resources",
227                                 togglable=True,
228                                 title="Resources",
229                                 outline_complete=True,
230                                 sons=[ resources_as_map, resources_as_list_area, resources_stats_cpu, resources_stats_mem, resources_stats_asb, resources_stats_arb ],
231                                 active_domid = 'resources-map',
232                                 )
233         main_stack.insert (resources_area)
234
235         # --------------------------------------------------------------------------
236         # USERS
237     
238         if do_query_users:
239             tab_users = Tabs(
240                 page                = page,
241                 domid               = 'users',
242                 outline_complete    = True,
243                 togglable           = True,
244                 title               = 'Users',
245                 active_domid        = 'users-list',
246                 )
247             main_stack.insert(tab_users)
248     
249             tab_users.insert(Hazelnut( 
250                 page        = page,
251                 title       = 'Users List',
252                 domid       = 'users-list',
253                 # tab's sons preferably turn this off
254                 togglable   = False,
255                 # this is the query at the core of the slice list
256                 query       = sq_user,
257                 query_all  = query_user_all,
258                 checkboxes  = True,
259                 datatables_options = { 
260                     'iDisplayLength' : 25,
261                     'bLengthChange'  : True,
262                     'bAutoWidth'     : True,
263                 },
264             ))
265
266 # DEMO    
267         # --------------------------------------------------------------------------
268         # MEASUREMENTS
269 #        tab_measurements = Tabs (
270 #            page                = page,
271 #            active_domid        = 'measurements-list',
272 #            outline_complete    = True,
273 #            togglable           = True,
274 #            title               = 'Measurements',
275 #            domid               = 'measurements',
276 #        )
277 #        main_stack.insert(tab_measurements)
278 #    
279 #        tab_measurements.insert(Hazelnut( 
280 #            page        = page,
281 #            title       = 'Measurements',
282 #            domid       = 'measurements-list',
283 #            # tab's sons preferably turn this off
284 #            togglable   = False,
285 #            # this is the query at the core of the slice list
286 #            query       = sq_measurement,
287 #            # do NOT set checkboxes to False
288 #            # this table being otherwise empty, it just does not fly with dataTables
289 #            checkboxes  = True,
290 #            datatables_options = { 
291 #                'iDisplayLength' : 25,
292 #                'bLengthChange'  : True,
293 #                'bAutoWidth'     : True,
294 #            },
295 #        ))
296 #    
297 #        # --------------------------------------------------------------------------
298 #        # MESSAGES (we use transient=False for now)
299 #        main_stack.insert(Messages(
300 #            page   = page,
301 #            title  = "Runtime messages for slice %s"%slicename,
302 #            domid  = "msgs-pre",
303 #            levels = "ALL",
304 #            # plain messages are probably less nice for production but more reliable for development for now
305 #            transient = False,
306 #            # these make sense only in non-transient mode..
307 #            togglable = True,
308 #            toggled = 'persistent',
309 #            outline_complete = True,
310 #        ))
311 #    
312     
313         # variables that will get passed to the view-unfold1.html template
314         template_env = {}
315         
316         # define 'unfold1_main' to the template engine - the main contents
317         template_env [ 'unfold1_main' ] = main_stack.render(request)
318     
319         # more general variables expected in the template
320         template_env [ 'title' ] = '%(slicename)s'%locals()
321         # the menu items on the top
322         template_env [ 'topmenu_items' ] = topmenu_items('Slice', request) 
323         # so we can sho who is logged
324         template_env [ 'username' ] = the_user (request) 
325     
326         # don't forget to run the requests
327         page.expose_queries ()
328     
329         # xxx create another plugin with the same query and a different layout (with_datatables)
330         # show that it worls as expected, one single api call to backend and 2 refreshed views
331     
332         # the prelude object in page contains a summary of the requirements() for all plugins
333         # define {js,css}_{files,chunks}
334         prelude_env = page.prelude_env()
335         template_env.update(prelude_env)
336         result=render_to_response ('view-unfold1.html',template_env,
337                                    context_instance=RequestContext(request))
338         return result