-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspecs.py
More file actions
141 lines (112 loc) · 4.25 KB
/
specs.py
File metadata and controls
141 lines (112 loc) · 4.25 KB
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
"""Spec endpoints."""
from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from api.cache import cache_key, get_cache, set_cache
from api.dependencies import require_db
from api.exceptions import raise_not_found
from api.schemas import ImplementationResponse, SpecDetailResponse, SpecListItem
from core.database import SpecRepository
router = APIRouter(tags=["specs"])
@router.get("/specs", response_model=list[SpecListItem])
async def get_specs(db: AsyncSession = Depends(require_db)):
"""
Get list of all specs with metadata.
Returns only specs that have at least one implementation.
"""
key = cache_key("specs_list")
cached = get_cache(key)
if cached:
return cached
repo = SpecRepository(db)
specs = await repo.get_all()
# Only return specs with at least one implementation
result = [
SpecListItem(
id=spec.id, title=spec.title, description=spec.description, tags=spec.tags, library_count=len(spec.impls)
)
for spec in specs
if spec.impls # Filter: only specs with implementations
]
set_cache(key, result)
return result
@router.get("/specs/{spec_id}", response_model=SpecDetailResponse)
async def get_spec(spec_id: str, db: AsyncSession = Depends(require_db)):
"""
Get detailed spec information including all implementations.
Args:
spec_id: The specification ID (e.g., 'scatter-basic')
Returns:
Full spec details with all library implementations and preview URLs
"""
key = cache_key("spec", spec_id)
cached = get_cache(key)
if cached:
return cached
repo = SpecRepository(db)
spec = await repo.get_by_id(spec_id)
if not spec:
raise_not_found("Spec", spec_id)
# Only return spec if it has implementations
if not spec.impls:
raise_not_found("Spec with implementations", spec_id)
impls = [
ImplementationResponse(
library_id=impl.library_id,
library_name=impl.library.name if impl.library else impl.library_id,
preview_url=impl.preview_url,
preview_thumb=impl.preview_thumb,
preview_html=impl.preview_html,
quality_score=impl.quality_score,
code=impl.code,
generated_at=impl.generated_at.isoformat() if impl.generated_at else None,
generated_by=impl.generated_by,
python_version=impl.python_version,
library_version=impl.library_version,
review_strengths=impl.review_strengths or [],
review_weaknesses=impl.review_weaknesses or [],
review_image_description=impl.review_image_description,
review_criteria_checklist=impl.review_criteria_checklist,
review_verdict=impl.review_verdict,
)
for impl in spec.impls
]
result = SpecDetailResponse(
id=spec.id,
title=spec.title,
description=spec.description,
applications=spec.applications or [],
data=spec.data or [],
notes=spec.notes or [],
tags=spec.tags,
issue=spec.issue,
suggested=spec.suggested,
created=spec.created.isoformat() if spec.created else None,
updated=spec.updated.isoformat() if spec.updated else None,
implementations=impls,
)
set_cache(key, result)
return result
@router.get("/specs/{spec_id}/images")
async def get_spec_images(spec_id: str, db: AsyncSession = Depends(require_db)):
"""
Get plot images for a specification across all libraries.
Returns preview_url, preview_thumb, and preview_html from database.
"""
key = cache_key("spec_images", spec_id)
cached = get_cache(key)
if cached:
return cached
repo = SpecRepository(db)
spec = await repo.get_by_id(spec_id)
if not spec:
raise_not_found("Spec", spec_id)
if not spec.impls:
raise_not_found("Spec with implementations", spec_id)
images = [
{"library": impl.library_id, "url": impl.preview_url, "thumb": impl.preview_thumb, "html": impl.preview_html}
for impl in spec.impls
if impl.preview_url # Only include if there's a preview
]
result = {"spec_id": spec_id, "images": images}
set_cache(key, result)
return result