From 14e0db26d7ad63835afa6dfa07d7fe1c464b3049 Mon Sep 17 00:00:00 2001 From: iyanfdezz Date: Sun, 3 Mar 2024 14:34:06 +0100 Subject: [PATCH] dockerized stats service (not working) --- docker-compose.yml | 18 ++++++++++++++++++ stats/.dockerignore | 2 ++ stats/Dockerfile | 20 ++++++++++++++++++++ stats/model/stats-getter.js | 3 ++- stats/stats-service.js | 19 +++---------------- 5 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 stats/.dockerignore create mode 100644 stats/Dockerfile diff --git a/docker-compose.yml b/docker-compose.yml index fbc8d143..89f7e1dc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -21,6 +21,22 @@ services: networks: - mynetwork + statsservice: + container_name: statsservice-${teamname:-defaultASW} + image: ghcr.io/arquisoft/wiq_es1a/stats:latest + profiles: ["dev", "prod"] + build: ./stats + depends_on: + - mongodb + - userservice + ports: + - "8004:8004" + networks: + - mynetwork + environment: + MONGODB_URI: mongodb://mongodb:27017/userdb + USER_SERVICE_URL: http://userservice:8001 + authservice: container_name: authservice-${teamname:-defaultASW} image: ghcr.io/arquisoft/wiq_es1a/authservice:latest @@ -59,11 +75,13 @@ services: - userservice - authservice - questionservice + - statsservice ports: - "8000:8000" networks: - mynetwork environment: + STATS_SERVICE_URL: http://statsservice:8004 QUESTION_SERVICE_URL: http://questionservice:8003 AUTH_SERVICE_URL: http://authservice:8002 USER_SERVICE_URL: http://userservice:8001 diff --git a/stats/.dockerignore b/stats/.dockerignore new file mode 100644 index 00000000..3091757a --- /dev/null +++ b/stats/.dockerignore @@ -0,0 +1,2 @@ +node_modules +coverage \ No newline at end of file diff --git a/stats/Dockerfile b/stats/Dockerfile new file mode 100644 index 00000000..42d46c3b --- /dev/null +++ b/stats/Dockerfile @@ -0,0 +1,20 @@ +# Use an official Node.js runtime as a parent image +FROM node:20 + +# Set the working directory in the container +WORKDIR /usr/src/stats + +# Copy package.json and package-lock.json to the working directory +COPY package*.json ./ + +# Install app dependencies +RUN npm install + +# Copy the app source code to the working directory +COPY . . + +# Expose the port the app runs on +EXPOSE 8004 + +# Define the command to run your app +CMD ["node", "stats-service.js"] diff --git a/stats/model/stats-getter.js b/stats/model/stats-getter.js index 073ba071..884446cb 100644 --- a/stats/model/stats-getter.js +++ b/stats/model/stats-getter.js @@ -1,4 +1,4 @@ -const User = require('../../users/userservice/user-model.js/User'); +const User = require('../../users/userservice/user-model.js'); class StatsForUser { @@ -16,6 +16,7 @@ class StatsForUser { var totalPoints = 0; var totalCorrectQuestions = 0; var totalIncorrectQuestions = 0; + var avgTime=0; for (const partida of partidas){ totalPoints += partida.points; diff --git a/stats/stats-service.js b/stats/stats-service.js index daca623c..0b800e64 100644 --- a/stats/stats-service.js +++ b/stats/stats-service.js @@ -5,8 +5,6 @@ const StatsForUser = require("./model/stats-getter"); const User = require("../users/user-model.js"); const cors = require('cors'); -const cron = require("node-cron"); - const app = express(); const port = 8004; @@ -60,19 +58,8 @@ app.get("/stats", async (req, res) => { } }); -const server = app.listen(port, async() => { - console.log(`Stats Service listening at http://localhost:${port}`); - statsGetter.getStatsForUser(req.query.user) - .then(() => { - console.log("Stats loaded successfully!"); - }) - .catch((error) => { - console.error("Error al cargar las estadísticas", error); - }); - }); - -cron.schedule("0 3 * * *", async () => { - await statsGetter.getStatsForUser(req.query.user); - }); +const server = app.listen(port, () => { + console.log(`Stats Service listening at http://localhost:${port}`); +}); module.exports = server; \ No newline at end of file