|
| 1 | +package sqlite |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/cschleiden/go-workflows/backend" |
| 9 | +) |
| 10 | + |
| 11 | +func (b *sqliteBackend) GetStats(ctx context.Context) (*backend.Stats, error) { |
| 12 | + s := &backend.Stats{} |
| 13 | + |
| 14 | + tx, err := b.db.BeginTx(ctx, &sql.TxOptions{ |
| 15 | + Isolation: sql.LevelReadCommitted, |
| 16 | + }) |
| 17 | + if err != nil { |
| 18 | + return nil, fmt.Errorf("failed to start transaction: %w", err) |
| 19 | + } |
| 20 | + defer tx.Rollback() |
| 21 | + |
| 22 | + row := tx.QueryRowContext( |
| 23 | + ctx, |
| 24 | + "SELECT COUNT(*) FROM instances i WHERE i.completed_at IS NULL", |
| 25 | + ) |
| 26 | + if err := row.Err(); err != nil { |
| 27 | + return nil, fmt.Errorf("failed to query active instances: %w", err) |
| 28 | + } |
| 29 | + |
| 30 | + var activeInstances int64 |
| 31 | + if err := row.Scan(&activeInstances); err != nil { |
| 32 | + return nil, fmt.Errorf("failed to scan active instances: %w", err) |
| 33 | + } |
| 34 | + |
| 35 | + s.ActiveWorkflowInstances = activeInstances |
| 36 | + |
| 37 | + // Get pending activities |
| 38 | + row = tx.QueryRowContext( |
| 39 | + ctx, |
| 40 | + "SELECT COUNT(*) FROM activities") |
| 41 | + if err := row.Err(); err != nil { |
| 42 | + return nil, fmt.Errorf("failed to query active activities: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + var pendingActivities int64 |
| 46 | + if err := row.Scan(&pendingActivities); err != nil { |
| 47 | + return nil, fmt.Errorf("failed to scan active activities: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + s.PendingActivities = pendingActivities |
| 51 | + |
| 52 | + return s, nil |
| 53 | +} |
0 commit comments