GithubHelp home page GithubHelp logo

00mjk / sales-records-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from talented/sales-records-api

0.0 0.0 0.0 281 KB

Sales Records API endpoint with Django Rest Framework and Django Filters with Docker

Python 95.95% Dockerfile 4.05%

sales-records-api's Introduction

Description

This repo can be used as a reference when building advanced API filters with DRF and PostgreSQL.

The app is dockerized for easy installation. You need to run just a few commands to set it up. This is also a good example on how to configure Docker-Compose for a django app with postgreSQL database.

I've used an open dataset for 2000 Sales records of 12 product types. The original dataset consists of 50000 records, so please let me know if you want to test out with a bigger database.


First 10 records from the dataset

Screenshot

System Requirements

  • Docker
  • Docker Compose

Build instructions with Docker Compose

  1. Clone the repo
git clone https://github.com/talented/Sales-Records-API.git
  1. Run
cd Sales-Records-Api

docker-compose build

docker-compose up
  1. Without stopping the running server open a new tab in your terminal, get into the same directory and run commands below to populate the postgreSQL database in your running container with the data from "2000_Sales_Records.csv" under pgdata folder
docker cp init.sql sales-records-api_db_1:/docker-entrypoint-initdb.d/init.sql

docker exec -u postgres sales-records-api_db_1 psql postgres postgres -f docker-entrypoint-initdb.d/init.sql
  1. Check your browser that your API endpoint is ready at
localhost:8000/api/sales

API filters

GET {website}/api/sales display a DRF List API view. Available filter types are listed below:

Filter by Field name

  • for filtering by field names like country=Germany, region=Europe or same values for sale price like price=500

Filter by date

  • for exact date filtering like date=01.06.2017
  • date_to - # filter by date as "is_less_than_or_equal_to" in format "%d.%m.%Y"
  • date_from - # filter by date as "is_greater_than_or_equal_to" in format "%d.%m.%Y"

Filter by 'LESS THAN OR EQUAL TO' & 'GREATER THAN OR EQUAL TO'

  • Defining minimum and maximum value filters in a row for numeric fields ('quantity', 'price', 'cost', 'revenue', 'profit', 'profit_percentage') to be used in filtering as "is_less_than_or_equal_to" and "is_greater_than_or_equal_to"
  • Usage is as min_revenue=10000 or max_price

Ordering Filter

  • ordering - to order queryset in ascending or descending as ordering=quantity ASC or ordering=-quantity DESC

Grouping Filter

  • groupby - to group by one or more columns: date, region, country, ptype, channel (not numeric columns) with a result of total values of quantity, price, cost, revenue, profit and calculated profit_percentage of total revenue and total profit

Search Filter

  • search - to make a case insensitive search in values for all items in a queryset
  • Usage is as search=Meat or even search=Me. First search will return items with ptype=Meat, second one will also return items like region=North America

USE-CASE TESTS

  1. Show the number of quantity and total cost of sales before 1st of June 2015, broken down by channel and country, sorted by profit_percentage in descending order.
GET {website}/api/sales?groupby=country&groupby=channel&date_to=01.06.2015&ordering=-profit_percentage
  1. Show the total cost of products in May of 2017 on ptype, broken down by date, sorted by date in ascending order.
GET {website}/api/sales?groupby=ptype&groupby=date&date_from=01.03.2010&date_to=31.03.2010&ordering=date
  1. Show revenue, earned in 2015 in Europe region, broken down by country and sorted by revenue in descending order.
GET {website}/api/sales?region=Europe&groupby=country&date_from=01.01.2015&date_to=31.12.2015&ordering=-revenue
  1. Show profit percentage for Turkey broken down by channel ordered by profit percentage in descending order.
GET {website}/api/sales?country=Turkey&groupby=channel&ordering=-profit_percentage

TO DO

  • Dockerize the app
  • Deploy to AWS
  • Build a React or Vue Frontend to dynamically filter Sales data
  • Visualize filtered results with charts (D3.js can be used)

sales-records-api's People

Contributors

talented avatar

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.