beeswax.vm.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. function BeeswaxViewModel(server, query_id) {
  17. var self = this;
  18. self.server = ko.observable(server);
  19. self.databases = ko.observableArray();
  20. self.selectedDatabase = ko.observable(0);
  21. self.query = ko.mapping.fromJS({
  22. 'id': query_id,
  23. 'query': '',
  24. 'name': null,
  25. 'description': null,
  26. 'settings': [],
  27. 'fileResources': [],
  28. 'functions': [],
  29. 'parameters': [],
  30. 'isParameterized': true,
  31. 'email': false,
  32. 'errors': [],
  33. 'explain': false
  34. });
  35. self.logs = ko.observableArray();
  36. self.explanation = ko.observable();
  37. self.watchURL = ko.observable();
  38. self.resultsURL = ko.observable(null);
  39. self.columns = ko.observableArray();
  40. self.rows = ko.observableArray();
  41. self.resultsEmpty = ko.observable(true);
  42. self.isRunning = ko.observable(false);
  43. self.hasMoreResults = ko.computed(function() {
  44. return self.resultsURL() != null;
  45. });
  46. self.database = ko.computed({
  47. 'read': function() {
  48. if (self.databases()) {
  49. return self.databases()[self.selectedDatabase()];
  50. } else{
  51. return "";
  52. }
  53. },
  54. 'write': function(value) {
  55. self.selectedDatabase(self.databases.indexOf(value));
  56. },
  57. 'deferEvaluation': true
  58. });
  59. self.updateDatabases = function(databases) {
  60. self.databases(databases);
  61. };
  62. self.updateQuery = function(design) {
  63. self.query.query(design.query);
  64. self.query.id(design.id);
  65. self.query.name(design.name);
  66. self.query.description(design.desc);
  67. self.database(design.database);
  68. self.query.isParameterized(design.is_parameterized);
  69. self.query.email(design.email_notify);
  70. self.query.settings.removeAll();
  71. self.query.fileResources.removeAll();
  72. self.query.functions.removeAll();
  73. $.each(design.settings, function(index, setting) {
  74. self.addSetting(setting.key, setting.value);
  75. });
  76. $.each(design.file_resources, function(index, file_resource) {
  77. self.addFileResources(file_resource.type, file_resource.path);
  78. });
  79. $.each(design.functions, function(index, _function) {
  80. self.addFunction(_function.name, _function.class_name);
  81. });
  82. };
  83. self.updateParameters = function(parameters) {
  84. self.query.parameters.removeAll();
  85. $.each(parameters, function(index, parameter) {
  86. self.addParameter(parameter.parameter, parameter.name, '');
  87. });
  88. };
  89. self.addParameter = function(parameter, name, value) {
  90. var obj = {
  91. 'parameter': ko.observable(parameter),
  92. 'name': ko.observable(name),
  93. 'value': ko.observable(value)
  94. };
  95. obj.parameter.subscribe(function() {
  96. self.query.parameters.valueHasMutated();
  97. });
  98. obj.name.subscribe(function() {
  99. self.query.parameters.valueHasMutated();
  100. });
  101. obj.value.subscribe(function() {
  102. self.query.parameters.valueHasMutated();
  103. });
  104. self.query.parameters.push(obj);
  105. };
  106. self.addSetting = function(key, value) {
  107. var obj = {
  108. 'key': ko.observable(key),
  109. 'value': ko.observable(value)
  110. };
  111. obj.key.subscribe(function() {
  112. self.query.settings.valueHasMutated();
  113. });
  114. obj.value.subscribe(function() {
  115. self.query.settings.valueHasMutated();
  116. });
  117. self.query.settings.push(obj);
  118. };
  119. self.removeSetting = function(index) {
  120. self.query.settings.splice(index, 1);
  121. };
  122. self.addFileResources = function(type, path) {
  123. var obj = {
  124. 'type': ko.observable(type),
  125. 'path': ko.observable(path)
  126. };
  127. obj.type.subscribe(function() {
  128. self.query.fileResources.valueHasMutated();
  129. });
  130. obj.path.subscribe(function() {
  131. self.query.fileResources.valueHasMutated();
  132. });
  133. self.query.fileResources.push(obj);
  134. };
  135. self.removeFileResources = function(index) {
  136. self.query.fileResources.splice(index, 1);
  137. };
  138. self.addFunction = function(name, class_name) {
  139. var obj = {
  140. 'name': ko.observable(name),
  141. 'class_name': ko.observable(class_name)
  142. };
  143. obj.name.subscribe(function() {
  144. self.query.functions.valueHasMutated();
  145. });
  146. obj.class_name.subscribe(function() {
  147. self.query.functions.valueHasMutated();
  148. });
  149. self.query.functions.push(obj);
  150. };
  151. self.removeFunction = function(index) {
  152. self.query.functions.splice(index, 1);
  153. };
  154. function getMultiFormData(prefix, arr, members) {
  155. var data = {};
  156. var index = 0;
  157. data[prefix + '-next_form_id'] = arr.length;
  158. ko.utils.arrayForEach(arr, function(obj) {
  159. $.each(members, function(index, member) {
  160. data[prefix + '-' + index + '-' + member] = obj[member]();
  161. });
  162. data[prefix + '-' + index + '-_exists'] = true;
  163. data[prefix + '-' + index + '-_deleted'] = false;
  164. });
  165. return data;
  166. }
  167. self.getSettingsFormData = function() {
  168. return getMultiFormData('settings', self.query.settings(), ['key', 'value']);
  169. };
  170. self.getFileResourcesFormData = function() {
  171. return getMultiFormData('file_resources', self.query.fileResources(), ['type', 'path']);
  172. };
  173. self.getFunctionsFormData = function() {
  174. return getMultiFormData('functions', self.query.functions(), ['name', 'class_name']);
  175. };
  176. self.getParametersFormData = function() {
  177. var data = {};
  178. $.each(self.query.parameters(), function(index, parameter) {
  179. data[parameter.parameter()] = parameter.value();
  180. });
  181. return data;
  182. };
  183. self.getOtherData = function() {
  184. var data = {
  185. 'query-email_notify': self.query.email(),
  186. 'query-is_parameterized': self.query.isParameterized()
  187. };
  188. return data;
  189. };
  190. var error_fn = function(jqXHR, status, errorThrown) {
  191. self.isRunning(false);
  192. try {
  193. $(document).trigger('server.error', $.parseJSON(jqXHR.responseText));
  194. } catch(e) {
  195. $(document).trigger('server.unmanageable_error', jqXHR.responseText);
  196. }
  197. };
  198. self.fetchDatabases = function() {
  199. var request = {
  200. url: '/' + self.server() + '/api/autocomplete',
  201. dataType: 'json',
  202. type: 'GET',
  203. success: function(data) {
  204. self.updateDatabases(data.databases);
  205. },
  206. error: error_fn
  207. };
  208. $.ajax(request);
  209. };
  210. self.fetchQuery = function() {
  211. $(document).trigger('fetch.query');
  212. var request = {
  213. url: '/' + self.server() + '/api/query/' + self.query.id() + '/get',
  214. dataType: 'json',
  215. type: 'GET',
  216. success: function(data) {
  217. self.updateQuery(data.design);
  218. $(document).trigger('fetched.query', data);
  219. },
  220. error: error_fn
  221. };
  222. $.ajax(request);
  223. };
  224. self.fetchParameters = function() {
  225. $(document).trigger('fetch.parameters');
  226. var data = {
  227. 'query-query': self.query.query(),
  228. 'query-database': self.database()
  229. };
  230. $.extend(data, self.getSettingsFormData());
  231. $.extend(data, self.getFileResourcesFormData());
  232. $.extend(data, self.getFunctionsFormData());
  233. $.extend(data, self.getParametersFormData());
  234. $.extend(data, self.getOtherData());
  235. var request = {
  236. url: '/' + self.server() + '/api/query/parameters',
  237. dataType: 'json',
  238. type: 'POST',
  239. success: function(data) {
  240. self.updateParameters(data.parameters);
  241. $(document).trigger('fetched.parameters');
  242. },
  243. error: error_fn,
  244. data: data
  245. };
  246. $.ajax(request);
  247. };
  248. self.explainQuery = function() {
  249. $(document).trigger('explain.query', data);
  250. self.query.explain(true);
  251. self.isRunning(true);
  252. var data = {
  253. 'query-query': self.query.query(),
  254. 'query-database': self.database()
  255. };
  256. $.extend(data, self.getSettingsFormData());
  257. $.extend(data, self.getFileResourcesFormData());
  258. $.extend(data, self.getFunctionsFormData());
  259. $.extend(data, self.getParametersFormData());
  260. $.extend(data, self.getOtherData());
  261. var request = {
  262. url: '/' + self.server() + '/api/query/execute/?explain=true',
  263. dataType: 'json',
  264. type: 'POST',
  265. success: function(data) {
  266. self.query.errors.removeAll();
  267. if (data.status == 0) {
  268. self.logs.removeAll();
  269. self.rows.removeAll();
  270. self.columns.removeAll();
  271. self.explanation(data.explanation);
  272. } else {
  273. self.query.errors.push(data.message);
  274. $(document).trigger('error.query');
  275. }
  276. $(document).trigger('explained.query', data);
  277. self.isRunning(false);
  278. },
  279. error: error_fn,
  280. data: data
  281. };
  282. $.ajax(request);
  283. };
  284. self.executeQuery = function() {
  285. $(document).trigger('execute.query', data);
  286. self.query.explain(false);
  287. self.isRunning(true);
  288. var data = {
  289. 'query-query': self.query.query(),
  290. 'query-database': self.database()
  291. };
  292. $.extend(data, self.getSettingsFormData());
  293. $.extend(data, self.getFileResourcesFormData());
  294. $.extend(data, self.getFunctionsFormData());
  295. $.extend(data, self.getParametersFormData());
  296. $.extend(data, self.getOtherData());
  297. var request = {
  298. url: '/' + self.server() + '/api/query/execute/',
  299. dataType: 'json',
  300. type: 'POST',
  301. success: function(data) {
  302. self.query.errors.removeAll();
  303. if (data.status == 0) {
  304. self.query.id(data.id);
  305. self.logs.removeAll();
  306. self.rows.removeAll();
  307. self.columns.removeAll();
  308. self.resultsURL('/' + self.server() + '/results/' + self.query.id() + '/0?format=json');
  309. self.watchURL(data.watch_url);
  310. self.watchQueryLoop();
  311. } else {
  312. self.query.errors.push(data.message);
  313. $(document).trigger('error.query');
  314. }
  315. $(document).trigger('executed.query', data);
  316. },
  317. error: error_fn,
  318. data: data
  319. };
  320. $.ajax(request);
  321. };
  322. self.watchQuery = function() {
  323. var data = {
  324. 'query-query': self.query.query(),
  325. 'query-database': self.database()
  326. };
  327. $.extend(data, self.getSettingsFormData());
  328. $.extend(data, self.getFileResourcesFormData());
  329. $.extend(data, self.getFunctionsFormData());
  330. $.extend(data, self.getParametersFormData());
  331. $.extend(data, self.getOtherData());
  332. var request = {
  333. url: self.watchURL(),
  334. dataType: 'json',
  335. type: 'POST',
  336. success: function(data) {
  337. $(document).trigger('watched.query', data);
  338. },
  339. error: error_fn,
  340. data: data
  341. };
  342. $.ajax(request);
  343. };
  344. self.watchQueryLoop = function() {
  345. var TIMEOUT = 1000;
  346. var timer = null;
  347. var executed_once = false;
  348. var fn = function() {
  349. $(document).one('watched.query', function(e, data) {
  350. if (executed_once && (data.isSuccess || data.isFailure)) {
  351. clearTimeout(timer);
  352. $(document).trigger('stop_watch.query');
  353. self.fetchResults();
  354. } else {
  355. executed_once = true;
  356. if (data.log) {
  357. self.logs.push(data.log);
  358. // scroll logs
  359. }
  360. timer = setTimeout(fn, TIMEOUT);
  361. }
  362. });
  363. self.watchQuery();
  364. };
  365. $(document).trigger('start_watch.query');
  366. timer = setTimeout(fn, TIMEOUT);
  367. };
  368. self.fetchResults = function() {
  369. $(document).trigger('fetch.results');
  370. var request = {
  371. url: self.resultsURL(),
  372. dataType: 'json',
  373. type: 'GET',
  374. success: function(data) {
  375. self.isRunning(false);
  376. self.columns(data.columns);
  377. self.rows.push.apply(self.rows, data.results);
  378. self.resultsEmpty(self.rows().length == 0);
  379. if (data.has_more) {
  380. self.resultsURL(data.next_json_set);
  381. } else {
  382. self.resultsURL(null);
  383. }
  384. $(document).trigger('fetched.results', data);
  385. },
  386. error: error_fn
  387. };
  388. $.ajax(request);
  389. };
  390. self.saveQuery = function() {
  391. var self = this;
  392. if (self.query.query() && self.query.name()) {
  393. var data = {
  394. 'query-query': self.query.query(),
  395. 'query-database': self.database()
  396. };
  397. $.extend(data, self.getSettingsFormData());
  398. $.extend(data, self.getFileResourcesFormData());
  399. $.extend(data, self.getFunctionsFormData());
  400. $.extend(data, self.getParametersFormData());
  401. $.extend(data, self.getOtherData());
  402. data['saveform-name'] = self.query.name();
  403. data['saveform-desc'] = self.query.description();
  404. if (self.query.id() > 0) {
  405. data['query-id'] = self.query.id();
  406. }
  407. var url = '/' + self.server() + '/api/query/';
  408. if (self.query.id() && self.query.id() != -1) {
  409. url += self.query.id();
  410. }
  411. var request = {
  412. url: url,
  413. dataType: 'json',
  414. type: 'POST',
  415. success: function(data) {
  416. self.query.id(data.design_id);
  417. $(document).trigger('saved.query', data);
  418. },
  419. error: function() {
  420. $(document).trigger('error_save.query');
  421. },
  422. data: data
  423. };
  424. $.ajax(request);
  425. }
  426. };
  427. self.cancelQuery = function() {
  428. $(document).trigger('cancel.query');
  429. var url = '/' + self.server() + '/api/query/' + self.query.id() + '/cancel';
  430. $.post(url,
  431. function(response) {
  432. if (response['status'] != 0) {
  433. $(document).trigger('error_cancel.query', response['message']);
  434. } else {
  435. $(document).trigger('cancelled.query');
  436. }
  437. }
  438. );
  439. };
  440. // Events
  441. // Remove watched query event that watchQueryLoop may be bound to.
  442. $(document).on('server.unmanageable_error', function() {
  443. $(document).off('watched.query');
  444. });
  445. }
  446. // For routie
  447. function showSection(section) {
  448. $('.section').hide();
  449. $('#' + section).show();
  450. $(window).scrollTop(0);
  451. }