mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-23 05:56:10 +08:00
added public stats dao
This commit is contained in:
parent
35262ba080
commit
8a44e170da
2 changed files with 32 additions and 26 deletions
32
backend/dao/publicstatsDAO.js
Normal file
32
backend/dao/publicstatsDAO.js
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
const MonkeyError = require("../handlers/error");
|
||||
const { mongoDB } = require("../init/mongodb");
|
||||
const { roundTo2 } = require("../handlers/misc");
|
||||
|
||||
class PublicStatsDAO {
|
||||
|
||||
static async increment(started, completed, time){
|
||||
time = roundTo2(time);
|
||||
await mongoDB()
|
||||
.collection("users")
|
||||
.updateOne(
|
||||
{ name: 'startedTests' },
|
||||
{ $inc: { value: started } }
|
||||
);
|
||||
await mongoDB()
|
||||
.collection("users")
|
||||
.updateOne(
|
||||
{ name: 'completedTests' },
|
||||
{ $inc: { value: completed } }
|
||||
);
|
||||
await mongoDB()
|
||||
.collection("users")
|
||||
.updateOne(
|
||||
{ name: 'timeTyping' },
|
||||
{ $inc: { value: time } }
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = PublicStatsDAO;
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
async function incrementPublicTypingStats(started, completed, time) {
|
||||
try {
|
||||
time = roundTo2(time);
|
||||
Stats.findOne({}, (err, stats) => {
|
||||
stats.completedTests += completed;
|
||||
stats.startedTests += started;
|
||||
stats.timeTyping += time;
|
||||
stats.save();
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(`Error while incrementing public stats: ${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Initialize stats database if none exists
|
||||
Stats.findOne((err, stats) => {
|
||||
if (!stats) {
|
||||
let newStats = new Stats({
|
||||
completedTests: 0,
|
||||
startedTests: 0,
|
||||
timeTyping: 0,
|
||||
});
|
||||
newStats.save();
|
||||
}
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue