forked from longtao.wu/droboticflow
145 lines
5.5 KiB
Go
145 lines
5.5 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"os"
|
|
"sort"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type SourceReadinessRow struct {
|
|
OrganizationID string `json:"organizationId" bson:"organizationId"`
|
|
Stage string `json:"stage" bson:"stage"`
|
|
Status string `json:"status" bson:"status"`
|
|
Total int64 `json:"total" bson:"total"`
|
|
Allowed int64 `json:"allowed" bson:"allowed"`
|
|
Unverified int64 `json:"unverified" bson:"unverified"`
|
|
Pending int64 `json:"pending" bson:"pending"`
|
|
Failed int64 `json:"failed" bson:"failed"`
|
|
}
|
|
|
|
type SourceReadiness struct {
|
|
CurrentMode string `json:"currentMode"`
|
|
Scope string `json:"scope"`
|
|
Database string `json:"database"`
|
|
Schema string `json:"schema"`
|
|
OrganizationID string `json:"organizationId"`
|
|
ProjectID string `json:"projectId"`
|
|
Namespace string `json:"namespace"`
|
|
BuildCommit string `json:"buildCommit"`
|
|
GeneratedAt string `json:"generatedAt"`
|
|
TargetMode string `json:"targetMode"`
|
|
LegacySourceCutoff string `json:"legacySourceCutoff"`
|
|
Complete bool `json:"complete"`
|
|
Rows []SourceReadinessRow `json:"rows"`
|
|
}
|
|
|
|
type SourceReadinessStore interface {
|
|
SourceAdmissionReadiness(context.Context, string, string) (*SourceReadiness, error)
|
|
}
|
|
|
|
func sourceReadiness(org, project string, rows []SourceReadinessRow) *SourceReadiness {
|
|
sort.Slice(rows, func(i, j int) bool {
|
|
if rows[i].OrganizationID != rows[j].OrganizationID {
|
|
return rows[i].OrganizationID < rows[j].OrganizationID
|
|
}
|
|
if rows[i].Stage != rows[j].Stage {
|
|
return rows[i].Stage < rows[j].Stage
|
|
}
|
|
return rows[i].Status < rows[j].Status
|
|
})
|
|
mode := "strict"
|
|
if AnnotationSourceAuditAdvisory() {
|
|
mode = "advisory"
|
|
}
|
|
scope := "organization"
|
|
if org == "" {
|
|
scope = "deployment"
|
|
}
|
|
return &SourceReadiness{CurrentMode: mode, Scope: scope, Database: "memory", Schema: "droboticflow.source-readiness/v1", OrganizationID: org, ProjectID: project, GeneratedAt: time.Now().UTC().Format(time.RFC3339Nano), TargetMode: "strict", LegacySourceCutoff: os.Getenv("ANNOTATION_LEGACY_SOURCE_CUTOFF"), Complete: true, Rows: rows}
|
|
}
|
|
|
|
// This is an explicit administrator inventory, never part of normal work reads.
|
|
// Missing proof is reported, not repaired or enqueued. No task body is returned.
|
|
func (s *MemoryStore) SourceAdmissionReadiness(ctx context.Context, org, project string) (*SourceReadiness, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
grouped := map[string]*SourceReadinessRow{}
|
|
for _, item := range s.humanItems {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if org != "" && item.OrganizationID != org || item.PluginID != "annotation-qa" || item.LegacyTaskID == "" || project != "" && item.ProjectID != project {
|
|
continue
|
|
}
|
|
key := item.OrganizationID + "\x00" + item.CurrentStageID + "\x00" + string(item.Status)
|
|
row := grouped[key]
|
|
if row == nil {
|
|
row = &SourceReadinessRow{OrganizationID: item.OrganizationID, Stage: item.CurrentStageID, Status: string(item.Status)}
|
|
grouped[key] = row
|
|
}
|
|
row.Total++
|
|
task := s.annotationTasks[item.LegacyTaskID]
|
|
t, tok := s.annotationIndexJobs[item.LegacyTaskID]
|
|
j, jok := s.annotationIndexJobs[sourceJobForItem(item)]
|
|
var tp, sp *domain.AnnotationIndexJob
|
|
if tok {
|
|
tp = &t
|
|
}
|
|
if jok {
|
|
sp = &j
|
|
}
|
|
switch annotationSourceAdmissionStatusStrict(task, item, tp, sp) {
|
|
case "":
|
|
row.Allowed++
|
|
case "pending":
|
|
row.Pending++
|
|
case "failed":
|
|
row.Failed++
|
|
default:
|
|
row.Unverified++
|
|
}
|
|
}
|
|
rows := []SourceReadinessRow{}
|
|
for _, v := range grouped {
|
|
rows = append(rows, *v)
|
|
}
|
|
return sourceReadiness(org, project, rows), nil
|
|
}
|
|
|
|
func (s *MongoStore) SourceAdmissionReadiness(ctx context.Context, org, project string) (*SourceReadiness, error) {
|
|
filter := bson.M{"pluginId": "annotation-qa", "legacyTaskId": bson.M{"$type": "string", "$ne": ""}}
|
|
if org != "" {
|
|
filter["organizationId"] = org
|
|
}
|
|
if project != "" {
|
|
filter["projectId"] = project
|
|
}
|
|
pipeline := mongo.Pipeline{{{Key: "$match", Value: filter}}, {{Key: "$project", Value: annotationSourceAdmissionProjection()}}}
|
|
pipeline = append(pipeline, annotationSourceAdmissionStatusPipelineForMode(false)...)
|
|
count := func(status string) bson.M {
|
|
return bson.M{"$sum": bson.M{"$cond": bson.A{bson.M{"$eq": bson.A{"$_sourceAdmissionStatus", status}}, 1, 0}}}
|
|
}
|
|
pipeline = append(pipeline, bson.D{{Key: "$group", Value: bson.M{"_id": bson.M{"organizationId": "$organizationId", "stage": "$currentStageId", "status": "$status"}, "total": bson.M{"$sum": 1}, "allowed": count(""), "pending": count("pending"), "failed": count("failed"), "unverified": count("unverified")}}}, bson.D{{Key: "$project", Value: bson.M{"_id": 0, "organizationId": "$_id.organizationId", "stage": "$_id.stage", "status": "$_id.status", "total": 1, "allowed": 1, "pending": 1, "failed": 1, "unverified": 1}}})
|
|
cursor, err := s.humanItems.Aggregate(ctx, pipeline, options.Aggregate().SetMaxTime(8*time.Second))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer cursor.Close(ctx)
|
|
rows := []SourceReadinessRow{}
|
|
if err = cursor.All(ctx, &rows); err != nil {
|
|
return nil, err
|
|
}
|
|
report := sourceReadiness(org, project, rows)
|
|
report.Database = s.db.Name()
|
|
return report, nil
|
|
}
|