Skip to content

Commit

Permalink
fix: update aggregation for filtered data (#854)
Browse files Browse the repository at this point in the history
* fix: update command parameter description

* fix: update aggregation for filtered data

Co-authored-by: Vladislav Sukhin <[email protected]>
  • Loading branch information
vsukhin and vsukhin authored Jan 25, 2022
1 parent 5f3e5af commit 6df927f
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/kubectl-testkube/commands/tests/executions.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewTestExecutionsCmd() *cobra.Command {
},
}

cmd.Flags().IntVar(&limit, "limit", 1000, "execution name, if empty will be autogenerated")
cmd.Flags().IntVar(&limit, "limit", 1000, "max number of records to return")
cmd.Flags().StringSliceVar(&tags, "tags", nil, "comma separated list of tags: --tags tag1,tag2,tag3")

return cmd
Expand Down
3 changes: 1 addition & 2 deletions internal/app/api/v1/executions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"go.mongodb.org/mongo-driver/mongo"
"k8s.io/apimachinery/pkg/api/errors"

"github.com/kubeshop/testkube/internal/pkg/api/repository/result"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/executor/client"
"github.com/kubeshop/testkube/pkg/rand"
Expand Down Expand Up @@ -133,7 +132,7 @@ func (s TestKubeAPI) ListExecutionsHandler() fiber.Handler {
return s.Error(c, http.StatusInternalServerError, err)
}

executionTotals, err := s.ExecutionResults.GetExecutionTotals(c.Context(), result.NewExecutionsFilter())
executionTotals, err := s.ExecutionResults.GetExecutionTotals(c.Context())
if err != nil {
return s.Error(c, http.StatusInternalServerError, err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/app/api/v1/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (s TestKubeAPI) ListTestExecutionsHandler() fiber.Handler {
if err != nil {
return s.Error(c, http.StatusBadRequest, err)
}
allExecutionsTotals, err := s.TestExecutionResults.GetExecutionsTotals(ctx, testresult.NewExecutionsFilter())
allExecutionsTotals, err := s.TestExecutionResults.GetExecutionsTotals(ctx)
if err != nil {
return s.Error(c, http.StatusBadRequest, err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/api/repository/result/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Repository interface {
// GetExecutions gets executions using a filter, use filter with no data for all
GetExecutions(ctx context.Context, filter Filter) ([]testkube.Execution, error)
// GetExecutionTotals gets the statistics on number of executions using a filter, use filter with no data for all
GetExecutionTotals(ctx context.Context, filter Filter) (result testkube.ExecutionsTotals, err error)
GetExecutionTotals(ctx context.Context, filter ...Filter) (result testkube.ExecutionsTotals, err error)
// Insert inserts new execution result
Insert(ctx context.Context, result testkube.Execution) error
// Update updates execution result
Expand Down
21 changes: 15 additions & 6 deletions internal/pkg/api/repository/result/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,26 @@ func (r *MongoRepository) GetExecutions(ctx context.Context, filter Filter) (res
return
}

func (r *MongoRepository) GetExecutionTotals(ctx context.Context, filter Filter) (totals testkube.ExecutionsTotals, err error) {
func (r *MongoRepository) GetExecutionTotals(ctx context.Context, filter ...Filter) (totals testkube.ExecutionsTotals, err error) {
var result []struct {
Status string `bson:"_id"`
Count int32 `bson:"count"`
}
query, _ := composeQueryAndOpts(filter)

cursor, err := r.Coll.Aggregate(ctx, mongo.Pipeline{
bson.D{{"$match", query}},
bson.D{{"$group", bson.D{{"_id", "$executionresult.status"}, {"count", bson.D{{"$sum", 1}}}}}},
})
query := bson.M{}
if len(filter) > 0 {
query, _ = composeQueryAndOpts(filter[0])
}

pipeline := []bson.D{{{"$match", query}}}
if len(filter) > 0 {
pipeline = append(pipeline, bson.D{{"$sort", bson.D{{"starttime", -1}}}})
pipeline = append(pipeline, bson.D{{"$skip", int64(filter[0].Page() * filter[0].PageSize())}})
pipeline = append(pipeline, bson.D{{"$limit", int64(filter[0].PageSize())}})
}

pipeline = append(pipeline, bson.D{{"$group", bson.D{{"_id", "$executionresult.status"}, {"count", bson.D{{"$sum", 1}}}}}})
cursor, err := r.Coll.Aggregate(ctx, pipeline)
if err != nil {
return totals, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/api/repository/result/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestStorage(t *testing.T) {
})

t.Run("getting totals without filters should return all the executions", func(t *testing.T) {
totals, err := repository.GetExecutionTotals(context.Background(), NewExecutionsFilter())
totals, err := repository.GetExecutionTotals(context.Background())

assert.NoError(err)
assert.Equal(int32(21), totals.Results)
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/api/repository/testresult/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Repository interface {
// GetByNameAndScript gets execution result by name
GetByNameAndScript(ctx context.Context, name, script string) (testkube.TestExecution, error)
// GetExecutionsTotals gets executions total stats using a filter, use filter with no data for all
GetExecutionsTotals(ctx context.Context, filter Filter) (totals testkube.ExecutionsTotals, err error)
GetExecutionsTotals(ctx context.Context, filter ...Filter) (totals testkube.ExecutionsTotals, err error)
// GetExecutions gets executions using a filter, use filter with no data for all
GetExecutions(ctx context.Context, filter Filter) ([]testkube.TestExecution, error)
// Insert inserts new execution result
Expand Down
22 changes: 15 additions & 7 deletions internal/pkg/api/repository/testresult/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,26 @@ func (r *MongoRepository) GetNewestExecutions(ctx context.Context, limit int) (r
return
}

func (r *MongoRepository) GetExecutionsTotals(ctx context.Context, filter Filter) (totals testkube.ExecutionsTotals, err error) {
func (r *MongoRepository) GetExecutionsTotals(ctx context.Context, filter ...Filter) (totals testkube.ExecutionsTotals, err error) {
var result []struct {
Status string `bson:"_id"`
Count int32 `bson:"count"`
}
query, _ := composeQueryAndOpts(filter)
// cursor, err := r.Coll.Find(ctx, query, opts)

cursor, err := r.Coll.Aggregate(ctx, mongo.Pipeline{
bson.D{{"$match", query}},
bson.D{{"$group", bson.D{{"_id", "$status"}, {"count", bson.D{{"$sum", 1}}}}}},
})
query := bson.M{}
if len(filter) > 0 {
query, _ = composeQueryAndOpts(filter[0])
}

pipeline := []bson.D{{{"$match", query}}}
if len(filter) > 0 {
pipeline = append(pipeline, bson.D{{"$sort", bson.D{{"starttime", -1}}}})
pipeline = append(pipeline, bson.D{{"$skip", int64(filter[0].Page() * filter[0].PageSize())}})
pipeline = append(pipeline, bson.D{{"$limit", int64(filter[0].PageSize())}})
}

pipeline = append(pipeline, bson.D{{"$group", bson.D{{"_id", "$status"}, {"count", bson.D{{"$sum", 1}}}}}})
cursor, err := r.Coll.Aggregate(ctx, pipeline)
if err != nil {
return totals, err
}
Expand Down

0 comments on commit 6df927f

Please sign in to comment.