Commit be866f44 authored by Curtis Adam's avatar Curtis Adam

Use batch-insert to speedup creating Leitner and Wozniak cards

parent 162ee246
......@@ -69,3 +69,4 @@ jquery@1.11.10
east5th:package-scan
wtfzn:dompurify
ros:publish-counts
mikowals:batch-insert
......@@ -99,6 +99,7 @@ meteor@1.8.6
meteor-base@1.3.0
meteorhacks:ssr@2.2.0
meteorspark:util@0.2.0
mikowals:batch-insert@1.1.14
minifier-css@1.3.1
minifier-js@2.3.4
minimongo@1.4.4
......
......@@ -94,62 +94,6 @@ if (Meteor.isServer) {
Leitner.remove({cardset_id: cardset_id});
Meteor.call("updateLearnerCount", cardset_id);
},
addLeitner: function (cardset_id, card_id, user_id) {
check(cardset_id, String);
check(card_id, String);
check(user_id, String);
// Make sure the user is logged in
if (!Meteor.isServer) {
throw new Meteor.Error("not-authorized");
} else {
Leitner.upsert({
cardset_id: cardset_id,
card_id: card_id,
user_id: user_id
}, {
$set: {
cardset_id: cardset_id,
card_id: card_id,
user_id: user_id
},
$setOnInsert: {
box: 1,
active: false,
nextDate: new Date(),
currentDate: new Date(),
skipped: 0
}
});
}
},
addWozniak: function (cardset_id, card_id, user_id) {
check(cardset_id, String);
check(card_id, String);
check(user_id, String);
// Make sure the user is logged in
if (!Meteor.isServer) {
throw new Meteor.Error("not-authorized");
} else {
Wozniak.upsert({
cardset_id: cardset_id,
card_id: card_id,
user_id: user_id
}, {
$set: {
cardset_id: cardset_id,
card_id: card_id,
user_id: user_id
},
$setOnInsert: {
ef: 2.5,
interval: 0,
reps: 0,
nextDate: new Date(),
skipped: 0
}
});
}
},
/** Function marks an active leitner card as learned
* @param {string} cardset_id - The cardset id from the card
* @param {string} card_id - The id from the card
......
......@@ -67,14 +67,38 @@ function addLeitnerCards(cardset, user_id) {
if (cardset.shuffled) {
cardsetFilter = cardset.cardGroups;
}
let existingItems = Leitner.find({
cardset_id: cardset._id,
user_id: user_id
}, {fields: {card_id: 1}}).fetch();
let excludedCards = [];
existingItems.forEach(function (existingItem) {
excludedCards.push(existingItem.card_id);
});
let newItems = [];
let nextDate = new Date();
cards = Cards.find({
_id: {$nin: excludedCards},
cardset_id: {$in: cardsetFilter},
cardType: {$in: CardType.getCardTypesWithLearningModes()}
});
}, {fields: {_id: 1}}).fetch();
cards.forEach(function (card) {
Meteor.call("addLeitner", cardset._id, card._id, user_id, false);
newItems.push({
card_id: card._id,
cardset_id: cardset._id,
user_id: user_id,
box: 1,
active: false,
nextDate: nextDate,
currentDate: nextDate,
skipped: 0
});
});
if (newItems.length > 0) {
Leitner.batchInsert(newItems);
}
Meteor.call("updateLearnerCount", cardset._id);
return true;
}
......@@ -339,7 +363,8 @@ Meteor.methods({
addWozniakCards: function (cardset_id) {
check(cardset_id, String);
let cardset = Cardsets.findOne({_id: cardset_id});
if (!Meteor.userId() || Roles.userIsInRole(this.userId, 'blocked') || cardset.learningActive) {
let user_id = this.userId;
if (!Meteor.userId() || Roles.userIsInRole(user_id, 'blocked') || cardset.learningActive) {
throw new Meteor.Error("not-authorized");
} else {
if (cardset.shuffled) {
......@@ -358,13 +383,38 @@ Meteor.methods({
if (cardset.shuffled) {
cardsetFilter = cardset.cardGroups;
}
let existingItems = Wozniak.find({
cardset_id: cardset._id,
user_id: user_id
}, {fields: {card_id: 1}}).fetch();
let excludedCards = [];
existingItems.forEach(function (existingItem) {
excludedCards.push(existingItem.card_id);
});
let newItems = [];
let nextDate = new Date();
cards = Cards.find({
_id: {$nin: excludedCards},
cardset_id: {$in: cardsetFilter},
cardType: {$in: CardType.getCardTypesWithLearningModes()}
});
}, {fields: {_id: 1}}).fetch();
cards.forEach(function (card) {
Meteor.call("addWozniak", cardset._id, card._id, Meteor.userId(), true);
newItems.push({
card_id: card._id,
cardset_id: cardset._id,
user_id: user_id,
ef: 2.5,
interval: 0,
reps: 0,
nextDate: nextDate,
skipped: 0
});
});
if (newItems.length > 0) {
Wozniak.batchInsert(newItems);
}
Meteor.call("updateLearnerCount", cardset._id);
return true;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment