Habits: store one history entry per day (#10442)

* initial refactor

* add scoredUp and scoredDown values for habits history entries, one entry per habit per day

* fix lint and add initial migration

* update old test

* remove scoreNotes

* dry run for migration

* migration fixes

* update migration and remove old test

* fix

* add challenges migration (read only)

* fix challenges migration

* handle custom day start

* update tasks in migration

* scoring: support cds

* add new test
This commit is contained in:
Matteo Pagliazzi
2018-06-21 21:25:19 +02:00
committed by GitHub
parent 8437b916c4
commit c1bd7f5dc5
11 changed files with 421 additions and 67 deletions

View File

@@ -184,31 +184,53 @@ TaskSchema.statics.sanitizeReminder = function sanitizeReminder (reminderObj) {
return reminderObj;
};
TaskSchema.methods.scoreChallengeTask = async function scoreChallengeTask (delta) {
TaskSchema.methods.scoreChallengeTask = async function scoreChallengeTask (delta, direction) {
let chalTask = this;
chalTask.value += delta;
if (chalTask.type === 'habit' || chalTask.type === 'daily') {
// Add only one history entry per day
let lastChallengHistoryIndex = chalTask.history.length - 1;
const history = chalTask.history;
const lastChallengHistoryIndex = history.length - 1;
const lastHistoryEntry = history[lastChallengHistoryIndex];
if (chalTask.history[lastChallengHistoryIndex] &&
moment(chalTask.history[lastChallengHistoryIndex].date).isSame(new Date(), 'day')) {
chalTask.history[lastChallengHistoryIndex] = {
if (
lastHistoryEntry && lastHistoryEntry.date &&
moment().isSame(lastHistoryEntry.date, 'day')
) {
lastHistoryEntry.value = chalTask.value;
lastHistoryEntry.date = Number(new Date());
if (chalTask.type === 'habit') {
// @TODO remove this extra check after migration has run to set scoredUp and scoredDown in every task
lastHistoryEntry.scoredUp = lastHistoryEntry.scoredUp || 0;
lastHistoryEntry.scoredDown = lastHistoryEntry.scoredDown || 0;
if (direction === 'up') {
lastHistoryEntry.scoredUp += 1;
} else {
lastHistoryEntry.scoredDown += 1;
}
}
chalTask.markModified(`history.${lastChallengHistoryIndex}`);
} else {
const historyEntry = {
date: Number(new Date()),
value: chalTask.value,
};
chalTask.markModified(`history.${lastChallengHistoryIndex}`);
} else {
chalTask.history.push({
date: Number(new Date()),
value: chalTask.value,
});
if (chalTask.type === 'habit') {
historyEntry.scoredUp = direction === 'up' ? 1 : 0;
historyEntry.scoredDown = direction === 'down' ? 1 : 0;
}
history.push(historyEntry);
// Only preen task history once a day when the task is scored first
if (chalTask.history.length > 365) {
chalTask.history = preenHistory(chalTask.history, true); // true means the challenge will retain as much entries as a subscribed user
chalTask.history = preenHistory(chalTask.history, true); // true means the challenge will retain as many entries as a subscribed user
}
}
}
@@ -220,7 +242,11 @@ export let Task = mongoose.model('Task', TaskSchema);
// habits and dailies shared fields
let habitDailySchema = () => {
return {history: Array}; // [{date:Date, value:Number}], // this causes major performance problems
// Schema not defined because it causes serious perf problems
// date is a date stored as a Number value
// value is a Number
// scoredUp and scoredDown only exist for habits and are numbers
return {history: Array};
};
// dailys and todos shared fields