beeswax.vm.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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.getFileResourceTypeErrors = function(index) {
  223. if (self.design.settings.errors() && self.design.settings.errors()[index]) {
  224. return self.design.settings.errors()[index];
  225. } else {
  226. return {};
  227. }
  228. };
  229. self.getFileResourcePathErrors = function(index) {
  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. self.getFunctionErrors = function(index) {
  248. if (self.design.settings.errors() && self.design.settings.errors()[index]) {
  249. return self.design.settings.errors()[index];
  250. } else {
  251. return {};
  252. }
  253. };
  254. var advancedErrorHandling = function(advanced_parameter, key) {
  255. return function(index) {
  256. var errors = self.design[advanced_parameter].errors();
  257. if (errors && errors[index]) {
  258. if (key in errors[index]) {
  259. return errors[index][key];
  260. } else {
  261. return [];
  262. }
  263. } else {
  264. return [];
  265. }
  266. };
  267. };
  268. self.getQueryErrors = advancedErrorHandling('query', 'query');
  269. self.getSettingKeyErrors = advancedErrorHandling('settings', 'key');
  270. self.getSettingValueErrors = advancedErrorHandling('settings', 'value');
  271. self.getFileResourceTypeErrors = advancedErrorHandling('fileResources', 'type');
  272. self.getFileResourcePathErrors = advancedErrorHandling('fileResources', 'path');
  273. self.getFunctionNameErrors = advancedErrorHandling('functions', 'name');
  274. self.getFunctionClassNameErrors = advancedErrorHandling('functions', 'class_name');
  275. function getMultiFormData(prefix, arr, members) {
  276. var data = {};
  277. var index = 0;
  278. data[prefix + '-next_form_id'] = arr.length;
  279. ko.utils.arrayForEach(arr, function(obj) {
  280. $.each(members, function(i, member) {
  281. data[prefix + '-' + index + '-' + member] = obj[member]();
  282. });
  283. data[prefix + '-' + index + '-_exists'] = true;
  284. data[prefix + '-' + index++ + '-_deleted'] = false;
  285. });
  286. return data;
  287. }
  288. self.getSettingsFormData = function() {
  289. return getMultiFormData('settings', self.design.settings.values(), ['key', 'value']);
  290. };
  291. self.getFileResourcesFormData = function() {
  292. return getMultiFormData('file_resources', self.design.fileResources.values(), ['type', 'path']);
  293. };
  294. self.getFunctionsFormData = function() {
  295. return getMultiFormData('functions', self.design.functions.values(), ['name', 'class_name']);
  296. };
  297. self.getParametersFormData = function() {
  298. var data = {};
  299. $.each(self.design.parameters(), function(index, parameter) {
  300. data[parameter.parameter()] = parameter.value();
  301. });
  302. return data;
  303. };
  304. self.getOtherData = function() {
  305. var data = {
  306. 'query-email_notify': self.design.email(),
  307. 'query-is_parameterized': self.design.isParameterized()
  308. };
  309. return data;
  310. };
  311. var error_fn = function(jqXHR, status, errorThrown) {
  312. self.design.isRunning(false);
  313. try {
  314. $(document).trigger('server.error', $.parseJSON(jqXHR.responseText));
  315. } catch(e) {
  316. $(document).trigger('server.unmanageable_error', jqXHR.responseText);
  317. }
  318. };
  319. self.fetchDatabases = function() {
  320. var request = {
  321. url: '/' + self.server() + '/api/autocomplete',
  322. dataType: 'json',
  323. type: 'GET',
  324. success: function(data) {
  325. self.updateDatabases(data.databases);
  326. },
  327. error: error_fn
  328. };
  329. $.ajax(request);
  330. };
  331. self.fetchDesign = function() {
  332. $(document).trigger('fetch.design');
  333. var request = {
  334. url: '/' + self.server() + '/api/design/' + self.design.id() + '/get',
  335. dataType: 'json',
  336. type: 'GET',
  337. success: function(data) {
  338. self.updateDesign(data.design);
  339. $(document).trigger('fetched.design', data);
  340. },
  341. error: error_fn
  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. $(document).trigger('fetched.query', data);
  354. },
  355. error: error_fn
  356. };
  357. $.ajax(request);
  358. };
  359. self.fetchParameters = function() {
  360. $(document).trigger('fetch.parameters');
  361. var data = {
  362. 'query-query': self.design.query.value(),
  363. 'query-database': self.database()
  364. };
  365. $.extend(data, self.getSettingsFormData());
  366. $.extend(data, self.getFileResourcesFormData());
  367. $.extend(data, self.getFunctionsFormData());
  368. $.extend(data, self.getParametersFormData());
  369. $.extend(data, self.getOtherData());
  370. var request = {
  371. url: '/' + self.server() + '/api/query/parameters',
  372. dataType: 'json',
  373. type: 'POST',
  374. success: function(data) {
  375. self.updateParameters(data.parameters);
  376. $(document).trigger('fetched.parameters');
  377. },
  378. error: error_fn,
  379. data: data
  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.resetErrors();
  389. var data = {
  390. 'query-query': self.design.query.value(),
  391. 'query-database': self.database()
  392. };
  393. $.extend(data, self.getSettingsFormData());
  394. $.extend(data, self.getFileResourcesFormData());
  395. $.extend(data, self.getFunctionsFormData());
  396. $.extend(data, self.getParametersFormData());
  397. $.extend(data, self.getOtherData());
  398. var request = {
  399. url: '/' + self.server() + '/api/query/execute/?explain=true',
  400. dataType: 'json',
  401. type: 'POST',
  402. success: function(data) {
  403. if (data.status == 0) {
  404. self.design.watch.logs.removeAll();
  405. self.design.results.rows.removeAll();
  406. self.design.results.columns.removeAll();
  407. self.design.results.explanation(data.explanation);
  408. } else {
  409. self.setErrors(data.message);
  410. $(document).trigger('error.query');
  411. }
  412. $(document).trigger('explained.query', data);
  413. self.design.isRunning(false);
  414. },
  415. error: error_fn,
  416. data: data
  417. };
  418. $.ajax(request);
  419. };
  420. self.executeQuery = function() {
  421. $(document).trigger('execute.query', data);
  422. self.design.explain(false);
  423. self.design.isRunning(true);
  424. self.design.isFinished(true);
  425. self.resetErrors();
  426. var data = {
  427. 'query-query': self.design.query.value(),
  428. 'query-database': self.database()
  429. };
  430. $.extend(data, self.getSettingsFormData());
  431. $.extend(data, self.getFileResourcesFormData());
  432. $.extend(data, self.getFunctionsFormData());
  433. $.extend(data, self.getParametersFormData());
  434. $.extend(data, self.getOtherData());
  435. var url = '/' + self.server() + '/api/query/execute/';
  436. if (self.design.id() > -1) {
  437. url += self.design.id();
  438. }
  439. var request = {
  440. url: url,
  441. dataType: 'json',
  442. type: 'POST',
  443. success: function(data) {
  444. self.resetErrors();
  445. if (data.status == 0) {
  446. self.design.results.url('/' + self.server() + '/results/' + data.id + '/0?format=json');
  447. self.design.watch.url(data.watch_url);
  448. self.design.statement(data.statement);
  449. self.design.history.id(data.id);
  450. self.watchQueryLoop();
  451. } else {
  452. self.setErrors(data.message, data.errors);
  453. self.design.isRunning(false);
  454. $(document).trigger('error.query');
  455. }
  456. $(document).trigger('executed.query', data);
  457. },
  458. error: error_fn,
  459. data: data
  460. };
  461. $.ajax(request);
  462. };
  463. self.executeNextStatement = function() {
  464. $(document).trigger('execute.query', data);
  465. self.design.explain(false);
  466. self.design.isRunning(true);
  467. self.design.isFinished(true);
  468. self.resetErrors();
  469. var data = {
  470. 'next': true
  471. };
  472. var request = {
  473. url: self.design.watch.url(),
  474. dataType: 'json',
  475. type: 'POST',
  476. success: function(data) {
  477. self.resetErrors();
  478. self.design.watch.logs.removeAll();
  479. self.design.statement(data.statement);
  480. self.design.watch.url(data.watch_url);
  481. self.design.results.url('/' + self.server() + '/results/' + data.id + '/0?format=json');
  482. self.watchQueryLoop();
  483. $(document).trigger('executed.query', data);
  484. },
  485. error: error_fn,
  486. data: data
  487. };
  488. $.ajax(request);
  489. };
  490. self.watchQuery = function() {
  491. var data = {
  492. 'query-query': self.design.query.value(),
  493. 'query-database': self.database()
  494. };
  495. $.extend(data, self.getSettingsFormData());
  496. $.extend(data, self.getFileResourcesFormData());
  497. $.extend(data, self.getFunctionsFormData());
  498. $.extend(data, self.getParametersFormData());
  499. $.extend(data, self.getOtherData());
  500. var request = {
  501. url: self.design.watch.url(),
  502. dataType: 'json',
  503. type: 'POST',
  504. success: function(data) {
  505. $(document).trigger('watched.query', data);
  506. },
  507. error: function(jqXHR, status, errorThrown) {
  508. self.design.isRunning(false);
  509. try {
  510. var data = $.parseJSON(jqXHR.responseText);
  511. self.design.watch.errors.push(data.error);
  512. } catch(e) {
  513. $(document).trigger('server.unmanageable_error', jqXHR.responseText);
  514. }
  515. },
  516. data: data
  517. };
  518. $.ajax(request);
  519. };
  520. self.watchQueryLoop = function(fn) {
  521. var TIMEOUT = 1000;
  522. var timer = null;
  523. self.design.watch.logs.removeAll();
  524. self.design.results.rows.removeAll();
  525. self.design.results.columns.removeAll();
  526. self.resetErrors();
  527. var _fn = function() {
  528. $(document).one('watched.query', function(e, data) {
  529. var failed = data.isFailure || data.status != 0;
  530. if (data.isSuccess || failed) {
  531. clearTimeout(timer);
  532. self.design.isRunning(false);
  533. if (data.log) {
  534. self.design.watch.logs(data.log.split("\n"));
  535. // scroll logs
  536. }
  537. if (!failed) {
  538. $(document).trigger('stop_watch.query');
  539. if (fn) {
  540. fn(data);
  541. } else {
  542. self.fetchResults();
  543. }
  544. }
  545. } else {
  546. self.design.statement(data.statement); // In case new no result statement executed
  547. if (data.log) {
  548. self.design.watch.logs(data.log.split("\n"));
  549. // scroll logs
  550. }
  551. timer = setTimeout(_fn, TIMEOUT);
  552. }
  553. });
  554. self.watchQuery();
  555. };
  556. $(document).trigger('start_watch.query');
  557. timer = setTimeout(_fn, TIMEOUT);
  558. };
  559. self.fetchResults = function() {
  560. $(document).trigger('fetch.results');
  561. self.resetErrors();
  562. var request = {
  563. url: self.design.results.url(),
  564. dataType: 'json',
  565. type: 'GET',
  566. success: function(data) {
  567. if (data.error) {
  568. self.design.results.errors.push(data.message);
  569. self.design.isRunning(false);
  570. self.design.results.empty(true);
  571. } else {
  572. self.design.isRunning(false);
  573. self.design.isFinished(data.is_finished);
  574. if (self.design.results.columns().length == 0){
  575. self.design.results.columns(data.columns);
  576. }
  577. self.design.results.rows.push.apply(self.design.results.rows, data.results);
  578. self.design.results.empty(self.design.results.rows().length == 0);
  579. if (data.has_more) {
  580. self.design.results.url(data.next_json_set);
  581. } else {
  582. self.design.results.url(null);
  583. }
  584. }
  585. $(document).trigger('fetched.results', [data]);
  586. },
  587. error: error_fn
  588. };
  589. $.ajax(request);
  590. };
  591. self.saveDesign = function() {
  592. var self = this;
  593. if (self.design.query.value() && self.design.name()) {
  594. var data = {
  595. 'query-query': self.design.query.value(),
  596. 'query-database': self.database()
  597. };
  598. $.extend(data, self.getSettingsFormData());
  599. $.extend(data, self.getFileResourcesFormData());
  600. $.extend(data, self.getFunctionsFormData());
  601. $.extend(data, self.getParametersFormData());
  602. $.extend(data, self.getOtherData());
  603. data['saveform-name'] = self.design.name();
  604. data['saveform-desc'] = self.design.description();
  605. if (self.design.id() > 0) {
  606. data['query-id'] = self.design.id();
  607. }
  608. var url = '/' + self.server() + '/api/design/';
  609. if (self.design.id() && self.design.id() != -1) {
  610. url += self.design.id();
  611. }
  612. var request = {
  613. url: url,
  614. dataType: 'json',
  615. type: 'POST',
  616. success: function(data) {
  617. self.design.id(data.design_id);
  618. $(document).trigger('saved.design', [data.design_id]);
  619. },
  620. error: function() {
  621. $(document).trigger('error_save.design');
  622. },
  623. data: data
  624. };
  625. $.ajax(request);
  626. }
  627. };
  628. self.cancelQuery = function() {
  629. $(document).trigger('cancel.query');
  630. var url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/cancel';
  631. $.post(url,
  632. function(response) {
  633. if (response['status'] != 0) {
  634. $(document).trigger('error_cancel.query', response['message']);
  635. } else {
  636. $(document).trigger('cancelled.query');
  637. }
  638. }
  639. );
  640. };
  641. self.closeQuery = function() {
  642. var self = this;
  643. if (self.design.history.id() && !self.design.isRunning()) {
  644. var data = {};
  645. var url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/close';
  646. var request = {
  647. url: url,
  648. dataType: 'json',
  649. type: 'POST',
  650. success: function(data) {
  651. if (data.status == 0) {
  652. $(document).trigger('closed.query', data);
  653. //self.resetQuery(); // Would fail multiqueries
  654. } else {
  655. $(document).trigger('error_close.query');
  656. }
  657. },
  658. error: function(data) {
  659. $(document).trigger('error_close.results');
  660. },
  661. data: data,
  662. async: false // for multi query
  663. };
  664. $.ajax(request);
  665. }
  666. };
  667. self.saveResults = function() {
  668. var self = this;
  669. if (self.design.id()) {
  670. var data = {
  671. 'database': self.database(),
  672. 'server': self.server(),
  673. 'type': self.design.results.save.type(),
  674. 'path': self.design.results.save.path()
  675. };
  676. var url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/results/save';
  677. var request = {
  678. url: url,
  679. dataType: 'json',
  680. type: 'POST',
  681. success: function(data) {
  682. if (data.status == 0) {
  683. self.design.results.save.errors(null);
  684. if (data.id) {
  685. // watch this ID.
  686. self.design.watch.url(data.watch_url);
  687. self.design.watch.logs.removeAll();
  688. self.watchQueryLoop(function() {
  689. window.location.href = data.success_url;
  690. });
  691. } else {
  692. // redirect to metastore app.
  693. window.location.href = data.success_url;
  694. }
  695. $(document).trigger('saved.results', data);
  696. } else {
  697. self.design.results.save.errors(data.errors);
  698. $(document).trigger('error_save.results');
  699. }
  700. },
  701. error: function(data) {
  702. self.design.results.save.errors(data);
  703. $(document).trigger('error_save.results');
  704. },
  705. data: data
  706. };
  707. $.ajax(request);
  708. }
  709. };
  710. // Events
  711. // Remove watched query event that watchQueryLoop may be bound to.
  712. $(document).on('server.unmanageable_error', function() {
  713. $(document).off('watched.query');
  714. });
  715. }
  716. // For routie
  717. function showSection(section) {
  718. $('.section').hide();
  719. $('#' + section).show();
  720. }
  721. // File browser button
  722. function getFileBrowseButton(inputElement) {
  723. return $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  724. e.preventDefault();
  725. $("#filechooser").jHueFileChooser({
  726. initialPath: inputElement.val(),
  727. onFileChoose: function (filePath) {
  728. inputElement.val(filePath);
  729. inputElement.trigger("change");
  730. $("#chooseFile").modal("hide");
  731. },
  732. selectFolder: false,
  733. createFolder: false
  734. });
  735. $("#chooseFile").modal("show");
  736. });
  737. }
  738. function getFileAndFolderBrowseButton(inputElement) {
  739. return $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  740. e.preventDefault();
  741. $("#folderchooser").jHueFileChooser({
  742. initialPath: inputElement.val(),
  743. onFolderChoose:function (folderPath) {
  744. inputElement.val(folderPath);
  745. inputElement.trigger("change");
  746. $("#chooseFolder").modal("hide");
  747. },
  748. selectFolder: true,
  749. createFolder: false,
  750. uploadFile:false
  751. });
  752. $("#chooseFolder").modal("show");
  753. });
  754. }
  755. // utils
  756. function clickHard(el) {
  757. var timer = setInterval(function () {
  758. if ($(el).length > 0) {
  759. $(el).click();
  760. clearInterval(timer);
  761. }
  762. }, 100);
  763. }
  764. function logGA(page) {
  765. if (typeof trackOnGA == 'function'){
  766. trackOnGA('beeswax/' + page);
  767. }
  768. }