From f1aa96dbc8a0883cd85b12533d1d1ee41ee964fa Mon Sep 17 00:00:00 2001 From: Ian Mancini Date: Mon, 14 Dec 2020 02:56:38 -0300 Subject: [PATCH] Save visit count --- packages/server/src/index.js | 19 ++++++++++++++++-- packages/server/src/models/museum.js | 30 ++++++++++++++++++++++++++++ packages/server/src/models/user.js | 4 ++++ packages/server/src/museumStats.js | 13 ++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 packages/server/src/models/museum.js create mode 100644 packages/server/src/museumStats.js diff --git a/packages/server/src/index.js b/packages/server/src/index.js index 7bc7370..1fe0718 100644 --- a/packages/server/src/index.js +++ b/packages/server/src/index.js @@ -24,6 +24,8 @@ import passport from 'passport' import mongoose from 'mongoose' import UserModel from './models/user' +import { getMuseumDocument } from './museumStats' + import redisClient, { asyncHEXISTS, asyncHGET, @@ -151,8 +153,21 @@ io.on('connection', async (socket) => { await asyncHDEL('socket', String(socket.request.user._id)) }) - socket.on('register-visit', () => { - console.log(`new visit from: ${socket.request.user.name}`) + socket.on('register-visit', async () => { + logger.debug(`Registering new new visit from: ${socket.request.user.name}`) + const museumStats = await getMuseumDocument() + museumStats.visitCount = museumStats.visitCount + 1 + + const socketCount = io.of('/').sockets.size + + if (museumStats.maxSimultaneousVisitsCount < socketCount) { + museumStats.maxSimultaneousVisitsCount = socketCount + } + museumStats.save() + + const user = await UserModel.findById(socket.request.user._id) + user.visitCount = user.visitCount + 1 + user.save() }) const user = await UserModel.findById(socket.request.user._id) diff --git a/packages/server/src/models/museum.js b/packages/server/src/models/museum.js new file mode 100644 index 0000000..f53c343 --- /dev/null +++ b/packages/server/src/models/museum.js @@ -0,0 +1,30 @@ +import { model, Schema } from 'mongoose' + +const museumModel = new Schema( + { + visitCount: { + type: Number, + default: 0, + required: true, + }, + maxSimultaneousVisitsCount: { + type: Number, + default: 0, + required: true, + }, + interactionCount: { + type: Number, + default: 0, + required: true, + }, + }, + { timestamps: true }, +) + +/* TODO + * store museum time + * store artwork with more visits + */ + +const UserModel = model('Museum', museumModel) +export default UserModel diff --git a/packages/server/src/models/user.js b/packages/server/src/models/user.js index 9bcf1e7..5a0ab06 100644 --- a/packages/server/src/models/user.js +++ b/packages/server/src/models/user.js @@ -18,6 +18,10 @@ const userSchema = new Schema( type: String, required: true, }, + visitCount: { + type: Number, + default: 0, + }, photo: String, lastLocation: Object, }, diff --git a/packages/server/src/museumStats.js b/packages/server/src/museumStats.js new file mode 100644 index 0000000..37f4a37 --- /dev/null +++ b/packages/server/src/museumStats.js @@ -0,0 +1,13 @@ +import MuseumModel from './models/museum' +import logger from './logger' + +export async function getMuseumDocument() { + const museum = await MuseumModel.findOne() + if (museum) { + return museum + } + + logger.info('Creating museum stats document (db)') + const document = await new MuseumModel().save() + return document +}