codeuino / social-platform-donut-backend

Donut API:
http://donut-api-prod.codeuino.org/
GNU General Public License v3.0
24 stars 57 forks source link

Write middleware to track user activity #150

Open kmehant opened 4 years ago

kmehant commented 4 years ago

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 commented 4 years ago

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

vaibhavdaren commented 4 years ago
HASH   ---> (KEY, HASH_VALUE)
                        |                
(USER)       |         (RANDOM)
           (ROUTE)
              |
        IS THIS UNIQUE?

            KEY           HASH       ID_OF_OBJECT_RETURNED
 HASH  ---> TIMESTAMP ---> ROUTE -->      (UUID_mogo_id)
    |
(per_user)
Rupeshiya commented 4 years ago

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

kmehant commented 4 years ago

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 commented 4 years ago

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

Rupeshiya commented 4 years ago

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

vaibhavdaren commented 4 years ago

@kmehant ETA?