Skip to content

Instantly share code, notes, and snippets.

@Th0rgal
Created July 30, 2023 15:47
Show Gist options
  • Save Th0rgal/5f2e78b4c01eff749c8928dd4964b917 to your computer and use it in GitHub Desktop.
Save Th0rgal/5f2e78b4c01eff749c8928dd4964b917 to your computer and use it in GitHub Desktop.
You are an experienced data scientist used to working with matplotlib in a jupyter notebook environment. I want to generate a Python code snippet using the `matplotlib` and `numpy` libraries to visualize specific data from our StarknetID MongoDB database. I need your help to create a chart that showcase data in a meaningful and human friendly way.
Here's a bit of context: we're a decentralized naming service similar to ENS, and we use this database to store information about domain sales. Each document in our "sales" collection contains the following fields:
"domain": the name of the domain that was sold
"type": the type of the sale (can be "purchase" or "renewal")
"price": the price paid by the buyer in ether wei
"timestamp": the time when the sale happened, represented in seconds
"duration": the duration of the domain rent, in seconds
"auto": a boolean indicating whether the domain was purchased automatically (true if the user activated the automatic renewal feature)
"sponsor": the address of a referral (if any) who helped us make the sale
"sponsor_comm": the amount received by the sponsor for this sale, in ether wei
You will use the existing code base to fetch the sales data and convert necessary values for charting:
```py
from pymongo import MongoClient
import matplotlib.pyplot as plt
import requests
# Connection string
client = MongoClient(
"mongodb://apibara:password@127.0.0.1/?retryWrites=true&w=majority"
)
# Database and collection
db = client["sales"]
sales = db["sales"]
# Constants for conversion
WEI_TO_ETH = 1e18
SEC_TO_DAY = 86400
DAYS_IN_YEAR = 365
DAYS_IN_MONTH = 30
DAYS_10_MONTHS = 10 * DAYS_IN_MONTH
DAYS_14_MONTHS = 14 * DAYS_IN_MONTH
DAYS_3_YEARS = 3 * DAYS_IN_YEAR
# Get current ETH price
response = requests.get(
"https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd"
)
eth_price = response.json()["ethereum"]["usd"]
```
You can refer to the example given to you about generating a pie chart representing the revenue distribution between renewals and purchases for a similar task:
```py
# query
revenue_by_sale_type = list(
sales.aggregate(
[
{
"$group": {
"_id": "$type",
"total": {"$sum": {"$divide": ["$price", WEI_TO_ETH]}},
}
}
]
)
)
# chart
labels = [revenue_type['_id'] for revenue_type in revenue_by_sale_type]
sizes = [revenue_type['total'] for revenue_type in revenue_by_sale_type]
# autopct lambda function to include ETH and USD values
autopct_lambda = lambda pct: f"{pct:.1f}%\n({pct*sum(sizes)/100:.2f} ETH, ${(pct*sum(sizes)/100)*eth_price:.2f})"
plt.pie(sizes, labels=labels, autopct=autopct_lambda, startangle=140)
plt.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.title("Revenue distribution by sale type (in ETH and USD)")
plt.tight_layout()
```
Considering all the above, please generate Python code that produces a plot of daily net revenue over all the data available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment