beeswax.vm.js 24 KB

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