beeswax.vm.js 25 KB

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