Browse Source

HUE-4135 [editor] Automatically update on save the variables of a scheduled query

Romain Rigaux 9 years ago
parent
commit
53040ce

+ 4 - 0
apps/oozie/src/oozie/decorators.py

@@ -52,6 +52,10 @@ def check_document_access_permission():
         elif 'doc_id' in kwargs:
           doc_id = kwargs['doc_id']
 
+        if doc_id and not doc_id.isdigit():
+          uuid = doc_id
+          doc_id = None
+
         if doc_id is not None:
           doc2 = Document2.objects.get(id=doc_id)
         elif uuid is not None:

+ 9 - 5
apps/oozie/src/oozie/models2.py

@@ -2802,22 +2802,26 @@ class WorkflowBuilder():
 
   def create_workflow(self, doc_uuid, user, name=None, managed=False):
     document = Document2.objects.get_by_uuid(user=user, uuid=doc_uuid)
-    notebook = Notebook(document=document)
-    parameters = find_dollar_braced_variables(notebook.get_str())
+    parameters = self.get_document_parameters(document)
 
     if name is None:
       name = _('Schedule of ') + document.name
-  
+
     workflow_doc = self.create_hive_document_workflow(name, doc_uuid, parameters, user, managed=managed)
     workflow_doc.dependencies.add(document)
 
     return workflow_doc
 
+  def get_document_parameters(self, document):
+    notebook = Notebook(document=document)
+    parameters = find_dollar_braced_variables(notebook.get_str())
+
+    return [{u'value': u'%s=${%s}' % (p, p)} for p in parameters]
+
   def create_hive_document_workflow(self, name, doc_uuid, parameters, user, managed=False):
     api = get_oozie(user)
 
     credentials = [HiveDocumentAction.DEFAULT_CREDENTIALS] if api.security_enabled else []
