Commit 570db99
committed
refactor: implement medium priority code quality improvements
This commit addresses 5 medium-priority refactoring tasks:
1. ✅ Refactor get_filtered_plots() - Reduce complexity
- Split 91-line function into 6 focused helper functions
- api/routers/plots.py:
- _parse_filter_groups() - Parse query parameters
- _build_cache_key() - Generate cache keys
- _build_spec_lookup() - Create spec lookup dict
- _collect_all_images() - Collect images from specs
- _build_spec_id_to_tags() - Map spec IDs to tags
- _filter_images() - Apply filters to images
- Main function now 35 lines (was 91)
- Much clearer flow and easier to test
2. ✅ Complete Repository Pattern with CRUD methods
- core/database/repositories.py:
- Added to SpecRepository: create(), update(), delete(), upsert()
- Added to LibraryRepository: create(), update(), delete(), upsert()
- Added to ImplRepository: create(), update(), delete(), upsert()
- All repositories now have full CRUD capabilities
- No more direct model manipulation in scripts
- Proper abstraction layer for database operations
3. ✅ Implement Cache Invalidation Strategy
- api/cache.py:
- clear_cache() - Clear entire cache
- clear_cache_by_pattern(pattern) - Clear by substring match
- clear_spec_cache(spec_id) - Clear all spec-related cache
- clear_library_cache(library_id) - Clear all library-related cache
- get_cache_stats() - Get cache statistics
- Granular cache invalidation now possible
- Can be integrated with sync workflows later
4. ✅ Fix Generic Exception Catching (Evaluated)
- Reviewed all `except Exception:` usages
- core/database/connection.py: Correct (transaction rollback pattern)
- scripts/*: Acceptable (external I/O operations)
- No changes needed - patterns are appropriate
5. ✅ Create Centralized Pydantic Config
- NEW: core/config.py
- Centralized settings using Pydantic BaseSettings
- Type-safe environment variable management
- All config in one place with validation and defaults
- Settings categories:
- Database (DATABASE_URL, INSTANCE_CONNECTION_NAME, etc.)
- Google Cloud (GCS_BUCKET, credentials)
- Application (ENVIRONMENT, BASE_URL, API_VERSION)
- GitHub (GITHUB_TOKEN, repository)
- API Keys (Anthropic, OpenAI, Google AI)
- Cache (TTL, maxsize)
- CORS (origins list)
- Helper properties: is_production, has_database, etc.
- Ready for adoption across codebase
Benefits:
- Improved code readability (plots.py 60% shorter)
- Better separation of concerns
- Full CRUD support in repositories
- Granular cache control
- Type-safe configuration management
- Easier testing and maintenance
Note: Integration of Pydantic settings into existing code
(connection.py, routers, etc.) deferred to avoid massive changeset.
Can be done incrementally in future PRs.1 parent 8ce3a74 commit 570db99
File tree
4 files changed
+632
-35
lines changed- api
- routers
- core
- database
4 files changed
+632
-35
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
174 | | - | |
175 | | - | |
| 174 | + | |
176 | 175 | | |
177 | | - | |
| 176 | + | |
178 | 177 | | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
| 178 | + | |
| 179 | + | |
191 | 180 | | |
192 | 181 | | |
193 | | - | |
| 182 | + | |
194 | 183 | | |
195 | | - | |
196 | | - | |
197 | 184 | | |
198 | 185 | | |
199 | 186 | | |
| |||
203 | 190 | | |
204 | 191 | | |
205 | 192 | | |
206 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
207 | 209 | | |
208 | | - | |
| 210 | + | |
209 | 211 | | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | 212 | | |
214 | | - | |
215 | | - | |
216 | | - | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
217 | 216 | | |
218 | | - | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
219 | 223 | | |
220 | 224 | | |
221 | 225 | | |
222 | 226 | | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
223 | 236 | | |
224 | | - | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
225 | 240 | | |
226 | 241 | | |
227 | 242 | | |
| |||
235 | 250 | | |
236 | 251 | | |
237 | 252 | | |
238 | | - | |
239 | | - | |
| 253 | + | |
240 | 254 | | |
241 | 255 | | |
| 256 | + | |
242 | 257 | | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | 258 | | |
248 | | - | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
249 | 269 | | |
250 | 270 | | |
251 | 271 | | |
252 | 272 | | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
253 | 334 | | |
254 | 335 | | |
255 | 336 | | |
256 | 337 | | |
257 | 338 | | |
258 | 339 | | |
| 340 | + | |
259 | 341 | | |
260 | 342 | | |
261 | 343 | | |
| |||
0 commit comments