質問

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?
役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません softwareengineering.stackexchange
scroll top