beeswax.vm.js 28 KB

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