Skip to content

peter-sattler/stats-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot Real-time Statistics REST API

I first worked on this simple REST API back in July 2018. It was one of the first stand-alone projects that I uploaded to GitHub. I recently updated it to Java 17 and cleaned-up some of the underlying implementation details.

Getting Started

These instructions will get you a copy of this project up and running on your local machine. Please make sure your JAVA_HOME environment variable is set to a valid JDK installation.

  1. Clone this Git repository:
git clone https://github.com/peter-sattler/stats-api
  1. Switch to the application directory:
cd stats-api
  1. Run the program:
./mvnw spring-boot:run

You can then point your browser to the Swagger UI to interact with the API:

Swagger UI Image

Specifications

The main use case for the API is to calculate real-time statistics for the last 60 seconds. There will be two end-points, one of them is called every time a transaction is made. It is also the sole input of this REST API. The other one collects the statistics based on the transactions in the last 60 seconds.

POST /transactions

  • Every time a new transaction occurs, this end-point will be called:
Body:
{
    "amount": 12.3,
    "timestamp": 1478192204000
}

Where:

  • amount - is a double specifying the transaction amount
  • timestamp - is a long specifying the transaction time in milliseconds from the epoch (UTC time zone). It is not the current timestamp.

Returns an empty body with either:

  • 201 - in case of success
  • 204 - if transaction is older than 60 seconds

GET /statistics

This is the main end-point of this task, this end-point have to execute in constant time and memory (O(1)). It returns the following statistics based on the transactions which happened in the last 60 seconds.

Returns:

{
    "sum": 1000,
    "avg": 100,
    "max": 200,
    "min": 50,
    "count": 10
}

Where:

  • sum is a double specifying the total sum of transaction value in the last 60 seconds
  • avg is a double specifying the average amount of transaction value in the last 60 seconds
  • max is a double specifying single highest transaction value in the last 60 seconds
  • min is a double specifying single lowest transaction value in the last 60 seconds
  • count is a long specifying the total number of transactions happened in the last 60 seconds

Other Considerations:

For the REST API, the biggest and maybe hardest requirement is to make the GET /statistics execute in constant time and space. The best solution would be O(1). It is highly recommended to tackle the O(1) requirement as the last thing to do.

Other requirements, which are obvious, but also listed here explicitly:

  • The API has to be thread-safe with concurrent requests.
  • The API has to function properly, with proper results.
  • The project should be buildable, and tests should also complete successfully.
  • The API should be able to deal with time discrepancy, which means, at any point of time, we could receive a transaction which have a timestamp of the past.
  • Make sure to send the case in memory solution without database (including in-memory database).
  • End-points have to execute in constant time and memory (O(1)).

Pete Sattler
March 2022
peter@sattler22.net

About

Spring Boot Real-time Statistics REST API

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages