forked from HabitRPG/habitica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20160529_fix_challenges.js
261 lines (201 loc) · 7.42 KB
/
20160529_fix_challenges.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
'use strict';
/****************************************
* Reason: After the api v3 maintenance migration, some challenge tasks
* became unlinked from their challenges. We're still not sure why,
* but this re-links them
*
* Note: We ran this on a local backup of the DB, and from that, grabbed
* the ids of the tasks that could be fixed and the updates that would
* be applied to them. We only ran the `updateTasks` promise task.
***************************************/
const authorName = 'Blade';
const authorUuid = '75f270e8-c5db-4722-a5e6-a83f1b23f76b';
global.Promise = require('bluebird');
const MongoClient = require('mongodb').MongoClient;
const TaskQueue = require('cwait').TaskQueue;
const logger = require('./utils/logger');
// PROD: Enable prod db
// const NODE_DB_URI = 'mongodb://username:[email protected]:XXXXX,dsXXXXXX-a1.mlab.com:XXXXX/habitica?replicaSet=rs-dsXXXXXX';
const NODE_DB_URI = 'mongodb://localhost/new-prod-copy';
// Cached ids from running the findBrokenChallengeTasks query on a local copy of the db
// These are all the ids that _are_ fixable
const TASK_IDS = require('../fixable_task_ids.json');
const TASK_UPDATE_DATA = require('../challenge_fixes.json');
let db;
let count = 0;
var timer = setInterval(function(){
count++;
if (count % 30 === 0) {
logger.warn('Process has been running for', count / 60, 'minutes');
}
}, 1000);
connectToDb()
// .then(findBrokenChallengeTasks)
// .then(getDataFromTasks)
// .then(getUserChallenges)
// .then(getChallengeTasks)
// .then(correctUserTasks)
.then(updateTasks)
.then(closeDb)
.catch(reportError)
function connectToDb () {
return new Promise((resolve, reject) => {
MongoClient.connect(NODE_DB_URI, (err, database) => {
if (err) {
logger.error('Uh oh... Problem connecting to the new database');
return reject(err);
}
logger.success('Connected to the database');
db = database;
resolve(db);
});
});
}
function reportError (err) {
logger.error('Uh oh, an error occurred');
closeDb();
throw err;
}
function unique (array) {
return Array.from(new Set(array));
}
function findBrokenChallengeTasks () {
logger.info('Looking for broken tasks...');
// return db.collection('tasks').find({'challenge.broken': 'CHALLENGE_TASK_NOT_FOUND'}).toArray()
return db.collection('tasks').find({'_id': { '$in': TASK_IDS }}).toArray()
.then((tasks) => {
logger.success('Found', tasks.length, 'broken tasks.');
return Promise.resolve(tasks);
});
}
function getDataFromTasks (tasks) {
logger.info('Collecting data about the tasks...');
let userTasks = {};
tasks.forEach((task) => {
let userId = task.userId;
if (!userTasks[userId]) {
userTasks[userId] = [];
}
userTasks[userId].push(task);
});
let users = unique(tasks.map(task => task.userId));
return Promise.resolve({
users,
userTasks,
tasks,
});
}
function getUserChallenges (data) {
logger.info('Collecting user challenges...');
return db.collection('users').find({_id: { '$in': data.users }}, {challenges: 1}).toArray().then((docs) => {
logger.success('Found', docs.length, 'users from broken challenge tasks.');
let challenges = [];
docs.forEach((user) => {
challenges.push.apply(challenges, user.challenges);
});
challenges = unique(challenges);
let userChallenges = {};
docs.forEach((user) => {
let userId = user._id;
if (!userChallenges[userId]) {
userChallenges[userId] = [];
}
userChallenges[userId].push.apply(userChallenges[userId], user.challenges);
});
data.userChallenges = userChallenges;
data.challenges = challenges;
logger.success('Found', challenges.length, 'unique challenges.');
return Promise.resolve(data);
});
}
function getChallengeTasks (data) {
logger.info('Looking up original challenge tasks...');
return db.collection('tasks').find({'userId': null, 'challenge.id': { '$in': data.challenges }}, [ 'text', 'type', 'challenge', '_legacyId' ]).toArray().then((docs) => {
logger.success('Found', docs.length, 'challenge tasks.');
let challengeTasks = {};
docs.forEach((task) => {
let chalId = task.challenge.id;
if (!challengeTasks[chalId]) {
challengeTasks[chalId] = [];
}
challengeTasks[chalId].push(task);
});
data.challengeTasks = challengeTasks;
return Promise.resolve(data);
});
}
function correctUserTasks (data) {
logger.info('Correcting user tasks...');
let tasksToUpdate = {};
let duplicateTasks = {};
for (let user in data.userChallenges) {
if (user === authorUuid) {
logger.success('Processing data for', authorName);
}
if (data.userChallenges.hasOwnProperty(user)) {
let challenges = data.userChallenges[user];
challenges.forEach((chal) => {
let challengeTasks = data.challengeTasks[chal];
let userTasks = data.userTasks[user];
if (challengeTasks) {
challengeTasks.forEach((challengeTask) => {
let text = challengeTask.text;
let type = challengeTask.type;
let taskId = challengeTask._id;
let legacyId = challengeTask._legacyId;
let foundTask = userTasks.find((task) => {
return TASK_IDS.indexOf(task._id) > -1 && task._legacyId === legacyId && task.type === type && task.text === text;
})
if (foundTask && !tasksToUpdate[foundTask._id]) {
tasksToUpdate[foundTask._id] = {
id: chal,
broken: null,
taskId,
}
} else if (foundTask && taskId !== tasksToUpdate[foundTask._id].taskId) {
logger.error('Duplicate task found, id:', foundTask._id);
duplicateTasks[foundTask._id] = duplicateTasks[foundTask._id] || [tasksToUpdate[foundTask._id].taskId];
duplicateTasks[foundTask._id].push(taskId);
}
});
}
});
}
}
let numberOfDuplicateTasksFound = Object.keys(duplicateTasks).length;
if (numberOfDuplicateTasksFound > 0) {
logger.error('Found', numberOfDuplicateTasksFound, 'duplicate taks');
}
data.tasksToUpdate = tasksToUpdate;
return Promise.resolve(data);
}
function updateTasks (data) {
let tasksToUpdate = TASK_UPDATE_DATA;
let taskIdsToUpdate = Object.keys(tasksToUpdate);
let queue = new TaskQueue(Promise, 300);
let promiseCount = 0;
logger.info('About to update', taskIdsToUpdate.length, 'user tasks');
function updateTaskById (taskId) {
promiseCount++;
if (promiseCount % 500 === 0) {
logger.info(promiseCount, 'updates started');
}
return db.collection('tasks').findOneAndUpdate({_id: taskId, 'challenge.broken': 'CHALLENGE_TASK_NOT_FOUND'}, {$set: {challenge: tasksToUpdate[taskId]}}, {returnOriginal: false})
}
return Promise.map(taskIdsToUpdate, queue.wrap(updateTaskById)).then((result) => {
let updates = result.filter(res => res.lastErrorObject.updatedExisting)
let failures = result.filter(res => !res.lastErrorObject.updatedExisting);
logger.success(updates.length, 'tasks have been fixed');
if (failures.length > 0) {
logger.error(failures.length, 'tasks could not be updated');
logger.error('Manually check these results');
logger.error(failures);
}
return Promise.resolve(data);
});
}
function closeDb (data) {
logger.success('The process took ' + count + ' seconds');
clearInterval(timer)
db.close();
}