beeswax.vm.js 14 KB

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