beeswax.vm.js 17 KB

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