beeswax.vm.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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.addFileResource(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. cache: false
  315. };
  316. $.ajax(request);
  317. };
  318. self.fetchDesign = function() {
  319. $(document).trigger('fetch.design');
  320. var request = {
  321. url: '/' + self.server() + '/api/design/' + self.design.id() + '/get',
  322. dataType: 'json',
  323. type: 'GET',
  324. success: function(data) {
  325. self.updateDesign(data.design);
  326. self.database(data.design.database);
  327. $(document).trigger('fetched.design', [data]);
  328. },
  329. error: error_fn,
  330. cache: false
  331. };
  332. $.ajax(request);
  333. };
  334. self.fetchQueryHistory = function() {
  335. $(document).trigger('fetch.query');
  336. var request = {
  337. url: '/' + self.server() + '/api/query/' + self.design.history.id() + '/get',
  338. dataType: 'json',
  339. type: 'GET',
  340. success: function(data) {
  341. self.updateHistory(data.query_history);
  342. self.database(data.query_history.database);
  343. $(document).trigger('fetched.query', [data]);
  344. },
  345. error: error_fn,
  346. cache: false
  347. };
  348. $.ajax(request);
  349. };
  350. self.fetchParameters = function() {
  351. $(document).trigger('fetch.parameters');
  352. var data = {
  353. 'query-query': self.design.query.value(),
  354. 'query-database': self.database()
  355. };
  356. $.extend(data, self.getSettingsFormData());
  357. $.extend(data, self.getFileResourcesFormData());
  358. $.extend(data, self.getFunctionsFormData());
  359. $.extend(data, self.getParametersFormData());
  360. $.extend(data, self.getOtherData());
  361. var request = {
  362. url: '/' + self.server() + '/api/query/parameters',
  363. dataType: 'json',
  364. type: 'POST',
  365. success: function(data) {
  366. self.updateParameters(data.parameters);
  367. $(document).trigger('fetched.parameters');
  368. },
  369. error: error_fn,
  370. data: data,
  371. cache: false
  372. };
  373. $.ajax(request);
  374. };
  375. self.explainQuery = function() {
  376. $(document).trigger('explain.query', data);
  377. self.design.explain(true);
  378. self.design.isRunning(true);
  379. self.design.isFinished(true);
  380. self.resetErrors();
  381. var data = {
  382. 'query-query': self.design.query.value(),
  383. 'query-database': self.database()
  384. };
  385. $.extend(data, self.getSettingsFormData());
  386. $.extend(data, self.getFileResourcesFormData());
  387. $.extend(data, self.getFunctionsFormData());
  388. $.extend(data, self.getParametersFormData());
  389. $.extend(data, self.getOtherData());
  390. var request = {
  391. url: '/' + self.server() + '/api/query/execute/?explain=true',
  392. dataType: 'json',
  393. type: 'POST',
  394. success: function(data) {
  395. if (data.status == 0) {
  396. self.design.watch.logs.removeAll();
  397. self.design.statement(data.statement);
  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. cache: false
  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. self.design.watch.errors.removeAll();
  440. if (data.status == 0) {
  441. self.design.results.url('/' + self.server() + '/results/' + data.id + '/0?format=json');
  442. self.design.watch.url(data.watch_url);
  443. self.design.statement(data.statement);
  444. self.design.history.id(data.id);
  445. self.watchQueryLoop();
  446. $(document).trigger('executed.query', data);
  447. } else {
  448. self.setErrors(data.message, data.errors);
  449. self.design.isRunning(false);
  450. $(document).trigger('error.query');
  451. }
  452. },
  453. error: error_fn,
  454. data: data,
  455. cache: false
  456. };
  457. $.ajax(request);
  458. };
  459. self.executeNextStatement = function() {
  460. $(document).trigger('execute.query', data);
  461. self.design.explain(false);
  462. self.design.isRunning(true);
  463. self.design.isFinished(true);
  464. self.resetErrors();
  465. var data = {
  466. 'next': true,
  467. 'query-query': self.design.query.value(),
  468. };
  469. var request = {
  470. url: self.design.watch.url(),
  471. dataType: 'json',
  472. type: 'POST',
  473. success: function(data) {
  474. if (data.status == 0) {
  475. self.design.watch.logs.removeAll();
  476. self.design.statement(data.statement);
  477. self.design.watch.url(data.watch_url);
  478. self.design.results.url('/' + self.server() + '/results/' + data.id + '/0?format=json');
  479. self.watchQueryLoop();
  480. $(document).trigger('executed.query', data);
  481. } else {
  482. self.setErrors(data.message, data.errors);
  483. self.design.isRunning(false);
  484. self.design.isFinished(false); // We propose to skip the failed statement
  485. $(document).trigger('error.query');
  486. }
  487. },
  488. error: error_fn,
  489. data: data,
  490. cache: false
  491. };
  492. $.ajax(request);
  493. };
  494. self.watchQuery = function() {
  495. var data = {
  496. 'query-query': self.design.query.value(),
  497. 'query-database': self.database()
  498. };
  499. $.extend(data, self.getSettingsFormData());
  500. $.extend(data, self.getFileResourcesFormData());
  501. $.extend(data, self.getFunctionsFormData());
  502. $.extend(data, self.getParametersFormData());
  503. $.extend(data, self.getOtherData());
  504. var request = {
  505. url: self.design.watch.url(),
  506. dataType: 'json',
  507. type: 'POST',
  508. success: function(data) {
  509. $(document).trigger('watched.query', data);
  510. },
  511. error: function(jqXHR, status, errorThrown) {
  512. self.design.isRunning(false);
  513. try {
  514. var data = $.parseJSON(jqXHR.responseText);
  515. self.design.watch.errors.push(data.error);
  516. } catch(e) {
  517. $(document).trigger('server.unmanageable_error', jqXHR.responseText);
  518. }
  519. },
  520. data: data,
  521. cache: false
  522. };
  523. $.ajax(request);
  524. };
  525. self.watchQueryLoop = function(fn) {
  526. var TIMEOUT = 1000;
  527. var timer = null;
  528. self.design.watch.logs.removeAll();
  529. self.design.results.rows.removeAll();
  530. self.design.results.columns.removeAll();
  531. self.resetErrors();
  532. var _fn = function() {
  533. $(document).one('watched.query', function(e, data) {
  534. var failed = data.isFailure || data.status != 0;
  535. if (data.isSuccess || failed) {
  536. clearTimeout(timer);
  537. self.design.isRunning(false);
  538. if (data.log) {
  539. self.design.watch.logs(data.log.split("\n"));
  540. // scroll logs
  541. }
  542. if (!failed) {
  543. $(document).trigger('stop_watch.query');
  544. if (fn) {
  545. fn(data);
  546. } else {
  547. self.fetchResults();
  548. }
  549. }
  550. } else {
  551. self.design.statement(data.statement); // In case new no result statement executed
  552. if (data.log) {
  553. self.design.watch.logs(data.log.split("\n"));
  554. // scroll logs
  555. }
  556. timer = setTimeout(_fn, TIMEOUT);
  557. }
  558. });
  559. self.watchQuery();
  560. };
  561. $(document).trigger('start_watch.query');
  562. timer = setTimeout(_fn, TIMEOUT);
  563. };
  564. self.fetchResults = function() {
  565. $(document).trigger('fetch.results');
  566. self.design.errors.removeAll();
  567. var request = {
  568. url: self.design.results.url(),
  569. dataType: 'json',
  570. type: 'GET',
  571. success: function(data) {
  572. if (data.error) {
  573. self.design.results.errors.push(data.message);
  574. self.design.isRunning(false);
  575. self.design.results.empty(true);
  576. } else {
  577. self.design.isRunning(false);
  578. self.design.isFinished(data.is_finished);
  579. if (self.design.results.columns().length == 0){
  580. if (data.columns != null){
  581. data.columns.unshift({ type:"INT_TYPE", name:"", comment:null});
  582. }
  583. self.design.results.columns(data.columns ? data.columns : []); // Some querysets have empty or null for columns
  584. }
  585. self.design.results.rows.push.apply(self.design.results.rows, data.results);
  586. self.design.results.empty(self.design.results.rows().length == 0);
  587. if (data.has_more) {
  588. self.design.results.url(data.next_json_set);
  589. } else {
  590. self.design.results.url(null);
  591. }
  592. }
  593. $(document).trigger('fetched.results', [data]);
  594. },
  595. error: error_fn,
  596. cache: false
  597. };
  598. $.ajax(request);
  599. };
  600. self.saveDesign = function() {
  601. var self = this;
  602. if (self.design.query.value() && self.design.name()) {
  603. var data = {
  604. 'query-query': self.design.query.value(),
  605. 'query-database': self.database()
  606. };
  607. $.extend(data, self.getSettingsFormData());
  608. $.extend(data, self.getFileResourcesFormData());
  609. $.extend(data, self.getFunctionsFormData());
  610. $.extend(data, self.getParametersFormData());
  611. $.extend(data, self.getOtherData());
  612. data['saveform-name'] = self.design.name();
  613. data['saveform-desc'] = self.design.description();
  614. if (self.design.id() > 0) {
  615. data['query-id'] = self.design.id();
  616. }
  617. var url = '/' + self.server() + '/api/design/';
  618. if (self.design.id() && self.design.id() != -1) {
  619. url += self.design.id();
  620. }
  621. var request = {
  622. url: url,
  623. dataType: 'json',
  624. type: 'POST',
  625. success: function(data) {
  626. self.design.id(data.design_id);
  627. $(document).trigger('saved.design', [data.design_id]);
  628. },
  629. error: function() {
  630. $(document).trigger('error_save.design');
  631. },
  632. data: data,
  633. cache: false
  634. };
  635. $.ajax(request);
  636. }
  637. };
  638. self.cancelQuery = function() {
  639. if (self.design.history.id() > 0) {
  640. $(document).trigger('cancel.query');
  641. var url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/cancel';
  642. $.post(url,
  643. function(response) {
  644. if (response['status'] != 0) {
  645. $(document).trigger('error_cancel.query', response['message']);
  646. } else {
  647. $(document).trigger('cancelled.query');
  648. }
  649. }
  650. );
  651. }
  652. };
  653. self.closeQuery = function() {
  654. var self = this;
  655. if (self.design.history.id() > 0 && !self.design.isRunning()) {
  656. var data = {};
  657. var url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/close';
  658. var request = {
  659. url: url,
  660. dataType: 'json',
  661. type: 'POST',
  662. success: function(data) {
  663. if (data.status == 0) {
  664. $(document).trigger('closed.query', data);
  665. //self.resetQuery(); // Would fail multiqueries
  666. } else {
  667. $(document).trigger('error_close.query');
  668. }
  669. },
  670. error: function(data) {
  671. $(document).trigger('error_close.results');
  672. },
  673. data: data,
  674. async: false, // for multi query
  675. cache: false
  676. };
  677. $.ajax(request);
  678. }
  679. };
  680. self.saveResults = function() {
  681. var self = this;
  682. if (self.design.id()) {
  683. var data = {
  684. 'database': self.database(),
  685. 'server': self.server(),
  686. 'type': self.design.results.save.type(),
  687. 'path': self.design.results.save.path(),
  688. 'rerun': self.design.results.save.rerun()
  689. };
  690. var url = '/' + self.server() + '/api/query/' + self.design.history.id() + '/results/save';
  691. var request = {
  692. url: url,
  693. dataType: 'json',
  694. type: 'POST',
  695. success: function(data) {
  696. if (data.status == 0) {
  697. self.design.results.save.errors(null);
  698. if (data.id) {
  699. // watch this ID.
  700. self.design.watch.url(data.watch_url);
  701. self.design.watch.logs.removeAll();
  702. self.watchQueryLoop(function() {
  703. window.location.href = data.success_url;
  704. });
  705. } else {
  706. // redirect to metastore app.
  707. window.location.href = data.success_url;
  708. }
  709. $(document).trigger('saved.results', data);
  710. } else {
  711. self.design.results.save.errors(data.errors);
  712. $(document).trigger('error_save.results');
  713. }
  714. },
  715. error: function(data) {
  716. self.design.results.save.errors(data);
  717. $(document).trigger('error_save.results');
  718. },
  719. data: data,
  720. cache: false
  721. };
  722. $.ajax(request);
  723. }
  724. };
  725. // Events
  726. // Remove watched query event that watchQueryLoop may be bound to.
  727. $(document).on('server.unmanageable_error', function() {
  728. $(document).off('watched.query');
  729. });
  730. }
  731. // For routie
  732. function showSection(section) {
  733. $('.section').hide();
  734. $('#' + section).show();
  735. }
  736. // File browser button
  737. function getFileBrowseButton(inputElement) {
  738. return $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  739. e.preventDefault();
  740. $("#filechooser").jHueFileChooser({
  741. initialPath: inputElement.val(),
  742. onFileChoose: function (filePath) {
  743. inputElement.val(filePath);
  744. inputElement.trigger("change");
  745. $("#chooseFile").modal("hide");
  746. },
  747. selectFolder: false,
  748. createFolder: false
  749. });
  750. $("#chooseFile").modal("show");
  751. });
  752. }
  753. function getFileAndFolderBrowseButton(inputElement) {
  754. return $("<button>").addClass("btn").addClass("fileChooserBtn").text("..").click(function (e) {
  755. e.preventDefault();
  756. $("#folderchooser").jHueFileChooser({
  757. initialPath: inputElement.val(),
  758. onFolderChoose:function (folderPath) {
  759. inputElement.val(folderPath);
  760. inputElement.trigger("change");
  761. $("#chooseFolder").modal("hide");
  762. },
  763. selectFolder: true,
  764. createFolder: false,
  765. uploadFile:false
  766. });
  767. $("#chooseFolder").modal("show");
  768. });
  769. }
  770. // utils
  771. function clickHard(el) {
  772. var timer = setInterval(function () {
  773. if ($(el).length > 0) {
  774. $(el).click();
  775. clearInterval(timer);
  776. }
  777. }, 100);
  778. }
  779. function logGA(page) {
  780. if (typeof trackOnGA == 'function'){
  781. trackOnGA('beeswax/' + page);
  782. }
  783. }