Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change the method of specifying query conditions #1388

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions pkg/autoops/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1112,16 +1112,28 @@ func (s *AutoOpsService) listAutoOpsRules(
localizer locale.Localizer,
storage v2as.AutoOpsRuleStorage,
) ([]*autoopsproto.AutoOpsRule, string, error) {
whereParts := []mysql.WherePart{
mysql.NewFilter("deleted", "=", false),
mysql.NewFilter("environment_id", "=", environmentId),
filters := []*mysql.FilterV2{
{
Column: "deleted",
Operator: mysql.OperatorEqual,
Value: false,
},
{
Column: "environment_id",
Operator: mysql.OperatorEqual,
Value: environmentId,
},
}
fIDs := make([]interface{}, 0, len(featureIds))
for _, fID := range featureIds {
fIDs = append(fIDs, fID)
}
var infilter *mysql.InFilter
if len(fIDs) > 0 {
whereParts = append(whereParts, mysql.NewInFilter("feature_id", fIDs))
infilter = &mysql.InFilter{
Column: "feature_id",
Values: fIDs,
}
}
limit := int(pageSize)
if cursor == "" {
Expand All @@ -1139,13 +1151,18 @@ func (s *AutoOpsService) listAutoOpsRules(
return nil, "", dt.Err()

}
autoOpsRules, nextCursor, err := storage.ListAutoOpsRules(
ctx,
whereParts,
nil,
limit,
offset,
)
listOptions := &mysql.ListOptions{
Limit: limit,
Offset: offset,
Filters: filters,
InFilter: infilter,
NullFilters: nil,
JSONFilters: nil,
SearchQuery: nil,
Orders: nil,
}

autoOpsRules, nextCursor, err := storage.ListAutoOpsRules(ctx, listOptions)
if err != nil {
s.logger.Error(
"Failed to list autoOpsRules",
Expand Down
37 changes: 25 additions & 12 deletions pkg/autoops/api/progressive_rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,12 @@ func (s *AutoOpsService) listProgressiveRollouts(
req *autoopsproto.ListProgressiveRolloutsRequest,
localizer locale.Localizer,
) ([]*autoopsproto.ProgressiveRollout, int64, int, error) {
whereParts := []mysql.WherePart{
mysql.NewFilter("environment_id", "=", req.EnvironmentId),
filters := []*mysql.FilterV2{
{
Column: "environment_id",
Operator: mysql.OperatorEqual,
Value: req.EnvironmentId,
},
}
limit := int(req.PageSize)
cursor := req.Cursor
Expand All @@ -628,9 +632,13 @@ func (s *AutoOpsService) listProgressiveRollouts(
}
return nil, 0, 0, dt.Err()
}
var inFilter *mysql.InFilter = nil
if len(req.FeatureIds) > 0 {
fIDs := s.convToInterfaceSlice(req.FeatureIds)
whereParts = append(whereParts, mysql.NewInFilter("feature_id", fIDs))
inFilter = &mysql.InFilter{
Column: "feature_id",
Values: fIDs,
}
}
orders, err := s.newListProgressiveRolloutsOrdersMySQL(
req.OrderBy,
Expand All @@ -648,19 +656,24 @@ func (s *AutoOpsService) listProgressiveRollouts(
return nil, 0, 0, err
}
if req.Type != nil {
whereParts = append(whereParts, mysql.NewFilter("type", "=", req.Type))
filters = append(filters, &mysql.FilterV2{Column: "type", Operator: mysql.OperatorEqual, Value: req.Type})
}
if req.Status != nil {
whereParts = append(whereParts, mysql.NewFilter("status", "=", req.Status))
filters = append(filters, &mysql.FilterV2{Column: "status", Operator: mysql.OperatorEqual, Value: req.Status})
}
listOptions := &mysql.ListOptions{
Filters: filters,
Orders: orders,
InFilter: inFilter,
NullFilters: nil,
JSONFilters: nil,
SearchQuery: nil,
Limit: limit,
Offset: offset,
}

storage := v2as.NewProgressiveRolloutStorage(s.mysqlClient)
progressiveRollouts, totalCount, nextOffset, err := storage.ListProgressiveRollouts(
ctx,
whereParts,
orders,
limit,
offset,
)
progressiveRollouts, totalCount, nextOffset, err := storage.ListProgressiveRollouts(ctx, listOptions)
if err != nil {
s.logger.Error(
"Failed to list progressive rollouts",
Expand Down
25 changes: 21 additions & 4 deletions pkg/autoops/api/stop_progressive_rollout_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,28 @@ func executeStopProgressiveRolloutOperation(
environmentId string,
operation autoopsproto.ProgressiveRollout_StoppedBy,
) error {
whereParts := []mysql.WherePart{
mysql.NewFilter("environment_id", "=", environmentId),
mysql.NewInFilter("feature_id", featureIDs),
filters := []*mysql.FilterV2{
{
Column: "environment_id",
Operator: mysql.OperatorEqual,
Value: environmentId,
},
}
list, _, _, err := storage.ListProgressiveRollouts(ctx, whereParts, nil, 0, 0)
inFilter := &mysql.InFilter{
Column: "feature_id",
Values: featureIDs,
}
listOptions := &mysql.ListOptions{
Filters: filters,
Orders: nil,
InFilter: inFilter,
NullFilters: nil,
JSONFilters: nil,
SearchQuery: nil,
Limit: 0,
Offset: 0,
}
list, _, _, err := storage.ListProgressiveRollouts(ctx, listOptions)
if err != nil {
return err
}
Expand Down
20 changes: 8 additions & 12 deletions pkg/autoops/storage/v2/auto_ops_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
_ "embed"
"errors"
"fmt"

"github.com/bucketeer-io/bucketeer/pkg/autoops/domain"
"github.com/bucketeer-io/bucketeer/pkg/storage/v2/mysql"
Expand Down Expand Up @@ -49,9 +48,7 @@ type AutoOpsRuleStorage interface {
GetAutoOpsRule(ctx context.Context, id, environmentId string) (*domain.AutoOpsRule, error)
ListAutoOpsRules(
ctx context.Context,
whereParts []mysql.WherePart,
orders []*mysql.Order,
limit, offset int,
options *mysql.ListOptions,
) ([]*proto.AutoOpsRule, int, error)
}

Expand Down Expand Up @@ -154,20 +151,15 @@ func (s *autoOpsRuleStorage) GetAutoOpsRule(

func (s *autoOpsRuleStorage) ListAutoOpsRules(
ctx context.Context,
whereParts []mysql.WherePart,
orders []*mysql.Order,
limit, offset int,
options *mysql.ListOptions,
) ([]*proto.AutoOpsRule, int, error) {
whereSQL, whereArgs := mysql.ConstructWhereSQLString(whereParts)
orderBySQL := mysql.ConstructOrderBySQLString(orders)
limitOffsetSQL := mysql.ConstructLimitOffsetSQLString(limit, offset)
query := fmt.Sprintf(selectAutoOpsRulesSQL, whereSQL, orderBySQL, limitOffsetSQL)
query, whereArgs := mysql.ConstructQueryAndWhereArgs(selectAutoOpsRulesSQL, options)
rows, err := s.qe.QueryContext(ctx, query, whereArgs...)
if err != nil {
return nil, 0, err
}
defer rows.Close()
autoOpsRules := make([]*proto.AutoOpsRule, 0, limit)
autoOpsRules := make([]*proto.AutoOpsRule, 0)
for rows.Next() {
autoOpsRule := proto.AutoOpsRule{}
var opsType int32
Expand All @@ -190,6 +182,10 @@ func (s *autoOpsRuleStorage) ListAutoOpsRules(
if rows.Err() != nil {
return nil, 0, err
}
var offset int
if options != nil {
offset = options.Offset
}
nextOffset := offset + len(autoOpsRules)
return autoOpsRules, nextOffset, nil
}
42 changes: 23 additions & 19 deletions pkg/autoops/storage/v2/auto_ops_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,7 @@ func TestListAutoOpsRules(t *testing.T) {
defer mockController.Finish()
patterns := []struct {
setup func(*autoOpsRuleStorage)
whereParts []mysql.WherePart
orders []*mysql.Order
limit int
offset int
listOpts *mysql.ListOptions
expected []*proto.AutoOpsRule
expectedCursor int
expectedErr error
Expand All @@ -203,10 +200,7 @@ func TestListAutoOpsRules(t *testing.T) {
gomock.Any(), gomock.Any(), gomock.Any(),
).Return(nil, errors.New("error"))
},
whereParts: nil,
orders: nil,
limit: 0,
offset: 0,
listOpts: nil,
expected: nil,
expectedCursor: 0,
expectedErr: errors.New("error"),
Expand All @@ -221,14 +215,27 @@ func TestListAutoOpsRules(t *testing.T) {
gomock.Any(), gomock.Any(), gomock.Any(),
).Return(rows, nil)
},
whereParts: []mysql.WherePart{
mysql.NewFilter("num", ">=", 5),
listOpts: &mysql.ListOptions{
Limit: 10,
Offset: 5,
Filters: []*mysql.FilterV2{
{
Column: "num",
Operator: mysql.OperatorGreaterThanOrEqual,
Value: 5,
},
},
InFilter: nil,
NullFilters: nil,
JSONFilters: nil,
SearchQuery: nil,
Orders: []*mysql.Order{
{
Column: "id",
Direction: mysql.OrderDirectionAsc,
},
},
},
orders: []*mysql.Order{
mysql.NewOrder("id", mysql.OrderDirectionAsc),
},
limit: 10,
offset: 5,
expected: []*proto.AutoOpsRule{},
expectedCursor: 5,
expectedErr: nil,
Expand All @@ -241,10 +248,7 @@ func TestListAutoOpsRules(t *testing.T) {
}
autoOpsRules, cursor, err := storage.ListAutoOpsRules(
context.Background(),
p.whereParts,
p.orders,
p.limit,
p.offset,
p.listOpts,
)
assert.Equal(t, p.expected, autoOpsRules)
assert.Equal(t, p.expectedCursor, cursor)
Expand Down
8 changes: 4 additions & 4 deletions pkg/autoops/storage/v2/mock/auto_ops_rule.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/autoops/storage/v2/mock/progressive_rollout.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 9 additions & 13 deletions pkg/autoops/storage/v2/progressive_rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
_ "embed"
"errors"
"fmt"

"github.com/bucketeer-io/bucketeer/pkg/autoops/domain"
"github.com/bucketeer-io/bucketeer/pkg/storage/v2/mysql"
Expand Down Expand Up @@ -61,9 +60,7 @@ type ProgressiveRolloutStorage interface {
DeleteProgressiveRollout(ctx context.Context, id, environmentId string) error
ListProgressiveRollouts(
ctx context.Context,
whereParts []mysql.WherePart,
orders []*mysql.Order,
limit, offset int,
options *mysql.ListOptions,
) ([]*autoopsproto.ProgressiveRollout, int64, int, error)
UpdateProgressiveRollout(ctx context.Context,
progressiveRollout *domain.ProgressiveRollout,
Expand Down Expand Up @@ -158,20 +155,15 @@ func (s *progressiveRolloutStorage) DeleteProgressiveRollout(

func (s *progressiveRolloutStorage) ListProgressiveRollouts(
ctx context.Context,
whereParts []mysql.WherePart,
orders []*mysql.Order,
limit, offset int,
options *mysql.ListOptions,
) ([]*autoopsproto.ProgressiveRollout, int64, int, error) {
whereSQL, whereArgs := mysql.ConstructWhereSQLString(whereParts)
orderBySQL := mysql.ConstructOrderBySQLString(orders)
limitOffsetSQL := mysql.ConstructLimitOffsetSQLString(limit, offset)
query := fmt.Sprintf(selectOpsProgressiveRolloutsSQL, whereSQL, orderBySQL, limitOffsetSQL)
query, whereArgs := mysql.ConstructQueryAndWhereArgs(selectOpsProgressiveRolloutsSQL, options)
rows, err := s.qe.QueryContext(ctx, query, whereArgs...)
if err != nil {
return nil, 0, 0, err
}
defer rows.Close()
progressiveRollouts := make([]*autoopsproto.ProgressiveRollout, 0, limit)
progressiveRollouts := make([]*autoopsproto.ProgressiveRollout, 0)
for rows.Next() {
progressiveRollout := autoopsproto.ProgressiveRollout{}
err := rows.Scan(
Expand All @@ -193,9 +185,13 @@ func (s *progressiveRolloutStorage) ListProgressiveRollouts(
if rows.Err() != nil {
return nil, 0, 0, err
}
var offset int
if options != nil {
offset = options.Offset
}
nextOffset := offset + len(progressiveRollouts)
var totalCount int64
countQuery := fmt.Sprintf(countOpsProgressiveRolloutsSQL, whereSQL)
countQuery, whereArgs := mysql.ConstructQueryAndWhereArgs(countOpsProgressiveRolloutsSQL, options)
err = s.qe.QueryRowContext(ctx, countQuery, whereArgs...).Scan(&totalCount)
if err != nil {
return nil, 0, 0, err
Expand Down
Loading
Loading