Files
query-orchestration/internal/document/batch/outcome/reporter.go
T
Jay Brown aafe7d5b5f Merged in feature/batch-status (pull request #215)
Implement and test the batch status feature

* working
2026-03-12 18:53:42 +00:00

88 lines
3.0 KiB
Go

// Package outcome provides a shared reporter that runners use to write
// pipeline stage outcomes to the batch_document_outcomes table.
// For non-batch documents (no outcome row exists), updates are a natural
// no-op (0 rows affected).
package outcome
import (
"context"
"queryorchestration/internal/database/repository"
"github.com/google/uuid"
)
// Reporter writes pipeline stage outcomes to batch_document_outcomes.
// Used by runners to report their processing results for batch-uploaded documents.
// For non-batch documents (no outcome row exists), updates are a no-op (0 rows affected).
//
// Fields:
// - queries: SQLC-generated database queries
type Reporter struct {
queries *repository.Queries
}
// New creates an outcome Reporter.
//
// Parameters:
// - q: SQLC-generated queries instance (from cfg.GetDBQueries())
//
// Returns:
// - *Reporter: the reporter instance
func New(q *repository.Queries) *Reporter {
return &Reporter{queries: q}
}
// Resolve sets the document_id on a batch outcome row and updates the outcome.
// Called by docInitRunner after creating or finding the document.
// Matches on batch_id + filename since document_id is NULL before init.
//
// Parameters:
// - ctx: request context
// - batchID: the batch this file belongs to (from the document's batch_id)
// - filename: original filename from the ZIP (from the document record)
// - documentID: the created or found document ID
// - outcomeStatus: the init-stage outcome ("init_complete" or "init_duplicate")
// - errorDetail: optional error message (nil for success)
//
// Returns:
// - error: database error, or nil
//
// If batchID is nil, this is a no-op (document was not from a batch upload).
func (r *Reporter) Resolve(ctx context.Context, batchID *uuid.UUID, filename string,
documentID uuid.UUID, outcomeStatus repository.BatchOutcomeStatus, errorDetail *string) error {
if batchID == nil {
return nil
}
return r.queries.ResolveBatchDocumentOutcome(ctx, &repository.ResolveBatchDocumentOutcomeParams{
BatchID: *batchID,
Filename: filename,
DocumentID: &documentID,
Column4: outcomeStatus,
ErrorDetail: errorDetail,
})
}
// Update updates the outcome for a document that already has document_id set.
// Called by docSyncRunner and docCleanRunner after processing.
//
// Parameters:
// - ctx: request context
// - documentID: the document being processed
// - outcomeStatus: the pipeline outcome (e.g. "sync_complete", "clean_passed")
// - errorDetail: optional error message (nil for success)
//
// Returns:
// - error: database error, or nil
//
// If no outcome row exists for this document_id (non-batch document), this
// affects 0 rows and is a no-op.
func (r *Reporter) Update(ctx context.Context, documentID uuid.UUID,
outcomeStatus repository.BatchOutcomeStatus, errorDetail *string) error {
return r.queries.UpdateBatchDocumentOutcomeByDocumentID(ctx, &repository.UpdateBatchDocumentOutcomeByDocumentIDParams{
DocumentID: &documentID,
Column2: outcomeStatus,
ErrorDetail: errorDetail,
})
}