api.js 3.1 KB

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