Commit 66eac320 by James Cropcho

Merge pull request #86 from todvora/master

Display ASCII tables' scalars indented to the right #83
parents c465b9c7 aeeb3829
......@@ -31,13 +31,13 @@ So, let's see what we've got here:
+------------------------------------------------------------+
| key | types | occurrences | percents |
| ------------------ | ------------ | ----------- | -------- |
| _id | ObjectId | 5 | 100 |
| name | String | 5 | 100 |
| bio | String | 3 | 60 |
| birthday | String | 2 | 40 |
| pets | String,Array | 2 | 40 |
| someBinData | BinData-old | 1 | 20 |
| someWeirdLegacyKey | String | 1 | 20 |
| _id | ObjectId | 5 | 100.0 |
| name | String | 5 | 100.0 |
| bio | String | 3 | 60.0 |
| birthday | String | 2 | 40.0 |
| pets | Array,String | 2 | 40.0 |
| someBinData | BinData-old | 1 | 20.0 |
| someWeirdLegacyKey | String | 1 | 20.0 |
+------------------------------------------------------------+
_("test" is the database containing the collection we are analyzing.)_
......@@ -69,9 +69,9 @@ Let's examine the results closely:
+----------------------------------------------------+
| key | types | occurrences | percents |
| ----------- | ----------- | ----------- | -------- |
| _id | ObjectId | 1 | 100 |
| name | String | 1 | 100 |
| someBinData | BinData-old | 1 | 100 |
| _id | ObjectId | 1 | 100.0 |
| name | String | 1 | 100.0 |
| someBinData | BinData-old | 1 | 100.0 |
+----------------------------------------------------+
We are only examining the last document here ("limit = 1"). It belongs to Geneviève, and only contains the _id, name and bio fields. So it makes sense these are the only three keys.
......@@ -91,14 +91,14 @@ The default will traverse all the way to the bottom of that structure:
+----------------------------------------------------------------+
| key | types | occurrences | percents |
| -------------------------- | -------- | ----------- | -------- |
| _id | ObjectId | 1 | 100 |
| name | String | 1 | 100 |
| someNestedObject | Object | 1 | 100 |
| someNestedObject.a | Object | 1 | 100 |
| someNestedObject.a.b | Object | 1 | 100 |
| someNestedObject.a.b.c | Object | 1 | 100 |
| someNestedObject.a.b.c.d | Object | 1 | 100 |
| someNestedObject.a.b.c.d.e | Number | 1 | 100 |
| _id | ObjectId | 1 | 100.0 |
| name | String | 1 | 100.0 |
| someNestedObject | Object | 1 | 100.0 |
| someNestedObject.a | Object | 1 | 100.0 |
| someNestedObject.a.b | Object | 1 | 100.0 |
| someNestedObject.a.b.c | Object | 1 | 100.0 |
| someNestedObject.a.b.c.d | Object | 1 | 100.0 |
| someNestedObject.a.b.c.d.e | Number | 1 | 100.0 |
+----------------------------------------------------------------+
$ mongo test --eval "var collection = 'users', maxDepth = 3" variety.js
......@@ -106,11 +106,11 @@ The default will traverse all the way to the bottom of that structure:
+----------------------------------------------------------+
| key | types | occurrences | percents |
| -------------------- | -------- | ----------- | -------- |
| _id | ObjectId | 1 | 100 |
| name | String | 1 | 100 |
| someNestedObject | Object | 1 | 100 |
| someNestedObject.a | Object | 1 | 100 |
| someNestedObject.a.b | Object | 1 | 100 |
| _id | ObjectId | 1 | 100.0 |
| name | String | 1 | 100.0 |
| someNestedObject | Object | 1 | 100.0 |
| someNestedObject.a | Object | 1 | 100.0 |
| someNestedObject.a.b | Object | 1 | 100.0 |
+----------------------------------------------------------+
As you can see, Variety only traversed three levels deep.
......
......@@ -16,17 +16,17 @@ class SampleData {
* against this table, to check correct formatting.
*/
public static final String EXPECTED_DATA_ASCII_TABLE =
"+------------------------------------------------------------+\n" +
"| key | types | occurrences | percents |\n" +
"| ------------------ | ------------ | ----------- | -------- |\n" +
"| _id | ObjectId | 5 | 100 |\n" +
"| name | String | 5 | 100 |\n" +
"| bio | String | 3 | 60 |\n" +
"| birthday | String | 2 | 40 |\n" +
"| pets | Array,String | 2 | 40 |\n" +
"| someBinData | BinData-old | 1 | 20 |\n" +
"| someWeirdLegacyKey | String | 1 | 20 |\n" +
"+------------------------------------------------------------+";
"+------------------------------------------------------------+\n" +
"| key | types | occurrences | percents |\n" +
"| ------------------ | ------------ | ----------- | -------- |\n" +
"| _id | ObjectId | 5 | 100.0 |\n" +
"| name | String | 5 | 100.0 |\n" +
"| bio | String | 3 | 60.0 |\n" +
"| birthday | String | 2 | 40.0 |\n" +
"| pets | Array,String | 2 | 40.0 |\n" +
"| someBinData | BinData-old | 1 | 20.0 |\n" +
"| someWeirdLegacyKey | String | 1 | 20.0 |\n" +
"+------------------------------------------------------------+";
/**
* Java representation of sample collection provided in variety README:<p>
......
......@@ -274,15 +274,24 @@ if($outputFormat === 'json') {
printjson(varietyResults); // valid formatted json output, compressed variant is printjsononeline()
} else { // output nice ascii table with results
var table = [['key', 'types', 'occurrences', 'percents'], ['', '', '', '']]; // header + delimiter rows
// return the number of decimal places or 1, if the number is int (1.23=>2, 100=>1, 0.1415=>4)
var significantDigits = function(value) {
var res = value.toString().match(/^[0-9]+\.([0-9]+)$/);
return res !== null ? res[1].length : 1;
};
var maxDigits = Math.max.apply(null, varietyResults.map(function(value){return significantDigits(value.percentContaining);}));
varietyResults.forEach(function(key) {
table.push([key._id.key, key.value.types.toString(), key.totalOccurrences.toString(), key.percentContaining.toString()]);
table.push([key._id.key, key.value.types.toString(), key.totalOccurrences.toString(), key.percentContaining.toFixed(maxDigits).toString()]);
});
var colMaxWidth = function(arr, index) {
return Math.max.apply(null, arr.map(function(row){return row[index].toString().length;}));
};
var pad = function(width, string, symbol) { return width <= string.length ? string : pad(width, string + symbol, symbol); };
var pad = function(width, string, symbol) { return width <= string.length ? string : pad(width, isNaN(string) ? string + symbol : symbol + string, symbol); };
var output = '';
table.forEach(function(row, ri){
......
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