-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
User activity tracker using redis (#174)
* update code * initial mechanism for user tracking initial mechanism for user tracking
- Loading branch information
Showing
21 changed files
with
376 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
PORT=5000 | ||
NODE_ENV="development" | ||
JWT_SECRET="thisismysupersecrettokenjustkidding" | ||
DATABASE_URL"mongodb://mongo:27017/donut-development" | ||
DATABASE_URL="mongodb://mongo:27017/donut-development" | ||
SENDGRID_API_KEY='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' | ||
SOCKET_PORT=8810 | ||
clientbaseurl = "http://localhost:3000/" | ||
clientbaseurl = "http://localhost:3000" | ||
SOCKET_PORT=8810 | ||
REDIS_HOST="redis" | ||
REDIS_PORT=6379 | ||
REDIS_PASSWORD="auth" | ||
REDIS_DB=0 | ||
REDIS_ACTIVITY_DB=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const User = require('../models/User') | ||
const Activity = require('../models/Activity') | ||
|
||
const HttpStatus = require('http-status-codes') | ||
module.exports = { | ||
|
||
getActivity: async (req, res, next) => { | ||
// userID whose activity will be fetched by admin | ||
const { id } = req.params | ||
|
||
try { | ||
// Check if user exists | ||
const user = await User.findById(req.user._id) | ||
if (!user) { | ||
return res.status(HttpStatus.BAD_REQUEST).json({ msg: 'No such user exists!' }) | ||
} | ||
|
||
// check if not admin | ||
if (user.isAdmin !== true) { | ||
return res.status(HttpStatus.BAD_REQUEST).json({ msg: 'You don\'t have permission!' }) | ||
} | ||
|
||
const data = await Activity.findOne({ userId: id }) | ||
return res.status(HttpStatus.OK).json({ activity: data.activity }) | ||
} catch (error) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.log(error.name, '-', error.message) | ||
} | ||
return res.status(HttpStatus.BAD_REQUEST).json({ error: error.message }) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const User = require('../models/User') | ||
const redis = require('../../config/redis') | ||
|
||
const activity = async (req, res, next) => { | ||
var redisClient = redis.redisClient | ||
var route = req.originalUrl.replace(/\?.*$/, '') | ||
var method = req.method | ||
var userID = req.user.id.toString() | ||
console.log('req.body ', req.body) | ||
console.log('res.locals.data ', res.locals.data) | ||
console.log('route ', route) | ||
console.log('methods ', method) | ||
|
||
if (route === '/user/logout') { | ||
var activityData = await redisClient.lrange(userID, 0, -1) | ||
const data = await User.findOne({ | ||
_id: userID | ||
}) | ||
var activityElement = { | ||
route: '', | ||
method: '', | ||
collectionType: '', | ||
id: '', | ||
timestamp: '' | ||
} | ||
for (let index = 0; index < activityData.length; index++) { | ||
var activityDataElement = activityData[index].split(',') | ||
activityElement.route = activityDataElement[0] | ||
activityElement.method = activityDataElement[1] | ||
activityElement.collectionType = activityDataElement[2] | ||
activityElement.id = activityDataElement[3] | ||
activityElement.timestamp = activityDataElement[4] | ||
data.activity.unshift(activityElement) | ||
} | ||
await data.update() | ||
console.log('DATA') | ||
console.log(data) | ||
// clear data from redis | ||
await redisClient.del(userID) | ||
} else if (method !== 'GET') { | ||
var objectID = res.locals.data._id | ||
userID = objectID | ||
var timeStamp = Date() | ||
var collectionType = res.locals.collectionType | ||
if (typeof res.locals.data.userId !== 'undefined') { | ||
userID = res.locals.data.userId | ||
} | ||
// example /auth/login,POST,user,5ed09e9d446f2b1c208b6ba8,Thu Jul 23 2020 20:28:29 GMT+0530 (India Standard Time) | ||
activityElement = route.concat(',', method, ',', collectionType, ',', objectID, ',', timeStamp) | ||
// userID => [(route, collection, method, objectID), (route,method, collection, objectID) ...] | ||
await redisClient.rpush(userID, activityElement) | ||
next() | ||
} | ||
} | ||
|
||
module.exports = activity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const mongoose = require('mongoose') | ||
const Schema = mongoose.Schema | ||
|
||
const activitySchema = new Schema({ | ||
userId: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'User' | ||
}, | ||
activity: [{ | ||
route: { | ||
type: String, | ||
required: true | ||
}, | ||
method: { | ||
type: String, | ||
required: true | ||
}, | ||
collectionType: { | ||
type: String, | ||
required: true | ||
}, | ||
id: { | ||
type: String, | ||
required: true | ||
}, | ||
timestamp: { | ||
type: String, | ||
required: true | ||
} | ||
}] | ||
}) | ||
|
||
module.exports = mongoose.model('Activity', activitySchema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const express = require('express') | ||
const router = express.Router() | ||
|
||
const activityController = require('../controllers/activity') | ||
const isUnderMaintenance = require('../middleware/maintenance') | ||
const auth = require('../middleware/auth') | ||
|
||
// get a User activity | ||
router.get( | ||
'/user/:id', | ||
isUnderMaintenance, | ||
auth, | ||
activityController.getActivity | ||
) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.