forked from longtao.wu/droboticflow
141 lines
4.9 KiB
Go
141 lines
4.9 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
// Audit metadata only: never load annotation bodies, opinions or media into
|
|
// the trace panel. The existing organization/item/createdAt index scopes reads.
|
|
func (s *MemoryStore) ListHumanTraceEvents(ctx context.Context, org, item string) ([]domain.HumanEventRecord, error) {
|
|
events, err := s.ListHumanEvents(ctx, org, item)
|
|
for i := range events {
|
|
events[i].Payload = map[string]any{"revision": events[i].Payload["revision"], "reviewedRevision": events[i].Payload["reviewedRevision"], "annotationRevision": events[i].Payload["annotationRevision"]}
|
|
}
|
|
return events, err
|
|
}
|
|
func (s *MongoStore) ListHumanTraceEvents(ctx context.Context, org, item string) ([]domain.HumanEventRecord, error) {
|
|
cursor, err := s.humanEvents.Find(ctx, bson.M{"organizationId": normalizedOrganizationID(org), "itemId": item}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: 1}}).SetProjection(bson.M{"_id": 0, "id": 1, "eventType": 1, "actorUserId": 1, "createdAt": 1, "payload.revision": 1, "payload.reviewedRevision": 1, "payload.annotationRevision": 1}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.HumanEventRecord](ctx, cursor)
|
|
}
|
|
|
|
type traceParty struct {
|
|
ID string `bson:"id"`
|
|
Name string `bson:"name"`
|
|
}
|
|
type traceCount struct {
|
|
ID string `bson:"_id"`
|
|
Created int `bson:"created"`
|
|
Rejected int `bson:"rejected"`
|
|
}
|
|
|
|
func traceRejected(kind string) bool {
|
|
return kind == "rejected" || kind == "combined_quality_rejected" || kind == "aggregate_acceptance_rejected"
|
|
}
|
|
func (s *MemoryStore) ListHumanTraceSummaries(ctx context.Context, org string, items []domain.HumanItemRecord) (map[string]domain.HumanTraceSummary, error) {
|
|
out := map[string]domain.HumanTraceSummary{}
|
|
for _, item := range items {
|
|
summary := domain.HumanTraceSummary{}
|
|
if u, err := s.GetUser(ctx, item.LatestAuthorUserID); err != nil {
|
|
return nil, err
|
|
} else if u != nil && humanOrganizationMatches(u.OrganizationID, org) {
|
|
summary.Author = u.Name
|
|
}
|
|
if supplier, err := s.GetSupplier(ctx, item.SupplierID); err != nil {
|
|
return nil, err
|
|
} else if supplier != nil && humanOrganizationMatches(supplier.OrganizationID, org) {
|
|
summary.Supplier = supplier.Name
|
|
}
|
|
events, err := s.ListHumanTraceEvents(ctx, org, item.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
complete, count := false, 0
|
|
for _, e := range events {
|
|
if e.EventType == "created" {
|
|
complete = true
|
|
}
|
|
if traceRejected(e.EventType) {
|
|
count++
|
|
}
|
|
}
|
|
if complete {
|
|
summary.Rejections = &count
|
|
}
|
|
out[item.ID] = summary
|
|
}
|
|
return out, nil
|
|
}
|
|
func (s *MongoStore) ListHumanTraceSummaries(ctx context.Context, org string, items []domain.HumanItemRecord) (map[string]domain.HumanTraceSummary, error) {
|
|
out := map[string]domain.HumanTraceSummary{}
|
|
if len(items) == 0 {
|
|
return out, nil
|
|
}
|
|
ids, authors, suppliers := []string{}, []string{}, []string{}
|
|
for _, item := range items {
|
|
ids = append(ids, item.ID)
|
|
if item.LatestAuthorUserID != "" {
|
|
authors = append(authors, item.LatestAuthorUserID)
|
|
}
|
|
if item.SupplierID != "" {
|
|
suppliers = append(suppliers, item.SupplierID)
|
|
}
|
|
}
|
|
names := func(collection string, ids []string) (map[string]string, error) {
|
|
result := map[string]string{}
|
|
if len(ids) == 0 {
|
|
return result, nil
|
|
}
|
|
cur, err := s.queryCollection(collection).Find(ctx, bson.M{"organizationId": normalizedOrganizationID(org), "id": bson.M{"$in": ids}}, options.Find().SetProjection(bson.M{"_id": 0, "id": 1, "name": 1}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rows, err := decodeMany[traceParty](ctx, cur)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, row := range rows {
|
|
result[row.ID] = row.Name
|
|
}
|
|
return result, nil
|
|
}
|
|
users, err := names("users", authors)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
vendors, err := names("suppliers", suppliers)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cur, err := s.humanEvents.Aggregate(ctx, []bson.M{
|
|
{"$match": bson.M{"organizationId": normalizedOrganizationID(org), "itemId": bson.M{"$in": ids}}},
|
|
{"$group": bson.M{"_id": "$itemId", "created": bson.M{"$sum": bson.M{"$cond": bson.A{bson.M{"$eq": bson.A{"$eventType", "created"}}, 1, 0}}}, "rejected": bson.M{"$sum": bson.M{"$cond": bson.A{bson.M{"$in": bson.A{"$eventType", bson.A{"rejected", "combined_quality_rejected", "aggregate_acceptance_rejected"}}}, 1, 0}}}}},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
counts, err := decodeMany[traceCount](ctx, cur)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
known := map[string]int{}
|
|
for _, count := range counts {
|
|
if count.Created > 0 {
|
|
known[count.ID] = count.Rejected
|
|
}
|
|
}
|
|
for _, item := range items {
|
|
summary := domain.HumanTraceSummary{Author: users[item.LatestAuthorUserID], Supplier: vendors[item.SupplierID]}
|
|
if count, ok := known[item.ID]; ok {
|
|
summary.Rejections = &count
|
|
}
|
|
out[item.ID] = summary
|
|
}
|
|
return out, nil
|
|
}
|