nakas commited on
Commit
3730d11
·
verified ·
1 Parent(s): 21f095e

Update troubleshooting.py

Browse files
Files changed (1) hide show
  1. troubleshooting.py +38 -83
troubleshooting.py CHANGED
@@ -34,19 +34,20 @@ def test_api_connection():
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}")
52
  return False
@@ -66,31 +67,13 @@ def test_state_lookup(state_code):
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]
@@ -98,12 +81,12 @@ def test_state_lookup(state_code):
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,22 +108,19 @@ def test_counties_lookup(state_code):
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:
@@ -171,17 +151,14 @@ def test_monitors_lookup(state_code):
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
 
@@ -193,33 +170,11 @@ def test_monitors_lookup(state_code):
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
 
@@ -238,8 +193,8 @@ def run_tests():
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)
 
34
  data = response.json()
35
 
36
  # Print the entire response structure for examination
37
+ print("First 500 characters of the response structure:")
38
+ response_str = json.dumps(data, indent=2)
39
+ print(response_str[:500] + "..." if len(response_str) > 500 else response_str)
40
 
41
+ # Specific handling for the observed response structure
42
+ if isinstance(data, dict) and "Header" in data and isinstance(data["Header"], list) and len(data["Header"]) > 0:
43
+ header = data["Header"][0]
44
+ if header.get("status") == "Success":
45
+ print("API connection successful!")
46
+ return True
47
+
48
+ print("API connection failed: Response format not as expected")
49
+ print("Full response keys:", list(data.keys()) if isinstance(data, dict) else "Not a dictionary")
50
+ return False
51
  except Exception as e:
52
  print(f"Exception during API connection test: {e}")
53
  return False
 
67
  response = requests.get(endpoint, params=params)
68
  data = response.json()
69
 
70
+ # Handle the specific response structure we observed
71
+ if isinstance(data, dict) and "Data" in data and isinstance(data["Data"], list):
72
+ states = data["Data"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  print(f"Found {len(states)} states in the API")
74
 
75
  # Look for the specific state
76
+ matching_states = [s for s in states if str(s.get("code")) == str(state_code) or s.get("value_represented") == state_code]
77
  if matching_states:
78
  print(f"Found matching state: {matching_states[0]}")
79
  return matching_states[0]
 
81
  print(f"No matching state found for code: {state_code}")
82
  print("Available state codes (first 10):")
83
  for s in states[:10]: # Print first 10 for brevity
84
+ print(f" {s.get('code')} - {s.get('value_represented')}")
85
  if len(states) > 10:
86
  print(f" ... and {len(states)-10} more")
87
  return None
88
  else:
89
+ print("API lookup failed: Unexpected response format")
90
  return None
91
  except Exception as e:
92
  print(f"Exception during state lookup: {e}")
 
108
  response = requests.get(endpoint, params=params)
109
  data = response.json()
110
 
111
+ # Print the beginning of the response structure for examination
112
+ print("First 500 characters of the response structure:")
113
+ response_str = json.dumps(data, indent=2)
114
+ print(response_str[:500] + "..." if len(response_str) > 500 else response_str)
115
 
 
116
  counties = []
117
+ if isinstance(data, dict) and "Data" in data and isinstance(data["Data"], list):
118
+ counties = data["Data"]
 
 
 
119
 
120
  print(f"Found {len(counties)} counties for state {state_code}")
121
 
122
  if counties:
123
+ print("Sample counties (first 5):")
124
  for c in counties[:5]: # Print first 5 for brevity
125
  print(f" {c}")
126
  if len(counties) > 5:
 
151
  response = requests.get(endpoint, params=params)
152
  data = response.json()
153
 
154
+ # Print the beginning of the response structure for examination
155
+ print("First 500 characters of the response structure:")
156
+ response_str = json.dumps(data, indent=2)
157
+ print(response_str[:500] + "..." if len(response_str) > 500 else response_str)
158
 
 
159
  monitors = []
160
+ if isinstance(data, dict) and "Data" in data and isinstance(data["Data"], list):
161
+ monitors = data["Data"]
 
 
 
162
 
163
  print(f"Found {len(monitors)} monitors for state {state_code}")
164
 
 
170
  # Check keys that might be causing issues
171
  print("\nChecking critical keys for indexing issues:")
172
  expected_keys = ["state_code", "county_code", "site_number", "parameter_code", "parameter_name", "latitude", "longitude", "local_site_name"]
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  for key in expected_keys:
174
+ if key in sample_monitor:
175
+ print(f" ✓ '{key}' found: {sample_monitor[key]}")
176
+ else:
177
+ print(f" ✗ '{key}' missing!")
 
 
 
 
 
 
 
 
 
178
  else:
179
  print("No monitors found for this state")
180
 
 
193
  for state_code in ["06", "36", "CA", "NY"]:
194
  state_info = test_state_lookup(state_code)
195
  if state_info:
196
+ # Use the state code according to what was found
197
+ state_code_to_use = state_info.get("code")
198
  if state_code_to_use:
199
  test_counties_lookup(state_code_to_use)
200
  test_monitors_lookup(state_code_to_use)