beeswax.vm.js 29 KB

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