-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_data.py
More file actions
196 lines (158 loc) · 7.75 KB
/
export_data.py
File metadata and controls
196 lines (158 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python3
"""Export Hummingbot volume data from Datadog for the past year."""
import os
import requests
import csv
import time
from datetime import datetime, timedelta
# Datadog credentials - set via environment variables
API_KEY = os.environ.get("DD_API_KEY", "")
APP_KEY = os.environ.get("DD_APP_KEY", "")
DATADOG_SITE = os.environ.get("DD_SITE", "datadoghq.com")
# Time range: 15 months ago (max Datadog retention) to now
end_time = int(time.time())
start_time = int((datetime.now() - timedelta(days=456)).timestamp()) # ~15 months ago
# Base URL
BASE_URL = f"https://api.{DATADOG_SITE}"
headers = {
"DD-API-KEY": API_KEY,
"DD-APPLICATION-KEY": APP_KEY,
"Content-Type": "application/json"
}
def query_metrics(query: str, from_ts: int, to_ts: int) -> dict:
"""Query Datadog metrics API."""
url = f"{BASE_URL}/api/v1/query"
params = {
"query": query,
"from": from_ts,
"to": to_ts
}
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
return response.json()
def extract_tag_value(scope: str, tag_name: str) -> str:
"""Extract the clean tag value from a Datadog scope string."""
# Scope looks like: "exchange:binance,!altmarkets,!kraken_perpetual,..."
# We want just "binance"
parts = scope.split(",")
for part in parts:
if part.startswith(f"{tag_name}:"):
return part.replace(f"{tag_name}:", "")
return scope
def export_volume_by_exchange():
"""Export daily volume by exchange connector."""
print("Fetching volume data by exchange...")
query = "sum:hummingbot.filled_usdt_volume{!exchange:*testnet,!exchange:altmarkets,!exchange:mcx_perpetual,!exchange:*sepolia,!exchange:lbank,!exchange:kraken_perpetual,!exchange:uae*,!exchange:yamata,!exchange:polymarket/ctf,!exchange:pancakeswap_binance-smart-chain_mainnet,!exchange:deribit_perpetual,!exchange:ekiden_perpetual} by {exchange}.rollup(sum, 86400)"
data = query_metrics(query, start_time, end_time)
rows = []
if "series" in data:
for series in data["series"]:
exchange = extract_tag_value(series.get("scope", "unknown"), "exchange")
pointlist = series.get("pointlist", [])
for point in pointlist:
timestamp_ms, value = point
if value is not None:
dt = datetime.fromtimestamp(timestamp_ms / 1000)
rows.append({
"date": dt.strftime("%Y-%m-%d"),
"exchange": exchange,
"volume_usdt": value
})
# Sort by date and exchange
rows.sort(key=lambda x: (x["date"], x["exchange"]))
# Write to CSV
output_file = "/Users/feng/fengtality/datadog/volume_by_exchange.csv"
with open(output_file, "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["date", "exchange", "volume_usdt"])
writer.writeheader()
writer.writerows(rows)
print(f"Exported {len(rows)} rows to {output_file}")
return rows
def export_volume_by_version():
"""Export daily volume by version."""
print("Fetching volume data by version...")
query = "sum:hummingbot.filled_usdt_volume{!exchange:*testnet,!exchange:altmarkets,!exchange:mcx_perpetual,!exchange:*sepolia,!exchange:lbank,!exchange:kraken_perpetual,!exchange:uae*,!exchange:yamata,!exchange:polymarket/ctf,!exchange:pancakeswap_binance-smart-chain_mainnet,!exchange:deribit_perpetual,!exchange:ekiden_perpetual} by {version}.rollup(sum, 86400)"
data = query_metrics(query, start_time, end_time)
rows = []
if "series" in data:
for series in data["series"]:
version = extract_tag_value(series.get("scope", "unknown"), "version")
pointlist = series.get("pointlist", [])
for point in pointlist:
timestamp_ms, value = point
if value is not None:
dt = datetime.fromtimestamp(timestamp_ms / 1000)
rows.append({
"date": dt.strftime("%Y-%m-%d"),
"version": version,
"volume_usdt": value
})
rows.sort(key=lambda x: (x["date"], x["version"]))
output_file = "/Users/feng/fengtality/datadog/volume_by_version.csv"
with open(output_file, "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["date", "version", "volume_usdt"])
writer.writeheader()
writer.writerows(rows)
print(f"Exported {len(rows)} rows to {output_file}")
return rows
def export_volume_by_instance():
"""Export daily volume by instance."""
print("Fetching volume data by instance...")
query = "sum:hummingbot.filled_usdt_volume{!exchange:*testnet,!exchange:altmarkets,!exchange:mcx_perpetual,!exchange:*sepolia,!exchange:lbank,!exchange:kraken_perpetual,!exchange:uae*,!exchange:yamata,!exchange:polymarket/ctf,!exchange:pancakeswap_binance-smart-chain_mainnet,!exchange:deribit_perpetual,!exchange:ekiden_perpetual} by {instance_id}.rollup(sum, 86400)"
data = query_metrics(query, start_time, end_time)
rows = []
if "series" in data:
for series in data["series"]:
instance_id = extract_tag_value(series.get("scope", "unknown"), "instance_id")
pointlist = series.get("pointlist", [])
for point in pointlist:
timestamp_ms, value = point
if value is not None:
dt = datetime.fromtimestamp(timestamp_ms / 1000)
rows.append({
"date": dt.strftime("%Y-%m-%d"),
"instance_id": instance_id,
"volume_usdt": value
})
rows.sort(key=lambda x: (x["date"], x["instance_id"]))
output_file = "/Users/feng/fengtality/datadog/volume_by_instance.csv"
with open(output_file, "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["date", "instance_id", "volume_usdt"])
writer.writeheader()
writer.writerows(rows)
print(f"Exported {len(rows)} rows to {output_file}")
return rows
def export_total_daily_volume():
"""Export total daily volume."""
print("Fetching total daily volume...")
query = "sum:hummingbot.filled_usdt_volume{!exchange:*testnet,!exchange:altmarkets,!exchange:mcx_perpetual,!exchange:*sepolia,!exchange:lbank,!exchange:kraken_perpetual,!exchange:uae*,!exchange:yamata,!exchange:polymarket/ctf,!exchange:pancakeswap_binance-smart-chain_mainnet,!exchange:deribit_perpetual,!exchange:ekiden_perpetual}.rollup(sum, 86400)"
data = query_metrics(query, start_time, end_time)
rows = []
if "series" in data:
for series in data["series"]:
pointlist = series.get("pointlist", [])
for point in pointlist:
timestamp_ms, value = point
if value is not None:
dt = datetime.fromtimestamp(timestamp_ms / 1000)
rows.append({
"date": dt.strftime("%Y-%m-%d"),
"total_volume_usdt": value
})
rows.sort(key=lambda x: x["date"])
output_file = "/Users/feng/fengtality/datadog/total_daily_volume.csv"
with open(output_file, "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["date", "total_volume_usdt"])
writer.writeheader()
writer.writerows(rows)
print(f"Exported {len(rows)} rows to {output_file}")
return rows
if __name__ == "__main__":
print(f"Exporting data from {datetime.fromtimestamp(start_time)} to {datetime.fromtimestamp(end_time)}")
print("-" * 50)
export_total_daily_volume()
export_volume_by_exchange()
export_volume_by_version()
export_volume_by_instance()
print("-" * 50)
print("Export complete!")