Skip to content

Commit a3f7316

Browse files
authored
fix: avoid getting all the records from the database when fetching list of the execution tags (#6269)
1 parent 6c50ebc commit a3f7316

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

internal/app/api/v1/labels_tags.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ func (s *TestkubeAPI) ListTagsHandler() fiber.Handler {
6969
if err != nil {
7070
return s.ClientError(c, errPrefix, err)
7171
}
72-
73-
results := make(map[string][]string)
74-
deduplicateMap(tags, results)
75-
76-
return c.JSON(results)
72+
return c.JSON(tags)
7773
}
7874
}
7975

pkg/repository/testworkflow/mongo.go

+12-14
Original file line numberDiff line numberDiff line change
@@ -691,16 +691,17 @@ func (r *MongoRepository) GetNextExecutionNumber(ctx context.Context, name strin
691691
}
692692

693693
func (r *MongoRepository) GetExecutionTags(ctx context.Context, testWorkflowName string) (tags map[string][]string, err error) {
694-
query := bson.M{"tags": bson.M{"$exists": true}}
694+
query := bson.M{"tags": bson.M{"$nin": bson.A{nil, bson.M{}}}}
695695
if testWorkflowName != "" {
696696
query["workflow.name"] = testWorkflowName
697697
}
698698

699699
pipeline := []bson.M{
700700
{"$match": query},
701-
{"$project": bson.M{
702-
"tags": 1,
703-
}},
701+
{"$project": bson.M{"_id": 0, "tags": bson.M{"$objectToArray": "$tags"}}},
702+
{"$unwind": "$tags"},
703+
{"$group": bson.M{"_id": "$tags.k", "values": bson.M{"$addToSet": "$tags.v"}}},
704+
{"$project": bson.M{"_id": 0, "name": "$_id", "values": 1}},
704705
}
705706

706707
opts := options.Aggregate()
@@ -713,21 +714,18 @@ func (r *MongoRepository) GetExecutionTags(ctx context.Context, testWorkflowName
713714
return nil, err
714715
}
715716

716-
var executions []testkube.TestWorkflowExecutionTags
717-
err = cursor.All(ctx, &executions)
717+
var res []struct {
718+
Name string `bson:"name"`
719+
Values []string `bson:"values"`
720+
}
721+
err = cursor.All(ctx, &res)
718722
if err != nil {
719723
return nil, err
720724
}
721725

722-
for i := range executions {
723-
executions[i].UnscapeDots()
724-
}
725-
726726
tags = make(map[string][]string)
727-
for _, execution := range executions {
728-
for key, value := range execution.Tags {
729-
tags[key] = append(tags[key], value)
730-
}
727+
for _, tag := range res {
728+
tags[tag.Name] = tag.Values
731729
}
732730

733731
return tags, nil

0 commit comments

Comments
 (0)