Skip to content
Browse files

databaseList view and db change functionality added.

  • Loading branch information...
1 parent a385d85 commit d0c4f32f08dbb26d7c5e303e4f3136e477e88c07 Erhan Gundogan committed
Showing with 170 additions and 6 deletions.
  1. +30 −0 app.js
  2. +89 −0 mongo/monitor.js
  3. +1 −1 package.json
  4. +40 −4 public/js/methods.js
  5. +5 −0 public/style/main.css
  6. +5 −1 views/modules/monitor/listDatabases.jade
View
30 app.js
@@ -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) {
var result = req.body.set;
if (result) {
View
89 mongo/monitor.js
@@ -198,6 +198,17 @@ Monitor.prototype.fn = function(method, callback) {
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) {
var self = this,
db = this.db;
@@ -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
*/
View
2 package.json
@@ -1,7 +1,7 @@
{
"name": "mon4mongo"
, "author": "Erhan Gundogan <erhan@trposta.net>"
- , "version": "0.0.5-2"
+ , "version": "0.0.5-3"
, "private": false
, "dependencies": {
"express": ">= 2.5.1"
View
44 public/js/methods.js
@@ -70,18 +70,54 @@ $(function(){
if (panel && panel.length > 0) {
return;
}
-
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({
url: address,
- dataType: "html",
+ dataType: responseType || "html",
success: function(err, result) {
var vAlign = new vAlignment();
var element = "#" + vAlign.calculateColumns().getKey().result.key;
View
5 public/style/main.css
@@ -124,4 +124,9 @@ footer {
.about h1 {
color: white;
+}
+
+a.active {
+ font-weight: bold;
+ color: #f00;
}
View
6 views/modules/monitor/listDatabases.jade
@@ -15,8 +15,12 @@
each value,key in result
tr
td
- .fit-to-cell=value.name
+ .fit-to-cell.db-link
+ a(href="#", data-db=value.name)=value.name
td
.fit-to-cell=value.sizeOnDisk
td
.fit-to-cell=value.empty
+ script
+ bindDBAddresses();
+ highlightActiveDB()

0 comments on commit d0c4f32

Please sign in to comment.
Something went wrong with that request. Please try again.