feat(generation): persist summary outcomes without articles
Record generation outcomes and summaries for history while keeping article content transient. Refs #7 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
59
backend/internal/models/generations.go
Normal file
59
backend/internal/models/generations.go
Normal file
@ -0,0 +1,59 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type GenerationHistory struct {
|
||||
ID string `json:"id"`
|
||||
SummaryID *string `json:"summary_id"`
|
||||
Outcome json.RawMessage `json:"outcome"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// SaveGeneration deliberately accepts no article data and commits both rows together.
|
||||
func (r *Repository) SaveGeneration(ctx context.Context, userID, id string, summary *Summary, outcome json.RawMessage, createdAt time.Time) error {
|
||||
tx, err := r.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
var summaryID *string
|
||||
if summary != nil {
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO summaries (id,user_id,content,ai_provider_id,generated_at) VALUES ($1,$2,$3,$4,$5)`, summary.ID, userID, summary.Content, summary.AIProviderID, summary.GeneratedAt); err != nil {
|
||||
return err
|
||||
}
|
||||
summaryID = &summary.ID
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO summary_generations (id,user_id,summary_id,outcome,created_at) VALUES ($1,$2,$3,$4,$5)`, id, userID, summaryID, string(outcome), createdAt); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
func (r *Repository) ListGenerations(ctx context.Context, userID string, limit int) ([]GenerationHistory, error) {
|
||||
rows, err := r.db.QueryContext(ctx, `SELECT id,summary_id,outcome,created_at FROM summary_generations WHERE user_id=$1 ORDER BY created_at DESC,id DESC LIMIT $2`, userID, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
result := []GenerationHistory{}
|
||||
for rows.Next() {
|
||||
var g GenerationHistory
|
||||
if err = rows.Scan(&g.ID, &g.SummaryID, &g.Outcome, &g.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, g)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
func (r *Repository) GetOwnedSummary(id, userID string) (*Summary, error) {
|
||||
s := &Summary{}
|
||||
err := r.db.QueryRow(`SELECT id,user_id,content,ai_provider_id,generated_at FROM summaries WHERE id=$1 AND user_id=$2`, id, userID).Scan(&s.ID, &s.UserID, &s.Content, &s.AIProviderID, &s.GeneratedAt)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return s, err
|
||||
}
|
||||
@ -3,10 +3,8 @@ package models
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/lib/pq"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
@ -167,23 +165,6 @@ func (r *Repository) RemoveUserAsset(userID, symbol string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repository) GetAllWatchedSymbols() ([]string, error) {
|
||||
rows, err := r.db.Query(`SELECT DISTINCT symbol FROM user_assets ORDER BY symbol`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var symbols []string
|
||||
for rows.Next() {
|
||||
var s string
|
||||
if err := rows.Scan(&s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
symbols = append(symbols, s)
|
||||
}
|
||||
return symbols, nil
|
||||
}
|
||||
|
||||
// ── Sources ────────────────────────────────────────────────────────────────
|
||||
|
||||
func (r *Repository) ListSources() ([]Source, error) {
|
||||
@ -226,141 +207,6 @@ func (r *Repository) UpdateSource(id string, enabled bool, articleLimit *int) er
|
||||
return err
|
||||
}
|
||||
|
||||
// ── Articles ───────────────────────────────────────────────────────────────
|
||||
|
||||
// InsertArticleIfNew insère l'article uniquement s'il n'existe pas déjà (par URL).
|
||||
// Retourne (article, true, nil) si inséré, (nil, false, nil) si déjà présent.
|
||||
func (r *Repository) InsertArticleIfNew(sourceID, title, content, url string, publishedAt *time.Time) (*Article, bool, error) {
|
||||
var pa sql.NullTime
|
||||
if publishedAt != nil {
|
||||
pa = sql.NullTime{Time: *publishedAt, Valid: true}
|
||||
}
|
||||
a := &Article{}
|
||||
err := r.db.QueryRow(`
|
||||
INSERT INTO articles (source_id, title, content, url, published_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (url) DO NOTHING
|
||||
RETURNING id, source_id, title, content, url, published_at, created_at`,
|
||||
sourceID, title, content, url, pa,
|
||||
).Scan(&a.ID, &a.SourceID, &a.Title, &a.Content, &a.URL, &a.PublishedAt, &a.CreatedAt)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, false, nil // déjà présent
|
||||
}
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
return a, true, nil
|
||||
}
|
||||
|
||||
func (r *Repository) AddArticleSymbol(articleID, symbol string) error {
|
||||
_, err := r.db.Exec(`
|
||||
INSERT INTO article_symbols (article_id, symbol)
|
||||
VALUES ($1, $2) ON CONFLICT DO NOTHING`,
|
||||
articleID, strings.ToUpper(symbol))
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repository) ListArticles(symbol string, limit, offset int) ([]Article, error) {
|
||||
query := `
|
||||
SELECT a.id, a.source_id, s.name, a.title, a.content, a.url, a.published_at, a.created_at
|
||||
FROM articles a
|
||||
JOIN sources s ON s.id = a.source_id`
|
||||
args := []interface{}{}
|
||||
if symbol != "" {
|
||||
query += `
|
||||
JOIN article_symbols asy ON asy.article_id = a.id AND asy.symbol = $1`
|
||||
args = append(args, strings.ToUpper(symbol))
|
||||
}
|
||||
query += ` ORDER BY a.published_at DESC NULLS LAST, a.created_at DESC`
|
||||
query += fmt.Sprintf(` LIMIT $%d OFFSET $%d`, len(args)+1, len(args)+2)
|
||||
args = append(args, limit, offset)
|
||||
|
||||
rows, err := r.db.Query(query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var articles []Article
|
||||
for rows.Next() {
|
||||
var a Article
|
||||
if err := rows.Scan(&a.ID, &a.SourceID, &a.SourceName, &a.Title, &a.Content, &a.URL, &a.PublishedAt, &a.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
articles = append(articles, a)
|
||||
}
|
||||
return articles, nil
|
||||
}
|
||||
|
||||
func (r *Repository) GetArticleByID(id string) (*Article, error) {
|
||||
a := &Article{}
|
||||
err := r.db.QueryRow(`
|
||||
SELECT a.id, a.source_id, s.name, a.title, a.content, a.url, a.published_at, a.created_at
|
||||
FROM articles a JOIN sources s ON s.id=a.source_id
|
||||
WHERE a.id=$1`, id,
|
||||
).Scan(&a.ID, &a.SourceID, &a.SourceName, &a.Title, &a.Content, &a.URL, &a.PublishedAt, &a.CreatedAt)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return a, err
|
||||
}
|
||||
|
||||
func (r *Repository) GetRecentArticles(hours int) ([]Article, error) {
|
||||
rows, err := r.db.Query(`
|
||||
SELECT a.id, a.source_id, s.name, a.title, a.content, a.url, a.published_at, a.created_at
|
||||
FROM articles a
|
||||
JOIN sources s ON s.id = a.source_id
|
||||
WHERE COALESCE(a.published_at, a.created_at) > NOW() - ($1 * INTERVAL '1 hour')
|
||||
ORDER BY a.published_at DESC NULLS LAST, a.created_at DESC`, hours)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var articles []Article
|
||||
for rows.Next() {
|
||||
var a Article
|
||||
if err := rows.Scan(&a.ID, &a.SourceID, &a.SourceName, &a.Title, &a.Content, &a.URL, &a.PublishedAt, &a.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
articles = append(articles, a)
|
||||
}
|
||||
return articles, nil
|
||||
}
|
||||
|
||||
func (r *Repository) GetRecentArticlesForSymbols(symbols []string, hours int) ([]Article, error) {
|
||||
if len(symbols) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
placeholders := make([]string, len(symbols))
|
||||
args := []interface{}{hours}
|
||||
for i, s := range symbols {
|
||||
placeholders[i] = fmt.Sprintf("$%d", i+2)
|
||||
args = append(args, strings.ToUpper(s))
|
||||
}
|
||||
query := fmt.Sprintf(`
|
||||
SELECT DISTINCT a.id, a.source_id, s.name, a.title, a.content, a.url, a.published_at, a.created_at
|
||||
FROM articles a
|
||||
JOIN sources s ON s.id = a.source_id
|
||||
JOIN article_symbols asy ON asy.article_id = a.id
|
||||
WHERE asy.symbol IN (%s)
|
||||
AND a.created_at > NOW() - ($1 * INTERVAL '1 hour')
|
||||
ORDER BY a.published_at DESC NULLS LAST, a.created_at DESC`,
|
||||
strings.Join(placeholders, ","))
|
||||
rows, err := r.db.Query(query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var articles []Article
|
||||
for rows.Next() {
|
||||
var a Article
|
||||
if err := rows.Scan(&a.ID, &a.SourceID, &a.SourceName, &a.Title, &a.Content, &a.URL, &a.PublishedAt, &a.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
articles = append(articles, a)
|
||||
}
|
||||
return articles, nil
|
||||
}
|
||||
|
||||
// ── Scrape Credentials ─────────────────────────────────────────────────────
|
||||
|
||||
func (r *Repository) GetCredentials(sourceID string) (*ScrapeCredential, error) {
|
||||
@ -385,55 +231,6 @@ func (r *Repository) UpsertCredentials(sourceID, username, passwordEncrypted str
|
||||
return err
|
||||
}
|
||||
|
||||
// ── Scrape Jobs ────────────────────────────────────────────────────────────
|
||||
|
||||
func (r *Repository) CreateScrapeJob(sourceID string) (*ScrapeJob, error) {
|
||||
j := &ScrapeJob{}
|
||||
err := r.db.QueryRow(`
|
||||
INSERT INTO scrape_jobs (source_id) VALUES ($1)
|
||||
RETURNING id, source_id, status, started_at, finished_at, articles_found, error_msg, created_at`,
|
||||
sourceID,
|
||||
).Scan(&j.ID, &j.SourceID, &j.Status, &j.StartedAt, &j.FinishedAt, &j.ArticlesFound, &j.ErrorMsg, &j.CreatedAt)
|
||||
return j, err
|
||||
}
|
||||
|
||||
func (r *Repository) UpdateScrapeJob(id, status string, articlesFound int, errMsg string) error {
|
||||
var finishedAt *time.Time
|
||||
if status == "done" || status == "error" {
|
||||
now := time.Now()
|
||||
finishedAt = &now
|
||||
}
|
||||
_, err := r.db.Exec(`
|
||||
UPDATE scrape_jobs
|
||||
SET status=$1, articles_found=$2, error_msg=$3, finished_at=$4,
|
||||
started_at=CASE WHEN status='pending' THEN NOW() ELSE started_at END
|
||||
WHERE id=$5`,
|
||||
status, articlesFound, errMsg, finishedAt, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repository) ListScrapeJobs(limit int) ([]ScrapeJob, error) {
|
||||
rows, err := r.db.Query(`
|
||||
SELECT j.id, j.source_id, s.name, j.status, j.started_at, j.finished_at,
|
||||
j.articles_found, j.error_msg, j.created_at
|
||||
FROM scrape_jobs j JOIN sources s ON s.id=j.source_id
|
||||
ORDER BY j.created_at DESC LIMIT $1`, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var jobs []ScrapeJob
|
||||
for rows.Next() {
|
||||
var j ScrapeJob
|
||||
if err := rows.Scan(&j.ID, &j.SourceID, &j.SourceName, &j.Status, &j.StartedAt,
|
||||
&j.FinishedAt, &j.ArticlesFound, &j.ErrorMsg, &j.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
jobs = append(jobs, j)
|
||||
}
|
||||
return jobs, nil
|
||||
}
|
||||
|
||||
// ── AI Providers ───────────────────────────────────────────────────────────
|
||||
|
||||
func (r *Repository) ListAIProviders() ([]AIProvider, error) {
|
||||
|
||||
Reference in New Issue
Block a user