GithubHelp home page GithubHelp logo

coding-challenge-statistics's Introduction

TRANSACTIONS-STATISTICS

Language Java 8
Built Using SpringBoot 2.1.2,Junit 4 and Prometheus(for metrics monitoring)
IDE used IntelliJ 2019.1

Code​ ​Challenge

We would like to have a restful API for our statistics. The main use case for our API is to calculate real-time statistic from the last 60 seconds. There will be two APIs, one of them is called every time a transaction is made. It is also the sole input of this rest API. The other one returns the statistic based of the transactions of the last 60 seconds.

Specs

POST /transactions

Every time a new transaction happened, this endpoint will be called.

Body:

{
  "amount": "12.3343",
  "timestamp": "2018-07-17T09:59:51.312Z"
}

Where:


amount – transaction amount; a string of arbitrary length that is parsable as a BigDecimal
timestamp – transaction time in the ISO 8601 format YYYY-MM-DDThh:mm:ss.sssZ in the UTC timezone (this is not the current timestamp)

Returns: Empty body with either 201 or 204.

201 – in case of success
204 – if the transaction is older than 60 seconds
400 – if the JSON is invalid
422 – if any of the fields are not parsable or the transaction date is in the future
GET /statistics

This returns statistics of last 60 seconds transactions.

Response:
{
  "sum": "1000.00",
  "avg": "100.53",
  "max": "200000.49",
  "min": "50.23",
  "count": 10
}
Where:

sum – a BigDecimal specifying the total sum of transaction value in the last 60 seconds
avg – a BigDecimal specifying the average amount of transaction value in the last 60 seconds
max – a BigDecimal specifying single highest transaction value in the last 60 seconds
min – a BigDecimal specifying single lowest transaction value in the last 60 seconds
count – a long specifying the total number of transactions that happened in the last 60 seconds
All BigDecimal values always contain exactly two decimal places and use `HALF_ROUND_UP` rounding. eg: 10.345 is returned as 10.35, 10.8 is returned as 10.80


DELETE /transactions
This endpoint causes all existing transactions to be deleted

The endpoint should accept an empty request body and return a 204 status code.

Requirements

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 very recommended to tackle the O(1) requirement as the last thing to do as it is not the only thing which will be rated in the code challenge. Other requirements, which are obvious, but also listed here explicitly:

● The API have to be threadsafe with concurrent requests
● The API have to function properly, with proper result
● The project should be buildable, and tests should also complete successfully. e.g. If maven is used, then
   mvn clean install should 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) ##Handling the solution wihtout database and having O(1) is biggest challenge.
● Endpoints have to execute in constant time and memory (O(1)).

Prerequisites

The project requires Java 8 and Maven to build.

Build

$ mvn clean install

Run

$ mvn spring-boot:run

Test

$ mvn clean integration-test

Included Actutators to do health check and for monitoring- attached screenshot of local metrics

/actuator/health or  /actuator/metrics or  /actuator/info 
/actuator/prometheus

Included Prometheus to do metrics monitoring for production level

query to run prometheus in 9090 port using docker

$ docker run -d --name=prometheus -p 9090:9090 -v <prometheus.yml>:/etc/

coding-challenge-statistics's People

Contributors

varadharajaan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.