-
Notifications
You must be signed in to change notification settings - Fork 6
/
activity.js
282 lines (243 loc) · 11.6 KB
/
activity.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/***************************************************************************/
/* Augeo.io is a web application that uses Natural Language Processing to */
/* classify a user's internet activity into different 'skills'. */
/* Copyright (C) 2016 Brian Redd */
/* */
/* This program is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/***************************************************************************/
/***************************************************************************/
/* Description: Logic for ACTIVITY database collection */
/***************************************************************************/
// Required libraries
var Mongoose = require('mongoose');
// Required local modules
var AugeoDB = require('../../database');
var Logger = require('../../../module/logger');
// Constants
var COLLECTION = 'activity-collection';
// Global variables
var log = new Logger();
// Schema declaration
var ACTIVITY = Mongoose.Schema({
classification: String,
classificationGlyphicon: String,
data: {type: Mongoose.Schema.Types.ObjectId, 'refPath':'kind'},
experience: Number,
kind: String,
timestamp: Date,
user: {type: Mongoose.Schema.Types.ObjectId, ref:'AUGEO_USER'}
});
/***************************************************************************/
/* Static Methods: Accessible at Model level */
/***************************************************************************/
ACTIVITY.statics.addActivities = function(activities, logData, callback) {
// Add activities if they don't exist and update when they do exist
upsertActivities(this, activities, logData, callback);
};
ACTIVITY.statics.addActivity = function(activity, logData, callback) {
// Add activity if it doesn't exist and update when it does exist
upsertActivity(this, activity, logData, callback);
};
ACTIVITY.statics.getActivities = function(dataId, logData, callback) {
this.find({data:dataId}, function(error, activities) {
if(error) {
log.functionError(COLLECTION, 'getActivities', logData.parentProcess, logData.username, 'Failed to get activities with dataId: ' + dataId
+ '. Error: ' + error);
callback();
} else {
log.functionCall(COLLECTION, 'getActivities', logData.parentProcess, logData.username, {"dataId":dataId});
callback(activities);
}
});
};
ACTIVITY.statics.getActivity = function(userId, dataId, logData, callback) {
this.findOne({$and:[{user:userId}, {data:dataId}]}, function(error, activity) {
if(error) {
log.functionError(COLLECTION, 'getActivity', logData.parentProcess, logData.username, 'Failed to get activity for user with ID: ' + userId +
'. Error: ' + error);
callback();
} else {
log.functionCall(COLLECTION, 'getActivity', logData.parentProcess, logData.username, {'userId': userId, 'dataId': dataId});
callback(activity);
}
});
};
ACTIVITY.statics.getSkillActivity = function(userId, skill, limit, maxTimestamp, logData, callback) {
var model = this;
if(!maxTimestamp) {
maxTimestamp = new Date(8640000000000000);
}
var query = {
$and:[
{
user: userId,
timestamp: {$lt: maxTimestamp}
}
]
};
if(skill && skill != 'Augeo') {
query.classification = skill;
}
// First grab the activity IDs
model.find(query)
.select('_id')
.sort({'timestamp': -1})
.limit(limit)
//.populate('data') // Can't use populate & sort due to Mongoose issue: https://github.com/Automattic/mongoose/issues/2202
.exec(function(error, activityIds) {
if(error) {
log.functionError(COLLECTION, 'getSkillActivity', logData.parentProcess, logData.username,
'Failed to retrieve ' + userId + ' activity IDs for skill:' + skill + '. Error: ' + error);
} else {
log.functionCall(COLLECTION, 'getSkillActivity', logData.parentProcess, logData.username,
{'userId':userId, 'skill':skill,'maxTimestamp':maxTimestamp}, 'Retrieved activity IDs');
// Second, get all populated objects
// Can't use MongoDB's $in function because of issue with mongoose populate
var activities = new Array();
if(activityIds && activityIds.length > 0) {
// Asynchronous method calls in loop - Using Recursion
(function myClojure(i) {
model.findOne({'_id':activityIds[i]})
.populate('data')
.exec(function(error, activity) {
if(error) {
log.functionError(COLLECTION, 'getSkillActivity', logData.parentProcess, logData.username,
'Failed to retrieve activity with id: ' + activityIds[i]);
callback();
} else {
activities.push(activity);
i++;
if (i < activityIds.length) {
myClojure(i);
} else {
log.functionCall(COLLECTION, 'getSkillActivity', logData.parentProcess, logData.username, {'activityId':activityIds[i]},
'Retrieved populated activities');
// Sort activities by timestamp in descending order
var sortedActivities = activities.sort(function (a, b) {
return new Date(b.timestamp) - new Date(a.timestamp);
});
callback(sortedActivities);
}
}
});
})(0); // Pass i as 0 and myArray to myClojure
} else {
callback(activities);
}
}
});
};
ACTIVITY.statics.getUserActivities = function(userId, logData, callback) {
this.find({user:userId}, function(error, activities) {
if(error) {
log.functionError(COLLECTION, 'getUserActivities', logData.parentProcess, logData.username, {'userId':userId});
callback();
} else {
log.functionCall(COLLECTION, 'getUserActivities', logData.parentProcess, logData.username, {'userId':userId});
callback(activities);
}
});
};
ACTIVITY.statics.increaseExperience = function(userId, dataId, experience, logData, callback) {
this.findOneAndUpdate({$and:[{user:userId}, {data:dataId}]}, {$inc:{experience:experience}}, {'new':true}, function(error, activity) {
if(error) {
log.functionError(COLLECTION, 'increaseExperience', logData.parentProcess, logData.username, 'Failed to increase activity with data ID:' +
dataId + ' . Error: ' + error);
} else {
log.functionCall(COLLECTION, 'increaseExperience', logData.parentProcess, logData.username, {'userId':userId, 'dataId':dataId, 'experience':experience});
callback(activity);
}
});
};
ACTIVITY.statics.removeActivities = function(userId, logData, callback) {
this.remove({user:userId}, function(error) {
if(error) {
log.functionError(COLLECTION, 'removeActivities', logData.parentProcess, logData.username, 'Failed to remove activities with userId: ' + userId
+ '. Error: ' + error);
callback();
} else {
log.functionCall(COLLECTION, 'removeActivities', logData.parentProcess, logData.username, {'userId': userId});
callback();
}
});
};
ACTIVITY.statics.removeActivity = function(userId, dataId, logData, callback) {
this.remove({$and:[{user:userId},{data:dataId}]}, function(error) {
if(error) {
log.functionError(COLLECTION, 'removeActivity', logData.parentProcess, logData.username, 'Failed to remove activity with userId: ' + userId
+ '. Error: ' + error);
callback();
} else {
log.functionCall(COLLECTION, 'removeActivity', logData.parentProcess, logData.username, {'userId': userId, dataId:dataId});
callback();
}
});
};
/***************************************************************************/
/* Private Methods */
/***************************************************************************/
var upsertActivity = function(activityDocument, activity, logData, callback) {
activityDocument.findOne({$and:[{user:activity.user},{data:activity.data}]}, function(error, foundActivity) {
if(error) {
log.functionError(COLLECTION, 'upsertActivity (private)', logData.parentProcess, logData.username,
'Failed to find activity with data ID: ' + (activity) ? activity.data : 'invalid');
callback();
} else {
var duplicateExperience = 0;
if (foundActivity && typeof foundActivity.experience == 'number') {
// Grab current experience so it's not duplicated in user's experience
duplicateExperience = foundActivity.experience;
}
activityDocument.findOneAndUpdate({$and: [{user: activity.user}, {data: activity.data}]}, activity, { upsert: true, 'new': true},
function (error, updatedActivity) {
if (error) {
log.functionError(COLLECTION, 'upsertActivity (private)', logData.parentProcess, logData.username,
'Failed to upsert activity with data ID: ' + (activity) ? activity.data : 'invalid' + '. Error: ' + error);
callback();
} else {
log.functionCall(COLLECTION, 'upsertActivity (private)', logData.parentProcess, logData.username, {
'activityDocument': (activityDocument) ? 'defined' : 'invalid',
'activity.data': (activity) ? activity.data : 'invalid'
});
// Add attribute for duplicate experience
updatedActivity.duplicateExperience = duplicateExperience;
callback(updatedActivity);
}
});
}
});
};
var upsertActivities = function(activityDocument, activities, logData, callback) {
var updatedActivities = new Array();
if(activities.length > 0) {
// Asynchronous method calls in loop - Using Recursion
(function myClojure(i) {
upsertActivity(activityDocument, activities[i], logData, function (updatedActivity) {
if(updatedActivity) {
updatedActivities.push(updatedActivity);
}
i++;
if (i < activities.length) {
myClojure(i);
} else {
callback(updatedActivities);
}
});
})(0); // Pass i as 0 and myArray to myClojure
} else {
callback();
}
};
// Declare Model
module.exports = AugeoDB.model('ACTIVITY', ACTIVITY);