Spaces:
Sleeping
Sleeping
File size: 4,676 Bytes
04858f5 | 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 | #!/usr/bin/env python3
"""
Quick API diagnostic script to check if data is being returned
"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
import requests
import json
from pprint import pprint
API_BASE = "http://localhost:8000"
HEADERS = {"X-API-Key": "changeme"}
def test_health():
print("=" * 60)
print("1. Testing /health endpoint")
print("=" * 60)
try:
r = requests.get(f"{API_BASE}/health", headers=HEADERS, timeout=3)
print(f"Status: {r.status_code}")
pprint(r.json())
except Exception as e:
print(f"β Error: {e}")
print()
def test_prices():
print("=" * 60)
print("2. Testing /prices/AAPL endpoint")
print("=" * 60)
try:
r = requests.get(f"{API_BASE}/prices/AAPL", headers=HEADERS, timeout=3)
print(f"Status: {r.status_code}")
if r.status_code == 200:
print("Response structure:")
pprint(r.json(), depth=2)
else:
print(f"Error: {r.text}")
except Exception as e:
print(f"β Error: {e}")
print()
def test_prices_history():
print("=" * 60)
print("3. Testing /prices/AAPL/history endpoint")
print("=" * 60)
try:
r = requests.get(f"{API_BASE}/prices/AAPL/history",
params={"limit": 5}, headers=HEADERS, timeout=3)
print(f"Status: {r.status_code}")
if r.status_code == 200:
print("Response structure:")
data = r.json()
print(f"Type: {type(data)}")
if isinstance(data, dict):
print("Keys:", list(data.keys())[:3])
elif isinstance(data, list):
print(f"List length: {len(data)}")
pprint(data, depth=2)
else:
print(f"Error: {r.text}")
except Exception as e:
print(f"β Error: {e}")
print()
def test_anomalies():
print("=" * 60)
print("4. Testing /anomalies endpoint")
print("=" * 60)
try:
r = requests.get(f"{API_BASE}/anomalies",
params={"ticker": "AAPL", "days": 30},
headers=HEADERS, timeout=3)
print(f"Status: {r.status_code}")
if r.status_code == 200:
print("Response structure:")
pprint(r.json(), depth=2)
else:
print(f"Error: {r.text}")
except Exception as e:
print(f"β Error: {e}")
print()
def test_sentiment():
print("=" * 60)
print("5. Testing /sentiment/timeline endpoint")
print("=" * 60)
try:
r = requests.get(f"{API_BASE}/sentiment/timeline",
params={"ticker": "AAPL", "days": 30},
headers=HEADERS, timeout=3)
print(f"Status: {r.status_code}")
if r.status_code == 200:
print("Response structure:")
pprint(r.json(), depth=2)
else:
print(f"Error: {r.text}")
except Exception as e:
print(f"β Error: {e}")
print()
def test_database():
print("=" * 60)
print("6. Testing Database Connection")
print("=" * 60)
try:
from database.connection import test_connection
result = test_connection()
print(f"Database connected: {result}")
except Exception as e:
print(f"β Error: {e}")
print()
def check_database_data():
print("=" * 60)
print("7. Checking Database Data")
print("=" * 60)
try:
from database.connection import get_session
with get_session() as session:
from database.models import MarketData, NewsSentiment, Anomaly, Forecast
market_count = session.query(MarketData).count()
sentiment_count = session.query(NewsSentiment).count()
anomaly_count = session.query(Anomaly).count()
forecast_count = session.query(Forecast).count()
print(f"MarketData rows: {market_count}")
print(f"NewsSentiment rows: {sentiment_count}")
print(f"Anomaly rows: {anomaly_count}")
print(f"Forecast rows: {forecast_count}")
if market_count > 0:
sample = session.query(MarketData).first()
print(f"\nSample MarketData: {sample.__dict__ if sample else 'None'}")
except Exception as e:
print(f"β Error: {e}")
print()
if __name__ == "__main__":
test_health()
test_database()
check_database_data()
test_prices()
test_prices_history()
test_anomalies()
test_sentiment()
print("=" * 60)
print("Diagnostic complete!")
print("=" * 60)
|