fix: #12444 add uncategorized topics counted stats to separate sorted set

This commit is contained in:
Julian Lam
2024-03-26 11:34:09 -04:00
parent c2890a3e74
commit 8dcdf8ef3b
3 changed files with 45 additions and 6 deletions

View File

@@ -50,6 +50,12 @@ module.exports = function (Topics) {
`cid:${topicData.cid}:tids:create`,
`cid:${topicData.cid}:uid:${topicData.uid}:tids`,
];
const countedSortedSetKeys = [
...['views', 'posts', 'votes'].map(prop => `${topicData.cid === -1 ? 'topicsRemote' : 'topics'}:${prop}`),
`cid:${topicData.cid}:tids:votes`,
`cid:${topicData.cid}:tids:posts`,
`cid:${topicData.cid}:tids:views`,
];
const scheduled = timestamp > Date.now();
if (scheduled) {
@@ -58,12 +64,7 @@ module.exports = function (Topics) {
await Promise.all([
db.sortedSetsAdd(timestampedSortedSetKeys, timestamp, topicData.tid),
db.sortedSetsAdd([
'topics:views', 'topics:posts', 'topics:votes',
`cid:${topicData.cid}:tids:votes`,
`cid:${topicData.cid}:tids:posts`,
`cid:${topicData.cid}:tids:views`,
], 0, topicData.tid),
db.sortedSetsAdd(countedSortedSetKeys, 0, topicData.tid),
user.addTopicIdToUser(topicData.uid, topicData.tid, timestamp),
db.incrObjectField(`category:${topicData.cid}`, 'topic_count'),
db.incrObjectField('global', 'topicCount'),

View File

@@ -0,0 +1,36 @@
// REMOVE THIS PRIOR TO 4.0 ALPHA
'use strict';
const db = require('../../database');
module.exports = {
name: 'Fix topic sorted sets for uncategorized topics',
timestamp: Date.UTC(2024, 2, 26),
method: async function () {
const props = ['views', 'posts', 'votes'];
const { progress } = this;
const tids = await db.getSortedSetMembers('cid:-1:tids');
progress.total = tids.length;
const remove = [];
const add = [];
await Promise.all(props.map(async (prop) => {
const set = `topics:${prop}`;
const newSet = `topicsRemote:${prop}`;
const scores = await db.sortedSetScores(set, tids);
scores.forEach((score, idx) => {
if (score !== null) {
remove.push([set, tids[idx]]);
add.push([newSet, score, tids[idx]]);
}
});
}));
await Promise.all([
db.sortedSetRemoveBulk(remove),
db.sortedSetAddBulk(add),
]);
},
};

View File

@@ -1,3 +1,5 @@
// REMOVE THIS PRIOR TO 4.0 ALPHA
'use strict';
const db = require('../../database');