Skip to content
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

Draft
wants to merge 7 commits into
base: development
Choose a base branch
from

Conversation

kmehant
Copy link
Contributor

@kmehant kmehant commented Jul 14, 2020

Signed-off-by: K mehant 411843@student.nitandhra.ac.in

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

  1. All requests other than get are recorded as below data models into Redis cache
  2. When logout was done, complete Redis cache is parsed and pushed into the MongoDB User model -> activity []
  3. Redis cache is then cleared.

Data model on Redis:
A Redis List for cache

userID => [ "route,collection,method,objectID", "route,collection,method,objectID",  ...]

Data model on Mongo:

User: 
.....
.....
 activity: [{
    route: {
      type: String,
      required: true
    },
    method: {
      type: String,
      required: true
    },
    collectionType: {
      type: String,
      required: true
    },
    id: {
      type: String,
      required: true
    },
  }]
  1. method request method
  2. route name of the route
  3. id Mongodb`s _id or object id (taken from the response)
  4. collection type : Type of the collection where we can use our id to fetch data such as created at and others.

partially closes #148

@kmehant kmehant changed the title [WIP] Activity tracking for admin [WIP] Write middleware to track user activity Jul 15, 2020
@kmehant kmehant changed the title [WIP] Write middleware to track user activity Write middleware to track user activity Jul 15, 2020
Copy link
Member

@Rupeshiya Rupeshiya left a 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:

  1. Send POST request to /post
  2. 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.

@kmehant
Copy link
Contributor Author

kmehant commented Jul 15, 2020

@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:

  1. Send POST request to /post
  2. 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
Thank you for the review
I had this doubt, I guess I spoke about the data model in the issue for your approval 🥺☹️ before implementing it.
For get requests thing, I haven't used the middleware yet, we can include this middleware to any route we wish to use.

@Rupeshiya @vaibhavdaren @devesh-verma
Please 🙏🙏 I guess it would be easy if we can discuss on the design, approach correctly and the outcomes so that we all will be on the same page ✌️ and intention.

var routeNamesDict
var mailid
const data = await User.findOne({
'tokens.token': token
Copy link
Member

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.

@vaibhavdaren
Copy link
Member

vaibhavdaren commented Jul 15, 2020

HASH   ---> (KEY, HASH_VALUE)
						|				 
(USER)       |         (RANDOM)
		   (ROUTE)
		      |
		IS THIS UNIQUE?

			KEY           HASH		 ID_OF_OBJECT_RETURNED
 HASH  ---> TIMESTAMP ---> ROUTE -->      (UUID_mogo_id)
    |
(per_user)

@kmehant kmehant requested a review from Rupeshiya July 16, 2020 08:53
Copy link
Member

@Rupeshiya Rupeshiya left a 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??

activityElement.method = activityDataElement[1]
activityElement.collectionType = activityDataElement[2]
activityElement.id = activityDataElement[3]
data.activity.push(activityElement)
Copy link
Member

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.

@Rupeshiya
Copy link
Member

@kmehant Also please add the API to fetch those user activity data from DB.

@kmehant
Copy link
Contributor Author

kmehant commented Jul 18, 2020

Also will it work in case of comment, because in that case we want postId as well as commentId??

@Rupeshiya Yes, it should work!
based on the response as I can see postid and commentid keys in the response object already. So we can use them.

console.log(data)
// clear data from redis
await redisClient.del(userID)
} else if (method != 'GET') {
Copy link
Contributor Author

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 😁

Copy link
Contributor Author

@kmehant kmehant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kmehant
Copy link
Contributor Author

kmehant commented Jul 18, 2020

@Rupeshiya Can you suggest API endpoints so that I can move forward

@Rupeshiya
Copy link
Member

Rupeshiya commented Jul 18, 2020

@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 <411843@student.nitandhra.ac.in>
Signed-off-by: K mehant <411843@student.nitandhra.ac.in>
Signed-off-by: K mehant <411843@student.nitandhra.ac.in>
Signed-off-by: K mehant <411843@student.nitandhra.ac.in>
Signed-off-by: K mehant <411843@student.nitandhra.ac.in>
Signed-off-by: K mehant <411843@student.nitandhra.ac.in>
Signed-off-by: K mehant <411843@student.nitandhra.ac.in>
@vaibhavdaren
Copy link
Member

@kmehant ETA?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement API to showcase activities of a user to the admins
3 participants