beeswax.vm.js 21 KB

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