api.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. var API = {
  17. //base querying function
  18. //query(functionName, ClusterName, arg0, arg1).done(callback)
  19. query: function() {
  20. // all url building should be in this function
  21. var url = "/hbase/api";
  22. var $_POST = {};
  23. for(var i=0;i<arguments.length;i++) {
  24. if (arguments[i] == null)
  25. arguments[i] = "";
  26. arguments[i] = arguments[i] + "";
  27. var key = arguments[i].slice(0, 15);
  28. if (key == "hbase-post-key-") {
  29. key += Object.keys($_POST).length;
  30. $_POST[key] = arguments[i].slice(15);
  31. arguments[i] = key;
  32. }
  33. url += '/' + arguments[i];
  34. }
  35. var queryObject = {url:url, method:'POST', startTime: new Date().getTime(), status:'running...'}
  36. var handler = $.post(url, $_POST).error(function(response) {
  37. $.jHueNotify.error(JSON.parse(response.responseText).message);
  38. });
  39. var doneHandle = handler.done;
  40. handler.done = function() {
  41. var cb = arguments[0];
  42. return doneHandle.apply(handler, [function(data) {
  43. app.views.tabledata.truncated(data.truncated); //change later
  44. app.views.tabledata.truncateLimit(data.limit);
  45. app.views.tabledata.truncateCount(data.truncate_count);
  46. data = data.data;
  47. return cb(data);
  48. }].concat(Array.prototype.slice.call(arguments).slice(1)));
  49. };
  50. return handler;
  51. },
  52. queryArray: function(action, args) {
  53. return API.query.apply(this, [action].concat(args));
  54. },
  55. //function,arg0,arg1, queries the current cluster
  56. queryCluster: function() {
  57. var args = Array.prototype.slice.call(arguments);
  58. args.splice(1, 0, app.cluster());
  59. return API.query.apply(this, args);
  60. },
  61. queryTable: function() {
  62. var args = Array.prototype.slice.call(arguments);
  63. args.splice(1, 0, app.views.tabledata.name());
  64. return API.queryCluster.apply(this, args);
  65. },
  66. //functions to abstract away API structure, in case API changes:
  67. //only have funciton name, data, and callbacks. no URL or api-facing.
  68. createTable: function(cluster, tableName, columns, callback) {
  69. return API.query('createTable', cluster, tableName, columns).done(callback);
  70. },
  71. getTableNames: function(cluster, callback) {
  72. return API.query('getTableNames', cluster).done(callback);
  73. },
  74. getTableList: function(cluster, callback) {
  75. return API.query('getTableList', cluster).done(callback);
  76. }
  77. }