beeswax.vm.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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 advancedErrorHandling = 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.getQueryErrors = advancedErrorHandling('query', 'query');
  263. self.getSettingKeyErrors = advancedErrorHandling('settings', 'key');
  264. self.getSettingValueErrors = advancedErrorHandling('settings', 'value');
  265. self.getFileResourceTypeErrors = advancedErrorHandling('fileResources', 'type');
  266. self.getFileResourcePathErrors = advancedErrorHandling('fileResources', 'path');
  267. self.getFunctionNameErrors = advancedErrorHandling('functions', 'name');
  268. self.getFunctionClassNameErrors = advancedErrorHandling('functions', 'class_name');
  269. function getMultiFormData(prefix, arr, members) {
  270. var data = {};
  271. var index = 0;
  272. data[prefix + '-next_form_id'] = arr.length;
  273. ko.utils.arrayForEach(arr, function(obj) {
  274. $.each(members, function(i, member) {
  275. data[prefix + '-' + index + '-' + member] = obj[member]();
  276. });
  277. data[prefix + '-' + index + '-_exists'] = true;
  278. data[prefix + '-' + index++ + '-_deleted'] = false;
  279. });
  280. return data;
  281. }
  282. self.getSettingsFormData = function() {
  283. return getMultiFormData('settings', self.design.settings.values(), ['key', 'value']);
  284. };
  285. self.getFileResourcesFormData = function() {
  286. return getMultiFormData('file_resources', self.design.fileResources.values(), ['type', 'path']);
  287. };
  288. self.getFunctionsFormData = function() {
  289. return getMultiFormData('functions', self.design.functions.values(), ['name', 'class_name']);
  290. };
  291. self.getParametersFormData = function() {
  292. var data = {};
  293. $.each(self.design.parameters(), function(index, parameter) {
  294. data[parameter.parameter()] = parameter.value();
  295. });
  296. return data;
  297. };
  298. self.getOtherData = function() {
  299. var data = {
  300. 'query-email_notify': self.design.email(),
  301. 'query-is_parameterized': self.design.isParameterized()
  302. };
  303. return data;
  304. };
  305. var error_fn = function(jqXHR, status, errorThrown) {
  306. self.design.isRunning(false);
  307. try {
  308. $(document).trigger('server.error', $.parseJSON(jqXHR.responseText));
  309. } catch(e) {
  310. $(document).trigger('server.unmanageable_error', jqXHR.responseText);
  311. }
  312. };
  313. self.fetchDatabases = function() {
  314. var request = {
  315. url: '/' + self.server() + '/api/autocomplete',
  316. dataType: 'json',
  317. type: 'GET',
  318. success: function(data) {
  319. self.updateDatabases(data.databases);
  320. },
  321. error: error_fn
  322. };
  323. $.ajax(request);
  324. };
  325. self.fetchDesign = function() {
  326. $(document).trigger('fetch.design');
  327. var request = {
  328. url: '/' + self.server() + '/api/design/' + self.design.id() + '/get',
  329. dataType: 'json',
  330. type: 'GET',
  331. success: function(data) {
  332. self.updateDesign(data.design);
  333. $(document).trigger('fetched.design', data);
  334. },
  335. error: error_fn
  336. };
  337. $.ajax(request);
  338. };
  339. self.fetchQueryHistory = function() {
  340. $(document).trigger('fetch.query');
  341. var request = {
  342. url: '/' + self.server() + '/api/query/' + self.design.history.id() + '/get',
  343. dataType: 'json',
  344. type: 'GET',
  345. success: function(data) {
  346. self.updateHistory(data.query_history);
  347. $(document).trigger('fetched.query', data);
  348. },
  349. error: error_fn
  350. };
  351. $.ajax(request);
  352. };
  353. self.fetchParameters = function() {
  354. $(document).trigger('fetch.parameters');
  355. var data = {
  356. 'query-query': self.design.query.value(),
  357. 'query-database': self.database()
  358. };
  359. $.extend(data, self.getSettingsFormData());
  360. $.extend(data, self.getFileResourcesFormData());
  361. $.extend(data, self.getFunctionsFormData());
  362. $.extend(data, self.getParametersFormData());
  363. $.extend(data, self.getOtherData());
  364. var request = {
  365. url: '/' + self.server() + '/api/query/parameters',
  366. dataType: 'json',
  367. type: 'POST',
  368. success: function(data) {
  369. self.updateParameters(data.parameters);
  370. $(document).trigger('fetched.parameters');
  371. },
  372. error: error_fn,
  373. data: data
  374. };
  375. $.ajax(request);
  376. };
  377. self.explainQuery = function() {
  378. $(document).trigger('explain.query', data);
  379. self.design.explain(true);
  380. self.design.isRunning(true);
  381. self.design.isFinished(true);
  382. self.resetErrors();
  383. var data = {
  384. 'query-query': self.design.query.value(),
  385. 'query-database': self.database()
  386. };
  387. $.extend(data, self.getSettingsFormData());
  388. $.extend(data, self.getFileResourcesFormData());
  389. $.extend(data, self.getFunctionsFormData());
  390. $.extend(data, self.getParametersFormData());
  391. $.extend(data, self.getOtherData());
  392. var request = {
  393. url: '/' + self.server() + '/api/query/execute/?explain=true',
  394. dataType: 'json',
  395. type: 'POST',
  396. success: function(data) {
  397. if (data.status == 0) {
  398. self.design.watch.logs.removeAll();
  399. self.design.results.rows.removeAll();
  400. self.design.results.columns.removeAll();
  401. self.design.results.explanation(data.explanation);
  402. } else {
  403. self.setErrors(data.message);
  404. $(document).trigger('error.query');
  405. }
  406. $(document).trigger('explained.query', data);
  407. self.design.isRunning(false);
  408. },
  409. error: error_fn,
  410. data: data
  411. };
  412. $.ajax(request);
  413. };
  414. self.executeQuery = function() {
  415. $(document).trigger('execute.query', data);
  416. self.design.explain(false);
  417. self.design.isRunning(true);
  418. self.design.isFinished(true);
  419. self.resetErrors();
  420. var data = {
  421. 'query-query': self.design.query.value(),
  422. 'query-database': self.database()
  423. };
  424. $.extend(data, self.getSettingsFormData());
  425. $.extend(data, self.getFileResourcesFormData());
  426. $.extend(data, self.getFunctionsFormData());
  427. $.extend(data, self.getParametersFormData());
  428. $.extend(data, self.getOtherData());
  429. var url = '/' + self.server() + '/api/query/execute/';
  430. if (self.design.id() > -1) {
  431. url += self.design.id();
  432. }
  433. var request = {
  434. url: url,
  435. dataType: 'json',
  436. type: 'POST',
  437. success: function(data) {
  438. self.design.errors.removeAll();
  439. if (data.status == 0) {
  440. self.design.results.url('/' + self.server() + '/results/' + data.id + '/0?format=json');
  441. self.design.watch.url(data.watch_url);
  442. self.design.statement(data.statement);
  443. self.design.history.id(data.id);
  444. self.watchQueryLoop();
  445. } else {
  446. self.setErrors(data.message, data.errors);
  447. self.design.isRunning(false);
  448. $(document).trigger('error.query');
  449. }
  450. $(document).trigger('executed.query', data);
  451. },
  452. error: error_fn,
  453. data: data
  454. };
  455. $.ajax(request);
  456. };
  457. self.executeNextStatement = function() {
  458. $(document).trigger('execute.query', data);
  459. self.design.explain(false);
  460. self.design.isRunning(true);
  461. self.design.isFinished(true);
  462. self.resetErrors();
  463. var data = {
  464. 'next': true
  465. };
  466. var request = {
  467. url: self.design.watch.url(),
  468. dataType: 'json',
  469. type: 'POST',
  470. success: function(data) {
  471. self.design.errors.removeAll();
  472. self.design.watch.logs.removeAll();
  473. self.design.statement(data.statement);
  474. self.design.watch.url(data.watch_url);
  475. self.design.results.url('/' + self.server() + '/results/' + data.id + '/0?format=json');
  476. self.watchQueryLoop();
  477. $(document).trigger('executed.query', data);
  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.push(data.log);
  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.push(data.log);
  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.results.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);
  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. $(document).trigger('cancel.query');
  624. var url = '/' + self.server() + '/api/query/' + self.design.id() + '/cancel';
  625. $.post(url,
  626. function(response) {
  627. if (response['status'] != 0) {
  628. $(document).trigger('error_cancel.query', response['message']);
  629. } else {
  630. $(document).trigger('cancelled.query');
  631. }
  632. }
  633. );
  634. };
  635. self.closeQuery = function() {
  636. var self = this;
  637. if (self.design.history.id()) {
  638. var data = {};
  639. var url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/close';
  640. var request = {
  641. url: url,
  642. dataType: 'json',
  643. type: 'POST',
  644. success: function(data) {
  645. if (data.status == 0) {
  646. $(document).trigger('closed.query', data);
  647. //self.resetQuery(); // Would fail multiqueries
  648. } else {
  649. $(document).trigger('error_close.query');
  650. }
  651. },
  652. error: function(data) {
  653. $(document).trigger('error_close.results');
  654. },
  655. data: data,
  656. async: false // for multi query
  657. };
  658. $.ajax(request);
  659. }
  660. };
  661. self.saveResults = function() {
  662. var self = this;
  663. if (self.design.id()) {
  664. var data = {
  665. 'database': self.database(),
  666. 'server': self.server(),
  667. 'type': self.design.results.save.type(),
  668. 'path': self.design.results.save.path()
  669. };
  670. var url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/results/save';
  671. var request = {
  672. url: url,
  673. dataType: 'json',
  674. type: 'POST',
  675. success: function(data) {
  676. if (data.status == 0) {
  677. self.design.results.save.errors(null);
  678. if (data.id) {
  679. // watch this ID.
  680. self.design.watch.url(data.watch_url);
  681. self.design.watch.logs.removeAll();
  682. self.watchQueryLoop(function() {
  683. window.location.href = data.success_url;
  684. });
  685. } else {
  686. // redirect to metastore app.
  687. window.location.href = data.success_url;
  688. }
  689. $(document).trigger('saved.results', data);
  690. } else {
  691. self.design.results.save.errors(data.errors);
  692. $(document).trigger('error_save.results');
  693. }
  694. },
  695. error: function(data) {
  696. self.design.results.save.errors(data);
  697. $(document).trigger('error_save.results');
  698. },
  699. data: data
  700. };
  701. $.ajax(request);
  702. }
  703. };
  704. // Events
  705. // Remove watched query event that watchQueryLoop may be bound to.
  706. $(document).on('server.unmanageable_error', function() {
  707. $(document).off('watched.query');
  708. });
  709. }
  710. // For routie
  711. function showSection(section) {
  712. $('.section').hide();
  713. $('#' + section).show();
  714. }
  715. // File browser button
  716. function getFileBrowseButton(inputElement) {
  717. return $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  718. e.preventDefault();
  719. $("#filechooser").jHueFileChooser({
  720. initialPath: inputElement.val(),
  721. onFileChoose: function (filePath) {
  722. inputElement.val(filePath);
  723. inputElement.trigger("change");
  724. $("#chooseFile").modal("hide");
  725. },
  726. selectFolder: false,
  727. createFolder: false
  728. });
  729. $("#chooseFile").modal("show");
  730. });
  731. }
  732. function getFileAndFolderBrowseButton(inputElement) {
  733. return $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  734. e.preventDefault();
  735. $("#folderchooser").jHueFileChooser({
  736. initialPath: inputElement.val(),
  737. onFolderChoose:function (folderPath) {
  738. inputElement.val(folderPath);
  739. inputElement.trigger("change");
  740. $("#chooseFolder").modal("hide");
  741. },
  742. selectFolder: true,
  743. createFolder: false,
  744. uploadFile:false
  745. });
  746. $("#chooseFolder").modal("show");
  747. });
  748. }
  749. // utils
  750. function clickHard(el) {
  751. var timer = setInterval(function () {
  752. if ($(el).length > 0) {
  753. $(el).click();
  754. clearInterval(timer);
  755. }
  756. }, 100);
  757. }