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
3efbfcda
Commit
3efbfcda
authored
Jul 29, 2012
by
Wes Freeman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixing occurrence bug. Might be able to simplify later.
parent
6e594a73
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
16 deletions
+45
-16
variety.js
variety.js
+45
-16
No files found.
variety.js
View file @
3efbfcda
...
...
@@ -5,12 +5,12 @@ outliers to that schema. Particularly useful when you inherit a codebase with
data dump and want to quickly learn how the data's structured. Also useful for
finding rare keys.
Please see https://github.com/
JamesCropcho
/variety for details.
Please see https://github.com/
variety
/variety for details.
Released by Maypop Inc, © 2012, under the MIT License. */
print
(
"Variety: A MongoDB Schema Analyzer"
)
print
(
"Version 1.2
, released 28
July 2012"
)
print
(
"Version 1.2
.1, released 29
July 2012"
)
var
dbs
=
new
Array
();
var
emptyDbs
=
new
Array
();
...
...
@@ -100,9 +100,6 @@ varietyTypeOf = function(thing) {
}
}
// store results here (no map reduce limit!)
var
varietyResults
=
{};
var
addTypeToArray
=
function
(
arr
,
value
)
{
var
t
=
varietyTypeOf
(
value
);
var
found
=
false
;
...
...
@@ -117,10 +114,10 @@ var addTypeToArray = function(arr, value) {
}
}
var
add
VarietyResult
=
function
(
key
,
value
)
{
cur
=
varietyResults
[
key
];
var
add
RecordResult
=
function
(
key
,
value
,
result
)
{
cur
=
result
[
key
];
if
(
cur
==
null
)
{
varietyResults
[
key
]
=
{
"_id"
:{
"key"
:
key
},
"value"
:
{
"type"
:
varietyTypeOf
(
value
)},
totalOccurrences
:
1
};
result
[
key
]
=
{
"_id"
:{
"key"
:
key
},
"value"
:
{
"type"
:
varietyTypeOf
(
value
)},
totalOccurrences
:
1
};
}
else
{
var
type
=
varietyTypeOf
(
value
);
if
(
cur
.
value
.
type
!=
type
)
{
...
...
@@ -130,19 +127,49 @@ var addVarietyResult = function(key, value) {
}
else
if
(
!
cur
.
value
.
type
)
{
addTypeToArray
(
cur
.
value
.
types
,
type
);
}
cur
.
totalOccurrences
++
;
varietyResults
[
key
]
=
cur
;
result
[
key
]
=
cur
;
}
}
var
mapRecursive
=
function
(
parentKey
,
obj
,
level
)
{
var
mapRecursive
=
function
(
parentKey
,
obj
,
level
,
result
)
{
for
(
var
key
in
obj
)
{
if
(
obj
.
hasOwnProperty
(
key
))
{
var
value
=
obj
[
key
];
key
=
(
parentKey
+
"."
+
key
).
replace
(
/
\.\d
+/g
,
'.XX'
);
add
VarietyResult
(
key
,
value
);
add
RecordResult
(
key
,
value
,
result
);
if
(
level
<
maxDepth
-
1
&&
varietyCanHaveChildren
(
value
))
{
mapRecursive
(
key
,
value
,
level
+
1
);
mapRecursive
(
key
,
value
,
level
+
1
,
result
);
}
}
}
}
// store results here (no map reduce limit!)
var
varietyResults
=
{};
var
addVarietyResults
=
function
(
result
)
{
for
(
var
key
in
result
)
{
if
(
result
.
hasOwnProperty
(
key
))
{
cur
=
varietyResults
[
key
];
var
value
=
result
[
key
];
if
(
cur
==
null
)
{
varietyResults
[
key
]
=
value
;
}
else
{
if
(
value
.
type
&&
value
.
type
==
cur
.
value
.
type
)
{
}
else
{
for
(
type
in
value
.
types
)
{
if
(
cur
.
value
.
type
!=
type
)
{
cur
.
value
.
types
=
[
cur
.
value
.
type
];
delete
cur
.
value
[
"type"
];
addTypeToArray
(
cur
.
value
.
types
,
type
);
}
else
if
(
!
cur
.
value
.
type
)
{
addTypeToArray
(
cur
.
value
.
types
,
type
);
}
}
}
cur
.
totalOccurrences
++
;
varietyResults
[
key
]
=
cur
;
}
}
}
...
...
@@ -150,15 +177,17 @@ var mapRecursive = function(parentKey, obj, level){
// main cursor
db
[
collection
].
find
().
sort
({
_id
:
-
1
}).
limit
(
limit
).
forEach
(
function
(
obj
)
{
var
recordResult
=
{};
for
(
var
key
in
obj
)
{
if
(
obj
.
hasOwnProperty
(
key
))
{
var
value
=
obj
[
key
];
add
VarietyResult
(
key
,
value
);
add
RecordResult
(
key
,
value
,
recordResult
);
if
(
maxDepth
>
1
&&
varietyCanHaveChildren
(
value
))
{
mapRecursive
(
key
,
value
,
1
);
mapRecursive
(
key
,
value
,
1
,
recordResult
);
}
}
}
addVarietyResults
(
recordResult
);
});
var
resultsDB
=
db
.
getMongo
().
getDB
(
"varietyResults"
);
...
...
@@ -196,7 +225,7 @@ resultsDB[resultsCollectionName].find({}).forEach(function(key) {
existsQuery
[
keyName
]
=
{
$exists
:
true
};
key
.
totalOccurrences
=
db
[
collection
].
count
(
existsQuery
);
}
key
.
percentContaining
=
(
key
.
totalOccurrences
/
numDocuments
)
*
100
;
key
.
percentContaining
=
(
key
.
totalOccurrences
/
numDocuments
)
*
100
.0
;
resultsDB
[
resultsCollectionName
].
save
(
key
);
});
...
...
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