-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost-process.js
More file actions
85 lines (79 loc) · 2.1 KB
/
post-process.js
File metadata and controls
85 lines (79 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var _ = require("lodash");
var Promise = require('bluebird');
var db = require("./db");
Promise.promisifyAll([
require("mongojs/lib/collection"),
require("mongojs/lib/database"),
require("mongojs/lib/cursor")
]);
function reduce(key, values) {
var result = {};
values.forEach(function(value) {
var field;
for (field in value) {
if (value.hasOwnProperty(field) && value[field] && value[field].length != 0) {
if (result[field] instanceof Array) {
result[field] = result[field].concat(value[field]);
} else {
result[field] = value[field];
}
}
}
});
return result;
}
var toolPromise = db.users.findAsync().then(function(users) {
function mapTools() {
var values = {
name: this.name,
application: this.application,
users: this.users,
recommendations: []
}
id = this._id;
users.forEach(function(user) {
emit({_id: id, user: user.email}, values);
});
}
return db.tools.mapReduceAsync(mapTools, reduce, {
out: {reduce: "user_tools"},
scope: {users: users}}); //make sure map function can access users
});
function mapRecs() {
var values = {
name: null,
application: null,
users: null,
recommendations: [{
rank: this.rank,
algorithm_type: this.algorithm_type,
reason_value: this.reason_value
}]
}
if (this.rank) {
emit({_id: this.command_id, user: this.user_id}, values);
}
}
function simplifyIds(collection) {
//combine flattening with id simplification
return collection.findAsync().map(function(item) {
complexId = item._id
collection.remove({_id: complexId});
delete item._id;
if (item.value) {
item = item.value
}
item.tool_id = complexId._id;
item.user = complexId.user;
collection.insert(item);
}).all();
}
toolPromise.then(function() {
var p = db.recommendations.mapReduceAsync(mapRecs, reduce, {out: {reduce: "user_tools"}});
p.delay(1000).then(function(){
return simplifyIds(db.user_tools);
}).then(function(){
//console.log("Finished");
db.close();
});
});