Spaces:
Sleeping
Sleeping
Update troubleshooting.py
Browse files- troubleshooting.py +111 -44
troubleshooting.py
CHANGED
@@ -32,13 +32,20 @@ def test_api_connection():
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
print("API connection successful!")
|
39 |
return True
|
40 |
else:
|
41 |
-
print(
|
42 |
return False
|
43 |
except Exception as e:
|
44 |
print(f"Exception during API connection test: {e}")
|
@@ -59,25 +66,44 @@ def test_state_lookup(state_code):
|
|
59 |
response = requests.get(endpoint, params=params)
|
60 |
data = response.json()
|
61 |
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(
|
81 |
return None
|
82 |
except Exception as e:
|
83 |
print(f"Exception during state lookup: {e}")
|
@@ -99,23 +125,30 @@ def test_counties_lookup(state_code):
|
|
99 |
response = requests.get(endpoint, params=params)
|
100 |
data = response.json()
|
101 |
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
counties = data.get("Data", [])
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
print("No counties found for this state")
|
114 |
-
|
115 |
-
return counties
|
116 |
else:
|
117 |
-
print(
|
118 |
-
|
|
|
119 |
except Exception as e:
|
120 |
print(f"Exception during counties lookup: {e}")
|
121 |
return None
|
@@ -138,29 +171,59 @@ def test_monitors_lookup(state_code):
|
|
138 |
response = requests.get(endpoint, params=params)
|
139 |
data = response.json()
|
140 |
|
141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
monitors = data.get("Data", [])
|
143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
-
|
146 |
-
|
147 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
else:
|
162 |
-
print(
|
163 |
-
|
|
|
164 |
except Exception as e:
|
165 |
print(f"Exception during monitors lookup: {e}")
|
166 |
return None
|
@@ -175,9 +238,13 @@ def run_tests():
|
|
175 |
for state_code in ["06", "36", "CA", "NY"]:
|
176 |
state_info = test_state_lookup(state_code)
|
177 |
if state_info:
|
178 |
-
|
179 |
-
|
180 |
-
|
|
|
|
|
|
|
|
|
181 |
|
182 |
print_separator()
|
183 |
print("Tests completed.")
|
|
|
32 |
response = requests.get(endpoint, params=params)
|
33 |
print(f"Response status code: {response.status_code}")
|
34 |
data = response.json()
|
|
|
35 |
|
36 |
+
# Print the entire response structure for examination
|
37 |
+
print("Full response structure:")
|
38 |
+
print(json.dumps(data, indent=2)[:500] + "..." if len(json.dumps(data)) > 500 else json.dumps(data, indent=2))
|
39 |
+
|
40 |
+
# Determine if the API call was successful based on response structure
|
41 |
+
if isinstance(data, list) and len(data) > 0 and isinstance(data[0], dict) and data[0].get("status") == "Success":
|
42 |
+
print("API connection successful!")
|
43 |
+
return True
|
44 |
+
elif isinstance(data, dict) and data.get("Header", {}).get("status") == "Success":
|
45 |
print("API connection successful!")
|
46 |
return True
|
47 |
else:
|
48 |
+
print("API connection failed: Unable to determine success status")
|
49 |
return False
|
50 |
except Exception as e:
|
51 |
print(f"Exception during API connection test: {e}")
|
|
|
66 |
response = requests.get(endpoint, params=params)
|
67 |
data = response.json()
|
68 |
|
69 |
+
# Handle the response based on its structure
|
70 |
+
if isinstance(data, list) and len(data) > 0 and isinstance(data[0], dict) and data[0].get("status") == "Success":
|
71 |
+
header = data[0]
|
72 |
+
states_data = data[1:] if len(data) > 1 else []
|
73 |
+
print(f"Found {len(states_data)} states in the API")
|
74 |
+
|
75 |
+
# Look for the specific state
|
76 |
+
matching_states = [s for s in states_data if str(s.get("code")) == str(state_code) or s.get("value") == state_code]
|
77 |
+
if matching_states:
|
78 |
+
print(f"Found matching state: {matching_states[0]}")
|
79 |
+
return matching_states[0]
|
80 |
+
else:
|
81 |
+
print(f"No matching state found for code: {state_code}")
|
82 |
+
print("Available state codes (first 10):")
|
83 |
+
for s in states_data[:10]: # Print first 10 for brevity
|
84 |
+
print(f" {s.get('code')} - {s.get('value')}")
|
85 |
+
if len(states_data) > 10:
|
86 |
+
print(f" ... and {len(states_data)-10} more")
|
87 |
+
return None
|
88 |
+
elif isinstance(data, dict) and data.get("Header", {}).get("status") == "Success":
|
89 |
states = data.get("Data", [])
|
90 |
print(f"Found {len(states)} states in the API")
|
91 |
|
92 |
# Look for the specific state
|
93 |
+
matching_states = [s for s in states if str(s.get("state_code")) == str(state_code) or s.get("value") == state_code]
|
94 |
if matching_states:
|
95 |
print(f"Found matching state: {matching_states[0]}")
|
96 |
return matching_states[0]
|
97 |
else:
|
98 |
print(f"No matching state found for code: {state_code}")
|
99 |
+
print("Available state codes (first 10):")
|
100 |
for s in states[:10]: # Print first 10 for brevity
|
101 |
print(f" {s.get('state_code')} - {s.get('value')}")
|
102 |
if len(states) > 10:
|
103 |
print(f" ... and {len(states)-10} more")
|
104 |
return None
|
105 |
else:
|
106 |
+
print("API lookup failed: Unable to determine success status")
|
107 |
return None
|
108 |
except Exception as e:
|
109 |
print(f"Exception during state lookup: {e}")
|
|
|
125 |
response = requests.get(endpoint, params=params)
|
126 |
data = response.json()
|
127 |
|
128 |
+
# Print the entire response structure for examination
|
129 |
+
print("Full response structure:")
|
130 |
+
print(json.dumps(data, indent=2)[:500] + "..." if len(json.dumps(data)) > 500 else json.dumps(data, indent=2))
|
131 |
+
|
132 |
+
# Handle both list and dictionary response structures
|
133 |
+
counties = []
|
134 |
+
if isinstance(data, list) and len(data) > 0:
|
135 |
+
if data[0].get("status") == "Success":
|
136 |
+
counties = data[1:] if len(data) > 1 else []
|
137 |
+
elif isinstance(data, dict) and data.get("Header", {}).get("status") == "Success":
|
138 |
counties = data.get("Data", [])
|
139 |
+
|
140 |
+
print(f"Found {len(counties)} counties for state {state_code}")
|
141 |
+
|
142 |
+
if counties:
|
143 |
+
print("Sample counties:")
|
144 |
+
for c in counties[:5]: # Print first 5 for brevity
|
145 |
+
print(f" {c}")
|
146 |
+
if len(counties) > 5:
|
147 |
+
print(f" ... and {len(counties)-5} more")
|
|
|
|
|
|
|
148 |
else:
|
149 |
+
print("No counties found for this state")
|
150 |
+
|
151 |
+
return counties
|
152 |
except Exception as e:
|
153 |
print(f"Exception during counties lookup: {e}")
|
154 |
return None
|
|
|
171 |
response = requests.get(endpoint, params=params)
|
172 |
data = response.json()
|
173 |
|
174 |
+
# Print the entire response structure for examination
|
175 |
+
print("First part of response structure:")
|
176 |
+
print(json.dumps(data, indent=2)[:500] + "..." if len(json.dumps(data)) > 500 else json.dumps(data, indent=2))
|
177 |
+
|
178 |
+
# Handle both list and dictionary response structures
|
179 |
+
monitors = []
|
180 |
+
if isinstance(data, list) and len(data) > 0:
|
181 |
+
if data[0].get("status") == "Success":
|
182 |
+
monitors = data[1:] if len(data) > 1 else []
|
183 |
+
elif isinstance(data, dict) and data.get("Header", {}).get("status") == "Success":
|
184 |
monitors = data.get("Data", [])
|
185 |
+
|
186 |
+
print(f"Found {len(monitors)} monitors for state {state_code}")
|
187 |
+
|
188 |
+
if monitors and len(monitors) > 0:
|
189 |
+
print("Sample monitor:")
|
190 |
+
sample_monitor = monitors[0]
|
191 |
+
print(json.dumps(sample_monitor, indent=2))
|
192 |
|
193 |
+
# Check keys that might be causing issues
|
194 |
+
print("\nChecking critical keys for indexing issues:")
|
195 |
+
expected_keys = ["state_code", "county_code", "site_number", "parameter_code", "parameter_name", "latitude", "longitude", "local_site_name"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
+
# Handle different potential key names
|
198 |
+
alternative_keys = {
|
199 |
+
"state_code": ["state_code", "state", "code"],
|
200 |
+
"county_code": ["county_code", "county", "county_id"],
|
201 |
+
"site_number": ["site_number", "site_num", "site", "site_id"],
|
202 |
+
"parameter_code": ["parameter_code", "param_code", "param"],
|
203 |
+
"parameter_name": ["parameter_name", "param_name", "parameter"],
|
204 |
+
"latitude": ["latitude", "lat"],
|
205 |
+
"longitude": ["longitude", "long", "lon"],
|
206 |
+
"local_site_name": ["local_site_name", "site_name", "name"]
|
207 |
+
}
|
208 |
+
|
209 |
+
for key in expected_keys:
|
210 |
+
# Try the primary key and alternatives
|
211 |
+
found = False
|
212 |
+
value = None
|
213 |
+
|
214 |
+
for alt_key in alternative_keys.get(key, [key]):
|
215 |
+
if alt_key in sample_monitor:
|
216 |
+
found = True
|
217 |
+
value = sample_monitor[alt_key]
|
218 |
+
print(f" ✓ '{key}' found as '{alt_key}': {value}")
|
219 |
+
break
|
220 |
+
|
221 |
+
if not found:
|
222 |
+
print(f" ✗ '{key}' missing (also tried alternatives: {alternative_keys.get(key, [key])})")
|
223 |
else:
|
224 |
+
print("No monitors found for this state")
|
225 |
+
|
226 |
+
return monitors
|
227 |
except Exception as e:
|
228 |
print(f"Exception during monitors lookup: {e}")
|
229 |
return None
|
|
|
238 |
for state_code in ["06", "36", "CA", "NY"]:
|
239 |
state_info = test_state_lookup(state_code)
|
240 |
if state_info:
|
241 |
+
# Use the correct state code format based on the response
|
242 |
+
state_code_to_use = state_info.get("code") or state_info.get("state_code")
|
243 |
+
if state_code_to_use:
|
244 |
+
test_counties_lookup(state_code_to_use)
|
245 |
+
test_monitors_lookup(state_code_to_use)
|
246 |
+
else:
|
247 |
+
print(f"Could not determine the proper state code format for {state_code}")
|
248 |
|
249 |
print_separator()
|
250 |
print("Tests completed.")
|