social-platform-donut-backend icon indicating copy to clipboard operation
social-platform-donut-backend copied to clipboard

Write middleware to track user activity

Open kmehant opened this issue 4 years ago • 7 comments

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

  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 avatar Jul 14 '20 16:07 kmehant

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

kmehant avatar Jul 15 '20 11:07 kmehant

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

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

vaibhavdaren avatar Jul 15 '20 16:07 vaibhavdaren

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

Rupeshiya avatar Jul 18 '20 06:07 Rupeshiya

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.

kmehant avatar Jul 18 '20 07:07 kmehant

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

kmehant avatar Jul 18 '20 07:07 kmehant

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

Rupeshiya avatar Jul 18 '20 15:07 Rupeshiya

@kmehant ETA?

vaibhavdaren avatar Aug 13 '20 14:08 vaibhavdaren