Skip to content

Commit

Permalink
fix: better filters totals (#932)
Browse files Browse the repository at this point in the history
* fix: better filters totals

* fix: mongo test add paging flag

Co-authored-by: nicufk <[email protected]>
  • Loading branch information
exu and nicufk authored Feb 3, 2022
1 parent d4d0d70 commit 3be610c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions internal/app/api/v1/executions.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ func (s TestKubeAPI) ListExecutionsHandler() fiber.Handler {
return s.Error(c, http.StatusInternalServerError, err)
}

executionTotals, err := s.ExecutionResults.GetExecutionTotals(c.Context())
executionTotals, err := s.ExecutionResults.GetExecutionTotals(c.Context(), false, filter)
if err != nil {
return s.Error(c, http.StatusInternalServerError, err)
}

filteredTotals, err := s.ExecutionResults.GetExecutionTotals(c.Context(), filter)
filteredTotals, err := s.ExecutionResults.GetExecutionTotals(c.Context(), true, filter)
if err != nil {
return s.Error(c, http.StatusInternalServerError, err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/api/repository/result/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type Repository interface {
GetByNameAndScript(ctx context.Context, name, script string) (testkube.Execution, error)
// 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 gets the statistics on number of executions using a filter, but without paging
GetExecutionTotals(ctx context.Context, paging bool, 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
8 changes: 5 additions & 3 deletions internal/pkg/api/repository/result/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ 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, paging bool, filter ...Filter) (totals testkube.ExecutionsTotals, err error) {
var result []struct {
Status string `bson:"_id"`
Count int32 `bson:"count"`
Expand All @@ -73,8 +73,10 @@ func (r *MongoRepository) GetExecutionTotals(ctx context.Context, filter ...Filt
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())}})
if paging {
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}}}}}})
Expand Down
14 changes: 7 additions & 7 deletions internal/pkg/api/repository/result/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestStorage(t *testing.T) {
})

t.Run("filter with status should return only totals with that status", func(t *testing.T) {
filteredTotals, err := repository.GetExecutionTotals(context.Background(), NewExecutionsFilter().WithStatus(testkube.ERROR__ExecutionStatus))
filteredTotals, err := repository.GetExecutionTotals(context.Background(), false, NewExecutionsFilter().WithStatus(testkube.ERROR__ExecutionStatus))

assert.NoError(err)
assert.Equal(int32(12), filteredTotals.Results)
Expand All @@ -97,7 +97,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())
totals, err := repository.GetExecutionTotals(context.Background(), false)

assert.NoError(err)
assert.Equal(int32(21), totals.Results)
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestStorage(t *testing.T) {
})

t.Run("getting totals with filter by date start date should return only the results after this date", func(t *testing.T) {
totals, err := repository.GetExecutionTotals(context.Background(), NewExecutionsFilter().WithStartDate(dateFilter.Start))
totals, err := repository.GetExecutionTotals(context.Background(), false, NewExecutionsFilter().WithStartDate(dateFilter.Start))

assert.NoError(err)
assert.Equal(int32(14), totals.Results)
Expand All @@ -147,7 +147,7 @@ func TestStorage(t *testing.T) {
})

t.Run("getting totals with filter by date start date should return only the results before this date", func(t *testing.T) {
totals, err := repository.GetExecutionTotals(context.Background(), NewExecutionsFilter().WithEndDate(dateFilter.End))
totals, err := repository.GetExecutionTotals(context.Background(), false, NewExecutionsFilter().WithEndDate(dateFilter.End))

assert.NoError(err)
assert.Equal(int32(7), totals.Results)
Expand All @@ -165,7 +165,7 @@ func TestStorage(t *testing.T) {
})

t.Run("getting totals with script name that doesn't exist should return 0 results", func(t *testing.T) {
totals, err := repository.GetExecutionTotals(context.Background(), NewExecutionsFilter().WithScriptName("noneExisting"))
totals, err := repository.GetExecutionTotals(context.Background(), false, NewExecutionsFilter().WithScriptName("noneExisting"))

assert.NoError(err)
assert.Equal(int32(0), totals.Results)
Expand All @@ -192,7 +192,7 @@ func TestStorage(t *testing.T) {
WithStartDate(twoDaysAgo).
WithEndDate(oneDayAgo).
WithScriptName(defaultName)
totals, err := repository.GetExecutionTotals(context.Background(), filter)
totals, err := repository.GetExecutionTotals(context.Background(), false, filter)

assert.NoError(err)
assert.Equal(int32(2), totals.Results)
Expand All @@ -215,7 +215,7 @@ func TestStorage(t *testing.T) {
})

t.Run("getting totals with script name should return result only for that script name", func(t *testing.T) {
totals, err := repository.GetExecutionTotals(context.Background(), NewExecutionsFilter().WithScriptName(name))
totals, err := repository.GetExecutionTotals(context.Background(), false, NewExecutionsFilter().WithScriptName(name))

assert.NoError(err)
assert.Equal(int32(1), totals.Results)
Expand Down

0 comments on commit 3be610c

Please sign in to comment.