Scheduler2 plugin: Queries handled by the plugin = query (resource in slice), query_l...
[myslice.git] / portal / sliceresourceview.py
1 from django.template                 import RequestContext
2 from django.shortcuts                import render_to_response
3
4 from manifold.core.query             import Query, AnalyzedQuery
5 from manifoldapi.manifoldapi         import execute_query
6
7 from django.views.generic.base      import TemplateView
8
9 from unfold.loginrequired           import LoginRequiredView
10 from django.http import HttpResponse
11 from django.shortcuts import render
12
13 from unfold.page                     import Page
14
15 from myslice.configengine            import ConfigEngine
16 from plugins.querytable              import QueryTable
17 from plugins.googlemap               import GoogleMap
18 from plugins.queryupdater            import QueryUpdater
19 from plugins.testbeds                import TestbedsPlugin
20 from plugins.scheduler2              import Scheduler2
21 from plugins.columns_editor          import ColumnsEditor
22
23 from myslice.theme import ThemeView
24
25 class SliceResourceView (LoginRequiredView, ThemeView):
26     template_name = "slice-resource-view.html"
27     
28     def get(self, request, slicename):
29
30         if request.GET.get('message') : 
31             msg = "Slice successfully updated"
32         else :
33             msg = None
34
35         page = Page(request)
36         metadata = page.get_metadata()
37         page.expose_js_metadata()
38
39         resource_md = metadata.details_by_object('resource')
40         resource_fields = [column['name'] for column in resource_md['column']]
41
42         user_md = metadata.details_by_object('user')
43         user_fields = ['user_hrn'] # [column['name'] for column in user_md['column']]
44
45         # TODO The query to run is embedded in the URL
46         main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename)
47         main_query.select(
48                 'slice_hrn',
49                 'resource.urn', 
50                 'resource.hostname', 'resource.type',
51                 'resource.network_hrn',
52                 'lease.urn',
53                 #'user.user_hrn',
54                 #'application.measurement_point.counter'
55         )
56         # for internal use in the querytable plugin;
57         # needs to be a unique column present for each returned record
58         main_query_init_key = 'urn'
59         aq = AnalyzedQuery(main_query, metadata=metadata)
60         page.enqueue_query(main_query, analyzed_query=aq)
61         sq_resource    = aq.subquery('resource')
62         sq_lease       = aq.subquery('lease')
63
64         query_resource_all = Query.get('resource').select(resource_fields)
65         page.enqueue_query(query_resource_all)
66
67         # leases query
68         lease_md = metadata.details_by_object('lease')
69         lease_fields = [column['name'] for column in lease_md['column']]
70
71         query_lease_all = Query.get('lease').select(lease_fields)
72         page.enqueue_query(query_lease_all)
73
74         # --------------------------------------------------------------------------
75         # ALL RESOURCES LIST
76         # resources as a list using datatable plugin
77  
78         list_resources = QueryTable(
79             page       = page,
80             domid      = 'resources-list',
81             title      = 'List view',
82             query      = sq_resource,
83             query_all  = query_resource_all,
84             init_key   = "urn",
85             checkboxes = True,
86             datatables_options = {
87                 'iDisplayLength': 25,
88                 'bLengthChange' : True,
89                 'bAutoWidth'    : True,
90                 },
91         )
92
93
94         # --------------------------------------------------------------------------
95         # RESERVED RESOURCES LIST
96         # resources as a list using datatable plugin
97  
98         list_reserved_resources = QueryTable(
99             page       = page,
100             domid      = 'resources-reserved-list',
101             title      = 'List view',
102             query      = sq_resource,
103             query_all  = sq_resource,
104             init_key   = "urn",
105             checkboxes = True,
106             datatables_options = {
107                 'iDisplayLength': 25,
108                 'bLengthChange' : True,
109                 'bAutoWidth'    : True,
110                 },
111         )
112
113         # --------------------------------------------------------------------------
114         # COLUMNS EDITOR
115         # list of fields to be applied on the query 
116         # this will add/remove columns in QueryTable plugin
117  
118         filter_column_editor = ColumnsEditor(
119             page  = page,
120             query = sq_resource, 
121             query_all = query_resource_all,
122             title = "Select Columns",
123             domid = 'select-columns',
124         )
125
126         # --------------------------------------------------------------------------
127         # RESOURCES MAP
128         # the resources part is made of a Tabs (Geographic, List), 
129
130         map_resources  = GoogleMap(
131             page       = page,
132             title      = 'Geographic view',
133             domid      = 'resources-map',
134             # tab's sons preferably turn this off
135             togglable  = False,
136             query      = sq_resource,
137             query_all  = query_resource_all,
138             # this key is the one issued by google
139             googlemap_api_key = ConfigEngine().googlemap_api_key(),
140             # the key to use at init-time
141             init_key   = main_query_init_key,
142             checkboxes = True,
143             # center on Paris
144             latitude   = 49.,
145             longitude  = 9,
146             zoom       = 8,
147         )
148
149         # --------------------------------------------------------------------------
150         # LEASES Nitos Scheduler
151         # Display the leases reservation timeslots of the resources
152
153         resources_as_scheduler2 = Scheduler2( 
154             page       = page,
155             domid      = 'scheduler',
156             title      = 'Scheduler',
157             # this is the query at the core of the slice list
158             query = sq_resource,
159             query_lease = sq_lease,
160             query_all_resources = query_resource_all,
161             query_all_leases = query_lease_all,
162         )
163
164         # --------------------------------------------------------------------------
165         # QueryUpdater (Pending Operations)
166  
167         pending_resources = QueryUpdater(
168             page                = page,
169             title               = 'Pending operations',
170             query               = main_query,
171             togglable           = True,
172             # start turned off, it will open up itself when stuff comes in
173             toggled             = False,
174             domid               = 'pending',
175             outline_complete    = True,
176         )
177
178         # --------------------------------------------------------------------------
179         # NETWORKS
180         # testbeds as a list of filters 
181
182         network_md = metadata.details_by_object('network')
183         network_fields = [column['name'] for column in network_md['column']]
184
185         query_network = Query.get('network').select(network_fields)
186         page.enqueue_query(query_network)
187
188         filter_testbeds = TestbedsPlugin(
189             page          = page,
190             domid         = 'testbeds-filter',
191             title         = 'Filter by testbeds',
192             query         = sq_resource,
193             query_all     = query_resource_all,
194             query_network = query_network,
195             init_key      = "network_hrn",
196             checkboxes    = True,
197             datatables_options = {
198                 'iDisplayLength': 25,
199                 'bLengthChange' : True,
200                 'bAutoWidth'    : True,
201                 },
202         )
203
204         template_env = {}
205         template_env['list_resources'] = list_resources.render(self.request)
206 #         template_env['list_reserved_resources'] = list_reserved_resources.render(self.request)
207
208         template_env['columns_editor'] = filter_column_editor.render(self.request)
209
210         template_env['filter_testbeds'] = filter_testbeds.render(self.request)
211         template_env['map_resources'] = map_resources.render(self.request)
212         template_env['scheduler'] = resources_as_scheduler2.render(self.request)
213         template_env['pending_resources'] = pending_resources.render(self.request)
214         template_env["theme"] = self.theme
215         template_env["username"] = request.user
216         template_env["slice"] = slicename
217         template_env["section"] = "resources"
218         template_env["msg"] = msg
219         template_env.update(page.prelude_env())
220
221         return render_to_response(self.template, template_env, context_instance=RequestContext(request))