Commit 757550aa by Tomas Dvorak

Results printed as a ascii table (and added json switch)

parent 4132a0fb
......@@ -23,11 +23,14 @@ public class Variety {
* Hardcoded database name in variety.js for analysis results
*/
public static final String VARIETY_RESULTS_DBNAME = "varietyResults";
public static final String FORMAT_JSON = "json";
public static final String FORMAT_ASCII = "ascii";
public static final String PARAM_QUERY = "query";
public static final String PARAM_SORT = "sort";
public static final String PARAM_MAXDEPTH = "maxDepth";
public static final String PARAM_LIMIT = "limit";
public static final String PARAM_OUTPUT_FORMAT = "outputFormat";
private final String inputDatabase;
......@@ -38,6 +41,7 @@ public class Variety {
private Integer maxDepth;
private String query;
private String sort;
private String outputFormat;
private boolean verbose = true;
......@@ -99,6 +103,11 @@ public class Variety {
return this;
}
public Variety withFormat(final String format) {
this.outputFormat = format;
return this;
}
/**
* 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
......@@ -154,6 +163,10 @@ public class Variety {
args.add(PARAM_SORT + " = " + sort);
}
if(outputFormat != null) {
args.add(PARAM_OUTPUT_FORMAT + " = '" + outputFormat + "'");
}
return args.toString();
}
......
......@@ -9,12 +9,10 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Variety outputs json-like results to stdout. Lets verify that.
*/
public class JsonOutputTest {
public class OutputFormatTest {
private Variety variety;
......@@ -32,7 +30,7 @@ public class JsonOutputTest {
@Test
public void verifyJsonEntries() throws Exception {
final VarietyAnalysis analysis = variety.runAnalysis();
final VarietyAnalysis analysis = variety.withFormat(Variety.FORMAT_JSON).runAnalysis();
// TODO: output itself is not valid JSON. It contains mongo shell output (can be removed with --quiet) and variety execution info.
// At the end of output, there are printed records from result collection, every record on new line.
......@@ -47,4 +45,28 @@ public class JsonOutputTest {
// there should be seven different json results in the stdout
Assert.assertEquals(7, objects.count());
}
@Test
public void verifyAsciiTableOutput() throws Exception {
final VarietyAnalysis analysis = variety.withFormat(Variety.FORMAT_ASCII).runAnalysis();
// filter only lines starting with character '|'
final String actual = Stream.of(analysis.getStdOut().split("\n"))
.filter(line -> line.startsWith("|"))
.collect(Collectors.joining("\n"));
final String expected =
"| 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 |";
Assert.assertEquals(expected, actual);
}
}
......@@ -79,6 +79,27 @@ public class ParametersParsingTest {
}
}
@Test
public void testDefaultOutputFormatParam() throws Exception {
final VarietyAnalysis analysis = variety.runAnalysis(); // format option not provided
final Map<String, String> params = getParamsMap(analysis.getStdOut());
Assert.assertEquals("ascii", params.get(Variety.PARAM_OUTPUT_FORMAT));
}
@Test
public void testAsciiOutputFormatParam() throws Exception {
final VarietyAnalysis analysis = variety.withFormat(Variety.FORMAT_ASCII).runAnalysis();
final Map<String, String> params = getParamsMap(analysis.getStdOut());
Assert.assertEquals("ascii", params.get(Variety.PARAM_OUTPUT_FORMAT));
}
@Test
public void testJsonOutputFormatParam() throws Exception {
final VarietyAnalysis analysis = variety.withFormat(Variety.FORMAT_JSON).runAnalysis();
final Map<String, String> params = getParamsMap(analysis.getStdOut());
Assert.assertEquals("json", params.get(Variety.PARAM_OUTPUT_FORMAT));
}
/**
* @param stdout Text from mongo shell, containing variety config output + json results
* @return Map of config values
......
......@@ -62,8 +62,10 @@ print('Using maxDepth of ' + maxDepth);
if (typeof sort === 'undefined') { var sort = {_id: -1}; }
print('Using sort of ' + tojson(sort));
if (typeof outputFormat === 'undefined') { var outputFormat = "ascii" }
print('Using outputFormat of ' + outputFormat);
varietyTypeOf = function(thing) {
if (typeof thing === 'undefined') { throw 'varietyTypeOf() requires an argument'; }
......@@ -160,7 +162,7 @@ db[collection].find(query).sort(sort).limit(limit).forEach(function(obj) {
interimResults[key]['types'][valueType] = true;
interimResults[key]['totalOccurrences']++;
}
}
}
});
......@@ -216,6 +218,24 @@ resultsDB[resultsCollectionName].find({}).forEach(function(key) {
});
var sortedKeys = resultsDB[resultsCollectionName].find({}).sort({totalOccurrences: -1});
sortedKeys.forEach(function(key) {
print(tojson(key, '', true));
});
if(outputFormat === 'json') {
sortedKeys.forEach(function(key) {
print(tojson(key, '', true));
});
} else { // output nice ascii table with results
var table = [["key", "types", "occurrences", "percents"], ["", "", "", ""]]; // header + delimiter rows
sortedKeys.forEach(function(key) {
table.push([key._id.key, key.value.types.toString(), key.totalOccurrences, key.percentContaining])
});
function colMaxWidth(arr, index) {
return Math.max.apply(null, arr.map(function(row){return row[index].toString().length}));
}
function pad(width, string, symbol) { return (width <= string.length) ? string : pad(width, string + symbol, symbol); }
table.forEach(function(row, ri){
print("| " + row.map(function(cell, i) {return pad(colMaxWidth(table, i), cell, ri == 1 ? "-" : " ")}).join(" | ") + " |");
});
}
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