beeswax.vm.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. var QUERY_DEFAULTS = {
  19. 'id': query_id,
  20. 'query': '',
  21. 'name': null,
  22. 'description': null,
  23. 'settings': [],
  24. 'fileResources': [],
  25. 'functions': [],
  26. 'parameters': [],
  27. 'isParameterized': true,
  28. 'email': false,
  29. 'errors': [],
  30. 'explain': false,
  31. 'results': {
  32. 'rows': [],
  33. 'columns': [],
  34. 'empty': true,
  35. 'explanation': null,
  36. 'url': null,
  37. 'save': {
  38. 'errors': null,
  39. 'type': 'table',
  40. 'path': null
  41. }
  42. },
  43. 'watch': {
  44. 'logs': [],
  45. 'url': null,
  46. 'errors': []
  47. },
  48. 'isRunning': false,
  49. 'statement': '',
  50. 'isFinished': true
  51. };
  52. self.server = ko.observable(server);
  53. self.databases = ko.observableArray();
  54. self.selectedDatabase = ko.observable(0);
  55. self.query = ko.mapping.fromJS(QUERY_DEFAULTS);
  56. self.hasMoreResults = ko.computed(function() {
  57. return self.query.results.url() != null;
  58. });
  59. self.query.results.save.saveTargetError = ko.computed(function() {
  60. return (self.query.results.save.errors() && 'save_target' in self.query.results.save.errors()) ? self.query.results.save.errors()['save_target'] : null;
  61. });
  62. self.query.results.save.targetTableError = ko.computed(function() {
  63. return (self.query.results.save.errors() && 'target_table' in self.query.results.save.errors()) ? self.query.results.save.errors()['target_table'] : null;
  64. });
  65. self.query.results.save.targetDirectoryError = ko.computed(function() {
  66. return (self.query.results.save.errors() && 'target_dir' in self.query.results.save.errors()) ? self.query.results.save.errors()['target_dir'] : null;
  67. });
  68. self.database = ko.computed({
  69. 'read': function() {
  70. if (self.databases()) {
  71. return self.databases()[self.selectedDatabase()];
  72. } else{
  73. return "";
  74. }
  75. },
  76. 'write': function(value) {
  77. if (value) {
  78. self.selectedDatabase(self.databases.indexOf(value));
  79. }
  80. },
  81. 'deferEvaluation': true
  82. });
  83. self.hasParametersFilled = ko.computed(function() {
  84. var hasBlank = false;
  85. $.each(self.query.parameters(), function(index, element) {
  86. if (element.value() == '') {
  87. hasBlank = true;
  88. }
  89. });
  90. return ! hasBlank;
  91. });
  92. self.resetQuery = function() {
  93. ko.mapping.fromJS(QUERY_DEFAULTS, self.query);
  94. };
  95. self.updateDatabases = function(databases) {
  96. self.databases(databases);
  97. };
  98. self.updateQuery = function(design) {
  99. self.query.query(design.query);
  100. self.query.id(design.id);
  101. self.query.name(design.name);
  102. self.query.description(design.desc);
  103. self.database(design.database);
  104. self.query.isParameterized(design.is_parameterized);
  105. self.query.email(design.email_notify);
  106. self.query.settings.removeAll();
  107. self.query.fileResources.removeAll();
  108. self.query.functions.removeAll();
  109. $.each(design.settings, function(index, setting) {
  110. self.addSetting(setting.key, setting.value);
  111. });
  112. $.each(design.file_resources, function(index, file_resource) {
  113. self.addFileResources(file_resource.type, file_resource.path);
  114. });
  115. $.each(design.functions, function(index, _function) {
  116. self.addFunction(_function.name, _function.class_name);
  117. });
  118. };
  119. self.updateParameters = function(parameters) {
  120. self.query.parameters.removeAll();
  121. $.each(parameters, function(index, parameter) {
  122. self.addParameter(parameter.parameter, parameter.name, '');
  123. });
  124. };
  125. self.addParameter = function(parameter, name, value) {
  126. var obj = {
  127. 'parameter': ko.observable(parameter),
  128. 'name': ko.observable(name),
  129. 'value': ko.observable(value)
  130. };
  131. obj.parameter.subscribe(function() {
  132. self.query.parameters.valueHasMutated();
  133. });
  134. obj.name.subscribe(function() {
  135. self.query.parameters.valueHasMutated();
  136. });
  137. obj.value.subscribe(function() {
  138. self.query.parameters.valueHasMutated();
  139. });
  140. self.query.parameters.push(obj);
  141. };
  142. self.addSetting = function(key, value) {
  143. var obj = {
  144. 'key': ko.observable(key),
  145. 'value': ko.observable(value)
  146. };
  147. obj.key.subscribe(function() {
  148. self.query.settings.valueHasMutated();
  149. });
  150. obj.value.subscribe(function() {
  151. self.query.settings.valueHasMutated();
  152. });
  153. self.query.settings.push(obj);
  154. };
  155. self.removeSetting = function(index) {
  156. self.query.settings.splice(index, 1);
  157. };
  158. self.addFileResources = function(type, path) {
  159. var obj = {
  160. 'type': ko.observable(type),
  161. 'path': ko.observable(path)
  162. };
  163. obj.type.subscribe(function() {
  164. self.query.fileResources.valueHasMutated();
  165. });
  166. obj.path.subscribe(function() {
  167. self.query.fileResources.valueHasMutated();
  168. });
  169. self.query.fileResources.push(obj);
  170. };
  171. self.removeFileResources = function(index) {
  172. self.query.fileResources.splice(index, 1);
  173. };
  174. self.addFunction = function(name, class_name) {
  175. var obj = {
  176. 'name': ko.observable(name),
  177. 'class_name': ko.observable(class_name)
  178. };
  179. obj.name.subscribe(function() {
  180. self.query.functions.valueHasMutated();
  181. });
  182. obj.class_name.subscribe(function() {
  183. self.query.functions.valueHasMutated();
  184. });
  185. self.query.functions.push(obj);
  186. };
  187. self.removeFunction = function(index) {
  188. self.query.functions.splice(index, 1);
  189. };
  190. function getMultiFormData(prefix, arr, members) {
  191. var data = {};
  192. var index = 0;
  193. data[prefix + '-next_form_id'] = arr.length;
  194. ko.utils.arrayForEach(arr, function(obj) {
  195. $.each(members, function(i, member) {
  196. data[prefix + '-' + index + '-' + member] = obj[member]();
  197. });
  198. data[prefix + '-' + index + '-_exists'] = true;
  199. data[prefix + '-' + index++ + '-_deleted'] = false;
  200. });
  201. return data;
  202. }
  203. self.getSettingsFormData = function() {
  204. return getMultiFormData('settings', self.query.settings(), ['key', 'value']);
  205. };
  206. self.getFileResourcesFormData = function() {
  207. return getMultiFormData('file_resources', self.query.fileResources(), ['type', 'path']);
  208. };
  209. self.getFunctionsFormData = function() {
  210. return getMultiFormData('functions', self.query.functions(), ['name', 'class_name']);
  211. };
  212. self.getParametersFormData = function() {
  213. var data = {};
  214. $.each(self.query.parameters(), function(index, parameter) {
  215. data[parameter.parameter()] = parameter.value();
  216. });
  217. return data;
  218. };
  219. self.getOtherData = function() {
  220. var data = {
  221. 'query-email_notify': self.query.email(),
  222. 'query-is_parameterized': self.query.isParameterized()
  223. };
  224. return data;
  225. };
  226. var error_fn = function(jqXHR, status, errorThrown) {
  227. self.query.isRunning(false);
  228. try {
  229. $(document).trigger('server.error', $.parseJSON(jqXHR.responseText));
  230. } catch(e) {
  231. $(document).trigger('server.unmanageable_error', jqXHR.responseText);
  232. }
  233. };
  234. self.fetchDatabases = function() {
  235. var request = {
  236. url: '/' + self.server() + '/api/autocomplete',
  237. dataType: 'json',
  238. type: 'GET',
  239. success: function(data) {
  240. self.updateDatabases(data.databases);
  241. },
  242. error: error_fn
  243. };
  244. $.ajax(request);
  245. };
  246. self.fetchQuery = function() {
  247. $(document).trigger('fetch.query');
  248. var request = {
  249. url: '/' + self.server() + '/api/query/' + self.query.id() + '/get',
  250. dataType: 'json',
  251. type: 'GET',
  252. success: function(data) {
  253. self.updateQuery(data.design);
  254. $(document).trigger('fetched.query', data);
  255. },
  256. error: error_fn
  257. };
  258. $.ajax(request);
  259. };
  260. self.fetchParameters = function() {
  261. $(document).trigger('fetch.parameters');
  262. var data = {
  263. 'query-query': self.query.query(),
  264. 'query-database': self.database()
  265. };
  266. $.extend(data, self.getSettingsFormData());
  267. $.extend(data, self.getFileResourcesFormData());
  268. $.extend(data, self.getFunctionsFormData());
  269. $.extend(data, self.getParametersFormData());
  270. $.extend(data, self.getOtherData());
  271. var request = {
  272. url: '/' + self.server() + '/api/query/parameters',
  273. dataType: 'json',
  274. type: 'POST',
  275. success: function(data) {
  276. self.updateParameters(data.parameters);
  277. $(document).trigger('fetched.parameters');
  278. },
  279. error: error_fn,
  280. data: data
  281. };
  282. $.ajax(request);
  283. };
  284. self.explainQuery = function() {
  285. $(document).trigger('explain.query', data);
  286. self.query.explain(true);
  287. self.query.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/?explain=true',
  299. dataType: 'json',
  300. type: 'POST',
  301. success: function(data) {
  302. self.query.errors.removeAll();
  303. if (data.status == 0) {
  304. self.query.watch.logs.removeAll();
  305. self.query.results.rows.removeAll();
  306. self.query.results.columns.removeAll();
  307. self.query.results.explanation(data.explanation);
  308. } else {
  309. self.query.errors.push(data.message);
  310. $(document).trigger('error.query');
  311. }
  312. $(document).trigger('explained.query', data);
  313. self.query.isRunning(false);
  314. },
  315. error: error_fn,
  316. data: data
  317. };
  318. $.ajax(request);
  319. };
  320. self.executeQuery = function() {
  321. $(document).trigger('execute.query', data);
  322. self.query.explain(false);
  323. self.query.isRunning(true);
  324. self.query.isFinished(true);
  325. self.query.errors.removeAll();
  326. var data = {
  327. 'query-query': self.query.query(),
  328. 'query-database': self.database()
  329. };
  330. $.extend(data, self.getSettingsFormData());
  331. $.extend(data, self.getFileResourcesFormData());
  332. $.extend(data, self.getFunctionsFormData());
  333. $.extend(data, self.getParametersFormData());
  334. $.extend(data, self.getOtherData());
  335. var request = {
  336. url: '/' + self.server() + '/api/query/execute/',
  337. dataType: 'json',
  338. type: 'POST',
  339. success: function(data) {
  340. self.query.errors.removeAll();
  341. if (data.status == 0) {
  342. self.query.id(data.id);
  343. self.query.results.url('/' + self.server() + '/results/' + self.query.id() + '/0?format=json');
  344. self.query.watch.url(data.watch_url);
  345. self.query.statement(data.statement);
  346. self.watchQueryLoop();
  347. } else {
  348. self.query.errors.push(data.message);
  349. self.query.isRunning(false);
  350. $(document).trigger('error.query');
  351. }
  352. $(document).trigger('executed.query', data);
  353. },
  354. error: error_fn,
  355. data: data
  356. };
  357. $.ajax(request);
  358. };
  359. self.executeNextStatement = function() {
  360. $(document).trigger('execute.query', data);
  361. self.query.explain(false);
  362. self.query.isRunning(true);
  363. self.query.isFinished(true);
  364. self.query.errors.removeAll();
  365. var data = {
  366. 'next': true
  367. };
  368. var request = {
  369. url: self.query.watch.url(),
  370. dataType: 'json',
  371. type: 'POST',
  372. success: function(data) {
  373. self.query.errors.removeAll();
  374. self.query.watch.logs.removeAll();
  375. self.query.statement(data.statement);
  376. self.query.watch.url(data.watch_url);
  377. self.query.results.url('/' + self.server() + '/results/' + self.query.id() + '/0?format=json');
  378. self.watchQueryLoop();
  379. $(document).trigger('executed.query', data);
  380. },
  381. error: error_fn,
  382. data: data
  383. };
  384. $.ajax(request);
  385. };
  386. self.watchQuery = function() {
  387. var data = {
  388. 'query-query': self.query.query(),
  389. 'query-database': self.database()
  390. };
  391. $.extend(data, self.getSettingsFormData());
  392. $.extend(data, self.getFileResourcesFormData());
  393. $.extend(data, self.getFunctionsFormData());
  394. $.extend(data, self.getParametersFormData());
  395. $.extend(data, self.getOtherData());
  396. var request = {
  397. url: self.query.watch.url(),
  398. dataType: 'json',
  399. type: 'POST',
  400. success: function(data) {
  401. $(document).trigger('watched.query', data);
  402. },
  403. error: function(jqXHR, status, errorThrown) {
  404. self.query.isRunning(false);
  405. try {
  406. var data = $.parseJSON(jqXHR.responseText);
  407. self.query.errors.push(data.error);
  408. } catch(e) {
  409. $(document).trigger('server.unmanageable_error', jqXHR.responseText);
  410. }
  411. },
  412. data: data
  413. };
  414. $.ajax(request);
  415. };
  416. self.watchQueryLoop = function(fn) {
  417. var TIMEOUT = 1000;
  418. var timer = null;
  419. self.query.watch.logs.removeAll();
  420. self.query.watch.errors.removeAll();
  421. self.query.results.rows.removeAll();
  422. self.query.results.columns.removeAll();
  423. var _fn = function() {
  424. $(document).one('watched.query', function(e, data) {
  425. if (data.isSuccess || data.isFailure) {
  426. clearTimeout(timer);
  427. self.query.isRunning(false);
  428. if (data.log) {
  429. self.query.watch.logs.push(data.log);
  430. // scroll logs
  431. }
  432. if (!data.isFailure) {
  433. $(document).trigger('stop_watch.query');
  434. if (fn) {
  435. fn(data);
  436. } else {
  437. self.fetchResults();
  438. }
  439. }
  440. } else {
  441. self.query.statement(data.statement); // In case new no result statement executed
  442. if (data.log) {
  443. self.query.watch.logs.push(data.log);
  444. // scroll logs
  445. }
  446. timer = setTimeout(_fn, TIMEOUT);
  447. }
  448. });
  449. self.watchQuery();
  450. };
  451. $(document).trigger('start_watch.query');
  452. timer = setTimeout(_fn, TIMEOUT);
  453. };
  454. self.fetchResults = function() {
  455. $(document).trigger('fetch.results');
  456. var request = {
  457. url: self.query.results.url(),
  458. dataType: 'json',
  459. type: 'GET',
  460. success: function(data) {
  461. self.query.isRunning(false);
  462. self.query.isFinished(data.is_finished);
  463. if (self.query.results.columns().length == 0){
  464. self.query.results.columns(data.columns);
  465. }
  466. self.query.results.rows.push.apply(self.query.results.rows, data.results);
  467. self.query.results.empty(self.query.results.rows().length == 0);
  468. if (data.has_more) {
  469. self.query.results.url(data.next_json_set);
  470. } else {
  471. self.query.results.url(null);
  472. }
  473. $(document).trigger('fetched.results', [data]);
  474. },
  475. error: error_fn
  476. };
  477. $.ajax(request);
  478. };
  479. self.saveQuery = function() {
  480. var self = this;
  481. if (self.query.query() && self.query.name()) {
  482. var data = {
  483. 'query-query': self.query.query(),
  484. 'query-database': self.database()
  485. };
  486. $.extend(data, self.getSettingsFormData());
  487. $.extend(data, self.getFileResourcesFormData());
  488. $.extend(data, self.getFunctionsFormData());
  489. $.extend(data, self.getParametersFormData());
  490. $.extend(data, self.getOtherData());
  491. data['saveform-name'] = self.query.name();
  492. data['saveform-desc'] = self.query.description();
  493. if (self.query.id() > 0) {
  494. data['query-id'] = self.query.id();
  495. }
  496. var url = '/' + self.server() + '/api/query/';
  497. if (self.query.id() && self.query.id() != -1) {
  498. url += self.query.id();
  499. }
  500. var request = {
  501. url: url,
  502. dataType: 'json',
  503. type: 'POST',
  504. success: function(data) {
  505. self.query.id(data.design_id);
  506. $(document).trigger('saved.query', [data.design_id]);
  507. },
  508. error: function() {
  509. $(document).trigger('error_save.query');
  510. },
  511. data: data
  512. };
  513. $.ajax(request);
  514. }
  515. };
  516. self.cancelQuery = function() {
  517. $(document).trigger('cancel.query');
  518. var url = '/' + self.server() + '/api/query/' + self.query.id() + '/cancel';
  519. $.post(url,
  520. function(response) {
  521. if (response['status'] != 0) {
  522. $(document).trigger('error_cancel.query', response['message']);
  523. } else {
  524. $(document).trigger('cancelled.query');
  525. }
  526. }
  527. );
  528. };
  529. self.closeQuery = function() {
  530. var self = this;
  531. if (self.query.id()) {
  532. var data = {};
  533. var url = '/' + self.server() + '/api/query/' + self.query.id() + '/close';
  534. var request = {
  535. url: url,
  536. dataType: 'json',
  537. type: 'POST',
  538. success: function(data) {
  539. if (data.status == 0) {
  540. $(document).trigger('closed.query', data);
  541. //self.resetQuery(); // Would fail multiqueries
  542. } else {
  543. $(document).trigger('error_close.query');
  544. }
  545. },
  546. error: function(data) {
  547. $(document).trigger('error_close.results');
  548. },
  549. data: data,
  550. async: false // for multi query
  551. };
  552. $.ajax(request);
  553. }
  554. };
  555. self.saveResults = function() {
  556. var self = this;
  557. if (self.query.id()) {
  558. var data = {
  559. 'type': self.query.results.save.type(),
  560. 'path': self.query.results.save.path()
  561. };
  562. var url = '/' + self.server() + '/api/query/' + self.query.id() + '/results/save';
  563. var request = {
  564. url: url,
  565. dataType: 'json',
  566. type: 'POST',
  567. success: function(data) {
  568. if (data.status == 0) {
  569. self.query.results.save.errors(null);
  570. if (data.id) {
  571. // watch this ID.
  572. self.query.watch.url(data.watch_url);
  573. self.query.watch.logs.removeAll();
  574. self.watchQueryLoop(function() {
  575. window.location.href = "/filebrowser/view" + data.path;
  576. });
  577. } else {
  578. // redirect to metastore app.
  579. window.location.href = "/metastore";
  580. }
  581. $(document).trigger('saved.results', data);
  582. } else {
  583. self.query.results.save.errors(data.errors);
  584. $(document).trigger('error_save.results');
  585. }
  586. },
  587. error: function(data) {
  588. self.query.results.save.errors(data);
  589. $(document).trigger('error_save.results');
  590. },
  591. data: data
  592. };
  593. $.ajax(request);
  594. }
  595. };
  596. // Events
  597. // Remove watched query event that watchQueryLoop may be bound to.
  598. $(document).on('server.unmanageable_error', function() {
  599. $(document).off('watched.query');
  600. });
  601. }
  602. // For routie
  603. function showSection(section) {
  604. $('.section').hide();
  605. $('#' + section).show();
  606. }
  607. // File browser button
  608. function getFileBrowseButton(inputElement) {
  609. return $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  610. e.preventDefault();
  611. $("#filechooser").jHueFileChooser({
  612. initialPath: inputElement.val(),
  613. onFileChoose: function (filePath) {
  614. inputElement.val(filePath);
  615. inputElement.trigger("change");
  616. $("#chooseFile").modal("hide");
  617. },
  618. createFolder: false
  619. });
  620. $("#chooseFile").modal("show");
  621. });
  622. }