119 lines
3.0 KiB
Go
119 lines
3.0 KiB
Go
// Package ingestionbench measures production ingestion ports, never ranking,
|
|
// synthesis, persistence or a second implementation of extraction.
|
|
package ingestionbench
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/tradarr/backend/internal/scraper"
|
|
)
|
|
|
|
type Feed interface {
|
|
scraper.Scraper
|
|
scraper.ArticleEnricher
|
|
}
|
|
type Source struct {
|
|
Key string
|
|
Feed Feed
|
|
Symbols []string
|
|
}
|
|
type ContentCounts struct {
|
|
Complete int `json:"complete"`
|
|
Incomplete int `json:"incomplete"`
|
|
Unavailable int `json:"unavailable"`
|
|
}
|
|
type DateCounts struct {
|
|
Observed int `json:"observed"`
|
|
Missing int `json:"missing"`
|
|
}
|
|
type Row struct {
|
|
Fetch scraper.FetchMetrics `json:"article_fetch"`
|
|
NotFetched *int `json:"not_fetched"`
|
|
Source string `json:"source"`
|
|
ListStatus string `json:"list_status"`
|
|
EnrichStatus string `json:"enrich_status"`
|
|
Listed int `json:"listed"`
|
|
Enriched int `json:"enriched"`
|
|
Dates DateCounts `json:"dates"`
|
|
Content ContentCounts `json:"content"`
|
|
}
|
|
|
|
func Run(ctx context.Context, sources []Source, limit int) ([]Row, error) {
|
|
if limit < 1 || limit > 3 {
|
|
return nil, errors.New("invalid_bench_limit")
|
|
}
|
|
rows := make([]Row, 0, len(sources))
|
|
for _, s := range sources {
|
|
if s.Key != "yahoo_finance" && s.Key != "cnbc_business" && s.Key != "bloomberg" {
|
|
return nil, errors.New("invalid_bench_source")
|
|
}
|
|
row := Row{Fetch: scraper.FetchMetrics{SchemaVersion: 1}, Source: s.Key, ListStatus: "unavailable", EnrichStatus: "not_attempted"}
|
|
if s.Feed == nil {
|
|
rows = append(rows, row)
|
|
continue
|
|
}
|
|
a, err := s.Feed.Scrape(ctx, s.Symbols)
|
|
if err != nil {
|
|
rows = append(rows, row)
|
|
continue
|
|
}
|
|
row.ListStatus = "ok"
|
|
if len(a) > limit {
|
|
a = a[:limit]
|
|
}
|
|
row.Listed = len(a)
|
|
if len(a) == 0 {
|
|
rows = append(rows, row)
|
|
continue
|
|
}
|
|
measuredCtx, meter := scraper.WithFetchMetrics(ctx)
|
|
enriched, err := s.Feed.Enrich(measuredCtx, append([]scraper.Article(nil), a...))
|
|
row.Fetch = meter.Finish()
|
|
if row.Fetch.Supported {
|
|
n := max(0, len(a)-row.Fetch.Attempted)
|
|
row.NotFetched = &n
|
|
}
|
|
valid := err == nil && len(enriched) == len(a)
|
|
if valid {
|
|
for i, v := range enriched {
|
|
if v.URL != a[i].URL || v.Title != a[i].Title {
|
|
valid = false
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if !valid {
|
|
row.EnrichStatus = "unavailable"
|
|
row.Content.Unavailable = len(a)
|
|
for _, v := range a {
|
|
if v.PublishedAt == nil {
|
|
row.Dates.Missing++
|
|
} else {
|
|
row.Dates.Observed++
|
|
}
|
|
}
|
|
rows = append(rows, row)
|
|
continue
|
|
}
|
|
row.EnrichStatus = "ok"
|
|
row.Enriched = len(enriched)
|
|
for _, v := range enriched {
|
|
if v.PublishedAt == nil {
|
|
row.Dates.Missing++
|
|
} else {
|
|
row.Dates.Observed++
|
|
}
|
|
switch scraper.ContentStatus(v) {
|
|
case scraper.ContentComplete:
|
|
row.Content.Complete++
|
|
case scraper.ContentIncomplete:
|
|
row.Content.Incomplete++
|
|
default:
|
|
row.Content.Unavailable++
|
|
}
|
|
}
|
|
rows = append(rows, row)
|
|
}
|
|
return rows, nil
|
|
}
|