-
-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write middleware to track user activity #150
base: development
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kmehant We should not only record the timestamp and routes but what we want is basically the activity of the user let say a user "X" is creating a post so what he will do:
- Send POST request to /post
- With some data
So it should store the activity like: userId, postId, timeStamp, so that we when we render in the client side then from there on clicking a particular activity an admin can navigate to that post.
Also, we should not call the middleware on GET requests as we are not interested in what the user is exploring on the platform but we are interested in what actions he/she is performing on the platform.
@Rupeshiya @Rupeshiya @vaibhavdaren @devesh-verma |
app/middleware/activity.js
Outdated
var routeNamesDict | ||
var mailid | ||
const data = await User.findOne({ | ||
'tokens.token': token |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use user-id instead of the token as by default indexing is done on _id in mongoose so it would give better performance.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kmehant Awesome!!
Also I think we need to add this middleware in every routes except GET requests. In addition to that do you think we need to track user login and logout??
Also will it work in case of comment, because in that case we want postId as well as commentId??
app/middleware/activity.js
Outdated
activityElement.method = activityDataElement[1] | ||
activityElement.collectionType = activityDataElement[2] | ||
activityElement.id = activityDataElement[3] | ||
data.activity.push(activityElement) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change push to unshift, we want recent activity as first element.
@kmehant Also please add the API to fetch those user activity data from DB. |
@Rupeshiya Yes, it should work! |
app/middleware/activity.js
Outdated
console.log(data) | ||
// clear data from redis | ||
await redisClient.del(userID) | ||
} else if (method != 'GET') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kmehant Awesome!!
Also I think we need to add this middleware in every routes except GET requests. In addition to that do you think we need to track user login and logout??
@Rupeshiya I have written check condition here so we can add it everywhere safely 😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Rupeshiya Can you suggest API endpoints so that I can move forward |
Yeah, let it be /user/:id/activity (here id is the userId as we want to fetch for each user by their id) |
Signed-off-by: K mehant <[email protected]>
Signed-off-by: K mehant <[email protected]>
Signed-off-by: K mehant <[email protected]>
Signed-off-by: K mehant <[email protected]>
Signed-off-by: K mehant <[email protected]>
Signed-off-by: K mehant <[email protected]>
Signed-off-by: K mehant <[email protected]>
@kmehant ETA? |
Signed-off-by: K mehant [email protected]
Context
This pull request adds an express middleware that can be used to track user activity and store them in a Mongo database.
Design
Taking several points from @vaibhavdaren and @Rupeshiya, I have come with this approach in designing the middleware
get
are recorded as below data models into Redis cacheData model on Redis:
A Redis List for cache
Data model on Mongo:
method
request methodroute
name of the routeid
Mongodb`s _id or object id (taken from the response)collection type
: Type of the collection where we can use ourid
to fetch data such ascreated at
and others.partially closes #148