Commit 629874d1 by James Cropcho

yeah, that's more like it

parent 512f0fdc
if (typeof collection == "undefined") { if (typeof collection == "undefined") {
throw "You have to supply a 'collection' variable, a la \"--eval 'var limit = 10'\""; throw "You have to supply a 'collection' variable, a la \"--eval 'var limit = 10'\"";
} }
//db.system.js.save( { _id : "getCollection", value : function() { return eval(collection); }} );
if (typeof limit == "undefined") { limit = db[collection].count(); } if (typeof limit == "undefined") { limit = db[collection].count(); }
print("Using limit of " + limit); print("Using limit of " + limit);
var alreadyEmitted = new Array(); // global //var alreadyEmitted = new Array(); // global
schemaAnalyzerKeyIsPresent = function(toBeEmitted, alreadyEmitted) { //schemaAnalyzerKeyIsPresent = function(toBeEmitted, alreadyEmitted) {
for(key in alreadyEmitted) { // for(key in alreadyEmitted) {
if(key["key"] === toBeEmitted["key"] && key["type"] === toBeEmitted["type"]) { // if(key["key"] === toBeEmitted["key"] && key["type"] === toBeEmitted["type"]) {
return true; // return true;
} // }
} // }
return false; // return false;
} //}
db.system.js.save( { _id : "schemaAnalyzerKeyIsPresent", value : schemaAnalyzerKeyIsPresent } ); //db.system.js.save( { _id : "schemaAnalyzerKeyIsPresent", value : schemaAnalyzerKeyIsPresent } );
schemaAnalyzerCanHaveChildren = function (v) { schemaAnalyzerCanHaveChildren = function (v) {
var isArray = v && var isArray = v &&
...@@ -35,10 +36,10 @@ schemaAnalyzerMapRecursive = function(parentKey, keys) { ...@@ -35,10 +36,10 @@ schemaAnalyzerMapRecursive = function(parentKey, keys) {
toBeEmitted = {key: key, type: typeof value}; toBeEmitted = {key: key, type: typeof value};
if(!schemaAnalyzerKeyIsPresent(toBeEmitted,alreadyEmitted)) { // if(!schemaAnalyzerKeyIsPresent(toBeEmitted,alreadyEmitted)) {
emit(toBeEmitted, {occurrences: 1}); emit(toBeEmitted, null);
alreadyEmitted.push(toBeEmitted); // alreadyEmitted.push(toBeEmitted);
} // }
if (schemaAnalyzerCanHaveChildren(value)) { if (schemaAnalyzerCanHaveChildren(value)) {
schemaAnalyzerMapRecursive(key, value); schemaAnalyzerMapRecursive(key, value);
...@@ -49,7 +50,7 @@ db.system.js.save({_id: "schemaAnalyzerMapRecursive", value: schemaAnalyzerMapRe ...@@ -49,7 +50,7 @@ db.system.js.save({_id: "schemaAnalyzerMapRecursive", value: schemaAnalyzerMapRe
map = function() { map = function() {
var keys = this; var keys = this;
var alreadyEmitted = new Array(); // reset global variable // var alreadyEmitted = new Array(); // reset global variable
for (var key in keys) { for (var key in keys) {
var value = keys[key]; var value = keys[key];
...@@ -58,10 +59,11 @@ map = function() { ...@@ -58,10 +59,11 @@ map = function() {
toBeEmitted = {key : key, type : typeof value}; toBeEmitted = {key : key, type : typeof value};
if(!schemaAnalyzerKeyIsPresent(toBeEmitted,alreadyEmitted)) { // if(!schemaAnalyzerKeyIsPresent(toBeEmitted,alreadyEmitted)) {
emit(toBeEmitted, {occurrences: 1}); // emit(toBeEmitted, {occurrences: 1});
alreadyEmitted.push(toBeEmitted); emit(toBeEmitted, null);
} // alreadyEmitted.push(toBeEmitted);
// }
if (schemaAnalyzerCanHaveChildren(value)) { if (schemaAnalyzerCanHaveChildren(value)) {
schemaAnalyzerMapRecursive(key, value); schemaAnalyzerMapRecursive(key, value);
...@@ -70,22 +72,32 @@ map = function() { ...@@ -70,22 +72,32 @@ map = function() {
} }
reduce = function(key, values){ reduce = function(key, values){
var occurrences = 0; // var occurrences = 0;
values.forEach(function(value) { // values.forEach(function(value) {
occurrences += value.occurrences; // occurrences += value.occurrences;
}); // });
return {occurrences: occurrences}; // return {occurrences: occurrences};
return null;
} }
finalize = function(key, value) { //finalize = function(key, value) {
value.percentage = value.occurrences / limit * 100.0; // value.percentage = value.occurrences / limit * 100.0;
return value; // return value;
} //}
//finalize = function(key, value) {
// if(!key["key"].match(/\.XX/)) {
// value = collectionDB.count({key: {$exists: true}});
// }
// return value;
//}
var resultsCollectionName = collection + "KeyNames"; var resultsCollectionName = collection + "Keys";
db[collection].mapReduce(map, reduce, { finalize: finalize, //resultsDB[resultsCollectionName].ensureIndex({ "_id.key" : 1 , "_id.type" : 1},{unique : true});
db[collection].mapReduce(map, reduce, { //finalize: finalize,
out: { out: {
replace : resultsCollectionName, replace : resultsCollectionName,
db : "schemaAnalyzerResults"}, db : "schemaAnalyzerResults"},
...@@ -95,9 +107,30 @@ db[collection].mapReduce(map, reduce, { finalize: finalize, ...@@ -95,9 +107,30 @@ db[collection].mapReduce(map, reduce, { finalize: finalize,
var resultsDB = db.getMongo().getDB("schemaAnalyzerResults"); var resultsDB = db.getMongo().getDB("schemaAnalyzerResults");
var keyNames = resultsDB[resultsCollectionName].find().sort({ "value.percentage": -1}); var keys = resultsDB[resultsCollectionName].find();
keys.forEach(function(key) {
delete key.value;
keyName = key["_id"].key; // should be keys??
keyNames.forEach(function(keyName) { var existsQuery = {};
print(tojson(keyName, '', true)); existsQuery[keyName] = {$exists: true};
if(!keyName.match(/\.XX/)) {
key.totalOccurences = db[collection].count(existsQuery);
}
else {
var sizeQuery = {};
sizeQuery[keyName] = {$size: true};
keyName.totalOccurences = db[collection].count(existsQuery) - db[collection].count(sizeQuery);
}
resultsDB[resultsCollectionName].save(key);
print(tojson(key, '', true));
}); });
//> db.users.count({"smartalert.daily": {$exists: true}});
//23
//> db.users.count({"smartalert.daily": {$size: 0}});
//3
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment