Giter VIP home page Giter VIP logo

ml-congestion-prediction's Introduction

Prediction API

This is a REST API built on top of fbprophet for forecasting time-series data. It is used by the Congestion Tracing API to predict future congestion.

Installation

Postgres database

Tested with: PostgreSQL 12.2

Create a database:

sudo -u postgres psql
create database "database_name" owner "database_owner";

Run the API in a docker container

Build docker container:

docker build -t prediction_api .

Run the container:

docker run --network=host prediction_api

API specification

Authorization

All API requests require the use of an API key. To authenticate an API request, you should append your API key as a URL parameter.

POST /api/prediction/?key=1234567890

Note: for security reasons there is NO default api key added into the database. For testing the API, a hashed key must be manually added, after running the container, as a SHA3_256 encoded string into the company table.
The hash can be obtained easily from here.
Example adding the SHA3_256 encoded hash of 1234567890 key in the database:

insert into company values(1, 'your_company_name', '01da8843e976913aa5c15a62d45f1c9267391dcbd0a76ad411919043f374a163');

Endpoints

Make a prediction

To make a prediction a POST request is needed containing the time-series data as JSON in the request body and the prediction date as a url parameter.
Latitude, longitude and radius are required and used just for caching the prediction model and uniquely identifying the saved cache to be retrieved later.

POST /api/prediction/?key=1234567890&prediction_date=2020-08-02T20:07:31Z&lat=44.4133671&lon=26.1630280&radius=10.0
  • URL Params

    Required:
    key=[string] - Authorization key.
    prediction_date=[timestamp] - A future timestamp for which you want the prediction to be made. The timestamp must use the ISO 8601 standard.
    lat=[numeric] - Latitude.
    lon=[numeric] - Longitude.
    radius=[numeric] - Radius.

  • Request body

    [
        {
            "timestamp": "2020-08-02T20:07:00Z",
            "value": 1
        },
        {
            "timestamp": "2020-08-02T20:07:30Z",
            "value": 4
        }
    ]
  • Sample call:

    curl --request POST 'http://localhost:5000/api/prediction?key=1234567890&prediction_date=2020-08-19T17:07:33.478200Z&lat=44.4133671&lon=26.1630280&radius=10.0' \
    --header 'Content-Type: application/json' \
    --data-raw '[
        {
            "timestamp": "2020-08-02T20:07:00Z",
            "value": 1
        },
        {
            "timestamp": "2020-08-02T20:07:30Z",
            "value": 4
        }
    ]'

    Success Response:

    Code: 200 OK
    Content:

       {
           "predicted_value": "4.041208956969186",
           "predicted_date": "2020-08-19T17:07:33.478200Z"
       }

    predicted_value - is the value predicted by the machine learning algorithm trained on data from the request body.
    predicted_date - represents the future date for which the prediction has been made.

Error Responses

Code: 400 BAD REQUEST
Content:

{
    "timestamp": "2020-08-22T13:21:05.045562Z",
    "status": 403,
    "error": "Bad Request",
    "message": "The client sent a request that this server could not understand.",
    "path": "/api/prediction"
}

Code: 403 FORBIDDEN
Content:

{
    "timestamp": "2020-08-22T13:21:05.045562Z",
    "status": 403,
    "error": "Forbidden",
    "message": "",
    "path": "/api/prediction"
}

Code: 404 NOT FOUND
Content:

{
    "timestamp": "2020-08-23T16:32:22.854640Z",
    "status": 404,
    "error": "Not Found",
    "message": "The requested URL was not found on the server.",
    "path": "/api/a-wrong-path"
}

Code: 500 INTERNAL SERVER ERROR
Content:

{
    "timestamp": "2020-08-22T13:21:05.045562Z",
    "status": 500,
    "error": "Internal Server Error",
    "message": "Oops! Something went wrong on our side.",
    "path": "/api/prediction"
}

ml-congestion-prediction's People

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.