109 lines
3.1 KiB
Go
109 lines
3.1 KiB
Go
package issueops
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/steveyegge/beads/internal/types"
|
|
)
|
|
|
|
// GetMoleculeProgressInTx returns progress stats for a molecule within an
|
|
// existing transaction. Routes to the correct table (issues/wisps) automatically.
|
|
//
|
|
//nolint:gosec // G201: table names come from WispTableRouting (hardcoded constants)
|
|
func GetMoleculeProgressInTx(ctx context.Context, tx *sql.Tx, moleculeID string) (*types.MoleculeProgressStats, error) {
|
|
stats := &types.MoleculeProgressStats{
|
|
MoleculeID: moleculeID,
|
|
}
|
|
|
|
isWisp := IsActiveWispInTx(ctx, tx, moleculeID)
|
|
issueTable, _, _, depTable := WispTableRouting(isWisp)
|
|
|
|
// Get molecule title.
|
|
var title sql.NullString
|
|
err := tx.QueryRowContext(ctx, fmt.Sprintf("SELECT title FROM %s WHERE id = ?", issueTable), moleculeID).Scan(&title)
|
|
if err == nil && title.Valid {
|
|
stats.MoleculeTitle = title.String
|
|
}
|
|
|
|
// Step 1: Get child issue IDs from dependencies table.
|
|
depRows, err := tx.QueryContext(ctx, fmt.Sprintf(`
|
|
SELECT issue_id FROM %s
|
|
WHERE depends_on_id = ? AND type = 'parent-child'
|
|
`, depTable), moleculeID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get molecule children: %w", err)
|
|
}
|
|
var childIDs []string
|
|
for depRows.Next() {
|
|
var id string
|
|
if err := depRows.Scan(&id); err != nil {
|
|
_ = depRows.Close()
|
|
return nil, fmt.Errorf("get molecule progress: scan child: %w", err)
|
|
}
|
|
childIDs = append(childIDs, id)
|
|
}
|
|
_ = depRows.Close()
|
|
if err := depRows.Err(); err != nil {
|
|
return nil, fmt.Errorf("get molecule progress: child rows: %w", err)
|
|
}
|
|
|
|
// Step 2: Batch-fetch status for all children.
|
|
// Children of a wisp molecule are also wisps, so use the same table.
|
|
if len(childIDs) > 0 {
|
|
type childInfo struct {
|
|
status string
|
|
}
|
|
childMap := make(map[string]childInfo)
|
|
for start := 0; start < len(childIDs); start += queryBatchSize {
|
|
end := start + queryBatchSize
|
|
if end > len(childIDs) {
|
|
end = len(childIDs)
|
|
}
|
|
batch := childIDs[start:end]
|
|
placeholders := make([]string, len(batch))
|
|
args := make([]any, len(batch))
|
|
for i, id := range batch {
|
|
placeholders[i] = "?"
|
|
args[i] = id
|
|
}
|
|
inClause := strings.Join(placeholders, ",")
|
|
|
|
query := fmt.Sprintf("SELECT id, status FROM %s WHERE id IN (%s)", issueTable, inClause)
|
|
statusRows, err := tx.QueryContext(ctx, query, args...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to batch-fetch child statuses: %w", err)
|
|
}
|
|
for statusRows.Next() {
|
|
var id, status string
|
|
if err := statusRows.Scan(&id, &status); err != nil {
|
|
_ = statusRows.Close()
|
|
return nil, fmt.Errorf("get molecule progress: scan status: %w", err)
|
|
}
|
|
childMap[id] = childInfo{status: status}
|
|
}
|
|
_ = statusRows.Close()
|
|
}
|
|
|
|
for _, childID := range childIDs {
|
|
info, ok := childMap[childID]
|
|
if !ok {
|
|
continue
|
|
}
|
|
stats.Total++
|
|
switch types.Status(info.status) {
|
|
case types.StatusClosed:
|
|
stats.Completed++
|
|
case types.StatusInProgress:
|
|
stats.InProgress++
|
|
if stats.CurrentStepID == "" {
|
|
stats.CurrentStepID = childID
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return stats, nil
|
|
}
|