Permalink
Browse files

listDatabases, postprocess functionality added

  • Loading branch information...
Erhan Gundogan
Erhan Gundogan committed Jan 15, 2012
1 parent 83e8b62 commit a385d85b34d32db2ea5a46e761f83f6729d8e99f
Showing with 162 additions and 23 deletions.
  1. +2 −1 .gitignore
  2. +7 −0 .idea/jsLibraryMappings.xml
  3. +60 −17 app.js
  4. +1 −1 mongo/admin.js
  5. +32 −2 mongo/monitor.js
  6. +1 −1 package.json
  7. +4 −1 public/js/methods.js
  8. +31 −0 utils.js
  9. +2 −0 views/master/sidebar.jade
  10. +22 −0 views/modules/monitor/listDatabases.jade
View
@@ -1 +1,2 @@
-node_modules/
+node_modules/
+.idea
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="JavaScriptLibraryMappings">
+ <file url="file://$PROJECT_DIR$/app.js" libraries="{Express, node}" />
+ </component>
+</project>
+
View
77 app.js
@@ -14,14 +14,16 @@ var express = require("express")
, MongoMonitor = require("./mongo/monitor")
, monitor = new MongoMonitor()
, MongoAdmin = require("./mongo/admin")
- , admin = new MongoAdmin(monitor);
+ , admin = new MongoAdmin(monitor)
+ , utils = require("./utils");
/**
* Configuration.
*/
app.configure(function(){
app.set("views", __dirname + "/views");
app.set("view engine", "jade");
+ app.use(express.logger());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
@@ -52,6 +54,9 @@ app.dynamicHelpers({
},
settings: function() {
return settings;
+ },
+ utils: function() {
+ return utils;
}
});
@@ -70,21 +75,45 @@ app.dynamicHelpers({
* @api public
*/
function getRoute(base, command, template) {
- return function(req, res) {
+ return function(req, res, next) {
base.fn(command, function(err, result) {
if (err) {
- res.partial("error", { err:err });
+ next(err);
} else {
- if (req.xhr) {
- res.partial(template, { result:result });
- } else {
- res.render(template, { result:result });
- }
+ req.pre_process = result;
+ req.template = template;
+ next();
}
});
}
}
+function postProcess(fn) {
+ return function(req, res, next) {
+ var result = req.pre_process;
+ function _process() {
+ if (req.xhr) {
+ return res.partial(req.template, { result:result });
+ } else {
+ return res.render(req.template, { result:result });
+ }
+ }
+
+ if (fn) {
+ fn(result, function(err, result) {
+ if (err) {
+ next(err);
+ } else {
+ _process(result);
+ }
+ });
+ } else {
+ _process(result);
+ }
+ }
+}
+
+
/**
* Routes.
*/
@@ -97,25 +126,39 @@ app.get("/about", function(req, res){
});
app.get("/getServer",
- getRoute(monitor, "getServer", "modules/monitor/getServer"));
+ getRoute(monitor, "getServer", "modules/monitor/getServer"), postProcess());
app.get("/getInformation",
- getRoute(admin, "serverInfo", "modules/admin/getInformation"));
+ getRoute(admin, "serverInfo", "modules/admin/getInformation"), postProcess());
app.get("/pingServer",
- getRoute(admin, "ping", "modules/admin/pingServer"));
+ getRoute(admin, "ping", "modules/admin/pingServer"), postProcess());
app.get("/profilingLevel",
- getRoute(admin, "profilingLevel", "modules/admin/profilingLevel"));
+ getRoute(admin, "profilingLevel", "modules/admin/profilingLevel"), postProcess());
app.get("/collectionsInfo",
- getRoute(monitor, "collectionsInfo", "modules/monitor/collectionsInfo"));
+ getRoute(monitor, "collectionsInfo", "modules/monitor/collectionsInfo"), postProcess());
app.get("/collectionsNames",
- getRoute(monitor, "collectionNames", "modules/monitor/collectionsNames"));
-
-app.get("/collectionsNames",
- getRoute(monitor, "collectionNames", "modules/monitor/collectionsNames"));
+ getRoute(monitor, "collectionNames", "modules/monitor/collectionsNames"), postProcess());
+
+app.get("/listDatabases",
+ getRoute(monitor, "listDatabases", "modules/monitor/listDatabases"),
+ postProcess(
+ /*
+ function(items, callback) {
+ try {
+ var result = items.map(function(item) {
+ return JSON.stringify(item);
+ });
+ callback(null, result);
+ } catch(err) {
+ callback(err, null);
+ }
+ }
+ */
+));
app.post("/profilingLevel", function(req, res, next) {
var result = req.body.set;
View
@@ -76,7 +76,7 @@ Admin.prototype.fn = function(method, callback) {
});
return this;
-}
+};
/*
* Exports
View
@@ -34,7 +34,7 @@ function Monitor(host, port, dbName) {
this.port = port || (process.env["MONGO_NODE_DRIVER_PORT"] != null
? process.env["MONGO_NODE_DRIVER_PORT"]
: Connection.DEFAULT_PORT);
- this.dbName = dbName || "test";
+ this.dbName = dbName || "admin";
}
/**
@@ -196,7 +196,37 @@ Monitor.prototype.fn = function(method, callback) {
}
return this;
-}
+};
+
+Monitor.prototype.listDatabases = function(callback) {
+ var self = this,
+ db = this.db;
+
+ function execCmd(db, command) {
+ db.executeDbCommand(command, function(err, result) {
+ if (err) {
+ console.log(err);
+ return callback(err, null);
+ } else {
+ if (result && result.documents && result.documents.length > 0) {
+ var dbs = result.documents[0];
+ if (dbs.hasOwnProperty("databases")) {
+ return callback(null, dbs.databases);
+ }
+ }
+ return callback(null, null);
+ }
+ });
+ }
+
+ if (!db) {
+ this.initialize(function(err, _db) {
+ execCmd(_db, {"listDatabases":1});
+ });
+ } else {
+ execCmd(db, {"listDatabases":1});
+ }
+};
/*
* Exports
View
@@ -1,7 +1,7 @@
{
"name": "mon4mongo"
, "author": "Erhan Gundogan <erhan@trposta.net>"
- , "version": "0.0.5-1"
+ , "version": "0.0.5-2"
, "private": false
, "dependencies": {
"express": ">= 2.5.1"
View
@@ -59,7 +59,10 @@ $(function(){
requestAddress: "/collectionsInfo" },
{ navLinkID: "#collectionsNamesLink",
bodyElementID: "#collectionsNames",
- requestAddress: "/collectionsNames" }
+ requestAddress: "/collectionsNames" },
+ { navLinkID: "#listDatabasesLink",
+ bodyElementID: "#listDatabases",
+ requestAddress: "/listDatabases" },
].map(function(item) {
$(item.navLinkID).bind("click", function(event) {
event.preventDefault();
View
@@ -0,0 +1,31 @@
+/**
+ * Flatten the given `arr`.
+ *
+ * @param {Array} arr
+ * @return {Array}
+ * @api private
+ */
+
+exports.flatten = function(arr, ret){
+ var ret = ret || []
+ , len = arr.length;
+ for (var i = 0; i < len; ++i) {
+ if (Array.isArray(arr[i])) {
+ exports.flatten(arr[i], ret);
+ } else {
+ ret.push(arr[i]);
+ }
+ }
+ return ret;
+};
+
+/**
+ * Detects if Object is Array or not
+ *
+ * @param {Object} v
+ */
+exports.isArray = function(v) {
+ return v.isArray ||
+ v instanceof Array ||
+ Object.prototype.toString.call(v) == '[object Array]';
+};
@@ -8,6 +8,8 @@
a(href="#", id="collectionsInfoLink") Collections Information
h5 Database
ul
+ li
+ a(href="#", id="listDatabasesLink") List Databases
li
a(href="#", id="collectionsNamesLink") Collection Names
h5 Administration
@@ -0,0 +1,22 @@
+- var classes = ["message"];
+- if (settings.theme.columnMode)
+ - classes.push("span6");
+
+#listDatabases(class=classes)
+ a.close(href="#", onclick="$('#listDatabases').remove()") ×
+ a.close(href="#", onclick="$('#listDatabases').remove(); bindAddress('/listDatabases');")
+ h4 Database List
+ - var tableClasses = ["bordered-table", "zebra-striped", "condensed-table", "info-table"];
+ table(class=tableClasses)
+ tr
+ th Name
+ th Size
+ th Empty
+ each value,key in result
+ tr
+ td
+ .fit-to-cell=value.name
+ td
+ .fit-to-cell=value.sizeOnDisk
+ td
+ .fit-to-cell=value.empty

0 comments on commit a385d85

Please sign in to comment.