beeswax.vm.js 21 KB

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