beeswax.vm.js 19 KB

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