droboticflow/internal/api/annotation_timeline_index.go

105 lines
3.9 KiB
Go

package api
import (
"bytes"
"compress/gzip"
"context"
"crypto/sha256"
"droboticflow/internal/domain"
"droboticflow/internal/store"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"math"
)
type persistedAnnotationTimeline struct {
Camera string `json:"camera"`
FrameCount int `json:"frameCount"`
LeftFrameCount int `json:"leftFrameCount"`
RightFrameCount int `json:"rightFrameCount"`
Origin int64 `json:"originTimestampUs"`
End int64 `json:"coverageEndTimestampUs"`
Seconds []float64 `json:"boundariesSeconds"`
RightSeconds []float64 `json:"rightBoundariesSeconds"`
Stamps []int64 `json:"boundaryTimestampUs"`
Left []int `json:"leftFrameIndices"`
Right []int `json:"rightFrameIndices"`
StartSecond float64 `json:"rangeStartSecond"`
EndSecond float64 `json:"rangeEndSecond"`
Contract string `json:"boundaryContract"`
Scale float64 `json:"ptsToMediaScale"`
Fingerprint string `json:"sourceFingerprint"`
}
func decodePersistentTimeline(record *domain.AnnotationTimelineIndex, fingerprint string) (map[string]any, error) {
if record == nil {
return nil, nil
}
sum := sha256.Sum256(record.Body)
if hex.EncodeToString(sum[:]) != record.SHA256 {
return nil, fmt.Errorf("timeline index checksum mismatch")
}
z, e := gzip.NewReader(bytes.NewReader(record.Body))
if e != nil {
return nil, e
}
defer z.Close()
b, e := io.ReadAll(io.LimitReader(z, 8<<20+1))
if e != nil || len(b) > 8<<20 {
return nil, fmt.Errorf("invalid timeline index size")
}
var t persistedAnnotationTimeline
if e = json.Unmarshal(b, &t); e != nil {
return nil, e
}
n := len(t.Seconds)
if t.Contract != "hardware-frame-v1" || t.Fingerprint != fingerprint || n < 2 || len(t.Stamps) != n || len(t.RightSeconds) != n || len(t.Left) != n || len(t.Right) != n || t.LeftFrameCount < 1 || t.RightFrameCount < 1 {
return nil, fmt.Errorf("invalid timeline index contract")
}
for i := 0; i < n; i++ {
if math.IsNaN(t.Seconds[i]) || math.IsInf(t.Seconds[i], 0) || i > 0 && (t.Stamps[i] <= t.Stamps[i-1] || t.Seconds[i] <= t.Seconds[i-1]) {
return nil, fmt.Errorf("invalid timeline index ordering")
}
}
return map[string]any{"camera": "left", "frameCount": t.LeftFrameCount, "leftFrameCount": t.LeftFrameCount, "rightFrameCount": t.RightFrameCount, "originTimestampUs": t.Origin, "coverageEndTimestampUs": t.End, "boundariesSeconds": t.Seconds, "rightBoundariesSeconds": t.RightSeconds, "boundaryTimestampUs": t.Stamps, "leftFrameIndices": t.Left, "rightFrameIndices": t.Right, "rangeStartSecond": t.StartSecond, "rangeEndSecond": t.EndSecond, "boundaryContract": t.Contract, "ptsToMediaScale": t.Scale, "sourceFingerprint": t.Fingerprint}, nil
}
func (s *server) persistentTimeline(ctx context.Context, t domain.AnnotationTaskRecord, key string, build func(context.Context) (map[string]any, error)) (map[string]any, error) {
db, ok := s.store.(store.AnnotationProcessingStore)
if !ok {
return build(ctx)
}
id := "annotation-timeline-v1:" + t.OrganizationID + ":" + key
record, e := db.GetAnnotationTimelineIndex(ctx, id)
if e != nil {
return nil, e
}
if cached, e := decodePersistentTimeline(record, key); e == nil && cached != nil {
return cached, nil
}
v, e := build(ctx)
if e != nil {
return nil, e
}
b, e := json.Marshal(v)
if e != nil {
return nil, e
}
if len(b) > 8<<20 {
return v, nil
}
var compressed bytes.Buffer
z := gzip.NewWriter(&compressed)
if _, e = z.Write(b); e != nil {
return nil, e
}
if e = z.Close(); e != nil {
return nil, e
}
sum := sha256.Sum256(compressed.Bytes())
e = db.PutAnnotationTimelineIndex(ctx, domain.AnnotationTimelineIndex{ID: id, OrganizationID: t.OrganizationID, ProjectID: t.ProjectID, WorkflowID: t.WorkflowID, RunID: t.RunID, Body: compressed.Bytes(), SHA256: hex.EncodeToString(sum[:]), CreatedAt: nowISO()})
return v, e
}