beeswax.vm.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. self.design.isFinished(true);
  314. self.design.errors.removeAll();
  315. var data = {
  316. 'query-query': self.design.query(),
  317. 'query-database': self.database()
  318. };
  319. $.extend(data, self.getSettingsFormData());
  320. $.extend(data, self.getFileResourcesFormData());
  321. $.extend(data, self.getFunctionsFormData());
  322. $.extend(data, self.getParametersFormData());
  323. $.extend(data, self.getOtherData());
  324. var request = {
  325. url: '/' + self.server() + '/api/query/execute/?explain=true',
  326. dataType: 'json',
  327. type: 'POST',
  328. success: function(data) {
  329. self.design.errors.removeAll();
  330. if (data.status == 0) {
  331. self.design.watch.logs.removeAll();
  332. self.design.results.rows.removeAll();
  333. self.design.results.columns.removeAll();
  334. self.design.results.explanation(data.explanation);
  335. } else {
  336. self.design.errors.push(data.message);
  337. $(document).trigger('error.query');
  338. }
  339. $(document).trigger('explained.query', data);
  340. self.design.isRunning(false);
  341. },
  342. error: error_fn,
  343. data: data
  344. };
  345. $.ajax(request);
  346. };
  347. self.executeQuery = function() {
  348. $(document).trigger('execute.query', data);
  349. self.design.explain(false);
  350. self.design.isRunning(true);
  351. self.design.isFinished(true);
  352. self.design.errors.removeAll();
  353. var data = {
  354. 'query-query': self.design.query(),
  355. 'query-database': self.database()
  356. };
  357. $.extend(data, self.getSettingsFormData());
  358. $.extend(data, self.getFileResourcesFormData());
  359. $.extend(data, self.getFunctionsFormData());
  360. $.extend(data, self.getParametersFormData());
  361. $.extend(data, self.getOtherData());
  362. var request = {
  363. url: '/' + self.server() + '/api/query/execute/',
  364. dataType: 'json',
  365. type: 'POST',
  366. success: function(data) {
  367. self.design.errors.removeAll();
  368. if (data.status == 0) {
  369. self.design.results.url('/' + self.server() + '/results/' + data.id + '/0?format=json');
  370. self.design.watch.url(data.watch_url);
  371. self.design.statement(data.statement);
  372. self.watchQueryLoop();
  373. } else {
  374. self.design.errors.push(data.message);
  375. self.design.isRunning(false);
  376. $(document).trigger('error.query');
  377. }
  378. $(document).trigger('executed.query', data);
  379. },
  380. error: error_fn,
  381. data: data
  382. };
  383. $.ajax(request);
  384. };
  385. self.executeNextStatement = function() {
  386. $(document).trigger('execute.query', data);
  387. self.design.explain(false);
  388. self.design.isRunning(true);
  389. self.design.isFinished(true);
  390. self.design.errors.removeAll();
  391. var data = {
  392. 'next': true
  393. };
  394. var request = {
  395. url: self.design.watch.url(),
  396. dataType: 'json',
  397. type: 'POST',
  398. success: function(data) {
  399. self.design.errors.removeAll();
  400. self.design.watch.logs.removeAll();
  401. self.design.statement(data.statement);
  402. self.design.watch.url(data.watch_url);
  403. self.design.results.url('/' + self.server() + '/results/' + data.id + '/0?format=json');
  404. self.watchQueryLoop();
  405. $(document).trigger('executed.query', data);
  406. },
  407. error: error_fn,
  408. data: data
  409. };
  410. $.ajax(request);
  411. };
  412. self.watchQuery = function() {
  413. var data = {
  414. 'query-query': self.design.query(),
  415. 'query-database': self.database()
  416. };
  417. $.extend(data, self.getSettingsFormData());
  418. $.extend(data, self.getFileResourcesFormData());
  419. $.extend(data, self.getFunctionsFormData());
  420. $.extend(data, self.getParametersFormData());
  421. $.extend(data, self.getOtherData());
  422. var request = {
  423. url: self.design.watch.url(),
  424. dataType: 'json',
  425. type: 'POST',
  426. success: function(data) {
  427. $(document).trigger('watched.query', data);
  428. },
  429. error: function(jqXHR, status, errorThrown) {
  430. self.design.isRunning(false);
  431. try {
  432. var data = $.parseJSON(jqXHR.responseText);
  433. self.design.errors.push(data.error);
  434. } catch(e) {
  435. $(document).trigger('server.unmanageable_error', jqXHR.responseText);
  436. }
  437. },
  438. data: data
  439. };
  440. $.ajax(request);
  441. };
  442. self.watchQueryLoop = function(fn) {
  443. var TIMEOUT = 1000;
  444. var timer = null;
  445. self.design.watch.logs.removeAll();
  446. self.design.watch.errors.removeAll();
  447. self.design.results.rows.removeAll();
  448. self.design.results.columns.removeAll();
  449. var _fn = function() {
  450. $(document).one('watched.query', function(e, data) {
  451. if (data.isSuccess || data.isFailure) {
  452. clearTimeout(timer);
  453. self.design.isRunning(false);
  454. if (data.log) {
  455. self.design.watch.logs.push(data.log);
  456. // scroll logs
  457. }
  458. if (!data.isFailure) {
  459. $(document).trigger('stop_watch.query');
  460. if (fn) {
  461. fn(data);
  462. } else {
  463. self.fetchResults();
  464. }
  465. }
  466. } else {
  467. self.design.statement(data.statement); // In case new no result statement executed
  468. if (data.log) {
  469. self.design.watch.logs.push(data.log);
  470. // scroll logs
  471. }
  472. timer = setTimeout(_fn, TIMEOUT);
  473. }
  474. });
  475. self.watchQuery();
  476. };
  477. $(document).trigger('start_watch.query');
  478. timer = setTimeout(_fn, TIMEOUT);
  479. };
  480. self.fetchResults = function() {
  481. $(document).trigger('fetch.results');
  482. self.design.results.errors.removeAll();
  483. var request = {
  484. url: self.design.results.url(),
  485. dataType: 'json',
  486. type: 'GET',
  487. success: function(data) {
  488. if (data.error) {
  489. self.design.results.errors.push(data.message);
  490. self.design.isRunning(false);
  491. self.design.results.empty(true);
  492. } else {
  493. self.design.isRunning(false);
  494. self.design.isFinished(data.is_finished);
  495. if (self.design.results.columns().length == 0){
  496. self.design.results.columns(data.columns);
  497. }
  498. self.design.results.rows.push.apply(self.design.results.rows, data.results);
  499. self.design.results.empty(self.design.results.rows().length == 0);
  500. if (data.has_more) {
  501. self.design.results.url(data.next_json_set);
  502. } else {
  503. self.design.results.url(null);
  504. }
  505. }
  506. $(document).trigger('fetched.results', [data]);
  507. },
  508. error: error_fn
  509. };
  510. $.ajax(request);
  511. };
  512. self.saveDesign = function() {
  513. var self = this;
  514. if (self.design.query() && self.design.name()) {
  515. var data = {
  516. 'query-query': self.design.query(),
  517. 'query-database': self.database()
  518. };
  519. $.extend(data, self.getSettingsFormData());
  520. $.extend(data, self.getFileResourcesFormData());
  521. $.extend(data, self.getFunctionsFormData());
  522. $.extend(data, self.getParametersFormData());
  523. $.extend(data, self.getOtherData());
  524. data['saveform-name'] = self.design.name();
  525. data['saveform-desc'] = self.design.description();
  526. if (self.design.id() > 0) {
  527. data['query-id'] = self.design.id();
  528. }
  529. var url = '/' + self.server() + '/api/design/';
  530. if (self.design.id() && self.design.id() != -1) {
  531. url += self.design.id();
  532. }
  533. var request = {
  534. url: url,
  535. dataType: 'json',
  536. type: 'POST',
  537. success: function(data) {
  538. self.design.id(data.design_id);
  539. $(document).trigger('saved.design', [data.design_id]);
  540. },
  541. error: function() {
  542. $(document).trigger('error_save.design');
  543. },
  544. data: data
  545. };
  546. $.ajax(request);
  547. }
  548. };
  549. self.cancelQuery = function() {
  550. $(document).trigger('cancel.query');
  551. var url = '/' + self.server() + '/api/query/' + self.design.id() + '/cancel';
  552. $.post(url,
  553. function(response) {
  554. if (response['status'] != 0) {
  555. $(document).trigger('error_cancel.query', response['message']);
  556. } else {
  557. $(document).trigger('cancelled.query');
  558. }
  559. }
  560. );
  561. };
  562. self.closeQuery = function() {
  563. var self = this;
  564. if (self.design.id()) {
  565. var data = {};
  566. var url = '/' + self.server() + '/api/query/' + self.design.id() + '/close';
  567. var request = {
  568. url: url,
  569. dataType: 'json',
  570. type: 'POST',
  571. success: function(data) {
  572. if (data.status == 0) {
  573. $(document).trigger('closed.query', data);
  574. //self.resetQuery(); // Would fail multiqueries
  575. } else {
  576. $(document).trigger('error_close.query');
  577. }
  578. },
  579. error: function(data) {
  580. $(document).trigger('error_close.results');
  581. },
  582. data: data,
  583. async: false // for multi query
  584. };
  585. $.ajax(request);
  586. }
  587. };
  588. self.saveResults = function() {
  589. var self = this;
  590. if (self.design.id()) {
  591. var data = {
  592. 'type': self.design.results.save.type(),
  593. 'path': self.design.results.save.path()
  594. };
  595. var url = '/' + self.server() + '/api/query/' + self.design.id() + '/results/save';
  596. var request = {
  597. url: url,
  598. dataType: 'json',
  599. type: 'POST',
  600. success: function(data) {
  601. if (data.status == 0) {
  602. self.design.results.save.errors(null);
  603. if (data.id) {
  604. // watch this ID.
  605. self.design.watch.url(data.watch_url);
  606. self.design.watch.logs.removeAll();
  607. self.watchQueryLoop(function() {
  608. window.location.href = "/filebrowser/view" + data.path;
  609. });
  610. } else {
  611. // redirect to metastore app.
  612. window.location.href = "/metastore";
  613. }
  614. $(document).trigger('saved.results', data);
  615. } else {
  616. self.design.results.save.errors(data.errors);
  617. $(document).trigger('error_save.results');
  618. }
  619. },
  620. error: function(data) {
  621. self.design.results.save.errors(data);
  622. $(document).trigger('error_save.results');
  623. },
  624. data: data
  625. };
  626. $.ajax(request);
  627. }
  628. };
  629. // Events
  630. // Remove watched query event that watchQueryLoop may be bound to.
  631. $(document).on('server.unmanageable_error', function() {
  632. $(document).off('watched.query');
  633. });
  634. }
  635. // For routie
  636. function showSection(section) {
  637. $('.section').hide();
  638. $('#' + section).show();
  639. }
  640. // File browser button
  641. function getFileBrowseButton(inputElement) {
  642. return $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  643. e.preventDefault();
  644. $("#filechooser").jHueFileChooser({
  645. initialPath: inputElement.val(),
  646. onFileChoose: function (filePath) {
  647. inputElement.val(filePath);
  648. inputElement.trigger("change");
  649. $("#chooseFile").modal("hide");
  650. },
  651. createFolder: false
  652. });
  653. $("#chooseFile").modal("show");
  654. });
  655. }