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