Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
variety
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fact-gitdep
variety
Commits
ce693568
Commit
ce693568
authored
Apr 29, 2015
by
Tomas Dvorak
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
plugin infrastructure
parent
eef2a54e
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
135 additions
and
3 deletions
+135
-3
.jshintrc
.jshintrc
+4
-1
Variety.java
test/src/main/java/com/github/variety/Variety.java
+13
-0
PluginsTest.java
test/src/test/java/com/github/variety/test/PluginsTest.java
+59
-0
csvplugin.js
test/src/test/resources/csvplugin.js
+18
-0
variety.js
variety.js
+41
-2
No files found.
.jshintrc
View file @
ce693568
...
@@ -13,13 +13,16 @@
...
@@ -13,13 +13,16 @@
"BinData": false,
"BinData": false,
"DBQuery": false,
"DBQuery": false,
"printjson": false,
"printjson": false,
"load": false,
"module": false,
"query": true,
"query": true,
"limit": true,
"limit": true,
"maxDepth": true,
"maxDepth": true,
"sort": true,
"sort": true,
"outputFormat": true,
"outputFormat": true,
"persistResults": true
"persistResults": true,
"plugins": true
},
},
"latedef": true,
"latedef": true,
"singleGroups": true,
"singleGroups": true,
...
...
test/src/main/java/com/github/variety/Variety.java
View file @
ce693568
...
@@ -32,6 +32,8 @@ public class Variety {
...
@@ -32,6 +32,8 @@ 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"
;
public
static
final
String
PARAM_PERSIST_RESULTS
=
"persistResults"
;
public
static
final
String
PARAM_PERSIST_RESULTS
=
"persistResults"
;
public
static
final
String
PARAM_PLUGINS
=
"plugins"
;
private
final
String
inputDatabase
;
private
final
String
inputDatabase
;
private
final
String
inputCollection
;
private
final
String
inputCollection
;
...
@@ -46,6 +48,7 @@ public class Variety {
...
@@ -46,6 +48,7 @@ public class Variety {
private
String
outputFormat
;
private
String
outputFormat
;
private
boolean
quiet
;
private
boolean
quiet
;
private
boolean
persistResults
;
private
boolean
persistResults
;
private
String
[]
plugins
;
/**
/**
* Create variety wrapper with defined connection do analysed database and collection
* Create variety wrapper with defined connection do analysed database and collection
...
@@ -141,6 +144,11 @@ public class Variety {
...
@@ -141,6 +144,11 @@ public class Variety {
return
this
;
return
this
;
}
}
public
Variety
withPlugins
(
String
...
plugins
)
{
this
.
plugins
=
plugins
;
return
this
;
}
/**
/**
* Executes mongo shell with configured variety options and variety.js script in path.
* Executes mongo shell with configured variety options and variety.js script in path.
* @return Stdout of variety.js
* @return Stdout of variety.js
...
@@ -192,6 +200,11 @@ public class Variety {
...
@@ -192,6 +200,11 @@ public class Variety {
if
(
persistResults
)
{
if
(
persistResults
)
{
args
.
add
(
PARAM_PERSIST_RESULTS
+
" = "
+
persistResults
);
args
.
add
(
PARAM_PERSIST_RESULTS
+
" = "
+
persistResults
);
}
}
if
(
plugins
!=
null
&&
plugins
.
length
>
0
)
{
args
.
add
(
PARAM_PLUGINS
+
" = \""
+
String
.
join
(
","
,
plugins
)
+
"\""
);
}
return
args
.
toString
();
return
args
.
toString
();
}
}
...
...
test/src/test/java/com/github/variety/test/PluginsTest.java
0 → 100644
View file @
ce693568
package
com
.
github
.
variety
.
test
;
import
com.github.variety.Variety
;
import
com.github.variety.validator.ResultsValidator
;
import
org.junit.After
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Test
;
import
java.net.URL
;
public
class
PluginsTest
{
private
Variety
variety
;
public
static
final
String
EXPECTED_OUTPUT
=
"key|types|occurrences|percents\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"
;
@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
();
}
/**
* Validate correct results read from DB
*/
@Test
public
void
verifyFormatResults
()
throws
Exception
{
final
String
path
=
getPluginPath
(
"/csvplugin.js"
);
final
ResultsValidator
analysis
=
variety
.
withQuiet
(
true
).
withPlugins
(
path
).
runDatabaseAnalysis
();
Assert
.
assertEquals
(
EXPECTED_OUTPUT
,
analysis
.
getStdOut
());
}
@Test
public
void
verifyPluginParamParsing
()
throws
Exception
{
final
String
path
=
getPluginPath
(
"/csvplugin.js"
);
final
ResultsValidator
analysis
=
variety
.
withPlugins
(
path
+
"|delimiter=;"
).
runDatabaseAnalysis
();
Assert
.
assertTrue
(
analysis
.
getStdOut
().
contains
(
"Using plugins of "
+
path
));
}
private
String
getPluginPath
(
final
String
name
)
{
final
URL
resource
=
this
.
getClass
().
getResource
(
name
);
return
resource
.
getFile
();
}
}
test/src/test/resources/csvplugin.js
0 → 100644
View file @
ce693568
var
getCsv
=
function
(
varietyResults
)
{
var
delimiter
=
this
.
delimiter
||
'|'
;
var
headers
=
[
'key'
,
'types'
,
'occurrences'
,
'percents'
];
var
table
=
[
headers
.
join
(
delimiter
)];
var
rows
=
varietyResults
.
map
(
function
(
key
)
{
return
[
key
.
_id
.
key
,
key
.
value
.
types
,
key
.
totalOccurrences
,
key
.
percentContaining
].
join
(
delimiter
);
},
this
);
return
table
.
concat
(
rows
).
join
(
'
\
n'
);
};
var
setConfig
=
function
(
pluginConfig
)
{
this
.
delimiter
=
pluginConfig
.
delimiter
;
};
module
.
exports
=
{
init
:
setConfig
,
formatResults
:
getCsv
};
variety.js
View file @
ce693568
...
@@ -101,6 +101,39 @@ log('Using persistResults of ' + $persistResults);
...
@@ -101,6 +101,39 @@ log('Using persistResults of ' + $persistResults);
log
(
'Using collection of '
+
collection
);
log
(
'Using collection of '
+
collection
);
var
$plugins
=
[];
if
(
typeof
plugins
!==
'undefined'
)
{
var
parsePath
=
function
(
val
)
{
return
val
.
slice
(
-
3
)
!==
'.js'
?
val
+
'.js'
:
val
;};
var
parseConfig
=
function
(
val
)
{
var
config
=
{};
val
.
split
(
'&'
).
reduce
(
function
(
acc
,
val
)
{
var
parts
=
val
.
split
(
'='
);
acc
[
parts
[
0
]]
=
parts
[
1
];
return
acc
;
},
config
);
return
config
;
};
$plugins
=
plugins
.
split
(
','
)
.
map
(
function
(
path
){
return
path
.
trim
();})
.
map
(
function
(
definition
){
var
path
=
parsePath
(
definition
.
split
(
'|'
)[
0
]);
var
config
=
parseConfig
(
definition
.
split
(
'|'
)[
1
]
||
''
);
this
.
module
=
this
.
module
||
{};
load
(
path
);
var
plugin
=
this
.
module
.
exports
;
plugin
.
path
=
path
;
if
(
typeof
plugin
.
init
===
'function'
)
{
plugin
.
init
(
config
);
}
return
plugin
;
},
this
);
log
(
'Using plugins of '
+
$plugins
.
map
(
function
(
plugin
){
return
plugin
.
path
;}));
}
var
varietyTypeOf
=
function
(
thing
)
{
var
varietyTypeOf
=
function
(
thing
)
{
if
(
typeof
thing
===
'undefined'
)
{
throw
'varietyTypeOf() requires an argument'
;
}
if
(
typeof
thing
===
'undefined'
)
{
throw
'varietyTypeOf() requires an argument'
;
}
...
@@ -270,7 +303,13 @@ if($persistResults) {
...
@@ -270,7 +303,13 @@ if($persistResults) {
resultsDB
[
resultsCollectionName
].
insert
(
varietyResults
);
resultsDB
[
resultsCollectionName
].
insert
(
varietyResults
);
}
}
if
(
$outputFormat
===
'json'
)
{
var
formatResultPlugins
=
$plugins
.
filter
(
function
(
plugin
){
return
typeof
plugin
.
formatResults
===
'function'
;});
if
(
formatResultPlugins
.
length
>
0
)
{
formatResultPlugins
.
forEach
(
function
(
plugin
){
print
(
plugin
.
formatResults
(
varietyResults
));
});
}
else
if
(
$outputFormat
===
'json'
)
{
printjson
(
varietyResults
);
// valid formatted json output, compressed variant is printjsononeline()
printjson
(
varietyResults
);
// valid formatted json output, compressed variant is printjsononeline()
}
else
{
// output nice ascii table with results
}
else
{
// output nice ascii table with results
var
table
=
[[
'key'
,
'types'
,
'occurrences'
,
'percents'
],
[
''
,
''
,
''
,
''
]];
// header + delimiter rows
var
table
=
[[
'key'
,
'types'
,
'occurrences'
,
'percents'
],
[
''
,
''
,
''
,
''
]];
// header + delimiter rows
...
@@ -302,4 +341,4 @@ if($outputFormat === 'json') {
...
@@ -302,4 +341,4 @@ if($outputFormat === 'json') {
print
(
border
+
'
\
n'
+
output
+
border
);
print
(
border
+
'
\
n'
+
output
+
border
);
}
}
}());
// end strict mode
}
.
bind
(
this
)
());
// end strict mode
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment