forked from 0xWheatyz/SPARC
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3b6411869d |
+9
-19
@@ -108,10 +108,12 @@ class CompanyAnalyzer:
|
|||||||
def analyze_single_patent(self, patent_id: str, company_name: str) -> str:
|
def analyze_single_patent(self, patent_id: str, company_name: str) -> str:
|
||||||
"""Analyze a single patent by ID.
|
"""Analyze a single patent by ID.
|
||||||
|
|
||||||
If the patent PDF is not already on disk, this method attempts to
|
Prerequisite:
|
||||||
download it automatically by looking up the PDF link in the database
|
The patent PDF must already exist at ``patents/{patent_id}.pdf``
|
||||||
cache. If the link is not cached either, a ``FileNotFoundError`` is
|
before calling this method. PDFs are downloaded automatically when
|
||||||
raised with instructions on how to obtain the PDF.
|
using the batch analysis pipeline (``analyze_company`` or the
|
||||||
|
``/analyze/batch`` API endpoint). For standalone usage, download
|
||||||
|
the PDF manually or call ``SERP.save_patents()`` first.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
patent_id: Publication ID of the patent (e.g. "US-11234567-B2")
|
patent_id: Publication ID of the patent (e.g. "US-11234567-B2")
|
||||||
@@ -121,7 +123,7 @@ class CompanyAnalyzer:
|
|||||||
Analysis of the specific patent's innovation quality
|
Analysis of the specific patent's innovation quality
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
FileNotFoundError: If the patent PDF cannot be found or downloaded.
|
FileNotFoundError: If the patent PDF is not found at the expected path.
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
logger.info("Analyzing patent %s for %s...", patent_id, company_name)
|
logger.info("Analyzing patent %s for %s...", patent_id, company_name)
|
||||||
@@ -129,21 +131,9 @@ class CompanyAnalyzer:
|
|||||||
patent_path = f"patents/{patent_id}.pdf"
|
patent_path = f"patents/{patent_id}.pdf"
|
||||||
|
|
||||||
if not os.path.exists(patent_path):
|
if not os.path.exists(patent_path):
|
||||||
# Attempt to download the PDF automatically from cached metadata
|
|
||||||
cached = self.db.get_cached_patent(patent_id)
|
|
||||||
pdf_link = cached.get("pdf_link") if cached else None
|
|
||||||
|
|
||||||
if pdf_link:
|
|
||||||
logger.info("PDF not on disk; downloading %s from cached link", patent_id)
|
|
||||||
patent = SERP.save_patents(
|
|
||||||
Patent(patent_id=patent_id, pdf_link=pdf_link)
|
|
||||||
)
|
|
||||||
patent_path = patent.pdf_path
|
|
||||||
else:
|
|
||||||
raise FileNotFoundError(
|
raise FileNotFoundError(
|
||||||
f"Patent PDF not found at '{patent_path}' and no download link is "
|
f"Patent PDF not found at '{patent_path}'. "
|
||||||
f"cached for '{patent_id}'. Run a company analysis first to populate "
|
f"Download the PDF first using SERP.save_patents() or the batch analysis pipeline."
|
||||||
f"the cache, or call SERP.save_patents() with the patent's PDF link."
|
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
+39
-37
@@ -77,6 +77,13 @@ class JobStatus(BaseModel):
|
|||||||
error: str | None = None
|
error: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class PaginatedJobsResponse(BaseModel):
|
||||||
|
"""Paginated response for job listings."""
|
||||||
|
|
||||||
|
items: list["JobStatus"]
|
||||||
|
next_cursor: str | None = None
|
||||||
|
|
||||||
|
|
||||||
class HealthResponse(BaseModel):
|
class HealthResponse(BaseModel):
|
||||||
"""Health check response."""
|
"""Health check response."""
|
||||||
|
|
||||||
@@ -429,38 +436,6 @@ async def analyze_company(
|
|||||||
return _convert_result(result)
|
return _convert_result(result)
|
||||||
|
|
||||||
|
|
||||||
@app.get(
|
|
||||||
"/analyze/patent/{patent_id}",
|
|
||||||
tags=["Analysis"],
|
|
||||||
)
|
|
||||||
async def analyze_single_patent(
|
|
||||||
patent_id: str,
|
|
||||||
company_name: str = Query(description="Company name for analysis context"),
|
|
||||||
_: UserResponse = Depends(get_current_user),
|
|
||||||
):
|
|
||||||
"""Analyze a single patent by its publication ID.
|
|
||||||
|
|
||||||
If the patent PDF is not already cached locally, the system will attempt
|
|
||||||
to download it automatically from a previously cached link. If no link
|
|
||||||
is available, a 404 error is returned.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
patent_id: Patent publication ID (e.g. "US-11234567-B2")
|
|
||||||
company_name: Company name for analysis context
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Analysis text for the patent
|
|
||||||
"""
|
|
||||||
if not _analyzer:
|
|
||||||
raise HTTPException(status_code=503, detail="Analyzer not initialized")
|
|
||||||
|
|
||||||
try:
|
|
||||||
analysis = _analyzer.analyze_single_patent(patent_id, company_name)
|
|
||||||
return {"patent_id": patent_id, "company_name": company_name, "analysis": analysis}
|
|
||||||
except FileNotFoundError as e:
|
|
||||||
raise HTTPException(status_code=404, detail=str(e))
|
|
||||||
|
|
||||||
|
|
||||||
@app.post(
|
@app.post(
|
||||||
"/analyze/batch",
|
"/analyze/batch",
|
||||||
response_model=BatchAnalysisResponse,
|
response_model=BatchAnalysisResponse,
|
||||||
@@ -609,24 +584,51 @@ async def get_job_status(
|
|||||||
return _job_row_to_status(job_row)
|
return _job_row_to_status(job_row)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/jobs", response_model=list[JobStatus], tags=["Jobs"])
|
@app.get("/jobs", response_model=PaginatedJobsResponse, tags=["Jobs"])
|
||||||
async def list_jobs(
|
async def list_jobs(
|
||||||
status: Annotated[
|
status: Annotated[
|
||||||
str | None,
|
str | None,
|
||||||
Query(description="Filter by status: pending, running, completed, failed"),
|
Query(description="Filter by status: pending, running, completed, failed"),
|
||||||
] = None,
|
] = None,
|
||||||
limit: Annotated[int, Query(ge=1, le=100)] = 10,
|
limit: Annotated[int, Query(ge=1, le=100)] = 10,
|
||||||
|
cursor: Annotated[
|
||||||
|
str | None,
|
||||||
|
Query(description="Opaque cursor from a previous response's next_cursor field"),
|
||||||
|
] = None,
|
||||||
_: UserResponse = Depends(get_current_user),
|
_: UserResponse = Depends(get_current_user),
|
||||||
):
|
):
|
||||||
"""List all analysis jobs.
|
"""List analysis jobs with cursor-based pagination.
|
||||||
|
|
||||||
|
Pass ``limit`` to control page size. The response includes a ``next_cursor``
|
||||||
|
field; pass it back as the ``cursor`` query parameter to fetch the next page.
|
||||||
|
When ``next_cursor`` is ``null``, there are no more results.
|
||||||
|
|
||||||
|
Existing clients that use only ``limit`` (without ``cursor``) continue to
|
||||||
|
work without modification.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
status: Optional filter by job status
|
status: Optional filter by job status
|
||||||
limit: Maximum number of jobs to return (default 10, max 100)
|
limit: Maximum number of jobs to return (default 10, max 100)
|
||||||
|
cursor: Opaque pagination cursor from a previous response
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
List of job statuses
|
Paginated list of job statuses
|
||||||
"""
|
"""
|
||||||
db = _get_job_db()
|
db = _get_job_db()
|
||||||
job_rows = db.list_jobs(status=status, limit=limit)
|
# Fetch one extra to determine if there is a next page
|
||||||
return [_job_row_to_status(row) for row in job_rows]
|
job_rows = db.list_jobs(status=status, limit=limit + 1, cursor=cursor)
|
||||||
|
|
||||||
|
has_next = len(job_rows) > limit
|
||||||
|
if has_next:
|
||||||
|
job_rows = job_rows[:limit]
|
||||||
|
|
||||||
|
items = [_job_row_to_status(row) for row in job_rows]
|
||||||
|
|
||||||
|
next_cursor = None
|
||||||
|
if has_next and job_rows:
|
||||||
|
last = job_rows[-1]
|
||||||
|
created = last["created_at"]
|
||||||
|
ts = created.isoformat() if hasattr(created, "isoformat") else str(created)
|
||||||
|
next_cursor = f"{ts}|{last['job_id']}"
|
||||||
|
|
||||||
|
return PaginatedJobsResponse(items=items, next_cursor=next_cursor)
|
||||||
|
|||||||
+32
-7
@@ -568,20 +568,45 @@ class DatabaseClient:
|
|||||||
self,
|
self,
|
||||||
status: Optional[str] = None,
|
status: Optional[str] = None,
|
||||||
limit: int = 10,
|
limit: int = 10,
|
||||||
|
cursor: Optional[str] = None,
|
||||||
) -> List[Dict]:
|
) -> List[Dict]:
|
||||||
"""List jobs, optionally filtered by status."""
|
"""List jobs with optional status filter and cursor-based pagination.
|
||||||
query = "SELECT * FROM jobs"
|
|
||||||
|
Args:
|
||||||
|
status: Optional status filter (pending, running, completed, failed).
|
||||||
|
limit: Maximum number of jobs to return.
|
||||||
|
cursor: Opaque cursor (``created_at|job_id``) from a previous
|
||||||
|
response. When provided, only jobs older than the cursor are
|
||||||
|
returned.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List of job dicts ordered by created_at descending.
|
||||||
|
"""
|
||||||
|
conditions: list[str] = []
|
||||||
params: list = []
|
params: list = []
|
||||||
|
|
||||||
if status:
|
if status:
|
||||||
query += " WHERE status = %s"
|
conditions.append("status = %s")
|
||||||
params.append(status)
|
params.append(status)
|
||||||
query += " ORDER BY created_at DESC LIMIT %s"
|
|
||||||
|
if cursor:
|
||||||
|
try:
|
||||||
|
ts_str, cursor_job_id = cursor.rsplit("|", 1)
|
||||||
|
conditions.append("(created_at, job_id) < (%s, %s)")
|
||||||
|
params.extend([ts_str, cursor_job_id])
|
||||||
|
except ValueError:
|
||||||
|
pass # Ignore malformed cursors; return from start
|
||||||
|
|
||||||
|
query = "SELECT * FROM jobs"
|
||||||
|
if conditions:
|
||||||
|
query += " WHERE " + " AND ".join(conditions)
|
||||||
|
query += " ORDER BY created_at DESC, job_id DESC LIMIT %s"
|
||||||
params.append(limit)
|
params.append(limit)
|
||||||
|
|
||||||
with self.get_conn() as conn:
|
with self.get_conn() as conn:
|
||||||
with conn.cursor(cursor_factory=RealDictCursor) as cursor:
|
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||||
cursor.execute(query, params)
|
cur.execute(query, params)
|
||||||
return [dict(row) for row in cursor.fetchall()]
|
return [dict(row) for row in cur.fetchall()]
|
||||||
|
|
||||||
def mark_stale_jobs_failed(self) -> int:
|
def mark_stale_jobs_failed(self) -> int:
|
||||||
"""Mark any jobs in 'running' or 'pending' state as 'failed'.
|
"""Mark any jobs in 'running' or 'pending' state as 'failed'.
|
||||||
|
|||||||
Reference in New Issue
Block a user