From 0cf495e127403c55d45fed632ecd0dddaa15afc9 Mon Sep 17 00:00:00 2001 From: Miodec Date: Mon, 2 Aug 2021 16:21:14 +0100 Subject: [PATCH] got public stats to work --- backend/api/controllers/result.js | 4 +++- backend/dao/public-stats.js | 22 +++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/backend/api/controllers/result.js b/backend/api/controllers/result.js index 4582a1449..a3ce6484b 100644 --- a/backend/api/controllers/result.js +++ b/backend/api/controllers/result.js @@ -1,6 +1,6 @@ const ResultDAO = require("../../dao/result"); const UserDAO = require("../../dao/user"); -// const PublicStatsDAO = require("../../dao/public-stats"); +const PublicStatsDAO = require("../../dao/public-stats"); const { validateObjectValues, validateResult, @@ -168,6 +168,8 @@ class ResultController { await UserDAO.updateTypingStats(uid, result.restartCount, tt); + await PublicStatsDAO.updateStats(result.restartCount, tt); + let addedResult = await ResultDAO.addResult(uid, result); return res.status(200).json({ diff --git a/backend/dao/public-stats.js b/backend/dao/public-stats.js index 887e6694e..368b3915f 100644 --- a/backend/dao/public-stats.js +++ b/backend/dao/public-stats.js @@ -4,17 +4,21 @@ const { roundTo2 } = require("../handlers/misc"); class PublicStatsDAO { //needs to be rewritten, this is public stats not user stats - static async increment(started, completed, time) { + static async updateStats(restartCount, 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 } }); + .collection("public") + .updateOne( + { type: "stats" }, + { + $inc: { + testsCompleted: 1, + testsStarted: restartCount + 1, + timeTyping: time, + }, + }, + { upsert: true } + ); return true; } }