connectiveproject / connective

Connective: The education marketplace
MIT License
4 stars 16 forks source link

Server | Google Maps Distance Calculator #42

Closed putskan closed 2 years ago

putskan commented 3 years ago

Feature Description

A function (management script) the calculates and saves the top 10 closest Activities for each School.

Why do we need this feature?

To be able to calculate distance between activities & schools. This helps recommending relevant activities to school staff.

What does the proposed solution look like? (Balsamiq / Technical Overview)

  1. First, we need to add lat, lan fields to School & Activity models (like Organization has). School Model can be found in schools/models.py, Activity inorganizations/models.py`.

  2. Add nearby_activities django's ManyToManyField to School model.

  3. Apply the changes to the DB using:

    python manage.py makemigrations
    python manage.py migrate
  4. Then, we need to create a management script file (similar to create_test_data.py script).

  5. The script can be ran the script using python manage.py <script_name> (e.g., python manage.py create_test_data)

  6. In the script we need to:

    • get all School & Activities and their location from DB

      
      from server.schools.models import School
      from server.organizations.models import Activity

    School.objects.all() Activity.objects.all()

    
    - For each school, get all the activities in 30km **air** distance. using something like [this](https://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula).
    - Get the closest activities from them based on **travel** distance. Use [Google Maps Distance API](https://developers.google.com/maps/documentation/distance-matrix/usage-and-billing#distance-matrix) for that purpose.
    - Populate the `nearby_activities` field for each school using the Activities found.
    * **Note:** we use air distance to narrow down the options and reduce time & costs.
    * **Note:** please don't spam Google API if not needed as it might get expensive.

How to test?

Pytest. use a mock for the API endpoint.

Blocked

Not blocked 🥇 talk to us to use our GCP account

Gitpod ready-to-code

putskan commented 3 years ago

Google Maps distance api pricing: