Inventory management and demand forecast
Track inventory levels and forecast demand to prevent shortages and optimize restocking schedules.
Overview
In fast-moving industries, monitoring inventory levels in real-time is essential to ensuring smooth and successful operations. There are countless factors that affect the supply chain: customer preferences shift, raw materials may suddenly become hard to obtain, and unforeseen circumstances can delay shipments.
Having a live view of stock levels allows companies to respond immediately to changes in demand and supply chain disruptions. With data constantly streamed in, businesses can adjust forecasts based on current sales trends. If delays occur, notifications can be promptly sent to customers, improving transparency higher customer satisfaction.
In this tutorial, you will learn how to utilize inventory and sales data to prevent stock shortages and forecast sales demand.
Prerequisites
- Ensure that the PostgreSQL interactive terminal,
psql
, is installed in your environment. For detailed instructions, see Download PostgreSQL. - Install and run RisingWave. For detailed instructions on how to quickly get started, see the Quick start guide.
- Ensure that a Python environment is set up and install the psycopg2 library.
Step 1: Set up the data source tables
Once RisingWave is installed and deployed, run the two SQL queries below to set up the tables. You will insert data into these tables to simulate live data streams.
-
The table
inventory
tracks the current stock levels of each product at each warehouse. -
The table
sales
describes the details of each transaction, such as the quantity purchased and the warehouse from which the item was sourced.
Step 2: Run the data generator
To keep this demo simple, a Python script is used to generate and insert data into the tables created above.
Clone the awesome-stream-processing repository.
Navigate to the warehouse_inventory_mgmt folder.
Run the data_generator.py
file. This Python script utilizes the psycopg2
library to establish a connection with RisingWave so you can generate and insert synthetic data into the tables positions
and market_data
.
If you are not running RisingWave locally or using default credentials, update the connection parameters accordingly:
Step 3: Create materialized views
In this demo, you will create three materialized views to manage inventory levels.
Materialized views contain the results of a view expression and are stored in the RisingWave database. The results of a materialized view are computed incrementally and updated whenever new events arrive and do not require to be refreshed. When you query from a materialized view, it will return the most up-to-date computation results.
Monitor inventory status
The inventory_status
materialized view indicates whether or not a product needs to be restocked.
You can query from inventory_status
to see the results.
Aggregate recent sales
The recent_sales
materialized view calculates the number of products sold from each warehouse within the past week. By understanding recent sale trends, you can forecast demand.
A temporal filter, timestamp > NOW() - INTERVAL '7 days'
is used to retrieve sales made within the past week. To learn more about temporal filters, see Temporal filters.
You can query from recent_sales
to see the results.
Forecast demand
The demand_forecast
materialized view predicts how long the current stock of each product will last based on recent sales trends.
A simple model is used to forecase demand, where the stock_level
found in inventory_status
is divided by the total_quantity_sold
in `recent_sales.
RisingWave supports creating materialized views on top of materialized views. When the source materialized view updates, the child materialized view will update accordingly as well.
You can query from demand_forecast
to see the results.
When finished, press Ctrl+C
to close the connection between RisingWave and psycopg2
.
Summary
In this tutorial, you learn:
- How to use temporal filters to retrieve data within a specific time range.
- How to create materialized views based on materialized views.
Was this page helpful?