Commit 5e9035c1 by Tomas Dvorak

Quiet option supported by Variety and Java wrapper

parent cdc1a205
...@@ -11,7 +11,9 @@ import java.io.InputStreamReader; ...@@ -11,7 +11,9 @@ import java.io.InputStreamReader;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner; import java.util.StringJoiner;
/** /**
...@@ -32,7 +34,6 @@ public class Variety { ...@@ -32,7 +34,6 @@ public class Variety {
public static final String PARAM_LIMIT = "limit"; public static final String PARAM_LIMIT = "limit";
public static final String PARAM_OUTPUT_FORMAT = "outputFormat"; public static final String PARAM_OUTPUT_FORMAT = "outputFormat";
private final String inputDatabase; private final String inputDatabase;
private final String inputCollection; private final String inputCollection;
private final MongoClient mongoClient; private final MongoClient mongoClient;
...@@ -42,8 +43,9 @@ public class Variety { ...@@ -42,8 +43,9 @@ public class Variety {
private String query; private String query;
private String sort; private String sort;
private String outputFormat; private String outputFormat;
private boolean quiet;
private boolean verbose = true; private boolean isStdoutForwarded = true;
/** /**
* Create variety wrapper with defined connection do analysed database and collection * Create variety wrapper with defined connection do analysed database and collection
...@@ -109,13 +111,22 @@ public class Variety { ...@@ -109,13 +111,22 @@ public class Variety {
} }
/** /**
* Wrapper for command line option '--quiet', that is passed to mongo shell. Variety is able to read this option
* and mute its logs with metadata.
*/
public Variety withQuiet(boolean quiet) {
this.quiet = quiet;
return this;
}
/**
* Enable analysis output stdout of script to stdout of java process. * Enable analysis output stdout of script to stdout of java process.
* Deprecated because it should only be used for debugging of test, not real/production tests itself. If you * Deprecated because it should only be used for debugging of test, not real/production tests itself. If you
* need to read stdout of variety, it can be accessed through {@link VarietyAnalysis#getStdOut()} * need to read stdout of variety, it can be accessed through {@link VarietyAnalysis#getStdOut()}
*/ */
@Deprecated() @Deprecated()
public Variety verbose() { public Variety withStdoutForwarded(final boolean isStdoutForwarded) {
this.verbose = true; this.isStdoutForwarded = isStdoutForwarded;
return this; return this;
} }
...@@ -126,15 +137,26 @@ public class Variety { ...@@ -126,15 +137,26 @@ public class Variety {
* @throws InterruptedException * @throws InterruptedException
*/ */
public VarietyAnalysis runAnalysis() throws IOException, InterruptedException { public VarietyAnalysis runAnalysis() throws IOException, InterruptedException {
final String[] commands = new String[]{"mongo", this.inputDatabase, "--eval", buildParams(), getVarietyPath()};
final Process child = Runtime.getRuntime().exec(commands); List<String> commands = new ArrayList<>();
commands.add("mongo");
commands.add(this.inputDatabase);
if(quiet) {
commands.add("--quiet");
}
commands.add("--eval");
commands.add(buildParams());
commands.add(getVarietyPath());
final String[] cmdarray = commands.toArray(new String[commands.size()]);
final Process child = Runtime.getRuntime().exec(cmdarray);
final int returnCode = child.waitFor(); final int returnCode = child.waitFor();
final String stdOut = readStream(child.getInputStream()); final String stdOut = readStream(child.getInputStream());
if(returnCode != 0) { if(returnCode != 0) {
throw new RuntimeException("Failed to execute variety.js with arguments: " + Arrays.toString(commands) + ".\n" + stdOut); throw new RuntimeException("Failed to execute variety.js with arguments: " + Arrays.toString(cmdarray) + ".\n" + stdOut);
} else if(verbose) { } else if(isStdoutForwarded) {
System.out.println(stdOut); System.out.println(stdOut);
} }
return new VarietyAnalysis(mongoClient, inputCollection, stdOut); return new VarietyAnalysis(mongoClient, inputCollection, stdOut);
......
...@@ -55,20 +55,7 @@ public class OutputFormatTest { ...@@ -55,20 +55,7 @@ public class OutputFormatTest {
.filter(line -> line.startsWith("|") || line.startsWith("+")) .filter(line -> line.startsWith("|") || line.startsWith("+"))
.collect(Collectors.joining("\n")); .collect(Collectors.joining("\n"));
final String expected = Assert.assertEquals(SampleData.EXPECTED_DATA_ASCII_TABLE, actual);
"+------------------------------------------------------------+\n" +
"| key | types | occurrences | percents |\n" +
"| ------------------ | ------------ | ----------- | -------- |\n" +
"| _id | ObjectId | 5 | 100 |\n" +
"| name | String | 5 | 100 |\n" +
"| bio | String | 3 | 60 |\n" +
"| pets | String,Array | 2 | 40 |\n" +
"| birthday | String | 2 | 40 |\n" +
"| someBinData | BinData-old | 1 | 20 |\n" +
"| someWeirdLegacyKey | String | 1 | 20 |\n" +
"+------------------------------------------------------------+";
Assert.assertEquals(expected, actual);
} }
} }
package com.github.variety.test;
import com.github.variety.Variety;
import com.github.variety.VarietyAnalysis;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* Variety can read '--quiet' option passed to mongo shell and mute all debug/metadata logs. In this case only
* results should be printed. Together with output format set to json should be possible to simply forward output
* from variety to another tool processing valid json.
*/
public class QuietOptionTest {
private Variety variety;
@Before
public void setUp() throws Exception {
this.variety = new Variety("test", "users");
variety.getSourceCollection().insert(SampleData.getDocuments());
}
@After
public void tearDown() throws Exception {
variety.getVarietyResultsDatabase().dropDatabase();
variety.getSourceCollection().drop();
}
/**
* verify, that output contains only results table and nothing more
*/
@Test
public void testQuietLogs() throws Exception {
final VarietyAnalysis varietyAnalysis = variety.withQuiet(true).runAnalysis();
Assert.assertEquals(SampleData.EXPECTED_DATA_ASCII_TABLE, varietyAnalysis.getStdOut());
}
}
...@@ -12,6 +12,23 @@ import java.util.List; ...@@ -12,6 +12,23 @@ import java.util.List;
class SampleData { class SampleData {
/** /**
* Ascii table representation of sample data results. It should be possible to verify actual output of Variety
* 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" +
"| pets | String,Array | 2 | 40 |\n" +
"| birthday | String | 2 | 40 |\n" +
"| someBinData | BinData-old | 1 | 20 |\n" +
"| someWeirdLegacyKey | String | 1 | 20 |\n" +
"+------------------------------------------------------------+";
/**
* Java representation of sample collection provided in variety README:<p> * Java representation of sample collection provided in variety README:<p>
* *
* {name: "Tom", bio: "A nice guy.", pets: ["monkey", "fish"], someWeirdLegacyKey: "I like Ike!"}<p> * {name: "Tom", bio: "A nice guy.", pets: ["monkey", "fish"], someWeirdLegacyKey: "I like Ike!"}<p>
......
...@@ -17,7 +17,7 @@ import java.util.regex.Pattern; ...@@ -17,7 +17,7 @@ import java.util.regex.Pattern;
*/ */
public class VersionInfoTest { public class VersionInfoTest {
public static final Pattern VARIETYJS_PATTERN = Pattern.compile("print\\('(.+), released (.+)'\\).*"); public static final Pattern VARIETYJS_PATTERN = Pattern.compile("\\w+\\('(.+), released (.+)'\\).*");
public static final Pattern CHANGELOG_PATTERN = Pattern.compile("\\((.+)\\)(.+):(.*)"); public static final Pattern CHANGELOG_PATTERN = Pattern.compile("\\((.+)\\)(.+):(.*)");
private List<String> varietyLines; private List<String> varietyLines;
......
...@@ -8,8 +8,15 @@ finding rare keys. ...@@ -8,8 +8,15 @@ finding rare keys.
Please see https://github.com/variety/variety for details. Please see https://github.com/variety/variety for details.
Released by Maypop Inc, © 2012-2014, under the MIT License. */ Released by Maypop Inc, © 2012-2014, under the MIT License. */
print('Variety: A MongoDB Schema Analyzer');
print('Version 1.4.1, released 14 Oct 2014'); var log = function(message) {
if(!__quiet) { // mongo shell param, coming from https://github.com/mongodb/mongo/blob/5fc306543cd3ba2637e5cb0662cc375f36868b28/src/mongo/shell/dbshell.cpp#L624
print(message);
}
};
log('Variety: A MongoDB Schema Analyzer');
log('Version 1.4.1, released 14 Oct 2014');
var dbs = []; var dbs = [];
var emptyDbs = []; var emptyDbs = [];
...@@ -51,19 +58,19 @@ if (db[collection].count() === 0) { ...@@ -51,19 +58,19 @@ if (db[collection].count() === 0) {
} }
if (typeof query === 'undefined') { var query = {}; } if (typeof query === 'undefined') { var query = {}; }
print('Using query of ' + tojson(query)); log('Using query of ' + tojson(query));
if (typeof limit === 'undefined') { var limit = db[collection].find(query).count(); } if (typeof limit === 'undefined') { var limit = db[collection].find(query).count(); }
print('Using limit of ' + limit); log('Using limit of ' + limit);
if (typeof maxDepth === 'undefined') { var maxDepth = 99; } if (typeof maxDepth === 'undefined') { var maxDepth = 99; }
print('Using maxDepth of ' + maxDepth); log('Using maxDepth of ' + maxDepth);
if (typeof sort === 'undefined') { var sort = {_id: -1}; } if (typeof sort === 'undefined') { var sort = {_id: -1}; }
print('Using sort of ' + tojson(sort)); log('Using sort of ' + tojson(sort));
if (typeof outputFormat === 'undefined') { var outputFormat = "ascii"; } if (typeof outputFormat === 'undefined') { var outputFormat = "ascii"; }
print('Using outputFormat of ' + outputFormat); log('Using outputFormat of ' + outputFormat);
varietyTypeOf = function(thing) { varietyTypeOf = function(thing) {
...@@ -182,15 +189,15 @@ var resultsDB = db.getMongo().getDB('varietyResults'); ...@@ -182,15 +189,15 @@ var resultsDB = db.getMongo().getDB('varietyResults');
var resultsCollectionName = collection + 'Keys'; var resultsCollectionName = collection + 'Keys';
// replace results collection // replace results collection
print('creating results collection: '+resultsCollectionName); log('creating results collection: '+resultsCollectionName);
resultsDB[resultsCollectionName].drop(); resultsDB[resultsCollectionName].drop();
for(var result in varietyResults) { for(var result in varietyResults) {
resultsDB[resultsCollectionName].insert(varietyResults[result]); resultsDB[resultsCollectionName].insert(varietyResults[result]);
} }
var numDocuments = db[collection].count(); var numDocuments = db[collection].count();
print('removing leaf arrays in results collection, and getting percentages'); log('removing leaf arrays in results collection, and getting percentages');
resultsDB[resultsCollectionName].find({}).forEach(function(key) { resultsDB[resultsCollectionName].find({}).forEach(function(key) {
var keyName = key._id.key; var keyName = key._id.key;
......
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