Refactor javascript code to consume api
This commit is contained in:
@@ -1,18 +1,9 @@
|
|||||||
function apiCall(path, cb) {
|
function apiCall(method, path, params, cb) {
|
||||||
$.getJSON(path, function(resp) { cb(resp); });
|
|
||||||
}
|
|
||||||
|
|
||||||
function getTables(cb) { apiCall("/tables", cb); }
|
|
||||||
function getTableStructure(table, cb) { apiCall("/tables/" + table, cb); }
|
|
||||||
function getTableIndexes(table, cb) { apiCall("/tables/" + table + "/indexes", cb); }
|
|
||||||
function getHistory(cb) { apiCall("/history", cb); }
|
|
||||||
|
|
||||||
function executeQuery(query, cb) {
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "/query",
|
url: path,
|
||||||
method: "post",
|
method: method,
|
||||||
cache: false,
|
cache: false,
|
||||||
data: { query: query, format: "json" },
|
data: params,
|
||||||
success: function(data) {
|
success: function(data) {
|
||||||
cb(data);
|
cb(data);
|
||||||
},
|
},
|
||||||
@@ -22,6 +13,19 @@ function executeQuery(query, cb) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getTables(cb) { apiCall("get", "/tables", {}, cb); }
|
||||||
|
function getTableStructure(table, cb) { apiCall("get", "/tables/" + table, {}, cb); }
|
||||||
|
function getTableIndexes(table, cb) { apiCall("get", "/tables/" + table + "/indexes", {}, cb); }
|
||||||
|
function getHistory(cb) { apiCall("get", "/history", {}, cb); }
|
||||||
|
|
||||||
|
function executeQuery(query, cb) {
|
||||||
|
apiCall("post", "/query", { query: query }, cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
function explainQuery(query, cb) {
|
||||||
|
apiCall("post", "/explain", { query: query }, cb);
|
||||||
|
}
|
||||||
|
|
||||||
function loadTables() {
|
function loadTables() {
|
||||||
getTables(function(data) {
|
getTables(function(data) {
|
||||||
data.forEach(function(item) {
|
data.forEach(function(item) {
|
||||||
@@ -150,6 +154,9 @@ function runQuery() {
|
|||||||
var query = $.trim(editor.getValue());
|
var query = $.trim(editor.getValue());
|
||||||
|
|
||||||
if (query.length == 0) {
|
if (query.length == 0) {
|
||||||
|
$("#run").removeAttr("disabled");
|
||||||
|
$("#explain").removeAttr("disabled");
|
||||||
|
$("#query_progress").hide();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,12 +181,13 @@ function runExplain() {
|
|||||||
var query = $.trim(editor.getValue());
|
var query = $.trim(editor.getValue());
|
||||||
|
|
||||||
if (query.length == 0) {
|
if (query.length == 0) {
|
||||||
|
$("#run").removeAttr("disabled");
|
||||||
|
$("#explain").removeAttr("disabled");
|
||||||
|
$("#query_progress").hide();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
query = "EXPLAIN ANALYZE " + query;
|
explainQuery(query, function(data) {
|
||||||
|
|
||||||
executeQuery(query, function(data) {
|
|
||||||
buildTable(data);
|
buildTable(data);
|
||||||
|
|
||||||
$("#run").removeAttr("disabled");
|
$("#run").removeAttr("disabled");
|
||||||
|
|||||||
Reference in New Issue
Block a user