Technology stack and tools
- Ruby v >= 2.7.6
- Rails v >= 7.1.2
- Postgresql for database
- Redis
Implementation notes
- Data models
App has 2 data models - City (stores location data) and AirQualityLog (stores air quality data from open weather API) - Open weather APIs
External API integration follow a service-object design pattern. Current data, historical data and location details are all seperate services, can be found underapp/services - Initial data population is taken care in the seed file. It will create location records, fetch their co-ordinates from external API, fetch historic AQI data from external API all during setup.
- The importer is implemented in the
FetchAqiServiceand is called through theFetchAndUpdateAqiDataJobwhich is asynchronously run by sidekiq. For demo (sub 1 minute) and application rule that needs to re-run it every 1 hour a cron replication is done usingsidekiq-cron. To change, set or reset the importer frequency refer sidekiq-cron - The app's UI has 3 screens -
- An index page that lists all locations and their respective data.
- A location show page that lists historic pollution data for a location.
- A monthly average page for a location which shows the monthly average AQI. All the queries are covered in these pages.
- Tests are implemented in Rspec.
Setup
- Clone the repository and bundle.
- Get the
master.keyand add it in theapp/config/master.keyfile. Create the file if needed. - Create a database with
rails db:create. - Switch on rails server with
rails s. - Turn on a redis server (or a daemon).
- Turn on sidekiq server with command
sidekiq. - Seed the database using
rails db:seed. At this point, the app should greet you with an index page of locations onlocalhost:300. Further navigation is self explanatory. - To manage the cron job that fetches AQI data from external API, alter the job with
Sidekiq::Cron::Job. Refer titsidekiq-cron for commands.