beeswax.vm.js 25 KB

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