api.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 += '/' + encodeURIComponent(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. $(document).trigger("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. {
  44. app.views.tabledata.truncateLimit(data.limit);
  45. data = data.data;
  46. return cb(data);
  47. }].concat(Array.prototype.slice.call(arguments).slice(1)));
  48. };
  49. return handler;
  50. },
  51. queryArray: function(action, args) {
  52. return API.query.apply(this, [action].concat(args));
  53. },
  54. //function,arg0,arg1, queries the current cluster
  55. queryCluster: function() {
  56. var args = Array.prototype.slice.call(arguments);
  57. args.splice(1, 0, app.cluster());
  58. return API.query.apply(this, args);
  59. },
  60. queryTable: function() {
  61. var args = Array.prototype.slice.call(arguments);
  62. args.splice(1, 0, app.views.tabledata.name());
  63. return API.queryCluster.apply(this, args);
  64. },
  65. //functions to abstract away API structure, in case API changes:
  66. //only have funciton name, data, and callbacks. no URL or api-facing.
  67. createTable: function(cluster, tableName, columns, callback) {
  68. return API.query('createTable', cluster, tableName, columns).done(callback);
  69. },
  70. getTableNames: function(cluster, callback) {
  71. return API.query('getTableNames', cluster).done(callback);
  72. },
  73. getTableList: function(cluster, callback) {
  74. return API.query('getTableList', cluster).done(callback);
  75. }
  76. }