Spaces:
Sleeping
Sleeping
Create troubleshooting.py
Browse files- troubleshooting.py +186 -0
troubleshooting.py
ADDED
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Get API credentials from environment variables
|
6 |
+
EPA_AQS_API_BASE_URL = "https://aqs.epa.gov/data/api"
|
7 |
+
EMAIL = os.environ.get("EPA_AQS_EMAIL", "")
|
8 |
+
API_KEY = os.environ.get("EPA_AQS_API_KEY", "")
|
9 |
+
|
10 |
+
def print_separator():
|
11 |
+
print("=" * 50)
|
12 |
+
|
13 |
+
def test_api_connection():
|
14 |
+
"""Test the basic API connectivity and credentials"""
|
15 |
+
print_separator()
|
16 |
+
print("TESTING API CONNECTION")
|
17 |
+
print(f"Using email: {EMAIL}")
|
18 |
+
print(f"API key provided: {'Yes' if API_KEY else 'No'}")
|
19 |
+
|
20 |
+
if not EMAIL or not API_KEY:
|
21 |
+
print("WARNING: Email or API key is missing. Real API calls will fail.")
|
22 |
+
return False
|
23 |
+
|
24 |
+
# Test with a simple API call
|
25 |
+
endpoint = f"{EPA_AQS_API_BASE_URL}/list/states"
|
26 |
+
params = {
|
27 |
+
"email": EMAIL,
|
28 |
+
"key": API_KEY
|
29 |
+
}
|
30 |
+
|
31 |
+
try:
|
32 |
+
response = requests.get(endpoint, params=params)
|
33 |
+
print(f"Response status code: {response.status_code}")
|
34 |
+
data = response.json()
|
35 |
+
print(f"Response header: {data.get('Header', {})}")
|
36 |
+
|
37 |
+
if data.get("Header", {}).get("status") == "Success":
|
38 |
+
print("API connection successful!")
|
39 |
+
return True
|
40 |
+
else:
|
41 |
+
print(f"API connection failed: {data.get('Header', {}).get('error', 'Unknown error')}")
|
42 |
+
return False
|
43 |
+
except Exception as e:
|
44 |
+
print(f"Exception during API connection test: {e}")
|
45 |
+
return False
|
46 |
+
|
47 |
+
def test_state_lookup(state_code):
|
48 |
+
"""Test looking up a specific state"""
|
49 |
+
print_separator()
|
50 |
+
print(f"TESTING STATE LOOKUP FOR: {state_code}")
|
51 |
+
|
52 |
+
endpoint = f"{EPA_AQS_API_BASE_URL}/list/states"
|
53 |
+
params = {
|
54 |
+
"email": EMAIL,
|
55 |
+
"key": API_KEY
|
56 |
+
}
|
57 |
+
|
58 |
+
try:
|
59 |
+
response = requests.get(endpoint, params=params)
|
60 |
+
data = response.json()
|
61 |
+
|
62 |
+
if data.get("Header", {}).get("status") == "Success":
|
63 |
+
states = data.get("Data", [])
|
64 |
+
print(f"Found {len(states)} states in the API")
|
65 |
+
|
66 |
+
# Look for the specific state
|
67 |
+
matching_states = [s for s in states if s.get("state_code") == state_code or s.get("value") == state_code]
|
68 |
+
if matching_states:
|
69 |
+
print(f"Found matching state: {matching_states[0]}")
|
70 |
+
return matching_states[0]
|
71 |
+
else:
|
72 |
+
print(f"No matching state found for code: {state_code}")
|
73 |
+
print("Available state codes:")
|
74 |
+
for s in states[:10]: # Print first 10 for brevity
|
75 |
+
print(f" {s.get('state_code')} - {s.get('value')}")
|
76 |
+
if len(states) > 10:
|
77 |
+
print(f" ... and {len(states)-10} more")
|
78 |
+
return None
|
79 |
+
else:
|
80 |
+
print(f"API lookup failed: {data.get('Header', {}).get('error', 'Unknown error')}")
|
81 |
+
return None
|
82 |
+
except Exception as e:
|
83 |
+
print(f"Exception during state lookup: {e}")
|
84 |
+
return None
|
85 |
+
|
86 |
+
def test_counties_lookup(state_code):
|
87 |
+
"""Test looking up counties for a state"""
|
88 |
+
print_separator()
|
89 |
+
print(f"TESTING COUNTIES LOOKUP FOR STATE: {state_code}")
|
90 |
+
|
91 |
+
endpoint = f"{EPA_AQS_API_BASE_URL}/list/countiesByState"
|
92 |
+
params = {
|
93 |
+
"email": EMAIL,
|
94 |
+
"key": API_KEY,
|
95 |
+
"state": state_code
|
96 |
+
}
|
97 |
+
|
98 |
+
try:
|
99 |
+
response = requests.get(endpoint, params=params)
|
100 |
+
data = response.json()
|
101 |
+
|
102 |
+
if data.get("Header", {}).get("status") == "Success":
|
103 |
+
counties = data.get("Data", [])
|
104 |
+
print(f"Found {len(counties)} counties for state {state_code}")
|
105 |
+
|
106 |
+
if counties:
|
107 |
+
print("Sample counties:")
|
108 |
+
for c in counties[:5]: # Print first 5 for brevity
|
109 |
+
print(f" {c}")
|
110 |
+
if len(counties) > 5:
|
111 |
+
print(f" ... and {len(counties)-5} more")
|
112 |
+
else:
|
113 |
+
print("No counties found for this state")
|
114 |
+
|
115 |
+
return counties
|
116 |
+
else:
|
117 |
+
print(f"API lookup failed: {data.get('Header', {}).get('error', 'Unknown error')}")
|
118 |
+
return None
|
119 |
+
except Exception as e:
|
120 |
+
print(f"Exception during counties lookup: {e}")
|
121 |
+
return None
|
122 |
+
|
123 |
+
def test_monitors_lookup(state_code):
|
124 |
+
"""Test looking up monitors for a state"""
|
125 |
+
print_separator()
|
126 |
+
print(f"TESTING MONITORS LOOKUP FOR STATE: {state_code}")
|
127 |
+
|
128 |
+
endpoint = f"{EPA_AQS_API_BASE_URL}/monitors/byState"
|
129 |
+
params = {
|
130 |
+
"email": EMAIL,
|
131 |
+
"key": API_KEY,
|
132 |
+
"state": state_code,
|
133 |
+
"bdate": "20240101",
|
134 |
+
"edate": "20240414"
|
135 |
+
}
|
136 |
+
|
137 |
+
try:
|
138 |
+
response = requests.get(endpoint, params=params)
|
139 |
+
data = response.json()
|
140 |
+
|
141 |
+
if data.get("Header", {}).get("status") == "Success":
|
142 |
+
monitors = data.get("Data", [])
|
143 |
+
print(f"Found {len(monitors)} monitors for state {state_code}")
|
144 |
+
|
145 |
+
if monitors:
|
146 |
+
print("Sample monitor:")
|
147 |
+
sample_monitor = monitors[0]
|
148 |
+
print(json.dumps(sample_monitor, indent=2))
|
149 |
+
|
150 |
+
# Check keys that might be causing issues
|
151 |
+
print("\nChecking critical keys for indexing issues:")
|
152 |
+
for key in ["state_code", "county_code", "site_number", "parameter_code", "parameter_name", "latitude", "longitude", "local_site_name"]:
|
153 |
+
if key in sample_monitor:
|
154 |
+
print(f" ✓ '{key}' found: {sample_monitor[key]}")
|
155 |
+
else:
|
156 |
+
print(f" ✗ '{key}' missing!")
|
157 |
+
else:
|
158 |
+
print("No monitors found for this state")
|
159 |
+
|
160 |
+
return monitors
|
161 |
+
else:
|
162 |
+
print(f"API lookup failed: {data.get('Header', {}).get('error', 'Unknown error')}")
|
163 |
+
return None
|
164 |
+
except Exception as e:
|
165 |
+
print(f"Exception during monitors lookup: {e}")
|
166 |
+
return None
|
167 |
+
|
168 |
+
def run_tests():
|
169 |
+
"""Run all tests"""
|
170 |
+
print("Starting API Tests")
|
171 |
+
api_working = test_api_connection()
|
172 |
+
|
173 |
+
if api_working:
|
174 |
+
# Test some states with different formats
|
175 |
+
for state_code in ["06", "36", "CA", "NY"]:
|
176 |
+
state_info = test_state_lookup(state_code)
|
177 |
+
if state_info:
|
178 |
+
state_code_to_use = state_info.get("state_code")
|
179 |
+
test_counties_lookup(state_code_to_use)
|
180 |
+
test_monitors_lookup(state_code_to_use)
|
181 |
+
|
182 |
+
print_separator()
|
183 |
+
print("Tests completed.")
|
184 |
+
|
185 |
+
if __name__ == "__main__":
|
186 |
+
run_tests()
|