Implements slice 1 of docs/design/2026-07-19-client-diagnostics.md: the versioned contract (schemas, fixtures, Go validator), storage-validated diagnostics.uploads_enabled gate, account-scoped status endpoint, hardened streaming multipart ingest with quota reservation and a receiving/ready/ failed report state machine, S3 streaming puts, acting-admin report API (list/detail/download/delete with audit events), and the retention + orphan-reconciliation cleanup task. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XppCCycoaskCsW7ja1fZct
112 lines
2.6 KiB
Go
112 lines
2.6 KiB
Go
package diagnostics
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const ReportDownloadContentType = BundleContentType
|
|
|
|
var ErrReportNotReady = errors.New("diagnostic report is not ready")
|
|
|
|
func (s *Service) ListForAdmin(ctx context.Context, filters ListFilters) (ListResult, error) {
|
|
if s.reports == nil {
|
|
return ListResult{}, ErrReportStoreUnavailable
|
|
}
|
|
return s.reports.ListForAdmin(ctx, filters)
|
|
}
|
|
|
|
func (s *Service) GetReport(ctx context.Context, id string) (*Report, error) {
|
|
if s.reports == nil {
|
|
return nil, ErrReportStoreUnavailable
|
|
}
|
|
return s.reports.GetByID(ctx, id)
|
|
}
|
|
|
|
func (s *Service) PresignReportDownload(ctx context.Context, report *Report, expiry time.Duration) (string, error) {
|
|
bucket, key, err := s.readyReportBlobLocation(report)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if expiry <= 0 {
|
|
expiry = 15 * time.Minute
|
|
}
|
|
return s.store.PresignGetURL(ctx, bucket, key, expiry)
|
|
}
|
|
|
|
func (s *Service) EffectiveReportDownloadTTL(requested time.Duration) time.Duration {
|
|
if requested <= 0 {
|
|
return requested
|
|
}
|
|
effectiveStore, ok := s.store.(interface {
|
|
EffectivePresignTTL(time.Duration) time.Duration
|
|
})
|
|
if !ok {
|
|
return requested
|
|
}
|
|
effective := effectiveStore.EffectivePresignTTL(requested)
|
|
if effective <= 0 {
|
|
return requested
|
|
}
|
|
return effective
|
|
}
|
|
|
|
func (s *Service) OpenReportDownload(ctx context.Context, report *Report) (io.ReadCloser, error) {
|
|
bucket, key, err := s.readyReportBlobLocation(report)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return s.store.GetObject(ctx, bucket, key)
|
|
}
|
|
|
|
func (s *Service) DeleteReport(ctx context.Context, id string) (*Report, error) {
|
|
if s.reports == nil {
|
|
return nil, ErrReportStoreUnavailable
|
|
}
|
|
report, err := s.reports.GetByID(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := deleteReportObjects(ctx, s.store, report, s.logger); err != nil {
|
|
return nil, err
|
|
}
|
|
deleted, err := s.reports.DeleteByID(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return deleted, nil
|
|
}
|
|
|
|
func (s *Service) readyReportBlobLocation(report *Report) (string, string, error) {
|
|
if report == nil {
|
|
return "", "", ErrNotFound
|
|
}
|
|
if report.State != StateReady {
|
|
return "", "", ErrReportNotReady
|
|
}
|
|
if s.store == nil || strings.TrimSpace(s.store.Bucket()) == "" {
|
|
return "", "", ErrStorageUnavailable
|
|
}
|
|
|
|
bucket := stringValue(report.BlobBucket)
|
|
if bucket == "" {
|
|
bucket = s.store.Bucket()
|
|
}
|
|
key := stringValue(report.BlobKey)
|
|
if key == "" {
|
|
return "", "", fmt.Errorf("%w: diagnostic report has no blob key", ErrStorageUnavailable)
|
|
}
|
|
return bucket, key, nil
|
|
}
|
|
|
|
func stringValue(value *string) string {
|
|
if value == nil {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(*value)
|
|
}
|