Spaces:
Runtime error
Runtime error
gauravlochab
commited on
Commit
·
e4a3f8e
1
Parent(s):
6aa0acf
Add week-over-week transaction metrics dashboard
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
# Function to fetch and process the transaction data from the API
|
| 8 |
+
def fetch_transactions():
|
| 9 |
+
url = "https://li.quest/v1/analytics/transfers?integrator=valory"
|
| 10 |
+
headers = {"accept": "application/json"}
|
| 11 |
+
response = requests.get(url, headers=headers)
|
| 12 |
+
return response.json()
|
| 13 |
+
|
| 14 |
+
# Function to parse the transaction data and prepare it for visualization
|
| 15 |
+
def process_transactions(data):
|
| 16 |
+
transactions = data["transfers"]
|
| 17 |
+
|
| 18 |
+
# Convert the data into a pandas DataFrame for easy manipulation
|
| 19 |
+
rows = []
|
| 20 |
+
for tx in transactions:
|
| 21 |
+
rows.append({
|
| 22 |
+
"transactionId": tx["transactionId"],
|
| 23 |
+
"from": tx["fromAddress"],
|
| 24 |
+
"to": tx["toAddress"],
|
| 25 |
+
"token_symbol": tx["sending"]["token"]["symbol"],
|
| 26 |
+
"amount": float(tx["sending"]["amount"]) / (10 ** tx["sending"]["token"]["decimals"]), # Normalize the amount
|
| 27 |
+
"amount_usd": float(tx["sending"]["amountUSD"]),
|
| 28 |
+
"gas_used": tx["sending"]["gasUsed"],
|
| 29 |
+
"timestamp": datetime.utcfromtimestamp(tx["sending"]["timestamp"]).strftime('%Y-%m-%d %H:%M:%S'),
|
| 30 |
+
"week": pd.to_datetime(datetime.utcfromtimestamp(tx["sending"]["timestamp"])).strftime('%Y-%W') # Group by week
|
| 31 |
+
})
|
| 32 |
+
|
| 33 |
+
df = pd.DataFrame(rows)
|
| 34 |
+
return df
|
| 35 |
+
|
| 36 |
+
# Function to create week-over-week visualizations
|
| 37 |
+
def create_wow_visualizations():
|
| 38 |
+
transactions_data = fetch_transactions()
|
| 39 |
+
df = process_transactions(transactions_data)
|
| 40 |
+
|
| 41 |
+
# Aggregate the data by week
|
| 42 |
+
weekly_data = df.groupby("week").agg({
|
| 43 |
+
"amount_usd": "sum", # Total USD amount
|
| 44 |
+
"gas_used": "sum" # Total gas used
|
| 45 |
+
}).reset_index()
|
| 46 |
+
|
| 47 |
+
# Plot: Total Transaction Amounts by Week
|
| 48 |
+
fig_amount = px.bar(weekly_data, x="week", y="amount_usd", title="Weekly Transaction Amounts in USD")
|
| 49 |
+
|
| 50 |
+
# Plot: Total Gas Used by Week
|
| 51 |
+
fig_gas = px.line(weekly_data, x="week", y="gas_used", title="Weekly Gas Used")
|
| 52 |
+
|
| 53 |
+
return fig_amount, fig_gas
|
| 54 |
+
|
| 55 |
+
# Gradio interface
|
| 56 |
+
def dashboard():
|
| 57 |
+
with gr.Blocks() as demo:
|
| 58 |
+
gr.Markdown("# Valory Transactions Dashboard (Week-over-Week)")
|
| 59 |
+
|
| 60 |
+
# Fetch and display visualizations
|
| 61 |
+
fig_amount, fig_gas = create_wow_visualizations()
|
| 62 |
+
gr.Plot(fig_amount)
|
| 63 |
+
gr.Plot(fig_gas)
|
| 64 |
+
|
| 65 |
+
return demo
|
| 66 |
+
|
| 67 |
+
# Launch the dashboard
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
dashboard().launch()
|