Skip to content

Instantly share code, notes, and snippets.

@pfurio
Last active June 23, 2017 13:53
Show Gist options
  • Save pfurio/91bcd3efec3a8253e9ad53121e5d5dcc to your computer and use it in GitHub Desktop.
Save pfurio/91bcd3efec3a8253e9ad53121e5d5dcc to your computer and use it in GitHub Desktop.
Remove old sessions (OpenCGA version < 1.2)
// Extracted from https://stackoverflow.com/questions/3066586/get-string-in-yyyymmdd-format-from-js-date-object
Date.prototype.opencgaDate = function() {
// We will get the current day minus 7 days
this.setDate(this.getDate() - 7);
var mm = this.getMonth() + 1; // getMonth() is zero-based
var dd = this.getDate();
return [this.getFullYear(),
(mm>9 ? '' : '0') + mm,
(dd>9 ? '' : '0') + dd,
"000000"
].join('');
};
// date will have OpenCGA format with current date - 7
date = parseInt(new Date().opencgaDate());
var users = db.user.find();
for (var i = 0; i < users.length(); i++) {
var user = users[i];
var sessions = user.sessions;
var newSessions = [];
for (var j = 0; j < sessions.length; j++) {
var session = sessions[j];
if (parseInt(session.date) > date) {
// We only keep the newest sessions
newSessions.push(session);
}
}
// We only update if there's any modification in the session array
if (newSessions.length != sessions.length) {
db.user.update({"_id": user._id}, {$set: {"sessions": newSessions}});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment