beeswax.vm.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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, query_id) {
  17. var self = this;
  18. self.server = ko.observable(server);
  19. self.databases = ko.observableArray();
  20. self.selectedDatabase = ko.observable(0);
  21. self.query = ko.mapping.fromJS({
  22. 'id': query_id,
  23. 'query': '',
  24. 'name': null,
  25. 'description': null,
  26. 'settings': [],
  27. 'fileResources': [],
  28. 'functions': [],
  29. 'parameters': [],
  30. 'isParameterized': true,
  31. 'email': false,
  32. 'errors': [],
  33. 'explain': false
  34. });
  35. self.logs = ko.observableArray();
  36. self.explanation = ko.observable();
  37. self.watchURL = ko.observable();
  38. self.resultsURL = ko.observable(null);
  39. self.columns = ko.observableArray();
  40. self.rows = ko.observableArray();
  41. self.resultsEmpty = ko.observable(true);
  42. self.isRunning = ko.observable(false);
  43. self.hasMoreResults = ko.computed(function() {
  44. return self.resultsURL() != null;
  45. });
  46. self.database = ko.computed({
  47. 'read': function() {
  48. if (self.databases()) {
  49. return self.databases()[self.selectedDatabase()];
  50. } else{
  51. return "";
  52. }
  53. },
  54. 'write': function(value) {
  55. self.selectedDatabase(self.databases.indexOf(value));
  56. },
  57. 'deferEvaluation': true
  58. });
  59. self.updateDatabases = function(databases) {
  60. self.databases(databases);
  61. var key = 'hueBeeswaxLastDatabase-' + self.server();
  62. var last = $.totalStorage(key) || ((databases.length > 0) ? databases[0] : null);
  63. if (last) {
  64. self.database(last);
  65. }
  66. };
  67. self.updateQuery = function(design) {
  68. self.query.query(design.query);
  69. self.query.id(design.id);
  70. self.query.name(design.name);
  71. self.query.description(design.desc);
  72. self.database(design.database);
  73. self.query.isParameterized(design.is_parameterized);
  74. self.query.email(design.email_notify);
  75. self.query.settings.removeAll();
  76. self.query.fileResources.removeAll();
  77. self.query.functions.removeAll();
  78. $.each(design.settings, function(index, setting) {
  79. self.addSetting(setting.key, setting.value);
  80. });
  81. $.each(design.file_resources, function(index, file_resource) {
  82. self.addFileResources(file_resource.type, file_resource.path);
  83. });
  84. $.each(design.functions, function(index, _function) {
  85. self.addFunction(_function.name, _function.class_name);
  86. });
  87. };
  88. self.updateParameters = function(parameters) {
  89. self.query.parameters.removeAll();
  90. $.each(parameters, function(index, parameter) {
  91. self.addParameter(parameter.parameter, parameter.name, '');
  92. });
  93. };
  94. self.addParameter = function(parameter, name, value) {
  95. var obj = {
  96. 'parameter': ko.observable(parameter),
  97. 'name': ko.observable(name),
  98. 'value': ko.observable(value)
  99. };
  100. obj.parameter.subscribe(function() {
  101. self.query.parameters.valueHasMutated();
  102. });
  103. obj.name.subscribe(function() {
  104. self.query.parameters.valueHasMutated();
  105. });
  106. obj.value.subscribe(function() {
  107. self.query.parameters.valueHasMutated();
  108. });
  109. self.query.parameters.push(obj);
  110. };
  111. self.addSetting = function(key, value) {
  112. var obj = {
  113. 'key': ko.observable(key),
  114. 'value': ko.observable(value)
  115. };
  116. obj.key.subscribe(function() {
  117. self.query.settings.valueHasMutated();
  118. });
  119. obj.value.subscribe(function() {
  120. self.query.settings.valueHasMutated();
  121. });
  122. self.query.settings.push(obj);
  123. };
  124. self.removeSetting = function(index) {
  125. self.query.settings.splice(index, 1);
  126. };
  127. self.addFileResources = function(type, path) {
  128. var obj = {
  129. 'type': ko.observable(type),
  130. 'path': ko.observable(path)
  131. };
  132. obj.type.subscribe(function() {
  133. self.query.fileResources.valueHasMutated();
  134. });
  135. obj.path.subscribe(function() {
  136. self.query.fileResources.valueHasMutated();
  137. });
  138. self.query.fileResources.push(obj);
  139. };
  140. self.removeFileResources = function(index) {
  141. self.query.fileResources.splice(index, 1);
  142. };
  143. self.addFunction = function(name, class_name) {
  144. var obj = {
  145. 'name': ko.observable(name),
  146. 'class_name': ko.observable(class_name)
  147. };
  148. obj.name.subscribe(function() {
  149. self.query.functions.valueHasMutated();
  150. });
  151. obj.class_name.subscribe(function() {
  152. self.query.functions.valueHasMutated();
  153. });
  154. self.query.functions.push(obj);
  155. };
  156. self.removeFunction = function(index) {
  157. self.query.functions.splice(index, 1);
  158. };
  159. function getMultiFormData(prefix, arr, members) {
  160. var data = {};
  161. var index = 0;
  162. data[prefix + '-next_form_id'] = arr.length;
  163. ko.utils.arrayForEach(arr, function(obj) {
  164. $.each(members, function(index, member) {
  165. data[prefix + '-' + index + '-' + member] = obj[member]();
  166. });
  167. data[prefix + '-' + index + '-_exists'] = true;
  168. data[prefix + '-' + index + '-_deleted'] = false;
  169. });
  170. return data;
  171. }
  172. self.getSettingsFormData = function() {
  173. return getMultiFormData('settings', self.query.settings(), ['key', 'value']);
  174. };
  175. self.getFileResourcesFormData = function() {
  176. return getMultiFormData('file_resources', self.query.fileResources(), ['type', 'path']);
  177. };
  178. self.getFunctionsFormData = function() {
  179. return getMultiFormData('functions', self.query.functions(), ['name', 'class_name']);
  180. };
  181. self.getParametersFormData = function() {
  182. var data = {};
  183. $.each(self.query.parameters(), function(index, parameter) {
  184. data[parameter.parameter()] = parameter.value();
  185. });
  186. return data;
  187. };
  188. self.getOtherData = function() {
  189. var data = {
  190. 'query-email_notify': self.query.email(),
  191. 'query-is_parameterized': self.query.isParameterized()
  192. };
  193. return data;
  194. };
  195. var error_fn = function(jqXHR, status, errorThrown) {
  196. self.isRunning(false);
  197. try {
  198. $(document).trigger('server.error', $.parseJSON(jqXHR.responseText));
  199. } catch(e) {
  200. $(document).trigger('server.unmanageable_error', jqXHR.responseText);
  201. }
  202. };
  203. self.fetchDatabases = function() {
  204. var request = {
  205. url: '/' + self.server() + '/api/autocomplete',
  206. dataType: 'json',
  207. type: 'GET',
  208. success: function(data) {
  209. self.updateDatabases(data.databases);
  210. },
  211. error: error_fn
  212. };
  213. $.ajax(request);
  214. };
  215. self.fetchQuery = function() {
  216. $(document).trigger('fetch.query');
  217. var request = {
  218. url: '/' + self.server() + '/api/query/' + self.query.id() + '/get',
  219. dataType: 'json',
  220. type: 'GET',
  221. success: function(data) {
  222. self.updateQuery(data.design);
  223. $(document).trigger('fetched.query', data);
  224. },
  225. error: error_fn
  226. };
  227. $.ajax(request);
  228. };
  229. self.fetchParameters = function() {
  230. $(document).trigger('fetch.parameters');
  231. var data = {
  232. 'query-query': self.query.query(),
  233. 'query-database': self.database()
  234. };
  235. $.extend(data, self.getSettingsFormData());
  236. $.extend(data, self.getFileResourcesFormData());
  237. $.extend(data, self.getFunctionsFormData());
  238. $.extend(data, self.getParametersFormData());
  239. $.extend(data, self.getOtherData());
  240. var request = {
  241. url: '/' + self.server() + '/api/query/parameters',
  242. dataType: 'json',
  243. type: 'POST',
  244. success: function(data) {
  245. self.updateParameters(data.parameters);
  246. $(document).trigger('fetched.parameters');
  247. },
  248. error: error_fn,
  249. data: data
  250. };
  251. $.ajax(request);
  252. };
  253. self.explainQuery = function() {
  254. $(document).trigger('explain.query', data);
  255. self.query.explain(true);
  256. self.isRunning(true);
  257. var data = {
  258. 'query-query': self.query.query(),
  259. 'query-database': self.database()
  260. };
  261. $.extend(data, self.getSettingsFormData());
  262. $.extend(data, self.getFileResourcesFormData());
  263. $.extend(data, self.getFunctionsFormData());
  264. $.extend(data, self.getParametersFormData());
  265. $.extend(data, self.getOtherData());
  266. var request = {
  267. url: '/' + self.server() + '/api/query/execute/?explain=true',
  268. dataType: 'json',
  269. type: 'POST',
  270. success: function(data) {
  271. self.query.errors.removeAll();
  272. if (data.status == 0) {
  273. self.logs.removeAll();
  274. self.rows.removeAll();
  275. self.columns.removeAll();
  276. self.explanation(data.explanation);
  277. } else {
  278. self.query.errors.push(data.message);
  279. $(document).trigger('error.query');
  280. }
  281. $(document).trigger('explained.query', data);
  282. self.isRunning(false);
  283. },
  284. error: error_fn,
  285. data: data
  286. };
  287. $.ajax(request);
  288. };
  289. self.executeQuery = function() {
  290. $(document).trigger('execute.query', data);
  291. self.query.explain(false);
  292. self.isRunning(true);
  293. var data = {
  294. 'query-query': self.query.query(),
  295. 'query-database': self.database()
  296. };
  297. $.extend(data, self.getSettingsFormData());
  298. $.extend(data, self.getFileResourcesFormData());
  299. $.extend(data, self.getFunctionsFormData());
  300. $.extend(data, self.getParametersFormData());
  301. $.extend(data, self.getOtherData());
  302. var request = {
  303. url: '/' + self.server() + '/api/query/execute/',
  304. dataType: 'json',
  305. type: 'POST',
  306. success: function(data) {
  307. self.query.errors.removeAll();
  308. if (data.status == 0) {
  309. self.query.id(data.id);
  310. self.logs.removeAll();
  311. self.rows.removeAll();
  312. self.columns.removeAll();
  313. self.resultsURL('/' + self.server() + '/results/' + self.query.id() + '/0?format=json');
  314. self.watchURL(data.watch_url);
  315. self.watchQueryLoop();
  316. } else {
  317. self.query.errors.push(data.message);
  318. $(document).trigger('error.query');
  319. }
  320. $(document).trigger('executed.query', data);
  321. },
  322. error: error_fn,
  323. data: data
  324. };
  325. $.ajax(request);
  326. };
  327. self.watchQuery = function() {
  328. var data = {
  329. 'query-query': self.query.query(),
  330. 'query-database': self.database()
  331. };
  332. $.extend(data, self.getSettingsFormData());
  333. $.extend(data, self.getFileResourcesFormData());
  334. $.extend(data, self.getFunctionsFormData());
  335. $.extend(data, self.getParametersFormData());
  336. $.extend(data, self.getOtherData());
  337. var request = {
  338. url: self.watchURL(),
  339. dataType: 'json',
  340. type: 'POST',
  341. success: function(data) {
  342. $(document).trigger('watched.query', data);
  343. },
  344. error: error_fn,
  345. data: data
  346. };
  347. $.ajax(request);
  348. };
  349. self.watchQueryLoop = function() {
  350. var TIMEOUT = 1000;
  351. var timer = null;
  352. var executed_once = false;
  353. var fn = function() {
  354. $(document).one('watched.query', function(e, data) {
  355. if (executed_once && (data.isSuccess || data.isFailure)) {
  356. clearTimeout(timer);
  357. $(document).trigger('stop_watch.query');
  358. self.fetchResults();
  359. } else {
  360. executed_once = true;
  361. if (data.log) {
  362. self.logs.push(data.log);
  363. // scroll logs
  364. }
  365. timer = setTimeout(fn, TIMEOUT);
  366. }
  367. });
  368. self.watchQuery();
  369. };
  370. $(document).trigger('start_watch.query');
  371. timer = setTimeout(fn, TIMEOUT);
  372. };
  373. self.fetchResults = function() {
  374. $(document).trigger('fetch.results');
  375. var request = {
  376. url: self.resultsURL(),
  377. dataType: 'json',
  378. type: 'GET',
  379. success: function(data) {
  380. self.isRunning(false);
  381. self.columns(data.columns);
  382. self.rows.push.apply(self.rows, data.results);
  383. self.resultsEmpty(self.rows().length == 0);
  384. if (data.has_more) {
  385. self.resultsURL(data.next_json_set);
  386. } else {
  387. self.resultsURL(null);
  388. }
  389. $(document).trigger('fetched.results', data);
  390. },
  391. error: error_fn
  392. };
  393. $.ajax(request);
  394. };
  395. self.saveQuery = function() {
  396. var self = this;
  397. if (self.query.query() && self.query.name()) {
  398. var data = {
  399. 'query-query': self.query.query(),
  400. 'query-database': self.database()
  401. };
  402. $.extend(data, self.getSettingsFormData());
  403. $.extend(data, self.getFileResourcesFormData());
  404. $.extend(data, self.getFunctionsFormData());
  405. $.extend(data, self.getParametersFormData());
  406. $.extend(data, self.getOtherData());
  407. data['saveform-name'] = self.query.name();
  408. data['saveform-desc'] = self.query.description();
  409. if (self.query.id() > 0) {
  410. data['query-id'] = self.query.id();
  411. }
  412. var url = '/' + self.server() + '/api/query/';
  413. if (self.query.id() && self.query.id() != -1) {
  414. url += self.query.id();
  415. }
  416. var request = {
  417. url: url,
  418. dataType: 'json',
  419. type: 'POST',
  420. success: function(data) {
  421. self.query.id(data.design_id);
  422. $(document).trigger('saved.query', data);
  423. },
  424. error: function() {
  425. $(document).trigger('error_save.query');
  426. },
  427. data: data
  428. };
  429. $.ajax(request);
  430. }
  431. };
  432. self.cancelQuery = function() {
  433. $(document).trigger('cancel.query');
  434. var url = '/' + self.server() + '/api/query/' + self.query.id() + '/cancel';
  435. $.post(url,
  436. function(response) {
  437. if (response['status'] != 0) {
  438. $(document).trigger('error_cancel.query', response['message']);
  439. } else {
  440. $(document).trigger('cancelled.query');
  441. }
  442. }
  443. );
  444. };
  445. // Events
  446. // Remove watched query event that watchQueryLoop may be bound to.
  447. $(document).on('server.unmanageable_error', function() {
  448. $(document).off('watched.query');
  449. });
  450. }
  451. // For routie
  452. function showSection(section) {
  453. $('.section').hide();
  454. $('#' + section).show();
  455. $(window).scrollTop(0);
  456. }