This project provides a GraphQL API to query the last known position of Starlink satellites and find the closest satellite to a given position at a specific time. It utilizes a PostgreSQL database to store and retrieve the satellite data.
-
Start the Docker containers:
docker-compose up -d
-
Check the logs and find the message of the import script.
$ docker logs blueonion-import-script-1 INFO:__main__:Inserted 3141 records into the database. INFO:__main__:Database connection closed. blueonion-import-script-1 exited with code 0
-
Go to
localhost:5000/graphql
and query the available fields and perform queries. Here are some example queries:-
Query the last known position of a satellite:
query { lastKnownPosition(satelliteId: "2020-012AC") { id longitude latitude } }
-
Find the closest satellite to a given position at a specific time:
query { closestSatellite(latitude: 40.7128, longitude: -74.0060, timestamp: "2023-05-28T12:00:00Z") { id longitude latitude distance } }
-
The database
folder contains the following files:
-
import_data.py
: This Python script is responsible for importing the Starlink historical data into the PostgreSQL database. It connects to the database, reads the data from thestarlink_historical_data.json
file, and inserts it into thestarlink_positions
table. -
entrypoint/init.sql
: This SQL script is executed when the PostgreSQL container starts. It contains the necessary schema definition and table creation statements for thestarlink_positions
table. -
postgres.py
: This Python module defines thePostgreSQL
class, which handles the connection, disconnection, and querying of the PostgreSQL database. It provides methods for inserting starlink data, querying the last known position, and getting the closest satellite. -
starlink_historical_data.json
: This JSON file contains the historical data of Starlink satellites. It is read by theimport_data.py
script to populate the database.
The database
folder is responsible for managing the database-related functionality, including the PostgreSQL database connection, data import, and query operations.
The gql
folder contains the following files:
-
app.py
: This Python script is responsible for starting the Flask application and configuring the GraphQL endpoint. It creates a Flask app instance and registers the GraphQL endpoint using theschema
defined inschema.py
. -
schema.py
: This Python module defines the GraphQL schema using thegraphene
library. It includes the definition of theStarlinkPosition
andClosestSatellite
GraphQL object types, as well as theQuery
object type. TheQuery
object type defines the available queries, includinglastKnownPosition
andclosestSatellite
. Each query has its corresponding resolver method defined in theQuery
class.
The gql
folder is responsible for handling the GraphQL functionality of the application. It defines the GraphQL schema and provides the resolvers for the defined queries. The app.py
script starts the Flask application and exposes the GraphQL endpoint for querying the data.
The docker-compose.yaml
file defines three services: postgres
, import-script
, and graphql-api
. Here's a breakdown of each service:
-
postgres
: This service uses thepostgres:latest
image and maps the container's port 5432 to the host's port 5432. -
import-script
: This service builds an image using the Dockerfile in the current context. It depends on thepostgres
service, so it will start after thepostgres
service is up and running. Theenvironment
section inherits the environment variables from thepostgres
service using the<<: *postgres-common-env
notation. Theentrypoint
specifies the command to be executed when the container starts, which is running theimport_data.py
script. -
graphql-api
: This service also builds an image using the Dockerfile in the current context. It depends on theimport-script
service, so it will start after theimport-script
service is up and running. Theports
section maps the container's port 5000 to the host's port 5000, allowing access to the GraphQL API. Theenvironment
section inherits the environment variables from thepostgres
service.
- Implement a data retrieval handler to fetch the latest data from the API and update the PostgreSQL database accordingly.
- Create unit tests to ensure the correctness of the implemented functionality.
- Implement error handling and exception handling mechanisms to handle potential errors and exceptions gracefully.
- Add logging statements or integrate a logging framework to log important events and error messages.
- Implement a periodic task or a scheduler to automatically fetch and update the data from the API at regular intervals.
- Consider implementing caching mechanisms to improve performance and reduce the number of API calls.