0

I am currently developing a spring boot web application in which no entity is persistent, and hence will not be saved on a database.

Although, I need to be able to query some business metrics from it. Suppose the following scenario:

  1. EntityA belongs to a third party API.
  2. An user on my web app requests to see some details of EntityA.
  3. My app then requests EntityA from the third party API, does some processing (could be combining that data with some other available on the web), and returns the desired info to the requesting user.

I am not required to persist EntityA because it is already being persisted inside the third party API. All the processing done is transient. But, I want to be able to check how many times EntityA was requested by an user on my web application, and what data it contained on each request. This metric is useful for audit purposes, for example, since the third party accepts input and output of data. I want to be able to check who input a certain piece of data, and how many times it was done.

If I were to persist EntityA, I would not need all fields that the domain model contain. Also, I would have to add some metadata fields to it (date of request, requesting user, etc.).

What is the correct approach to it?

  1. Persist the domain model of EntityA in my web app, adding the metadata fields on, for example, a superclass?
  2. Create a new entity that express the desired metric, for example RequestedEntity, and map the desired fields of EntityA to RequestedEntity before persisting?
6
  • Why do you want to store the viewcount in a database/domainmodel? What role does that count play in your business rules?
    – marstato
    Jul 1, 2020 at 11:12
  • EDIT: I want to be able to check how many times EntityA was requested by an user on my web application, and what data it contained on each request. This metric is useful for audit purposes, for example, since the third party accepts input and output of data. I want to be able to check who input a certain piece of data, and how many times it was done. Jul 1, 2020 at 11:21
  • 2
    So do you want to audit-log the response from the API for read-only requests or the input from a user on your side to writing requests, or both? In any case i'd not store it "within" something called EntityA. The entity is separate from audit logs you store on it.
    – marstato
    Jul 1, 2020 at 11:24
  • 1
    If your requirements are only what you described, I mean, you just need to persist some information from users requests and it can be modeled as a simple entity, I would just create this model and persist it in some external database service (let's say Firebase). However, If your requirements turns out to be more complex, then you could start thinking about a more reliable solution. In most web frameworks there are means to intercept user requests and extract detailed information. In Spring you could use Filters, and in Node JS, you could attach interceptor callbacks to express. Jul 1, 2020 at 11:27
  • Both. Storing the response from read-only requests enables me to query information about use cases of my app. The storage of input of data to the third API is heavier on the audit side, but is also useful for use case information. Jul 1, 2020 at 11:28

1 Answer 1

4

The easiest way to handle it depends on whether the web app directly calls for EntityA or whether it calls an API layer of yours that happens to call the third party API for EntityA.

If you have no API layer of your own acting in the middle, a front-end analytics platform like Google Analytics or Matomo would be able to capture the counts. You could do something like this:

           (1)
 [Web App] ---> [EntityA API]
     |
     | (2)
     v 
  { GA }

In this case the front-end code fires off a call to indicate the event occurred. In GA, this often looks something like:

 ga.push(['EntityA', '22222'])

If you do have an API layer, then you can lean on the API layer to emit events that you can analyze later.

 [Web App]  --->  [Your API] ---> [EntityA API]
                       |
                       .--> (Kafka/EventHubs) ---> (Analytics Database)

In this case, you stand up a separate database for the metrics you want to track and fire off a quick message to a streaming data broker (like Apache Kafka or Microsoft EventHubs) and have a process listening to these little events and compiling a total count.

Obviously, you can choose to do the first approach either way. It largely depends on how much you want to own of the whole process.

1
  • 1
    Thank you kind sir. I do have an API layer in the middle, but I like the idea of using both: GA for frontend events, and Kafka for post processed data on backend. Jul 1, 2020 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.