beeswax.vm.js 29 KB

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