beeswax.vm.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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.fetchDesign = function() {
  321. $(document).trigger('fetch.design');
  322. var request = {
  323. url: '/' + self.server() + '/api/design/' + self.design.id() + '/get',
  324. dataType: 'json',
  325. type: 'GET',
  326. success: function(data) {
  327. self.updateDesign(data.design);
  328. self.database(data.design.database);
  329. $(document).trigger('fetched.design', [data]);
  330. },
  331. error: error_fn,
  332. cache: false
  333. };
  334. $.ajax(request);
  335. };
  336. self.fetchQueryHistory = function() {
  337. $(document).trigger('fetch.query');
  338. var request = {
  339. url: '/' + self.server() + '/api/query/' + self.design.history.id() + '/get',
  340. dataType: 'json',
  341. type: 'GET',
  342. success: function(data) {
  343. self.updateHistory(data.query_history);
  344. self.database(data.query_history.database);
  345. $(document).trigger('fetched.query', [data]);
  346. },
  347. error: error_fn,
  348. cache: false
  349. };
  350. $.ajax(request);
  351. };
  352. self.fetchParameters = function() {
  353. $(document).trigger('fetch.parameters');
  354. var data = {
  355. 'query-query': self.design.query.value(),
  356. 'query-database': self.database()
  357. };
  358. $.extend(data, self.getSettingsFormData());
  359. $.extend(data, self.getFileResourcesFormData());
  360. $.extend(data, self.getFunctionsFormData());
  361. $.extend(data, self.getParametersFormData());
  362. $.extend(data, self.getOtherData());
  363. var request = {
  364. url: '/' + self.server() + '/api/query/parameters',
  365. dataType: 'json',
  366. type: 'POST',
  367. success: function(data) {
  368. self.updateParameters(data.parameters);
  369. $(document).trigger('fetched.parameters');
  370. },
  371. error: error_fn,
  372. data: data,
  373. cache: false
  374. };
  375. $.ajax(request);
  376. };
  377. self.explainQuery = function() {
  378. $(document).trigger('explain.query', data);
  379. self.design.explain(true);
  380. self.design.isRunning(true);
  381. self.design.isFinished(true);
  382. self.design.results.expired(false);
  383. self.design.results.empty(false);
  384. self.resetErrors();
  385. var data = {
  386. 'query-query': self.design.query.value(),
  387. 'query-database': self.database()
  388. };
  389. $.extend(data, self.getSettingsFormData());
  390. $.extend(data, self.getFileResourcesFormData());
  391. $.extend(data, self.getFunctionsFormData());
  392. $.extend(data, self.getParametersFormData());
  393. $.extend(data, self.getOtherData());
  394. var request = {
  395. url: '/' + self.server() + '/api/query/execute/?explain=true',
  396. dataType: 'json',
  397. type: 'POST',
  398. success: function(data) {
  399. if (data.status == 0) {
  400. self.design.watch.logs.removeAll();
  401. self.design.watch.jobUrls.removeAll();
  402. self.design.statement(data.statement);
  403. self.design.results.rows.removeAll();
  404. self.design.results.columns.removeAll();
  405. self.design.results.explanation(data.explanation);
  406. } else {
  407. self.setErrors(data.message);
  408. $(document).trigger('error.query');
  409. }
  410. $(document).trigger('explained.query', data);
  411. self.design.isRunning(false);
  412. },
  413. error: error_fn,
  414. data: data,
  415. cache: false
  416. };
  417. $.ajax(request);
  418. };
  419. self.executeQuery = function() {
  420. $(document).trigger('execute.query', data);
  421. self.design.explain(false);
  422. self.design.isRunning(true);
  423. self.design.isFinished(true);
  424. self.design.results.expired(false);
  425. self.design.results.empty(false);
  426. self.resetErrors();
  427. var data = {
  428. 'query-query': self.design.query.value(),
  429. 'query-database': self.database()
  430. };
  431. $.extend(data, self.getSettingsFormData());
  432. $.extend(data, self.getFileResourcesFormData());
  433. $.extend(data, self.getFunctionsFormData());
  434. $.extend(data, self.getParametersFormData());
  435. $.extend(data, self.getOtherData());
  436. var url = '/' + self.server() + '/api/query/execute/';
  437. if (self.design.id() > -1) {
  438. url += self.design.id();
  439. }
  440. var request = {
  441. url: url,
  442. dataType: 'json',
  443. type: 'POST',
  444. success: function(data) {
  445. self.design.errors.removeAll();
  446. self.design.watch.errors.removeAll();
  447. if (data.status == 0) {
  448. self.design.results.url('/' + self.server() + '/results/' + data.id + '/0?format=json');
  449. self.design.watch.url(data.watch_url);
  450. self.design.statement(data.statement);
  451. self.design.history.id(data.id);
  452. self.watchQueryLoop();
  453. $(document).trigger('executed.query', data);
  454. } else {
  455. self.setErrors(data.message, data.errors);
  456. self.design.isRunning(false);
  457. $(document).trigger('error.query');
  458. }
  459. },
  460. error: error_fn,
  461. data: data,
  462. cache: false
  463. };
  464. $.ajax(request);
  465. };
  466. self.executeNextStatement = function() {
  467. $(document).trigger('execute.query', data);
  468. self.design.explain(false);
  469. self.design.isRunning(true);
  470. self.design.isFinished(true);
  471. self.resetErrors();
  472. var data = {
  473. 'next': true,
  474. 'query-query': self.design.query.value(),
  475. };
  476. var request = {
  477. url: self.design.watch.url(),
  478. dataType: 'json',
  479. type: 'POST',
  480. success: function(data) {
  481. if (data.status == 0) {
  482. self.design.watch.logs.removeAll();
  483. self.design.watch.jobUrls.removeAll();
  484. self.design.statement(data.statement);
  485. self.design.watch.url(data.watch_url);
  486. self.design.results.url('/' + self.server() + '/results/' + data.id + '/0?format=json');
  487. self.watchQueryLoop();
  488. $(document).trigger('executed.query', data);
  489. } else {
  490. self.setErrors(data.message, data.errors);
  491. self.design.isRunning(false);
  492. self.design.isFinished(false); // We propose to skip the failed statement
  493. $(document).trigger('error.query');
  494. }
  495. },
  496. error: error_fn,
  497. data: data,
  498. cache: false
  499. };
  500. $.ajax(request);
  501. };
  502. self.watchQuery = function() {
  503. var data = {
  504. 'query-query': self.design.query.value(),
  505. 'query-database': self.database()
  506. };
  507. $.extend(data, self.getSettingsFormData());
  508. $.extend(data, self.getFileResourcesFormData());
  509. $.extend(data, self.getFunctionsFormData());
  510. $.extend(data, self.getParametersFormData());
  511. $.extend(data, self.getOtherData());
  512. var request = {
  513. url: self.design.watch.url(),
  514. dataType: 'json',
  515. type: 'POST',
  516. success: function(data) {
  517. $(document).trigger('watched.query', data);
  518. },
  519. error: function(jqXHR, status, errorThrown) {
  520. self.design.isRunning(false);
  521. try {
  522. var data = $.parseJSON(jqXHR.responseText);
  523. self.design.watch.errors.push(data.error);
  524. } catch(e) {
  525. $(document).trigger('server.unmanageable_error', jqXHR.responseText);
  526. }
  527. },
  528. data: data,
  529. cache: false
  530. };
  531. $.ajax(request);
  532. };
  533. self.watchQueryLoop = function(fn) {
  534. var TIMEOUT = 100;
  535. var timer = null;
  536. self.design.isRunning(true);
  537. self.design.watch.logs.removeAll();
  538. self.design.watch.jobUrls.removeAll();
  539. self.design.results.rows.removeAll();
  540. self.design.results.columns.removeAll();
  541. self.resetErrors();
  542. var _fn = function() {
  543. $(document).one('watched.query', function(e, data) {
  544. var failed = data.isFailure || data.status != 0;
  545. if (data.isSuccess || failed) {
  546. clearTimeout(timer);
  547. self.design.isRunning(false);
  548. if (data.log) {
  549. self.design.watch.logs(data.log.split("\n"));
  550. // scroll logs
  551. self.design.watch.jobUrls(data.jobUrls);
  552. }
  553. if (!failed) {
  554. $(document).trigger('stop_watch.query');
  555. if (fn) {
  556. fn(data);
  557. } else {
  558. self.fetchResults();
  559. }
  560. }
  561. } else {
  562. self.design.statement(data.statement); // In case new no result statement executed
  563. if (data.log) {
  564. self.design.watch.logs(data.log.split("\n"));
  565. // scroll logs
  566. self.design.watch.jobUrls(data.jobUrls);
  567. }
  568. TIMEOUT = Math.min(TIMEOUT + 100, 2000);
  569. timer = setTimeout(_fn, TIMEOUT);
  570. }
  571. });
  572. self.watchQuery();
  573. };
  574. $(document).trigger('start_watch.query');
  575. timer = setTimeout(_fn, TIMEOUT);
  576. };
  577. self.fetchResults = function() {
  578. $(document).trigger('fetch.results');
  579. self.design.errors.removeAll();
  580. self.design.results.errors.removeAll();
  581. var request = {
  582. url: self.design.results.url(),
  583. dataType: 'text',
  584. type: 'GET',
  585. success: function(data) {
  586. data = JSON.bigdataParse(data);
  587. if (data.error) {
  588. self.design.results.errors.push(data.message);
  589. self.design.isRunning(false);
  590. self.design.results.empty(true);
  591. } else {
  592. self.design.isRunning(false);
  593. self.design.isFinished(data.is_finished);
  594. if (self.design.results.columns().length == 0){
  595. if (data.columns != null){
  596. data.columns.unshift({ type:"INT_TYPE", name:"", comment:null});
  597. }
  598. self.design.results.columns(data.columns ? data.columns : []); // Some querysets have empty or null for columns
  599. }
  600. self.design.results.rows.push.apply(self.design.results.rows, data.results);
  601. self.design.results.empty(self.design.results.rows().length == 0);
  602. if (data.has_more) {
  603. self.design.results.url(data.next_json_set);
  604. } else {
  605. self.design.results.url(null);
  606. }
  607. }
  608. $(document).trigger('fetched.results', [data]);
  609. },
  610. error: error_fn,
  611. cache: false
  612. };
  613. $.ajax(request);
  614. };
  615. self.saveDesign = function() {
  616. var self = this;
  617. if (self.design.query.value() && self.design.name()) {
  618. var data = {
  619. 'query-query': self.design.query.value(),
  620. 'query-database': self.database()
  621. };
  622. $.extend(data, self.getSettingsFormData());
  623. $.extend(data, self.getFileResourcesFormData());
  624. $.extend(data, self.getFunctionsFormData());
  625. $.extend(data, self.getParametersFormData());
  626. $.extend(data, self.getOtherData());
  627. data['saveform-name'] = self.design.name();
  628. data['saveform-desc'] = self.design.description();
  629. if (self.design.id() > 0) {
  630. data['query-id'] = self.design.id();
  631. }
  632. var url = '/' + self.server() + '/api/design/';
  633. if (self.design.id() && self.design.id() != -1) {
  634. url += self.design.id();
  635. }
  636. var request = {
  637. url: url,
  638. dataType: 'json',
  639. type: 'POST',
  640. success: function(data) {
  641. if (data.status == 0) {
  642. self.design.id(data.design_id);
  643. $(document).trigger('saved.design', [data.design_id]);
  644. } else {
  645. self.setErrors("", data.errors);
  646. $(document).trigger('error_save.design', [data.message]);
  647. }
  648. },
  649. error: function() {
  650. $(document).trigger('error_save.design');
  651. },
  652. data: data,
  653. cache: false
  654. };
  655. $.ajax(request);
  656. }
  657. };
  658. self.cancelQuery = function() {
  659. if (self.design.history.id() > 0) {
  660. $(document).trigger('cancel.query');
  661. var url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/cancel';
  662. $.post(url,
  663. function(response) {
  664. if (response['status'] != 0) {
  665. $(document).trigger('error_cancel.query', response['message']);
  666. } else {
  667. $(document).trigger('cancelled.query');
  668. }
  669. }
  670. );
  671. }
  672. };
  673. self.closeQuery = function() {
  674. var self = this;
  675. if (self.design.history.id() > 0 && !self.design.isRunning()) {
  676. var data = {};
  677. var url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/close';
  678. var request = {
  679. url: url,
  680. dataType: 'json',
  681. type: 'POST',
  682. success: function(data) {
  683. if (data.status == 0) {
  684. $(document).trigger('closed.query', data);
  685. //self.resetQuery(); // Would fail multiqueries
  686. } else {
  687. $(document).trigger('error_close.query');
  688. }
  689. },
  690. error: function(data) {
  691. $(document).trigger('error_close.results');
  692. },
  693. data: data,
  694. async: false, // for multi query
  695. cache: false
  696. };
  697. $.ajax(request);
  698. }
  699. };
  700. self.saveResults = function() {
  701. var self = this;
  702. self.design.isRunning(true);
  703. self.resetErrors();
  704. if (self.design.id()) {
  705. var data, url;
  706. switch(self.design.results.save.type()) {
  707. case 'hdfs-directory':
  708. data = {
  709. 'server': self.server(),
  710. 'path': self.design.results.save.path()
  711. };
  712. url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/results/save/hdfs/directory';
  713. break;
  714. case 'hdfs-file':
  715. data = {
  716. 'server': self.server(),
  717. 'path': self.design.results.save.path(),
  718. 'overwrite': self.design.results.save.overwrite()
  719. };
  720. url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/results/save/hdfs/file';
  721. break;
  722. // case 'hive-table':
  723. default:
  724. data = {
  725. 'database': self.database(),
  726. 'server': self.server(),
  727. 'table': self.design.results.save.path()
  728. };
  729. url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/results/save/hive/table';
  730. break;
  731. }
  732. var request = {
  733. url: url,
  734. dataType: 'json',
  735. type: 'POST',
  736. success: function(data) {
  737. if (data.status == 0) {
  738. self.design.results.save.errors(null);
  739. var redirect_fn = function() {
  740. window.location.href = data.success_url;
  741. self.design.isRunning(false);
  742. };
  743. if (data.id) {
  744. // watch this ID.
  745. self.design.watch.url(data.watch_url);
  746. self.design.watch.logs.removeAll();
  747. self.design.watch.jobUrls.removeAll();
  748. self.watchQueryLoop(redirect_fn);
  749. } else {
  750. // redirect to metastore app.
  751. redirect_fn();
  752. }
  753. $(document).trigger('saved.results', data);
  754. } else {
  755. self.design.isRunning(false);
  756. self.design.results.save.errors(data.errors);
  757. $(document).trigger('error_save.results', [data.message]);
  758. }
  759. },
  760. error: function(data) {
  761. self.design.isRunning(false);
  762. self.design.results.save.errors(data);
  763. $(document).trigger('error_save.results', [data]);
  764. },
  765. data: data,
  766. cache: false
  767. };
  768. return $.ajax(request);
  769. }
  770. };
  771. // Events
  772. // Remove watched query event that watchQueryLoop may be bound to.
  773. $(document).on('server.unmanageable_error', function() {
  774. $(document).off('watched.query');
  775. });
  776. }
  777. // For routie
  778. function showSection(section) {
  779. $('.section').hide();
  780. $('#' + section).show();
  781. }
  782. // File browser button
  783. function getFileBrowseButton(inputElement) {
  784. return $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  785. e.preventDefault();
  786. $("#filechooser").jHueFileChooser({
  787. initialPath: inputElement.val(),
  788. onFileChoose: function (filePath) {
  789. inputElement.val(filePath);
  790. inputElement.trigger("change");
  791. $("#chooseFile").modal("hide");
  792. },
  793. selectFolder: false,
  794. createFolder: false
  795. });
  796. $("#chooseFile").modal("show");
  797. });
  798. }
  799. function folderChooser(inputElement) {
  800. $("#folderchooser").jHueFileChooser({
  801. initialPath: inputElement.val(),
  802. onFolderChoose:function (folderPath) {
  803. inputElement.val(folderPath);
  804. inputElement.trigger("change");
  805. $("#chooseFolder").modal("hide");
  806. },
  807. selectFolder: true,
  808. createFolder: true,
  809. selectFile: false,
  810. uploadFile: false
  811. });
  812. $("#chooseFolder").modal("show");
  813. }
  814. function getFolderBrowseButton(inputElement) {
  815. return $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  816. e.preventDefault();
  817. folderChooser(inputElement);
  818. });
  819. }
  820. function pathChooser(inputElement) {
  821. $("#pathchooser").jHueFileChooser({
  822. initialPath: inputElement.val(),
  823. onFolderChoose:function (folderPath) {
  824. if (folderPath[folderPath.length - 1] != '/') {
  825. folderPath += '/';
  826. }
  827. inputElement.val(folderPath + 'results.csv');
  828. inputElement.trigger("change");
  829. $("#choosePath").modal("hide");
  830. },
  831. onFileChoose: function (filePath) {
  832. inputElement.val(filePath);
  833. inputElement.trigger("change");
  834. $("#choosePath").modal("hide");
  835. },
  836. selectFolder: true,
  837. createFolder: true,
  838. selectFile: true,
  839. uploadFile: false
  840. });
  841. $("#choosePath").modal("show");
  842. }
  843. function getPathBrowseButton(inputElement) {
  844. return $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  845. e.preventDefault();
  846. pathChooser(inputElement);
  847. });
  848. }
  849. // utils
  850. function clickHard(el) {
  851. var timer = setInterval(function () {
  852. if ($(el).length > 0) {
  853. $(el).click();
  854. clearInterval(timer);
  855. }
  856. }, 100);
  857. }
  858. function logGA(page) {
  859. if (typeof trackOnGA == 'function'){
  860. trackOnGA('beeswax/' + page);
  861. }
  862. }