Giter VIP home page Giter VIP logo

kerala-private-bus-timing-api's Introduction

kerala-private-bus-timing-api's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

kerala-private-bus-timing-api's Issues

Use self join to retrieve schedules

We are currently looping over the schedules to fetch the required data.

Fetching data in a loop results in n+1 queries. It will slow down the application gradually as the database size increases.
Ref: https://guides.rubyonrails.org/active_record_querying.html#n-1-queries-problem,
https://www.bigbinary.com/blog/preload-vs-eager-load-vs-joins-vs-includes

The image below shows the number of database queries generated.
image

The n+1 query issue can be fixed by using a self-join query to retrieve all the possible bus schedules.

  arrival_time = "06:35 am"

  stations = Station.select("s1.name departure_station, s2.name destination_station, s1.schedule_id schedule_id, s1.departure_time departure_time, s2.arrival_time arrival_time")
    .joins("s1, stations s2")
    .preload(schedule: { route: :bus_schedule })
    .where("s1.schedule_id=s2.schedule_id and s1.name ILIKE ? and s2.name ILIKE ? and s1.arrival_time > ?",
      "%#{params[:departure]}%", "%#{params[:destination]}%", arrival_time)
    .order("s1.arrival_time ASC")

The above code will fire only 4 database queries independent of the database size.

image

Use query params instead of path params in schedules controller

It's better to use query params instead of using dynamic segments in the route.

get 'schedules/:departure/:destination', to: 'schedules#index'

The final code will look like:

resources :schedules, only: :index

Expected URL will be like:
https://localhost/api/v1/schedules?departure=From&destination=To

destination_station = params[:destination]&.upcase&.gsub('_', ' ')

Also, let's URL encode the parameters rather than using an underscore instead of space, rails will automatically decode it.
(URL encoding will be handled by browser/axios automatically)

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.