This microservice API sorts and tracks a person's flight path from unordered flight records. It accepts a list of flights defined by source and destination airport codes, sorts them, and finds the total flight path from starting to ending airports.
POST http://localhost:8080/calculate
The request body should be a JSON object containing an array of flight segments. Each segment is an array with two elements: the source and destination airport codes.
Example:
{
"flights": [
["IND", "EWR"],
["SFO", "ATL"],
["GSO", "IND"],
["ATL", "GSO"]
]
}
The response will be a JSON object containing the ordered flight path from the starting airport to the ending airport.
Example:
{
"path": ["SFO", "EWR"]
}
-
400 Bad Request: Invalid input
{ "error": "Invalid input: The request body must contain a \"flights\" array of arrays, where each sub-array represents a flight with a source and destination airport code. Example: { \"flights\": [[\"SFO\", \"EWR\"], [\"ATL\", \"EWR\"]] }" }
-
500 Internal Server Error:
- No unique starting point
{ "error": "Invalid flight data: no unique starting point found." }
- Cycle detected
{ "error": "Cycle detected in the flight path." }
- Disjoint or unconnected flights
{ "error": "Invalid flight data: disjoint or unconnected flights detected." }
- No unique starting point
-
Clone the repository:
git clone https://github.com/dattgoswami/flight-path-service cd flight-path-service
-
Install dependencies:
npm install
-
Create
.env
file:touch .env echo "PORT=8080" >> .env
-
Start the server:
npm start
-
Run in development mode:
npm run dev
-
Run tests:
npm test
-
Lint the code:
npm run lint
Files and directories:
flight-path-service/
├── index.js
├── controllers/
│ └── flightController.js
├── routes/
│ └── flightRoutes.js
├── middleware/
│ ├── errorHandler.js
│ └── logger.js
├── tests/
| └── flightRoutes.test.js
├── .env //to be created from .env.example
├── server.log
├── package.json
└── README.md
- It can be hosted on AWS Lambda; provisioned concurrency can be used to pre-initialize execution environments to respond to incoming requests meediately(i.e. reducing cold starts)
- With a trigger from API Gateway set on this Lambda
- It can be made asynchronous by decoupling the API Gateway and Lambda using AWS SQS(queue) in between
The following 10 test cases validate the calculateFlightPath
function thoroughly:
-
Valid flight path:
- Confirms the correct ordering of a simple set of flights.
-
Valid flight path with multiple segments:
- Ensures the function handles longer flight paths correctly.
-
No unique starting point:
- Detects when there is no unique starting point in the flight data.
-
Single flight:
- Handles a case with only one flight segment.
-
Empty input:
- Handles empty flight data gracefully.
-
Invalid input (not an array):
- Ensures the input must be an array of flight segments.
-
Invalid input (malformed flight data):
- Validates the structure of each flight segment.
-
Unconnected flight:
- Detects if there are any unconnected flights.
-
Disjoint flights:
- Identifies when flights are not connected to form a single path.
-
Cycle detected:
- Checks for cycles in the flight path.
The Flight Path Microservice efficiently handles unordered flight records to provide a sorted flight path. By following the setup instructions and utilizing the error handling, the service ensures robust performance and accurate results.