Skip to content
This repository has been archived by the owner on Jan 4, 2020. It is now read-only.

Commit

Permalink
databaseList view and db change functionality added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Erhan Gundogan committed Jan 18, 2012
1 parent a385d85 commit d0c4f32
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 6 deletions.
30 changes: 30 additions & 0 deletions app.js
Expand Up @@ -160,6 +160,36 @@ app.get("/listDatabases",
*/ */
)); ));


app.get("/activeDatabase", function(req, res) {
monitor.initialize(function(err, db) {
if (err) {
res.end();
} else {
res.json(db.databaseName);
}
});
/*
monitor.activeDatabaseInfo("db", function(err, dbName) {
res.json(dbName);
})
*/
});

app.get("/changeDatabase/:db", function(req, res) {
if (!req.params.db) {
res.end();
} else {
admin.initialize(function(err, _admin) {
if (err) {
res.json(false);
} else {
_admin.db.databaseName = req.params.db;
res.json(true);
}
});
}
});

app.post("/profilingLevel", function(req, res, next) { app.post("/profilingLevel", function(req, res, next) {
var result = req.body.set; var result = req.body.set;
if (result) { if (result) {
Expand Down
89 changes: 89 additions & 0 deletions mongo/monitor.js
Expand Up @@ -198,6 +198,17 @@ Monitor.prototype.fn = function(method, callback) {
return this; return this;
}; };


/**
* Get list of databases on server.
* Must use admin database to execute {"listDatabases":1} command
* TODO: switch to admin db before execute
*
* Mongo shell usage:
* use admin
* db.runCommand({"listDatabases":1});
*
* @param {function} callback
*/
Monitor.prototype.listDatabases = function(callback) { Monitor.prototype.listDatabases = function(callback) {
var self = this, var self = this,
db = this.db; db = this.db;
Expand Down Expand Up @@ -228,6 +239,84 @@ Monitor.prototype.listDatabases = function(callback) {
} }
}; };


/**
* Command execution
*
* @param {string} db
* @param {object} command
* @param {function} callback
*/
function executeCommand(db, command, callback) {
if (!db) {
return callback(null, null);
} else {
db.executeDbCommand(command, function(err, result) {
if (err) {
console.log(err);
return callback(err, null);
} else {
return callback(null, result);
}
});
}
}

/**
* Gets active databases stats
* use stat property to get specific variable.
* Without specific variable results are:
{
"db" : "databaseName",
"collections" : 5,
"objects" : 23,
"avgObjSize" : 138.6086956521739,
"dataSize" : 3188,
"storageSize" : 110592,
"numExtents" : 6,
"indexes" : 8,
"indexSize" : 65408,
"fileSize" : 201326592,
"nsSizeMB" : 16,
"ok" : 1
}
*
* @param {string} stat (optional)
* @param {function} callback
*/
Monitor.prototype.activeDatabaseInfo = function(stat, callback) {
var self = this,
db = this.db,
haveStat = true;

if (!callback && "function" == typeof stat) {
callback = stat;
haveStat = false;
}

function _callback(err, result) {
if (err) {
return callback(err);
} else {
if (result && result.documents && result.documents.length > 0) {
var dbs = result.documents[0];
return haveStat && dbs.hasOwnProperty(stat) ?
callback(null, dbs[stat]) :
callback(null, dbs);
} else {
return callback(null, null);
}
}
}

if (!db) {
this.initialize(function(err, _db) {
executeCommand(_db, {"listDatabases":1}, _callback);
});
} else {
executeCommand(db, {"listDatabases":1}, _callback);
}
};

/* /*
* Exports * Exports
*/ */
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{ {
"name": "mon4mongo" "name": "mon4mongo"
, "author": "Erhan Gundogan <erhan@trposta.net>" , "author": "Erhan Gundogan <erhan@trposta.net>"
, "version": "0.0.5-2" , "version": "0.0.5-3"
, "private": false , "private": false
, "dependencies": { , "dependencies": {
"express": ">= 2.5.1" "express": ">= 2.5.1"
Expand Down
44 changes: 40 additions & 4 deletions public/js/methods.js
Expand Up @@ -70,18 +70,54 @@ $(function(){
if (panel && panel.length > 0) { if (panel && panel.length > 0) {
return; return;
} }

bindAddress(item.requestAddress); bindAddress(item.requestAddress);
}); });
}); });
});


function bindDBAddresses() {
$(".db-link a").each(function(index, item) {
$(this).bind("click", function(event) {
event.preventDefault();
var address = "/changeDatabase/" + $(this).attr("data-db");
$.ajax({
url: address,
dataType: "json",
error: function(jqXHR, textStatus, errorThrown) {
alert(textStatus);
console.log(jqXHR);
},
complete: function(jqXHR, textStatus) {
highlightActiveDB();
}
});
});
});
}


}); function highlightActiveDB() {
$.ajax({
url: "/activeDatabase",
dataType: "json",
success: function(data, textStatus, jqXHR) {
if (data) {
$(".db-link a").each(function(index) {
var db = $(this).attr("data-db");
if (data == db) {
$(this).attr("class", "active");
} else {
$(this).removeAttr("class");
}
});
}
}
});
}


function bindAddress(address) { function bindAddress(address, responseType) {
$.ajax({ $.ajax({
url: address, url: address,
dataType: "html", dataType: responseType || "html",
success: function(err, result) { success: function(err, result) {
var vAlign = new vAlignment(); var vAlign = new vAlignment();
var element = "#" + vAlign.calculateColumns().getKey().result.key; var element = "#" + vAlign.calculateColumns().getKey().result.key;
Expand Down
5 changes: 5 additions & 0 deletions public/style/main.css
Expand Up @@ -124,4 +124,9 @@ footer {


.about h1 { .about h1 {
color: white; color: white;
}

a.active {
font-weight: bold;
color: #f00;
} }
6 changes: 5 additions & 1 deletion views/modules/monitor/listDatabases.jade
Expand Up @@ -15,8 +15,12 @@
each value,key in result each value,key in result
tr tr
td td
.fit-to-cell=value.name .fit-to-cell.db-link
a(href="#", data-db=value.name)=value.name
td td
.fit-to-cell=value.sizeOnDisk .fit-to-cell=value.sizeOnDisk
td td
.fit-to-cell=value.empty .fit-to-cell=value.empty
script
bindDBAddresses();
highlightActiveDB()

0 comments on commit d0c4f32

Please sign in to comment.