-    params = [{u'value': u'%s=${%s}' % (p, p)} for p in parameters]
 
     data = json.dumps({'workflow': {
       u'name': name,
@@ -2878,7 +2882,7 @@ class WorkflowBuilder():
               u'files': [],
               u'job_xml': u'',
               u'uuid': doc_uuid,
-              u'parameters': params,
+              u'parameters': parameters,
               u'retry_interval': [],
               u'retry_max': [],
               u'job_properties': [],

+ 1 - 1
apps/oozie/src/oozie/templates/editor2/common_scheduler.inc.mako

@@ -170,7 +170,7 @@ from django.utils.translation import ugettext as _
 
 
       <div class="card card-home" data-bind="visible: coordinator.properties.workflow()" style="margin-top: 20px; margin-bottom: 20px">
-        <h1 class="card-heading simple">${ _('Workflow Parameters') }</h1>
+        <h1 class="card-heading simple">${ _('Parameters') }</h1>
 
         <div class="card-body">
           <span class="muted" data-bind="visible: coordinator.variables().length == 0 && ! isEditing()">${ _('This coordinator has no defined parameters.') }</span>

+ 1 - 0
apps/oozie/src/oozie/urls.py

@@ -81,6 +81,7 @@ urlpatterns += patterns(
   url(r'^editor/workflow/add_node/$', 'add_node', name='add_node'),
   url(r'^editor/workflow/parameters/$', 'workflow_parameters', name='workflow_parameters'),
   url(r'^editor/workflow/action/parameters/$', 'action_parameters', name='action_parameters'),
+  url(r'^editor/workflow/action/refresh_parameters/$', 'refresh_action_parameters', name='refresh_action_parameters'),
   url(r'^editor/workflow/gen_xml/$', 'gen_xml_workflow', name='gen_xml_workflow'),
   url(r'^editor/workflow/open_v1/$', 'open_old_workflow', name='open_old_workflow'),
   

+ 42 - 4
apps/oozie/src/oozie/views/editor2.py

@@ -25,6 +25,7 @@ from django.shortcuts import redirect
 from django.utils.translation import ugettext as _
 
 from desktop.conf import USE_NEW_EDITOR
+from desktop.lib import django_mako
 from desktop.lib.django_util import JsonResponse, render
 from desktop.lib.exceptions_renderable import PopupException
 from desktop.lib.i18n import smart_str
@@ -35,6 +36,7 @@ from desktop.models import Document, Document2
 from liboozie.credentials import Credentials
 from liboozie.oozie_api import get_oozie
 from liboozie.submission2 import Submission
+from notebook.connectors.base import Notebook
 
 from oozie.decorators import check_document_access_permission, check_document_modify_permission,\
   check_editor_access_permission
@@ -44,7 +46,6 @@ from oozie.models2 import Node, Workflow, Coordinator, Bundle, NODES, WORKFLOW_N
     find_dollar_variables, find_dollar_braced_variables, WorkflowBuilder
 from oozie.utils import convert_to_server_timezone
 from oozie.views.editor import edit_workflow as old_edit_workflow, edit_coordinator as old_edit_coordinator, edit_bundle as old_edit_bundle
-from desktop.lib import django_mako
 
 
 LOG = logging.getLogger(__name__)
@@ -348,8 +349,8 @@ def workflow_parameters(request):
   response = {'status': -1}
 
   try:
-    workflow = Workflow(document=Document2.objects.get(type='oozie-workflow2', uuid=request.GET.get('uuid')),
-                        user=request.user)
+    workflow_doc = Document2.objects.get(type='oozie-workflow2', uuid=request.GET.get('uuid'))
+    workflow = Workflow(document=workflow_doc, user=request.user)
 
     response['status'] = 0
     response['parameters'] = workflow.find_all_parameters(with_lib_path=False)
@@ -359,6 +360,41 @@ def workflow_parameters(request):
   return JsonResponse(response)
 
 
+@check_editor_access_permission
+@check_document_access_permission()
+def refresh_action_parameters(request):
+  response = {'status': -1}
+
+  try:
+    coord_uuid = request.POST.get('uuid')
+    workflow_doc = Document2.objects.get(type='oozie-workflow2', owner=request.user, is_managed=True, dependents__uuid__in=[coord_uuid])
+
+    # Refresh the action parameters of a document action in case the document changed
+    workflow = Workflow(document=workflow_doc, user=request.user)
+
+    _data = workflow.get_data()
+    hive_node = _data['workflow']['nodes'][3]
+    query_document = Document2.objects.get_by_uuid(user=request.user, uuid=hive_node['properties']['uuid'])
+    parameters = WorkflowBuilder().get_document_parameters(query_document)
+
+    changed = set([p['value'] for p in parameters]) != set([p['value'] for p in hive_node['properties']['parameters']])
+
+    if changed:
+      hive_node['properties']['parameters'] = parameters
+      workflow.data = json.dumps(_data)
+
+      workflow_doc.update_data({'workflow': _data['workflow']})
+      workflow_doc.save()
+
+    response['status'] = 0
+    response['parameters'] = parameters
+    response['changed'] = changed
+  except Exception, e:
+    response['message'] = str(e)
+
+  return JsonResponse(response)
+
+
 @check_editor_access_permission
 def gen_xml_workflow(request):
   response = {'status': -1}
@@ -498,6 +534,8 @@ def edit_coordinator(request):
       workflow_doc = workflows.get()
     else:
       workflow_doc = WorkflowBuilder().create_workflow(doc_uuid=document_uuid, user=request.user, managed=True)
+      if doc:
+        doc.dependencies.add(workflow_doc)
     workflow_uuid = workflow_doc.uuid
     coordinator.data['name'] = _('Schedule of %s') % workflow_doc.name
   elif request.GET.get('workflow'):
@@ -524,7 +562,7 @@ def edit_coordinator(request):
   if coordinator_id and not filter(lambda a: a['uuid'] == coordinator.data['properties']['workflow'], workflows):
     raise PopupException(_('You don\'t have access to the workflow of this coordinator.'))
 
-  if request.GET.get('format') == 'json':
+  if request.GET.get('format') == 'json': # For Editor
     return JsonResponse({
       'coordinator': coordinator.get_data_for_json(),
       'credentials': credentials.credentials.keys(),

+ 22 - 7
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -267,10 +267,10 @@
     self.queriesFilterVisible = ko.observable(false);
     self.queriesFilter.extend({ rateLimit: 300 });
     self.queriesFilter.subscribe(function(val){
-      fetchQueries();
+      self.fetchQueries();
     });
 
-    var fetchQueries = function () {
+    self.fetchQueries = function () {
       var QUERIES_PER_PAGE = 50;
       if (self.loadingQueries()) {
         return;
@@ -300,21 +300,21 @@
     self.currentQueryTab.subscribe(function (newValue) {
       huePubSub.publish('redraw.fixed.headers');
       if (newValue === 'savedQueries' && (self.queries().length === 0 || lastQueriesPage !== self.queriesCurrentPage())) {
-        fetchQueries();
+        self.fetchQueries();
       }
     });
 
     self.prevQueriesPage = function () {
       if (self.queriesCurrentPage() !== 1) {
         self.queriesCurrentPage(self.queriesCurrentPage() - 1);
-        fetchQueries();
+        self.fetchQueries();
       }
     };
 
     self.nextQueriesPage = function () {
       if (self.queriesCurrentPage() !== self.queriesTotalPages()) {
         self.queriesCurrentPage(self.queriesCurrentPage() + 1);
-        fetchQueries();
+        self.fetchQueries();
       }
     };
 
@@ -1378,6 +1378,8 @@
             }
             if (! self.schedulerViewModel) {
               self.loadScheduler();
+            } else {
+              self.refreshSchedulerParameters();
             }
             hueUtils.changeURL('/notebook/editor?editor=' + data.id);
           }
@@ -1575,14 +1577,26 @@
 
         self.schedulerViewModel.coordinator.properties.cron_advanced.valueHasMutated(); // Update jsCron enabled status
         self.schedulerViewModel.coordinator.tracker().markCurrentStateAsClean();
+        self.schedulerViewModel.isEditing(true);
       }).fail(function (xhr) {
         $(document).trigger("error", xhr.responseText);
       });
     };
 
-    self.saveScheduler = function() { console.log(self.coordinatorUuid());
+    self.refreshSchedulerParameters = function() {
+      $.post("/oozie/editor/workflow/action/refresh_parameters/", {
+        uuid: self.coordinatorUuid()
+      }, function(data) {
+    	if (data.status == 0 && data.changed) {
+    	  self.schedulerViewModel.coordinator.refreshParameters()
+    	} else {
+          $(document).trigger("error", data.message);
+        }
+      });
+    }
+    
+    self.saveScheduler = function() {
       if (! self.coordinatorUuid() || self.schedulerViewModel.coordinator.isDirty()) {
-        self.schedulerViewModel.coordinator.name('My daily run');  // TODO Temp fix until js errors are gone
         self.schedulerViewModel.save(function(data) {
     	  self.coordinatorUuid(data.uuid);
         });
@@ -1864,6 +1878,7 @@
 
         if (notebook.isSaved()) {
           notebook.loadScheduler();
+          notebook.snippets()[0].fetchQueries();
           notebook.snippets()[0].currentQueryTab('savedQueries');
         }
       }

+ 0 - 1
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -1048,7 +1048,6 @@ ${ hueIcons.symbols() }
       <!-- ko if: $root.selectedNotebook() -->
       <!-- ko with: $root.selectedNotebook() -->
         <!-- ko if: $root.selectedNotebook().isSaved() -->
-          <a data-bind="click: function() { schedulerViewModel.coordinator.refreshParameters(); }">Refresh</a></br>
           <a data-bind="click: showSubmitPopup">Start</a></br>
           <a href="#scheduledJobsTab" data-toggle="tab">${_('View')}</a>