sonda/internal/storage/naming.go
tgiachi a880b87a9e chore: move the module path to git.kmalloc.org/squid/sonda
Migrating hosting from Codeberg to git.kmalloc.org. Renames the
module path in go.mod, every internal import, and the OCI image
source label in the Dockerfile. Build and full test suite verified
green after the rename.
2026-11-15 17:30:29 +01:00

115 lines
4 KiB
Go

package storage
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
"github.com/uptrace/bun"
"git.kmalloc.org/squid/sonda/internal/naming"
)
// NamingRepo keeps the names Claude gave, and counts the genres of songs for the facts about a list. It implements
// naming.Store.
type NamingRepo struct{ db *bun.DB }
// NewNamingRepo builds a NamingRepo.
func NewNamingRepo(db *bun.DB) *NamingRepo { return &NamingRepo{db: db} }
// Get returns a kept name, found=false when there is none.
func (r *NamingRepo) Get(ctx context.Context, key string) (naming.Suggestion, bool, error) {
var s naming.Suggestion
err := r.db.QueryRowContext(ctx, "SELECT name, description FROM generated_names WHERE key = ?", key).Scan(&s.Name, &s.Description)
if errors.Is(err, sql.ErrNoRows) {
return naming.Suggestion{}, false, nil
}
if err != nil {
return naming.Suggestion{}, false, fmt.Errorf("read a kept name: %w", err)
}
s.Source = naming.SourceClaude // only what Claude gave is kept
return s, true, nil
}
// Put keeps a name. When the key is there already, from a request that asked at the same time, the first stays.
func (r *NamingRepo) Put(ctx context.Context, key string, s naming.Suggestion, now time.Time) error {
_, err := r.db.ExecContext(ctx,
"INSERT INTO generated_names (key, name, description, created_at) VALUES (?, ?, ?, ?) ON CONFLICT (key) DO NOTHING",
key, s.Name, s.Description, now.Unix())
if err != nil {
return fmt.Errorf("keep a name: %w", err)
}
return nil
}
// GenreCounts returns how many of the songs have each genre, as the files spell it.
func (r *NamingRepo) GenreCounts(ctx context.Context, trackIDs []int64) (map[string]int, error) {
out := map[string]int{}
if len(trackIDs) == 0 {
return out, nil
}
rows, err := r.db.QueryContext(ctx, "SELECT genre, COUNT(DISTINCT track_id) FROM track_genres WHERE track_id IN (?) GROUP BY genre", bun.In(trackIDs))
if err != nil {
return nil, fmt.Errorf("count genres: %w", err)
}
defer rows.Close()
for rows.Next() {
var (
g string
n int
)
if err := rows.Scan(&g, &n); err != nil {
return nil, err
}
out[g] = n
}
return out, rows.Err()
}
// keepUsage is how long the account of the calls is kept.
const keepUsage = 365 * 24 * time.Hour
// Record keeps the account of one call to Claude, and forgets the ones that are older than a year.
func (r *NamingRepo) Record(ctx context.Context, c naming.Call) error {
_, err := r.db.ExecContext(ctx,
`INSERT INTO claude_usage (at, kind, model, outcome, reason, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
c.At.Unix(), c.Kind, c.Model, c.Outcome, c.Reason, c.Usage.Input, c.Usage.Output, c.Usage.CacheRead, c.Usage.CacheWrite)
if err != nil {
return fmt.Errorf("record a call: %w", err)
}
if _, err := r.db.ExecContext(ctx, "DELETE FROM claude_usage WHERE at < ?", c.At.Add(-keepUsage).Unix()); err != nil {
return fmt.Errorf("forget old calls: %w", err)
}
return nil
}
// Since returns what happened from a moment on, a row for each day (UTC) and model, the newest day first.
func (r *NamingRepo) Since(ctx context.Context, since time.Time) ([]naming.DayRow, error) {
rows, err := r.db.QueryContext(ctx,
`SELECT at / 86400, model,
SUM(CASE WHEN outcome = 'made' THEN 1 ELSE 0 END),
SUM(CASE WHEN outcome = 'kept' THEN 1 ELSE 0 END),
SUM(CASE WHEN outcome = 'failed' THEN 1 ELSE 0 END),
SUM(input_tokens), SUM(output_tokens), SUM(cache_read_tokens), SUM(cache_write_tokens)
FROM claude_usage WHERE at >= ? GROUP BY 1, 2 ORDER BY 1 DESC, 2`, since.Unix())
if err != nil {
return nil, fmt.Errorf("read what Claude cost: %w", err)
}
defer rows.Close()
out := []naming.DayRow{}
for rows.Next() {
var (
d naming.DayRow
day int64
)
if err := rows.Scan(&day, &d.Model, &d.Made, &d.Kept, &d.Failed, &d.Usage.Input, &d.Usage.Output, &d.Usage.CacheRead, &d.Usage.CacheWrite); err != nil {
return nil, err
}
d.Day = time.Unix(day*86400, 0).UTC()
out = append(out, d)
}
return out, rows.Err()
}