api.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. app.queries.errorQuery(queryObject);
  43. $.jHueNotify.error(JSON.parse(response.responseText).message);
  44. });
  45. var doneHandle = handler.done;
  46. handler.done = function()
  47. {
  48. var cb = arguments[0];
  49. return doneHandle.apply(handler, [function(data)
  50. {
  51. app.views.tabledata.truncated(data.truncated); //change later
  52. app.views.tabledata.truncateLimit(data.limit);
  53. app.views.tabledata.truncateCount(data.truncate_count);
  54. data = data.data;
  55. return cb(data);
  56. }].concat(Array.prototype.slice.call(arguments).slice(1)));
  57. };
  58. return handler;
  59. },
  60. queryArray: function(action, args)
  61. {
  62. return API.query.apply(this, [action].concat(args));
  63. },
  64. //function,arg0,arg1, queries the current cluster
  65. queryCluster: function()
  66. {
  67. var args = Array.prototype.slice.call(arguments);
  68. args.splice(1, 0, app.cluster());
  69. return API.query.apply(this, args);
  70. },
  71. queryTable: function()
  72. {
  73. var args = Array.prototype.slice.call(arguments);
  74. args.splice(1, 0, app.views.tabledata.name());
  75. return API.queryCluster.apply(this, args);
  76. },
  77. //functions to abstract away API structure, in case API changes:
  78. //only have funciton name, data, and callbacks. no URL or api-facing.
  79. createTable: function(cluster, tableName, columns, callback)
  80. {
  81. return API.query('createTable', cluster, tableName, columns).done(callback);
  82. },
  83. getTableNames: function(cluster, callback)
  84. {
  85. return API.query('getTableNames', cluster).done(callback);
  86. },
  87. getTableList: function(cluster, callback)
  88. {
  89. return API.query('getTableList', cluster).done(callback);
  90. }
  91. }