During a simulation, the producer and the marketplace are constantly logging sales and the activity on the market to Kafka. These information are organised in topics. In order to estimate customer demand and predict good prices, merchants can use the Kafka API to access this data.
The merchants gets the data in form of a pandas DataFrame.
If you want to try the following examples, make sure that the Pricewars plattform is running. Either by deploying them individually or by using the docker setup.
The following step is specific for this notebook. It is not necessary if your merchant is in the repository root.
In [1]:
import sys
sys.path.append('../')
You need a merchant token to use the Kafka API. To get one, register the merchant at the marketplace.
In [6]:
from api import Marketplace
marketplace = Marketplace()
registration = marketplace.register(
'http://nobody:55000/',
merchant_name='kafka_notebook_merchant',
algorithm_name='human')
registration
Out[6]:
It was not possible to connect to the marketplace if you got the following error:
ConnectionError: HTTPConnectionPool(host='marketplace', port=8080)
In that case, make sure that the marketplace is running and host and port are correct. If host or port are wrong, you can change it by creating a marketplace object with the host argument:
marketplace = Marketplace(host='www.another_host.com:1234')
Same is true for the upcoming Kafka API
Next, initialize the Kafka API:
In [9]:
from api import Kafka
kafka = Kafka(token=registration.merchant_token)
You can request data for specific topics. The most important topics are buyOffer which contains your own sales and marketSituation which contains a history of market situations.
The call will return the data in form of a pandas DataFrame.
Depending on how active the simulation is and how much data is logged, this can take some time.
In [15]:
sales_data = kafka.download_topic_data('buyOffer')
sales_data.head()
Out[15]:
This method may return None if it was not possible to obtain the data. For example, this happens if the merchant doesn't have any sales.
In [16]:
len(sales_data)
Out[16]:
In [18]:
market_situations = kafka.download_topic_data('marketSituation')
print(len(market_situations))
market_situations.head()
Out[18]: