forked from 0xWheatyz/SPARC
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1bd9dccdb8 |
@@ -40,9 +40,3 @@ JWT_SECRET=your-secure-jwt-secret-change-in-production
|
|||||||
# When USE_CACHE=true: check database for cached responses before making API calls
|
# When USE_CACHE=true: check database for cached responses before making API calls
|
||||||
# When USE_CACHE=false: always make fresh API calls (still stores results in database)
|
# When USE_CACHE=false: always make fresh API calls (still stores results in database)
|
||||||
USE_CACHE=true
|
USE_CACHE=true
|
||||||
|
|
||||||
# ---- Webhooks ----
|
|
||||||
|
|
||||||
# Comma-separated list of webhook URLs for job completion and alert notifications
|
|
||||||
# Supports generic HTTP POST and Slack/Discord incoming webhooks
|
|
||||||
# WEBHOOK_URLS=https://hooks.slack.com/services/XXX,https://example.com/webhook
|
|
||||||
|
|||||||
+56
-18
@@ -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
|
||||||
@@ -389,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 ==============
|
||||||
|
|
||||||
|
|
||||||
@@ -519,25 +574,8 @@ def _run_batch_job(job_id: str, companies: list[str], max_workers: int):
|
|||||||
progress=100,
|
progress=100,
|
||||||
result_json=_json.dumps(batch_response.model_dump(), default=str),
|
result_json=_json.dumps(batch_response.model_dump(), default=str),
|
||||||
)
|
)
|
||||||
# Fire webhook notification
|
|
||||||
from SPARC.webhooks import notify_job_completed
|
|
||||||
notify_job_completed(
|
|
||||||
job_id=job_id,
|
|
||||||
status="completed",
|
|
||||||
total_companies=result.total_companies,
|
|
||||||
successful=result.successful,
|
|
||||||
failed=result.failed,
|
|
||||||
)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
db.update_job(job_id, status="failed", error=str(e))
|
db.update_job(job_id, status="failed", error=str(e))
|
||||||
from SPARC.webhooks import notify_job_completed
|
|
||||||
notify_job_completed(
|
|
||||||
job_id=job_id,
|
|
||||||
status="failed",
|
|
||||||
total_companies=len(companies),
|
|
||||||
successful=0,
|
|
||||||
failed=len(companies),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/analyze/batch/async", response_model=JobStatus, tags=["Analysis"])
|
@app.post("/analyze/batch/async", response_model=JobStatus, tags=["Analysis"])
|
||||||
|
|||||||
@@ -1,139 +0,0 @@
|
|||||||
"""Webhook notifications for job completion and alert events.
|
|
||||||
|
|
||||||
Sends JSON payloads to configured webhook URLs with retry logic.
|
|
||||||
Supports generic HTTP POST and Slack-compatible text payloads.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import logging
|
|
||||||
import os
|
|
||||||
import time
|
|
||||||
from datetime import datetime
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
import requests
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
# Comma-separated list of webhook URLs (env var based config)
|
|
||||||
_WEBHOOK_URLS_RAW = os.getenv("WEBHOOK_URLS", "")
|
|
||||||
WEBHOOK_URLS: list[str] = [
|
|
||||||
url.strip() for url in _WEBHOOK_URLS_RAW.split(",") if url.strip()
|
|
||||||
]
|
|
||||||
|
|
||||||
MAX_RETRIES = 3
|
|
||||||
BACKOFF_BASE = 2 # seconds
|
|
||||||
|
|
||||||
|
|
||||||
def _is_slack_url(url: str) -> bool:
|
|
||||||
"""Check if a URL looks like a Slack incoming webhook."""
|
|
||||||
return "hooks.slack.com" in url or "discord.com/api/webhooks" in url
|
|
||||||
|
|
||||||
|
|
||||||
def _build_payload(event_type: str, data: dict[str, Any], slack: bool = False) -> dict:
|
|
||||||
"""Build the webhook payload.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
event_type: Type of event (e.g., "job_completed", "alert")
|
|
||||||
data: Event-specific data
|
|
||||||
slack: If True, wrap in Slack-compatible ``text`` format
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
JSON-serializable payload dict
|
|
||||||
"""
|
|
||||||
payload = {
|
|
||||||
"event": event_type,
|
|
||||||
"timestamp": datetime.utcnow().isoformat() + "Z",
|
|
||||||
**data,
|
|
||||||
}
|
|
||||||
|
|
||||||
if slack:
|
|
||||||
# Build a human-readable summary for Slack/Discord
|
|
||||||
lines = [f"*[SPARC] {event_type}*"]
|
|
||||||
for key, value in data.items():
|
|
||||||
lines.append(f" {key}: {value}")
|
|
||||||
return {"text": "\n".join(lines)}
|
|
||||||
|
|
||||||
return payload
|
|
||||||
|
|
||||||
|
|
||||||
def _send_with_retry(url: str, payload: dict) -> bool:
|
|
||||||
"""Send a POST request with exponential backoff retry.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
url: Webhook URL
|
|
||||||
payload: JSON payload to send
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
True if delivered successfully, False after all retries exhausted
|
|
||||||
"""
|
|
||||||
for attempt in range(1, MAX_RETRIES + 1):
|
|
||||||
try:
|
|
||||||
response = requests.post(url, json=payload, timeout=10)
|
|
||||||
if response.status_code < 300:
|
|
||||||
logger.debug("Webhook delivered to %s (attempt %d)", url, attempt)
|
|
||||||
return True
|
|
||||||
logger.warning(
|
|
||||||
"Webhook %s returned %d (attempt %d/%d)",
|
|
||||||
url, response.status_code, attempt, MAX_RETRIES,
|
|
||||||
)
|
|
||||||
except requests.RequestException as e:
|
|
||||||
logger.warning(
|
|
||||||
"Webhook delivery failed for %s (attempt %d/%d): %s",
|
|
||||||
url, attempt, MAX_RETRIES, e,
|
|
||||||
)
|
|
||||||
|
|
||||||
if attempt < MAX_RETRIES:
|
|
||||||
wait = BACKOFF_BASE ** attempt
|
|
||||||
time.sleep(wait)
|
|
||||||
|
|
||||||
logger.error("Webhook permanently failed for %s after %d attempts", url, MAX_RETRIES)
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def notify(event_type: str, data: dict[str, Any]) -> None:
|
|
||||||
"""Fire all configured webhooks for an event.
|
|
||||||
|
|
||||||
Safe to call even when no webhooks are configured (returns immediately).
|
|
||||||
|
|
||||||
Args:
|
|
||||||
event_type: Event identifier (e.g., "job_completed", "patent_alert")
|
|
||||||
data: Event data to include in the payload
|
|
||||||
"""
|
|
||||||
if not WEBHOOK_URLS:
|
|
||||||
return
|
|
||||||
|
|
||||||
for url in WEBHOOK_URLS:
|
|
||||||
slack = _is_slack_url(url)
|
|
||||||
payload = _build_payload(event_type, data, slack=slack)
|
|
||||||
_send_with_retry(url, payload)
|
|
||||||
|
|
||||||
|
|
||||||
def notify_job_completed(
|
|
||||||
job_id: str,
|
|
||||||
status: str,
|
|
||||||
total_companies: int,
|
|
||||||
successful: int,
|
|
||||||
failed: int,
|
|
||||||
) -> None:
|
|
||||||
"""Send notification when a batch job completes."""
|
|
||||||
notify("job_completed", {
|
|
||||||
"job_id": job_id,
|
|
||||||
"status": status,
|
|
||||||
"total_companies": total_companies,
|
|
||||||
"successful": successful,
|
|
||||||
"failed": failed,
|
|
||||||
"summary": f"Batch job {job_id}: {successful}/{total_companies} succeeded",
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
def notify_alert(
|
|
||||||
company_name: str,
|
|
||||||
alert_type: str,
|
|
||||||
message: str,
|
|
||||||
) -> None:
|
|
||||||
"""Send notification for a tracked company alert."""
|
|
||||||
notify("patent_alert", {
|
|
||||||
"company_name": company_name,
|
|
||||||
"alert_type": alert_type,
|
|
||||||
"message": message,
|
|
||||||
})
|
|
||||||
@@ -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