Commit 6faf9a1b by Tomas Dvorak

Fix documents percentages, if the limit is higher than real count of the documents #76

parent 384ecdf0
...@@ -35,4 +35,20 @@ public class LimitResultsAnalysisTest { ...@@ -35,4 +35,20 @@ public class LimitResultsAnalysisTest {
analysis.validate("name", 1, 100, "String"); analysis.validate("name", 1, 100, "String");
analysis.validate("someBinData", 1, 100, "BinData-old"); analysis.validate("someBinData", 1, 100, "BinData-old");
} }
@Test
public void verifyLimitOverMaxResults() throws Exception {
// limit is set to higher number, that the actual number of documents in collection
// analysis should compute percentages based on the real number of documents, not on the
// number provided in the limit var.
final ResultsValidator analysis = variety.withLimit(10).runDatabaseAnalysis();
analysis.validate("_id", 5, 100, "ObjectId");
analysis.validate("name", 5, 100, "String");
analysis.validate("bio", 3, 60, "String");
analysis.validate("pets", 2, 40, "String", "Array");
analysis.validate("someBinData", 1, 20, "BinData-old");
analysis.validate("someWeirdLegacyKey", 1, 20, "String");
}
} }
...@@ -205,7 +205,7 @@ var mergeDocument = function(docResult, interimResults) { ...@@ -205,7 +205,7 @@ var mergeDocument = function(docResult, interimResults) {
} }
}; };
var convertResults = function(interimResults) { var convertResults = function(interimResults, documentsCount) {
var getKeys = function(obj) { var getKeys = function(obj) {
var keys = []; var keys = [];
for(var key in obj) { for(var key in obj) {
...@@ -221,7 +221,7 @@ var convertResults = function(interimResults) { ...@@ -221,7 +221,7 @@ var convertResults = function(interimResults) {
'_id': {'key':key}, '_id': {'key':key},
'value': {'types':getKeys(entry.types)}, 'value': {'types':getKeys(entry.types)},
'totalOccurrences': entry['totalOccurrences'], 'totalOccurrences': entry['totalOccurrences'],
'percentContaining': entry['totalOccurrences'] * 100 / $limit 'percentContaining': entry['totalOccurrences'] * 100 / documentsCount
}); });
} }
return varietyResults; return varietyResults;
...@@ -255,13 +255,9 @@ DBQuery.prototype.reduce = function(callback, initialValue) { ...@@ -255,13 +255,9 @@ DBQuery.prototype.reduce = function(callback, initialValue) {
return result; return result;
}; };
var interimResults = db[collection] var cursor = db[collection].find($query).sort($sort).limit($limit);
.find($query) var interimResults = cursor.reduce(reduceDocuments, {});
.sort($sort) var varietyResults = convertResults(interimResults, cursor.size())
.limit($limit)
.reduce(reduceDocuments, {});
var varietyResults = convertResults(interimResults)
.filter(filter) .filter(filter)
.sort(comparator); .sort(comparator);
......
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