beeswax.vm.js 28 KB

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