Visitors Counter Using Spring Boot

1

I have a Mangos project using Spring Boot with AngularJs and, I wanted to implement a counter of visualizations, accessions, for each sleeve viewed. So I could sort the most Viewed Category Mangas per Week, Monthly and Total Views, same as MangaDex

Example.

{
    "id": 113,
    "nome": "HUNTER X HUNTER",
    "viewsCount": 35
},
{
    "id": 44,
    "nome": "ONE PIECE",
    "viewsCount": 215
}

How could you do this count? Maybe add a counter in the endpoint and add each time they access each manga?

200 GET /user/manga/113 viewsCount:0
200 PUT /user/manga/113 viewsCount:1
    
asked by anonymous 12.09.2018 / 21:54

1 answer

2

One solution is simply to count the moment the GET of the image is accessed.

GET /user/manga/113

To know the number of views, you can have an endpoint for this:

GET /user/manga/113/views

Each access will generate a record in a table, with some visitor data or simply a simple record of the visit. To know how many times it has been visited, just make the count of the records in the table. This solution, if you save information from the user you viewed, allows you to do a total count of views and a count of views excluding multiple visits from the same user (this is the concept of "unique visitors").

You could also create a "counter" column in the table and increase each view as long as you make a UPDATE in the direct counter in the database:

UPDATE manga SET views = views +1 WHERE id = ?

Letting the database itself deal with the writing competition issue if multiple people are accessing the same image at the same time.

If you opt, for whatever reason, to make a SELECT in value, increment and then make UPDATE , you will have writing concurrency problems of this value. For example, if two GET calls occur simultaneously in an image with 5 views, both will get the current value of the bank, increment by 1, and save. So, instead of having 7 views you may have only recorded 6 in the database.

    
12.09.2018 / 23:20