a year ago
Hello everyone, I'm working on a Discord bot for tracking activity metrics like time spent in voice chats and messages sent. It has a weekly & all time leaderboard. Recently, an issue came up where after the weekly leaderboard reset, some users would have their stats reverted to before the reset.
The bot uses JSON as storage through a volume. I've attached both the storage code and the code that handles the leaderboard frontend through Discord.
Relevant snippets:
Reset Weekly Stats:
/**
* Resets the weekly stats for all users
*/
async function resetWeek() {
try {
await usersMutex.acquire();
const data = await fs.readFile(USERS_FILE);
let users = JSON.parse(data);
const newUsers = users.map((user) => {
user["weeklyStats"] = {
vcMinutes: 0,
messages: 0,
weekStart: dayjs().startOf("week").toISOString(),
};
return user;
});
await fs.writeFile(USERS_FILE, JSON.stringify(newUsers));
} finally {
usersMutex.release();
}
}```8 Replies
a year ago
227a9950-e5f9-426b-aedd-3194b613eae8
a year ago
What problem are you having? It's not clearly stated in your question
a year ago
My recommendation no matter what the problem will be to use a proper database rather than a JSON file, and potentially change the structure of that database to store data over time rather than just point in time
a year ago
Having your weekly leaderboards be a SELECT * FROM database WHERE timestamp > TIMESTAMP_DIFF(NOW(), -7, days) rather than requiring a full reset is much much less work in the future
a year ago
I would tend to agree with you on the use of the database but part of the requirements for this project was to use JSON. Additionally I would like to avoid storing individual messages/minutes since the amount of data would get very large quickly. The server in question generated well over 10k messages in the first week. I think you're right and thinking about it a little more I think it's worth restructuring the data.
As for the exact problem, the flow is like this
- Throughout the week, messages & voice chat minutes are accumulated
- Sunday rolls around, and the week is reset where all weekly stats are set to 0
- The leaderboard displays this correctly for a time
- After a few select users (seemingly random) send another message, it reverts the number of weekly messages/minutes to the data from the previous week
a year ago
This would not be an issue with Railway's volumes, as volumes just act as persistent folder storage. This is very likely a code issue
a year ago
I figured as much but I thought it was worth a shot. You can mark it as solved
a year ago
!s
Status changed to Solved adam • over 1 year ago