We monitor several restaurants in the US and need to monitor if the store is online or not. All restaurants are supposed to be online during their business hours. Due to some unknown reasons, a store might go inactive for a few hours. Restaurant owners want to get a report of the how often this happened in the past.
We want to build backend APIs that will help restaurant owners achieve this goal.
We will have 3 sources of data
We poll every store roughly every hour and have data about whether the store was active or not in a CSV. The CSV has 3 columns (store_id, timestamp_utc, status
) where status is active or inactive. All timestamps are in UTC
a. Data can be found in CSV format here
We have the business hours of all the stores - schema of this data is store_id, dayOfWeek(0=Monday, 6=Sunday), start_time_local, end_time_local
a. These times are in the local time zone
b. If data is missing for a store, assume it is open 24*7
c. Data can be found in CSV format here
Timezone for the stores - schema is store_id, timezone_str
a. If data is missing for a store, assume it is America/Chicago
b. This is used so that data sources 1 and 2 can be compared against each other.
c. Data can be found in CSV format here
There are two API endpoints -
/trigger_report endpoint that will trigger report generation from the data provided (stored in DB)-\ a. No input\ b. Output - report_id\ c. report_id will be used for polling the status of report completion
/get_report endpoint that will return the status of the report or the csv-\ a. Input - report_id\ b. Output
Make sure you have Python installed on your system. Refer to this link to download latest version of Python.
Make sure to have Django installed on your system before running this program. Refer to this link to install latest version of Django.
python3 -m venv myenv
source myenv/bin/activate
git clone https://github.com/Apoorvg2000/store_monitoring_loop.git
NOTE: Make sure to run below commands from the parent directory of manage.py file.
pip install -r requirements.txt
Setup a Postgresql database, and change the database credentials in the main/settings.py
file. Refer to this link to setup a PostgreSQL database.
To populate the database with the data in .csv files in scripts/data
directory, run the below command -
python3 manage.py runscript load
This will populate the database with all the required data before running the server.
python3 manage.py runserver
Now open your web browser and enter the following address to launch the web app http://127.0.0.1:8000/
Trigger Report
button on the webpage. This will trigger the /trigger_report
API endpoint and store the generated report in the database as well as in your local computer. The output will be a report_id
using which you can view the generated report.NOTE: We are generating report for only the first 100 stores as it will take a lot of time to generate the report for all the stores. We can change that number anytime in the code.
report_id
and hit Get Report
button. This will show the generated report and its url on the local computer, on the webpage if it is completed with its Status
as "Completed", otherwise its Status
will be shown as "Running".Refer to this link to watch the demo video.