forked from 0xWheatyz/SPARC
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1bd9dccdb8 |
+61
-40
@@ -9,7 +9,7 @@ from typing import Annotated, List
|
|||||||
|
|
||||||
from fastapi import BackgroundTasks, Depends, FastAPI, HTTPException, Query, Request
|
from fastapi import BackgroundTasks, Depends, FastAPI, HTTPException, Query, Request
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse, StreamingResponse
|
||||||
from pydantic import BaseModel, EmailStr, Field
|
from pydantic import BaseModel, EmailStr, Field
|
||||||
from slowapi import Limiter
|
from slowapi import Limiter
|
||||||
from slowapi.errors import RateLimitExceeded
|
from slowapi.errors import RateLimitExceeded
|
||||||
@@ -77,13 +77,6 @@ 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."""
|
||||||
|
|
||||||
@@ -396,6 +389,61 @@ async def get_analytics(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# ============== Export Endpoints ==============
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/export/{company_name}", tags=["Export"])
|
||||||
|
async def export_company_csv(
|
||||||
|
company_name: str,
|
||||||
|
_: UserResponse = Depends(get_current_user),
|
||||||
|
):
|
||||||
|
"""Export analysis results for a company as a CSV file.
|
||||||
|
|
||||||
|
Returns all stored analysis records for the given company, including
|
||||||
|
analysis type, model used, response text, and timestamp.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
company_name: Company name to export results for
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
CSV file download
|
||||||
|
"""
|
||||||
|
import csv
|
||||||
|
import io
|
||||||
|
|
||||||
|
db = get_db_client()
|
||||||
|
# Query all non-cached analysis results for this company
|
||||||
|
with db.get_conn() as conn:
|
||||||
|
with conn.cursor() as cur:
|
||||||
|
cur.execute(
|
||||||
|
"""
|
||||||
|
SELECT company_name, analysis_type, model, response, timestamp
|
||||||
|
FROM llm_messages
|
||||||
|
WHERE LOWER(company_name) = LOWER(%s) AND is_cached = FALSE
|
||||||
|
ORDER BY timestamp DESC
|
||||||
|
""",
|
||||||
|
(company_name,),
|
||||||
|
)
|
||||||
|
rows = cur.fetchall()
|
||||||
|
|
||||||
|
if not rows:
|
||||||
|
raise HTTPException(status_code=404, detail=f"No analysis results found for '{company_name}'")
|
||||||
|
|
||||||
|
output = io.StringIO()
|
||||||
|
writer = csv.writer(output)
|
||||||
|
writer.writerow(["company_name", "analysis_type", "model", "analysis", "timestamp"])
|
||||||
|
for row in rows:
|
||||||
|
writer.writerow(row)
|
||||||
|
|
||||||
|
output.seek(0)
|
||||||
|
safe_name = company_name.replace(" ", "_").lower()
|
||||||
|
return StreamingResponse(
|
||||||
|
iter([output.getvalue()]),
|
||||||
|
media_type="text/csv",
|
||||||
|
headers={"Content-Disposition": f'attachment; filename="sparc_{safe_name}_export.csv"'},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# ============== System Endpoints ==============
|
# ============== System Endpoints ==============
|
||||||
|
|
||||||
|
|
||||||
@@ -584,51 +632,24 @@ async def get_job_status(
|
|||||||
return _job_row_to_status(job_row)
|
return _job_row_to_status(job_row)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/jobs", response_model=PaginatedJobsResponse, tags=["Jobs"])
|
@app.get("/jobs", response_model=list[JobStatus], 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 analysis jobs with cursor-based pagination.
|
"""List all analysis jobs.
|
||||||
|
|
||||||
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:
|
||||||
Paginated list of job statuses
|
List of job statuses
|
||||||
"""
|
"""
|
||||||
db = _get_job_db()
|
db = _get_job_db()
|
||||||
# Fetch one extra to determine if there is a next page
|
job_rows = db.list_jobs(status=status, limit=limit)
|
||||||
job_rows = db.list_jobs(status=status, limit=limit + 1, cursor=cursor)
|
return [_job_row_to_status(row) for row in job_rows]
|
||||||
|
|
||||||
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)
|
|
||||||
|
|||||||
+9
-34
@@ -568,45 +568,20 @@ 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 with optional status filter and cursor-based pagination.
|
"""List jobs, optionally filtered by status."""
|
||||||
|
|
||||||
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 = []
|
|
||||||
|
|
||||||
if status:
|
|
||||||
conditions.append("status = %s")
|
|
||||||
params.append(status)
|
|
||||||
|
|
||||||
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"
|
query = "SELECT * FROM jobs"
|
||||||
if conditions:
|
params: list = []
|
||||||
query += " WHERE " + " AND ".join(conditions)
|
if status:
|
||||||
query += " ORDER BY created_at DESC, job_id DESC LIMIT %s"
|
query += " WHERE status = %s"
|
||||||
|
params.append(status)
|
||||||
|
query += " ORDER BY created_at 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 cur:
|
with conn.cursor(cursor_factory=RealDictCursor) as cursor:
|
||||||
cur.execute(query, params)
|
cursor.execute(query, params)
|
||||||
return [dict(row) for row in cur.fetchall()]
|
return [dict(row) for row in cursor.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'.
|
||||||
|
|||||||
@@ -126,6 +126,23 @@ export const analysisApi = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Export API
|
||||||
|
export const exportApi = {
|
||||||
|
exportCsv: async (companyName: string): Promise<void> => {
|
||||||
|
const response = await api.get(`/export/${encodeURIComponent(companyName)}`, {
|
||||||
|
responseType: 'blob',
|
||||||
|
});
|
||||||
|
const url = window.URL.createObjectURL(new Blob([response.data]));
|
||||||
|
const link = document.createElement('a');
|
||||||
|
link.href = url;
|
||||||
|
link.setAttribute('download', `sparc_${companyName.toLowerCase().replace(/\s+/g, '_')}_export.csv`);
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
link.remove();
|
||||||
|
window.URL.revokeObjectURL(url);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
// Analytics API
|
// Analytics API
|
||||||
export const analyticsApi = {
|
export const analyticsApi = {
|
||||||
getAnalytics: async (days = 30): Promise<Analytics> => {
|
getAnalytics: async (days = 30): Promise<Analytics> => {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { useMutation } from '@tanstack/react-query';
|
import { useMutation } from '@tanstack/react-query';
|
||||||
import { analysisApi } from '../api/client';
|
import { analysisApi, exportApi } from '../api/client';
|
||||||
import { Search, CheckCircle, AlertCircle, Clock, FileText } from 'lucide-react';
|
import { Search, CheckCircle, AlertCircle, Clock, FileText, Download } from 'lucide-react';
|
||||||
import type { CompanyAnalysis } from '../types';
|
import type { CompanyAnalysis } from '../types';
|
||||||
|
|
||||||
export function Analysis() {
|
export function Analysis() {
|
||||||
@@ -106,9 +106,18 @@ export function Analysis() {
|
|||||||
{/* Analysis Content */}
|
{/* Analysis Content */}
|
||||||
{result.success && result.analysis && (
|
{result.success && result.analysis && (
|
||||||
<div className="bg-bg-card/60 backdrop-blur-lg border border-primary/15 rounded-2xl p-6">
|
<div className="bg-bg-card/60 backdrop-blur-lg border border-primary/15 rounded-2xl p-6">
|
||||||
<h3 className="text-lg font-semibold text-text-primary border-b-2 border-primary/30 pb-2 mb-4">
|
<div className="flex items-center justify-between border-b-2 border-primary/30 pb-2 mb-4">
|
||||||
AI Analysis Results
|
<h3 className="text-lg font-semibold text-text-primary">
|
||||||
</h3>
|
AI Analysis Results
|
||||||
|
</h3>
|
||||||
|
<button
|
||||||
|
onClick={() => exportApi.exportCsv(result.company_name)}
|
||||||
|
className="flex items-center gap-2 text-sm bg-primary/20 hover:bg-primary/30 text-primary font-medium px-3 py-1.5 rounded-lg transition-colors"
|
||||||
|
>
|
||||||
|
<Download size={14} />
|
||||||
|
Export CSV
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
<div className="prose prose-invert max-w-none">
|
<div className="prose prose-invert max-w-none">
|
||||||
<div className="text-text-primary whitespace-pre-wrap leading-relaxed">
|
<div className="text-text-primary whitespace-pre-wrap leading-relaxed">
|
||||||
{result.analysis}
|
{result.analysis}
|
||||||
|
|||||||
Reference in New Issue
Block a user