beeswax.vm.js 27 KB

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