beeswax.vm.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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. 'value': '',
  22. 'errors': []
  23. },
  24. 'name': null,
  25. 'description': null,
  26. 'settings': {
  27. 'values': [],
  28. 'errors': []
  29. },
  30. 'fileResources': {
  31. 'values': [],
  32. 'errors': []
  33. },
  34. 'functions': {
  35. 'values': [],
  36. 'errors': []
  37. },
  38. 'parameters': [],
  39. 'isParameterized': true,
  40. 'email': false,
  41. 'errors': [],
  42. 'explain': false,
  43. 'history': {
  44. 'id': -1
  45. },
  46. 'results': {
  47. 'rows': [],
  48. 'columns': [],
  49. 'empty': true,
  50. 'expired': false,
  51. 'explanation': null,
  52. 'url': null,
  53. 'errors': [],
  54. 'save': {
  55. 'errors': null,
  56. 'type': 'hdfs-file',
  57. 'path': null,
  58. 'overwrite': true
  59. }
  60. },
  61. 'watch': {
  62. 'logs': [],
  63. 'jobUrls': [],
  64. 'url': null,
  65. 'errors': []
  66. },
  67. 'isRunning': false,
  68. 'statement': '',
  69. 'isFinished': true
  70. };
  71. self.design = ko.mapping.fromJS(DESIGN_DEFAULTS);
  72. self.chartType = ko.observable("bars");
  73. self.chartData = ko.observableArray();
  74. self.server = ko.observable(server);
  75. self.databases = ko.observableArray();
  76. self.selectedDatabase = ko.observable(0);
  77. self.isReady = ko.observable(false);
  78. // Use a view model attribute so that we don't have to override KO.
  79. // This allows Hue to disable the execute button until the query placeholder dies.
  80. self.queryEditorBlank = ko.observable(false);
  81. self.canExecute = ko.computed(function() {
  82. return !self.design.isRunning() && self.design.isFinished();
  83. });
  84. self.hasMoreResults = ko.computed(function() {
  85. return self.design.results.url() != null;
  86. });
  87. self.design.results.save.saveTargetError = ko.computed(function() {
  88. return (self.design.results.save.errors() && 'save_target' in self.design.results.save.errors()) ? self.design.results.save.errors()['save_target'] : null;
  89. });
  90. self.design.results.save.targetTableError = ko.computed(function() {
  91. return (self.design.results.save.errors() && 'target_table' in self.design.results.save.errors()) ? self.design.results.save.errors()['target_table'] : null;
  92. });
  93. self.design.results.save.targetDirectoryError = ko.computed(function() {
  94. return (self.design.results.save.errors() && 'target_dir' in self.design.results.save.errors()) ? self.design.results.save.errors()['target_dir'] : null;
  95. });
  96. self.design.results.save.targetFileError = ko.computed(function() {
  97. return (self.design.results.save.errors() && 'target_file' in self.design.results.save.errors()) ? self.design.results.save.errors()['target_file'] : null;
  98. });
  99. self.database = ko.computed({
  100. 'read': function() {
  101. if (self.databases()) {
  102. return self.databases()[self.selectedDatabase()];
  103. } else{
  104. return "";
  105. }
  106. },
  107. 'write': function(value) {
  108. if (value) {
  109. self.selectedDatabase(self.databases.indexOf(value));
  110. }
  111. },
  112. 'deferEvaluation': true
  113. });
  114. self.hasParametersFilled = ko.computed(function() {
  115. var hasBlank = false;
  116. $.each(self.design.parameters(), function(index, element) {
  117. if (element.value() == '') {
  118. hasBlank = true;
  119. }
  120. });
  121. return ! hasBlank;
  122. });
  123. self.hasResults = ko.computed(function() {
  124. return !self.design.results.empty() && !self.design.results.expired();
  125. });
  126. self.resetQuery = function() {
  127. ko.mapping.fromJS(QUERY_DEFAULTS, self.design);
  128. };
  129. self.resetErrors = function() {
  130. self.design.errors.removeAll();
  131. self.design.query.errors.removeAll();
  132. self.design.settings.errors.removeAll();
  133. self.design.fileResources.errors.removeAll();
  134. self.design.functions.errors.removeAll();
  135. };
  136. self.setErrors = function(message, errors) {
  137. self.resetErrors();
  138. if (message) {
  139. self.design.errors.push(message);
  140. }
  141. if (errors) {
  142. self.design.query.errors.push.apply(self.design.query.errors, errors.query);
  143. self.design.settings.errors.push.apply(self.design.settings.errors, errors.settings);
  144. self.design.fileResources.errors.push.apply(self.design.fileResources.errors, errors.file_resources);
  145. self.design.functions.errors.push.apply(self.design.functions.errors, errors.functions);
  146. }
  147. };
  148. self.updateDatabases = function(databases) {
  149. if (databases) {
  150. var i = databases.indexOf("_impala_builtins"); // Blacklist of system databases
  151. if (i != -1) {
  152. databases.splice(i, 1);
  153. }
  154. self.databases(databases);
  155. }
  156. };
  157. self.updateDesign = function(design) {
  158. self.design.query.value(design.query);
  159. self.design.id(design.id);
  160. self.design.name(design.name);
  161. self.design.description(design.desc);
  162. self.database(design.database);
  163. self.design.isParameterized(design.is_parameterized);
  164. self.design.email(design.email_notify);
  165. self.design.settings.values.removeAll();
  166. self.design.fileResources.values.removeAll();
  167. self.design.functions.values.removeAll();
  168. $.each(design.settings, function(index, setting) {
  169. self.addSetting(setting.key, setting.value);
  170. });
  171. $.each(design.file_resources, function(index, file_resource) {
  172. self.addFileResource(file_resource.type, file_resource.path);
  173. });
  174. $.each(design.functions, function(index, _function) {
  175. self.addFunction(_function.name, _function.class_name);
  176. });
  177. };
  178. self.updateHistory = function(history) {
  179. self.design.history.id(history.id);
  180. self.design.results.url('/' + self.server() + '/results/' + history.id + '/0?format=json');
  181. self.design.watch.url('/' + self.server() + '/api/watch/json/' + history.id);
  182. if (history.design) {
  183. self.updateDesign(history.design);
  184. }
  185. };
  186. self.updateParameters = function(parameters) {
  187. self.design.parameters.removeAll();
  188. $.each(parameters, function(index, parameter) {
  189. self.addParameter(parameter.parameter, parameter.name, '');
  190. });
  191. };
  192. self.addParameter = function(parameter, name, value) {
  193. var obj = {
  194. 'parameter': ko.observable(parameter),
  195. 'name': ko.observable(name),
  196. 'value': ko.observable(value)
  197. };
  198. obj.parameter.subscribe(function() {
  199. self.design.parameters.valueHasMutated();
  200. });
  201. obj.name.subscribe(function() {
  202. self.design.parameters.valueHasMutated();
  203. });
  204. obj.value.subscribe(function() {
  205. self.design.parameters.valueHasMutated();
  206. });
  207. self.design.parameters.push(obj);
  208. };
  209. self.addSetting = function(key, value) {
  210. var obj = {
  211. 'key': ko.observable(key),
  212. 'value': ko.observable(value)
  213. };
  214. obj.key.subscribe(function() {
  215. self.design.settings.values.valueHasMutated();
  216. });
  217. obj.value.subscribe(function() {
  218. self.design.settings.values.valueHasMutated();
  219. });
  220. self.design.settings.values.push(obj);
  221. };
  222. self.removeSetting = function(index) {
  223. self.design.settings.values.splice(index, 1);
  224. };
  225. self.addFileResource = function(type, path) {
  226. var obj = {
  227. 'type': ko.observable(type),
  228. 'path': ko.observable(path)
  229. };
  230. obj.type.subscribe(function() {
  231. self.design.fileResources.values.valueHasMutated();
  232. });
  233. obj.path.subscribe(function() {
  234. self.design.fileResources.values.valueHasMutated();
  235. });
  236. self.design.fileResources.values.push(obj);
  237. };
  238. self.removeFileResource = function(index) {
  239. self.design.fileResources.values.splice(index, 1);
  240. };
  241. self.addFunction = function(name, class_name) {
  242. var obj = {
  243. 'name': ko.observable(name),
  244. 'class_name': ko.observable(class_name)
  245. };
  246. obj.name.subscribe(function() {
  247. self.design.functions.values.valueHasMutated();
  248. });
  249. obj.class_name.subscribe(function() {
  250. self.design.functions.values.valueHasMutated();
  251. });
  252. self.design.functions.values.push(obj);
  253. };
  254. self.removeFunction = function(index) {
  255. self.design.functions.values.splice(index, 1);
  256. };
  257. var advancedErrorHandling = function(advanced_parameter, key) {
  258. return function(index) {
  259. var errors = self.design[advanced_parameter].errors();
  260. if (errors && errors[index]) {
  261. if (key in errors[index]) {
  262. return errors[index][key];
  263. } else {
  264. return [];
  265. }
  266. } else {
  267. return [];
  268. }
  269. };
  270. };
  271. self.getQueryErrors = advancedErrorHandling('query', 'query');
  272. self.getSettingKeyErrors = advancedErrorHandling('settings', 'key');
  273. self.getSettingValueErrors = advancedErrorHandling('settings', 'value');
  274. self.getFileResourceTypeErrors = advancedErrorHandling('fileResources', 'type');
  275. self.getFileResourcePathErrors = advancedErrorHandling('fileResources', 'path');
  276. self.getFunctionNameErrors = advancedErrorHandling('functions', 'name');
  277. self.getFunctionClassNameErrors = advancedErrorHandling('functions', 'class_name');
  278. function getMultiFormData(prefix, arr, members) {
  279. var data = {};
  280. var index = 0;
  281. data[prefix + '-next_form_id'] = arr.length;
  282. ko.utils.arrayForEach(arr, function(obj) {
  283. $.each(members, function(i, member) {
  284. data[prefix + '-' + index + '-' + member] = obj[member]();
  285. });
  286. data[prefix + '-' + index + '-_exists'] = true;
  287. data[prefix + '-' + index++ + '-_deleted'] = false;
  288. });
  289. return data;
  290. }
  291. self.getSettingsFormData = function() {
  292. return getMultiFormData('settings', self.design.settings.values(), ['key', 'value']);
  293. };
  294. self.getFileResourcesFormData = function() {
  295. return getMultiFormData('file_resources', self.design.fileResources.values(), ['type', 'path']);
  296. };
  297. self.getFunctionsFormData = function() {
  298. return getMultiFormData('functions', self.design.functions.values(), ['name', 'class_name']);
  299. };
  300. self.getParametersFormData = function() {
  301. var data = {};
  302. $.each(self.design.parameters(), function(index, parameter) {
  303. data[parameter.parameter()] = parameter.value();
  304. });
  305. return data;
  306. };
  307. self.getOtherData = function() {
  308. var data = {
  309. 'query-email_notify': self.design.email(),
  310. 'query-is_parameterized': self.design.isParameterized()
  311. };
  312. return data;
  313. };
  314. var error_fn = function(jqXHR, status, errorThrown) {
  315. self.design.isRunning(false);
  316. try {
  317. $(document).trigger('server.error', $.parseJSON(jqXHR.responseText));
  318. } catch(e) {
  319. $(document).trigger('server.unmanageable_error', jqXHR.responseText);
  320. }
  321. };
  322. self.fetchDesign = function() {
  323. $(document).trigger('fetch.design');
  324. var request = {
  325. url: '/' + self.server() + '/api/design/' + self.design.id() + '/get',
  326. dataType: 'json',
  327. type: 'GET',
  328. success: function(data) {
  329. self.updateDesign(data.design);
  330. self.database(data.design.database);
  331. $(document).trigger('fetched.design', [data]);
  332. },
  333. error: error_fn,
  334. cache: false
  335. };
  336. $.ajax(request);
  337. };
  338. self.fetchQueryHistory = function() {
  339. $(document).trigger('fetch.query');
  340. var request = {
  341. url: '/' + self.server() + '/api/query/' + self.design.history.id() + '/get',
  342. dataType: 'json',
  343. type: 'GET',
  344. success: function(data) {
  345. self.updateHistory(data.query_history);
  346. self.database(data.query_history.database);
  347. $(document).trigger('fetched.query', [data]);
  348. },
  349. error: error_fn,
  350. cache: false
  351. };
  352. $.ajax(request);
  353. };
  354. self.fetchParameters = function() {
  355. $(document).trigger('fetch.parameters');
  356. var data = {
  357. 'query-query': self.design.query.value(),
  358. 'query-database': self.database()
  359. };
  360. $.extend(data, self.getSettingsFormData());
  361. $.extend(data, self.getFileResourcesFormData());
  362. $.extend(data, self.getFunctionsFormData());
  363. $.extend(data, self.getParametersFormData());
  364. $.extend(data, self.getOtherData());
  365. var request = {
  366. url: '/' + self.server() + '/api/query/parameters',
  367. dataType: 'json',
  368. type: 'POST',
  369. success: function(data) {
  370. self.updateParameters(data.parameters);
  371. $(document).trigger('fetched.parameters');
  372. },
  373. error: error_fn,
  374. data: data,
  375. cache: false
  376. };
  377. $.ajax(request);
  378. };
  379. self.explainQuery = function() {
  380. $(document).trigger('explain.query', data);
  381. self.design.explain(true);
  382. self.design.isRunning(true);
  383. self.design.isFinished(true);
  384. self.design.results.expired(false);
  385. self.design.results.empty(false);
  386. self.resetErrors();
  387. var data = {
  388. 'query-query': self.design.query.value(),
  389. 'query-database': self.database()
  390. };
  391. $.extend(data, self.getSettingsFormData());
  392. $.extend(data, self.getFileResourcesFormData());
  393. $.extend(data, self.getFunctionsFormData());
  394. $.extend(data, self.getParametersFormData());
  395. $.extend(data, self.getOtherData());
  396. var request = {
  397. url: '/' + self.server() + '/api/query/execute/?explain=true',
  398. dataType: 'json',
  399. type: 'POST',
  400. success: function(data) {
  401. if (data.status == 0) {
  402. self.design.watch.logs.removeAll();
  403. self.design.watch.jobUrls.removeAll();
  404. self.design.statement(data.statement);
  405. self.design.results.rows.removeAll();
  406. self.design.results.columns.removeAll();
  407. self.design.results.explanation(data.explanation);
  408. } else {
  409. self.setErrors(data.message);
  410. $(document).trigger('error.query');
  411. }
  412. $(document).trigger('explained.query', data);
  413. self.design.isRunning(false);
  414. },
  415. error: error_fn,
  416. data: data,
  417. cache: false
  418. };
  419. $.ajax(request);
  420. };
  421. self.executeQuery = function() {
  422. $(document).trigger('execute.query', data);
  423. self.design.explain(false);
  424. self.design.isRunning(true);
  425. self.design.isFinished(true);
  426. self.design.results.expired(false);
  427. self.design.results.empty(false);
  428. self.resetErrors();
  429. var data = {
  430. 'query-query': self.design.query.value(),
  431. 'query-database': self.database()
  432. };
  433. $.extend(data, self.getSettingsFormData());
  434. $.extend(data, self.getFileResourcesFormData());
  435. $.extend(data, self.getFunctionsFormData());
  436. $.extend(data, self.getParametersFormData());
  437. $.extend(data, self.getOtherData());
  438. var url = '/' + self.server() + '/api/query/execute/';
  439. if (self.design.id() > -1) {
  440. url += self.design.id();
  441. }
  442. var request = {
  443. url: url,
  444. dataType: 'json',
  445. type: 'POST',
  446. success: function(data) {
  447. self.design.errors.removeAll();
  448. self.design.watch.errors.removeAll();
  449. if (data.status == 0) {
  450. self.design.results.url('/' + self.server() + '/results/' + data.id + '/0?format=json');
  451. self.design.watch.url(data.watch_url);
  452. self.design.statement(data.statement);
  453. self.design.history.id(data.id);
  454. self.watchQueryLoop();
  455. $(document).trigger('executed.query', data);
  456. } else {
  457. self.setErrors(data.message, data.errors);
  458. self.design.isRunning(false);
  459. $(document).trigger('error.query');
  460. }
  461. },
  462. error: error_fn,
  463. data: data,
  464. cache: false
  465. };
  466. $.ajax(request);
  467. };
  468. self.executeNextStatement = function() {
  469. $(document).trigger('execute.query', data);
  470. self.design.explain(false);
  471. self.design.isRunning(true);
  472. self.design.isFinished(true);
  473. self.resetErrors();
  474. var data = {
  475. 'next': true,
  476. 'query-query': self.design.query.value(),
  477. };
  478. var request = {
  479. url: self.design.watch.url(),
  480. dataType: 'json',
  481. type: 'POST',
  482. success: function(data) {
  483. if (data.status == 0) {
  484. self.design.watch.logs.removeAll();
  485. self.design.watch.jobUrls.removeAll();
  486. self.design.statement(data.statement);
  487. self.design.watch.url(data.watch_url);
  488. self.design.results.url('/' + self.server() + '/results/' + data.id + '/0?format=json');
  489. self.watchQueryLoop();
  490. $(document).trigger('executed.query', data);
  491. } else {
  492. self.setErrors(data.message, data.errors);
  493. self.design.isRunning(false);
  494. self.design.isFinished(false); // We propose to skip the failed statement
  495. $(document).trigger('error.query');
  496. }
  497. },
  498. error: error_fn,
  499. data: data,
  500. cache: false
  501. };
  502. $.ajax(request);
  503. };
  504. self.watchQuery = function() {
  505. var data = {
  506. 'query-query': self.design.query.value(),
  507. 'query-database': self.database()
  508. };
  509. $.extend(data, self.getSettingsFormData());
  510. $.extend(data, self.getFileResourcesFormData());
  511. $.extend(data, self.getFunctionsFormData());
  512. $.extend(data, self.getParametersFormData());
  513. $.extend(data, self.getOtherData());
  514. var request = {
  515. url: self.design.watch.url(),
  516. dataType: 'json',
  517. type: 'POST',
  518. success: function(data) {
  519. $(document).trigger('watched.query', data);
  520. },
  521. error: function(jqXHR, status, errorThrown) {
  522. self.design.isRunning(false);
  523. try {
  524. var data = $.parseJSON(jqXHR.responseText);
  525. self.design.watch.errors.push(data.error);
  526. } catch(e) {
  527. $(document).trigger('server.unmanageable_error', jqXHR.responseText);
  528. }
  529. },
  530. data: data,
  531. cache: false
  532. };
  533. $.ajax(request);
  534. };
  535. self.watchQueryLoop = function(fn) {
  536. var TIMEOUT = 100;
  537. var timer = null;
  538. self.design.isRunning(true);
  539. self.design.watch.logs.removeAll();
  540. self.design.watch.jobUrls.removeAll();
  541. self.design.results.rows.removeAll();
  542. self.design.results.columns.removeAll();
  543. self.resetErrors();
  544. var _fn = function() {
  545. $(document).one('watched.query', function(e, data) {
  546. var failed = data.isFailure || data.status != 0;
  547. if (data.isSuccess || failed) {
  548. clearTimeout(timer);
  549. self.design.isRunning(false);
  550. if (data.log) {
  551. self.design.watch.logs(data.log.split("\n"));
  552. // scroll logs
  553. self.design.watch.jobUrls(data.jobUrls);
  554. }
  555. if (!failed) {
  556. $(document).trigger('stop_watch.query');
  557. if (fn) {
  558. fn(data);
  559. } else {
  560. self.fetchResults();
  561. }
  562. }
  563. } else {
  564. self.design.statement(data.statement); // In case new no result statement executed
  565. if (data.log) {
  566. self.design.watch.logs(data.log.split("\n"));
  567. // scroll logs
  568. self.design.watch.jobUrls(data.jobUrls);
  569. }
  570. TIMEOUT = Math.min(TIMEOUT + 100, 2000);
  571. timer = setTimeout(_fn, TIMEOUT);
  572. }
  573. });
  574. self.watchQuery();
  575. };
  576. $(document).trigger('start_watch.query');
  577. timer = setTimeout(_fn, TIMEOUT);
  578. };
  579. self.fetchResults = function() {
  580. $(document).trigger('fetch.results');
  581. self.design.errors.removeAll();
  582. self.design.results.errors.removeAll();
  583. var request = {
  584. url: self.design.results.url(),
  585. dataType: 'text',
  586. type: 'GET',
  587. success: function(data) {
  588. data = JSON.bigdataParse(data);
  589. if (data.error) {
  590. self.design.results.errors.push(data.message);
  591. self.design.isRunning(false);
  592. self.design.results.empty(true);
  593. } else {
  594. self.design.isRunning(false);
  595. self.design.isFinished(data.is_finished);
  596. if (self.design.results.columns().length == 0){
  597. if (data.columns != null){
  598. data.columns.unshift({ type:"INT_TYPE", name:"", comment:null});
  599. }
  600. self.design.results.columns(data.columns ? data.columns : []); // Some querysets have empty or null for columns
  601. }
  602. self.design.results.rows.push.apply(self.design.results.rows, data.results);
  603. self.design.results.empty(self.design.results.rows().length == 0);
  604. if (data.has_more) {
  605. self.design.results.url(data.next_json_set);
  606. } else {
  607. self.design.results.url(null);
  608. }
  609. }
  610. $(document).trigger('fetched.results', [data]);
  611. },
  612. error: error_fn,
  613. cache: false
  614. };
  615. $.ajax(request);
  616. };
  617. self.saveDesign = function() {
  618. var self = this;
  619. if (self.design.query.value() && self.design.name()) {
  620. var data = {
  621. 'query-query': self.design.query.value(),
  622. 'query-database': self.database()
  623. };
  624. $.extend(data, self.getSettingsFormData());
  625. $.extend(data, self.getFileResourcesFormData());
  626. $.extend(data, self.getFunctionsFormData());
  627. $.extend(data, self.getParametersFormData());
  628. $.extend(data, self.getOtherData());
  629. data['saveform-name'] = self.design.name();
  630. data['saveform-desc'] = self.design.description();
  631. if (self.design.id() > 0) {
  632. data['query-id'] = self.design.id();
  633. }
  634. var url = '/' + self.server() + '/api/design/';
  635. if (self.design.id() && self.design.id() != -1) {
  636. url += self.design.id();
  637. }
  638. var request = {
  639. url: url,
  640. dataType: 'json',
  641. type: 'POST',
  642. success: function(data) {
  643. if (data.status == 0) {
  644. self.design.id(data.design_id);
  645. $(document).trigger('saved.design', [data.design_id]);
  646. } else {
  647. self.setErrors("", data.errors);
  648. $(document).trigger('error_save.design', [data.message]);
  649. }
  650. },
  651. error: function() {
  652. $(document).trigger('error_save.design');
  653. },
  654. data: data,
  655. cache: false
  656. };
  657. $.ajax(request);
  658. }
  659. };
  660. self.cancelQuery = function() {
  661. if (self.design.history.id() > 0) {
  662. $(document).trigger('cancel.query');
  663. var url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/cancel';
  664. $.post(url,
  665. function(response) {
  666. if (response['status'] != 0) {
  667. $(document).trigger('error_cancel.query', response['message']);
  668. } else {
  669. $(document).trigger('cancelled.query');
  670. }
  671. }
  672. );
  673. }
  674. };
  675. self.closeQuery = function() {
  676. var self = this;
  677. if (self.design.history.id() > 0 && !self.design.isRunning()) {
  678. var data = {};
  679. var url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/close';
  680. var request = {
  681. url: url,
  682. dataType: 'json',
  683. type: 'POST',
  684. success: function(data) {
  685. if (data.status == 0) {
  686. $(document).trigger('closed.query', data);
  687. //self.resetQuery(); // Would fail multiqueries
  688. } else {
  689. $(document).trigger('error_close.query');
  690. }
  691. },
  692. error: function(data) {
  693. $(document).trigger('error_close.results');
  694. },
  695. data: data,
  696. async: false, // for multi query
  697. cache: false
  698. };
  699. $.ajax(request);
  700. }
  701. };
  702. self.saveResults = function() {
  703. var self = this;
  704. self.design.isRunning(true);
  705. self.resetErrors();
  706. if (self.design.id()) {
  707. var data, url;
  708. switch(self.design.results.save.type()) {
  709. case 'hdfs-directory':
  710. data = {
  711. 'server': self.server(),
  712. 'path': self.design.results.save.path()
  713. };
  714. url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/results/save/hdfs/directory';
  715. break;
  716. case 'hdfs-file':
  717. data = {
  718. 'server': self.server(),
  719. 'path': self.design.results.save.path(),
  720. 'overwrite': self.design.results.save.overwrite()
  721. };
  722. url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/results/save/hdfs/file';
  723. break;
  724. // case 'hive-table':
  725. default:
  726. data = {
  727. 'database': self.database(),
  728. 'server': self.server(),
  729. 'table': self.design.results.save.path()
  730. };
  731. url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/results/save/hive/table';
  732. break;
  733. }
  734. var request = {
  735. url: url,
  736. dataType: 'json',
  737. type: 'POST',
  738. success: function(data) {
  739. if (data.status == 0) {
  740. self.design.results.save.errors(null);
  741. var redirect_fn = function() {
  742. window.location.href = data.success_url;
  743. self.design.isRunning(false);
  744. };
  745. if (data.id) {
  746. // watch this ID.
  747. self.design.watch.url(data.watch_url);
  748. self.design.watch.logs.removeAll();
  749. self.design.watch.jobUrls.removeAll();
  750. self.watchQueryLoop(redirect_fn);
  751. } else {
  752. // redirect to metastore app.
  753. redirect_fn();
  754. }
  755. $(document).trigger('saved.results', data);
  756. } else {
  757. self.design.isRunning(false);
  758. self.design.results.save.errors(data.errors);
  759. $(document).trigger('error_save.results', [data.message]);
  760. }
  761. },
  762. error: function(data) {
  763. self.design.isRunning(false);
  764. self.design.results.save.errors(data);
  765. $(document).trigger('error_save.results', [data]);
  766. },
  767. data: data,
  768. cache: false
  769. };
  770. return $.ajax(request);
  771. }
  772. };
  773. // Events
  774. // Remove watched query event that watchQueryLoop may be bound to.
  775. $(document).on('server.unmanageable_error', function() {
  776. $(document).off('watched.query');
  777. });
  778. }
  779. // For routie
  780. function showSection(section) {
  781. $('.section').hide();
  782. $('#' + section).show();
  783. }
  784. // File browser button
  785. function getFileBrowseButton(inputElement) {
  786. return $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  787. e.preventDefault();
  788. $("#filechooser").jHueFileChooser({
  789. initialPath: inputElement.val(),
  790. onFileChoose: function (filePath) {
  791. inputElement.val(filePath);
  792. inputElement.trigger("change");
  793. $("#chooseFile").modal("hide");
  794. },
  795. selectFolder: false,
  796. createFolder: false
  797. });
  798. $("#chooseFile").modal("show");
  799. });
  800. }
  801. function folderChooser(inputElement) {
  802. $("#folderchooser").jHueFileChooser({
  803. initialPath: inputElement.val(),
  804. onFolderChoose:function (folderPath) {
  805. inputElement.val(folderPath);
  806. inputElement.trigger("change");
  807. $("#chooseFolder").modal("hide");
  808. },
  809. selectFolder: true,
  810. createFolder: true,
  811. selectFile: false,
  812. uploadFile: false
  813. });
  814. $("#chooseFolder").modal("show");
  815. }
  816. function getFolderBrowseButton(inputElement) {
  817. return $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  818. e.preventDefault();
  819. folderChooser(inputElement);
  820. });
  821. }
  822. function pathChooser(inputElement) {
  823. $("#pathchooser").jHueFileChooser({
  824. initialPath: inputElement.val(),
  825. onFolderChoose:function (folderPath) {
  826. if (folderPath[folderPath.length - 1] != '/') {
  827. folderPath += '/';
  828. }
  829. inputElement.val(folderPath + 'results.csv');
  830. inputElement.trigger("change");
  831. $("#choosePath").modal("hide");
  832. },
  833. onFileChoose: function (filePath) {
  834. inputElement.val(filePath);
  835. inputElement.trigger("change");
  836. $("#choosePath").modal("hide");
  837. },
  838. selectFolder: true,
  839. createFolder: true,
  840. selectFile: true,
  841. uploadFile: false
  842. });
  843. $("#choosePath").modal("show");
  844. }
  845. function getPathBrowseButton(inputElement) {
  846. return $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  847. e.preventDefault();
  848. pathChooser(inputElement);
  849. });
  850. }
  851. // utils
  852. function clickHard(el) {
  853. var timer = setInterval(function () {
  854. if ($(el).length > 0) {
  855. $(el).click();
  856. clearInterval(timer);
  857. }
  858. }, 100);
  859. }
  860. function logGA(page) {
  861. if (typeof trackOnGA == 'function'){
  862. trackOnGA('beeswax/' + page);
  863. }
  864. }