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.
Tested with: PostgreSQL 12.2
Create a database:
sudo -u postgres psql
create database "database_name" owner "database_owner";
Build docker container:
docker build -t prediction_api .
Run the container:
docker run --network=host prediction_api
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');
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.
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"
}