added public stats dao

This commit is contained in:
Miodec 2021-06-12 13:39:30 +01:00
parent 35262ba080
commit 8a44e170da
2 changed files with 32 additions and 26 deletions

View 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;

View file

@ -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();
}
});