beeswax.vm.js 18 KB

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