Assignment aggregation system

.
Assignment 
Build aggregation system. 
You need create an endpoint to receive the events, and an endpoint to get each of the stats/one endpoint with parameters.
If you write any tests for the assignment please attach those along with the code


Requirements:
1. Accept over http json objects (counters). Counter
includes fields: 
{\"timestamp\": 1608102631, \"A\": 3, \"B\": 4}
"A" is domain and 3 is a number of requests domain got 
during 1608102631 (Unix epoch time)

2. System should be able to show statistic​:
    - Top 10 domains last round minute (with number of requests). If now 54min and 30 sec. We need stats for 53 minute.
    - Top 10 domains last hour (with number of requests)'''
to
'''
3. System should be able to show statistic​:
    - Top 10 domains last round minute (with number of requests). If now 54min and 30 sec. We need stats for 53 minute.
    - Top 10 domains last round hour (with number of requests). Same logic as above



Usage

Start Server

  • With docker
    Create empty directory postgres-data in project root, for postgres container

$ docker-compose up -d 

By default server ip is 10.5.0.3

  • Populate data with mock script

./mock.sh generated_token

(see “Security” section below for -token option)

Adding records

POST /events

┌──(kali㉿kali)-[~]
└─$ curl -X POST 'http://10.5.0.3:25478/events?token=f9403fc5f537b4ab332d' -d "{\"timestamp\": \"1636030745\", \"A\": 3, \"B\": 4, \"C\":8}"
{"ok":true,"path":"Succesfull colect"}               

Listing stats

  • By minutes:
    GET /stats/minutes

┌──(kali㉿kali)-[~]                     
└─$ curl 'http://10.5.0.3:25478/stats/minutes?token=4d22c7a2ef53cdf521e4'
{"ok":true,"message":"[{\"Domain\":\"C\",\"NumberOfRequests\":8},{\"Domain\":\"B\",\"NumberOfRequests\":4},{\"Domain\":\"A\",\"NumberOfRequests\":3}]"}
  • By hours
    GET /stats/hours

┌──(kali㉿kali)-[~]                         
└─$ curl 'http://10.5.0.3:25478/stats/hours?token=a2ba4d300cf35c0f97c0'     
{"ok":true,"message":"[{\"Domain\":\"V\",\"NumberOfRequests\":216},{\"Domain\":\"W\",\"NumberOfRequests\":205},{\"Domain\":\"L\",\"NumberOfRequests\":143},{\"Domain\":\"T\",\"NumberOfRequests\":116},{\"Domain\":\"U\",\"NumberOfRequests\":100},{\"Domain\":\"R\",\"NumberOfRequests\":99},{\"Domain\":\"S\",\"NumberOfReq
uests\":95},{\"Domain\":\"E\",\"NumberOfRequests\":90},{\"Domain\":\"I\",\"NumberOfRequests\":84},{\"Domain\":\"Z\",\"NumberOfRequests\":82}]"}   

TLS

To enable TLS support, add -cert and -key options:

$ ./stackpath-api -cert ./cert.pem -key ./key.pem
INFO[0000] starting up server
WARN[0000] token generated                               token=28d93c74c8589ab62b5e
INFO[0000] start listening TLS                           cert=./cert.pem key=./key.pem port=25443
INFO[0000] start listening                               ip=0.0.0.0 port=25478 token=28d93c74c8589ab62b5e
...

This server listens on 25443/tcp for TLS connections by default. This can be changed by passing -tlsport option.

NOTE: The endpoint using HTTP is still active even if TLS is enabled.

Security

Token

There is no Basic/Digest authentication. This app implements dead simple authentication: “security token”.

All requests should have token parameter (it can be passed as a query string or a form parameter). The server accepts the request only when the token is matched; otherwise, the server rejects the request and respond 401 Unauthorized.

You can specify the server’s token on startup by -token option. If you don’t so, the server generates the token and writes it to STDOUT at WARN level log, like as:

$ ./stackpath-api -token=f9403fc5f537b4ab332d 

INFO[0000] starting up server                           
INFO[0000] start listening              ip=0.0.0.0 port=25478 token=f9403fc5f537b4ab332d

NOTE: The token is generated from the random number, so it will change every time you start the server.

GitHub

View Github