1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 
19 package org.apache.hadoop.yarn.server.resourcemanager.webapp;
20 
21 import org.apache.hadoop.yarn.webapp.view.HtmlBlock;
22 
23 public class SchedulerPageUtil {
24 
25   static class QueueBlockUtil extends HtmlBlock {
26 
reopenQueue(Block html)27     private void reopenQueue(Block html) {
28       html.
29           script().$type("text/javascript").
30           _("function reopenQueryNodes() {",
31             "  var currentParam = window.location.href.split('?');",
32             "  var tmpCurrentParam = currentParam;",
33             "  var queryQueuesString = '';",
34             "  if (tmpCurrentParam.length > 1) {",
35             "    // openQueues=q1#q2&param1=value1&param2=value2",
36             "    tmpCurrentParam = tmpCurrentParam[1];",
37             "    if (tmpCurrentParam.indexOf('openQueues=') != -1 ) {",
38             "      tmpCurrentParam = tmpCurrentParam.split('openQueues=')[1].split('&')[0];",
39             "      queryQueuesString = tmpCurrentParam;",
40             "    }",
41             "  }",
42             "  if (queryQueuesString != '') {",
43             "    queueArray = queryQueuesString.split('#');",
44             "    $('#cs .q').each(function() {",
45             "      var name = $(this).html();",
46             "      if (name != 'root' && $.inArray(name, queueArray) != -1) {",
47             "        $(this).closest('li').removeClass('jstree-closed').addClass('jstree-open'); ",
48             "      }",
49             "    });",
50             "  }",
51             "  $('#cs').bind( {",
52             "                  'open_node.jstree' :function(e, data) { storeExpandedQueue(e, data); },",
53             "                  'close_node.jstree':function(e, data) { storeExpandedQueue(e, data); }",
54             "  });",
55             "}")._();
56     }
57 
storeExpandedQueue(Block html)58     private void storeExpandedQueue (Block html) {
59       html.
60           script().$type("text/javascript").
61           _("function storeExpandedQueue(e, data) {",
62             "  var OPEN_QUEUES = 'openQueues';",
63             "  var ACTION_OPEN = 'open';",
64             "  var ACTION_CLOSED = 'closed';",
65             "  var $li = $(data.args[0]);",
66             "  var action = ACTION_CLOSED;  //closed or open",
67             "  var queueName = ''",
68             "  if ($li.hasClass('jstree-open')) {",
69             "      action=ACTION_OPEN;",
70             "  }",
71             "  queueName = $li.find('.q').html();",
72             "  // http://localhost:8088/cluster/scheduler?openQueues=q1#q2&param1=value1&param2=value2 ",
73             "  //   ==> [http://localhost:8088/cluster/scheduler , openQueues=q1#q2&param1=value1&param2=value2]",
74             "  var currentParam = window.location.href.split('?');",
75             "  var tmpCurrentParam = currentParam;",
76             "  var queryString = '';",
77             "  if (tmpCurrentParam.length > 1) {",
78             "    // openQueues=q1#q2&param1=value1&param2=value2",
79             "    tmpCurrentParam = tmpCurrentParam[1];",
80             "    currentParam = tmpCurrentParam;",
81             "    tmpCurrentParam = tmpCurrentParam.split('&');",
82             "    var len = tmpCurrentParam.length;",
83             "    var paramExist = false;",
84             "    if (len > 1) {    // Currently no query param are present but in future if any are added for that handling it now",
85             "      queryString = '';",
86             "      for (var i = 0 ; i < len ; i++) {  // searching for param openQueues",
87             "        if (tmpCurrentParam[i].substr(0,11) == OPEN_QUEUES + '=') {",
88             "          if (action == ACTION_OPEN) {",
89             "            tmpCurrentParam[i] = addQueueName(tmpCurrentParam[i],queueName);",
90             "          }",
91             "          else if (action == ACTION_CLOSED) {",
92             "            tmpCurrentParam[i] = removeQueueName(tmpCurrentParam[i] , queueName);",
93             "          }",
94             "          paramExist = true;",
95             "        }",
96             "        if (i > 0) {",
97             "          queryString += '&';",
98             "        }",
99             "        queryString += tmpCurrentParam[i];",
100             "      }",
101             "      // If in existing query string OPEN_QUEUES param is not present",
102             "      if (action == ACTION_OPEN && !paramExist) {",
103             "        queryString = currentParam + '&' + OPEN_QUEUES + '=' + queueName;",
104             "      }",
105             "    } ",
106             "    // Only one param is present in current query string",
107             "    else {",
108             "      tmpCurrentParam=tmpCurrentParam[0];",
109             "      // checking if the only param present in query string is OPEN_QUEUES or not and making queryString accordingly",
110             "      if (tmpCurrentParam.substr(0,11) == OPEN_QUEUES + '=') {",
111             "        if (action == ACTION_OPEN) {",
112             "          queryString = addQueueName(tmpCurrentParam,queueName);",
113             "        }",
114             "        else if (action == ACTION_CLOSED) {",
115             "          queryString = removeQueueName(tmpCurrentParam , queueName);",
116             "        }",
117             "      }",
118             "      else {",
119             "        if (action == ACTION_OPEN) {",
120             "          queryString = tmpCurrentParam + '&' + OPEN_QUEUES + '=' + queueName;",
121             "        }",
122             "      }",
123             "    }",
124             "  } else {",
125             "    if (action == ACTION_OPEN) {",
126             "      tmpCurrentParam = '';",
127             "      currentParam = tmpCurrentParam;",
128             "      queryString = OPEN_QUEUES+'='+queueName;",
129             "    }",
130             "  }",
131             "  if (queryString != '') {",
132             "    queryString = '?' + queryString;",
133             "  }",
134             "  var url = window.location.protocol + '//' + window.location.host + window.location.pathname + queryString;",
135             "  window.history.pushState( { path : url }, '', url);",
136             "};",
137             "",
138             "function removeQueueName(queryString, queueName) {",
139             "  var index = queryString.indexOf(queueName);",
140             "  // Finding if queue is present in query param then only remove it",
141             "  if (index != -1) {",
142             "    // removing openQueues=",
143             "    var tmp = queryString.substr(11, queryString.length);",
144             "    tmp = tmp.split('#');",
145             "    var len = tmp.length;",
146             "    var newQueryString = '';",
147             "    for (var i = 0 ; i < len ; i++) {",
148             "      if (tmp[i] != queueName) {",
149             "        if (newQueryString != '') {",
150             "          newQueryString += '#';",
151             "        }",
152             "        newQueryString += tmp[i];",
153             "      }",
154             "    }",
155             "    queryString = newQueryString;",
156             "    if (newQueryString != '') {",
157             "      queryString = 'openQueues=' + newQueryString;",
158             "    }",
159             "  }",
160             "  return queryString;",
161             "}",
162             "",
163             "function addQueueName(queryString, queueName) {",
164             "  queueArray = queryString.split('#');",
165             "  if ($.inArray(queueArray, queueName) == -1) {",
166             "    queryString = queryString + '#' + queueName;",
167             "  }",
168             "  return queryString;",
169             "}")._();
170     }
171 
render(Block html)172     @Override protected void render(Block html) {
173       reopenQueue(html);
174       storeExpandedQueue(html);
175     }
176   }
177 }
178