58 lines
2.0 KiB
Go
58 lines
2.0 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// loadRunUsage fills each listed run's Usage with one grouped aggregate query
|
|
// per bounded batch over the stored per-stage usage rows: no GetRun, step load
|
|
// or transcript parse per row. It uses the same column sums as the
|
|
// history-wide totals, so a listed run's total equals its detail's TotalUsage
|
|
// over the same rows; a usage row without its recorded step is never counted.
|
|
// A run with no recorded usage keeps Usage nil, and a
|
|
// database without the step_usage table lists runs without usage.
|
|
func loadRunUsage(ctx context.Context, tx runGraphQuerier, runs []protocol.RunSummary) error {
|
|
const batchSize = 100
|
|
for start := 0; start < len(runs); start += batchSize {
|
|
end := min(start+batchSize, len(runs))
|
|
args := make([]any, 0, end-start)
|
|
byID := make(map[int64]*protocol.RunSummary, end-start)
|
|
for i := start; i < end; i++ {
|
|
byID[runs[i].ID] = &runs[i]
|
|
args = append(args, runs[i].ID)
|
|
}
|
|
rows, err := tx.QueryContext(ctx, `SELECT u.run_id,`+usageSumColumns+` `+usageFrom+`
|
|
WHERE u.run_id IN (`+strings.TrimSuffix(strings.Repeat("?,", len(args)), ",")+`) GROUP BY u.run_id`, args...)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "no such table") {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
for rows.Next() {
|
|
var id int64
|
|
var u protocol.RunUsage
|
|
if err := rows.Scan(&id, &u.Stages, &u.TokensTotal, &u.TokensKnownStages, &u.TokensIn, &u.TokensOut, &u.CostUSD, &u.CostKnownStages, &u.SplitUnknownStages, &u.TokensReasoning, &u.CacheRead, &u.CacheWrite, &u.CacheKnownStages); err != nil {
|
|
rows.Close()
|
|
return err
|
|
}
|
|
if u.Stages == 0 || byID[id] == nil {
|
|
continue
|
|
}
|
|
u.CostUnknownStages = u.Stages - u.CostKnownStages
|
|
u.SplitKnown = u.TokensKnownStages > 0 && u.SplitUnknownStages == 0
|
|
u.CacheKnown = u.TokensKnownStages > 0 && u.CacheKnownStages == u.TokensKnownStages
|
|
byID[id].Usage = &u
|
|
}
|
|
err = rows.Err()
|
|
rows.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|