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 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
23 changes: 15 additions & 8 deletions pkg/autoops/storage/v2/auto_ops_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,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,13 +152,22 @@ 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) {
var whereParts []mysql.WherePart = []mysql.WherePart{}
var orderBySQL string = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zero value of string and int are "" and 0 already, so I think there won't be necessary to declare the value here! But if you want it to be clear for everyone, I think it's ok to keep it, I want to know your opinion 😅

var limitOffsetSQL string = ""
var limit int = 0
var offset int = 0
if options != nil {
whereParts = options.CreateWhereParts()
orderBySQL = mysql.ConstructOrderBySQLString(options.Orders)
limitOffsetSQL = mysql.ConstructLimitOffsetSQLString(options.Limit, options.Offset)
limit = options.Limit
offset = options.Offset
}

whereSQL, whereArgs := mysql.ConstructWhereSQLString(whereParts)
orderBySQL := mysql.ConstructOrderBySQLString(orders)
limitOffsetSQL := mysql.ConstructLimitOffsetSQLString(limit, offset)
query := fmt.Sprintf(selectAutoOpsRulesSQL, whereSQL, orderBySQL, limitOffsetSQL)
rows, err := s.qe.QueryContext(ctx, query, whereArgs...)
if err != nil {
Expand Down
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.

23 changes: 15 additions & 8 deletions pkg/autoops/storage/v2/progressive_rollout.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,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,13 +156,22 @@ 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) {
var whereParts []mysql.WherePart = []mysql.WherePart{}
var orderBySQL string = ""
var limitOffsetSQL string = ""
var limit int = 0
var offset int = 0
if options != nil {
whereParts = options.CreateWhereParts()
orderBySQL = mysql.ConstructOrderBySQLString(options.Orders)
limitOffsetSQL = mysql.ConstructLimitOffsetSQLString(options.Limit, options.Offset)
limit = options.Limit
offset = options.Offset
}

whereSQL, whereArgs := mysql.ConstructWhereSQLString(whereParts)
orderBySQL := mysql.ConstructOrderBySQLString(orders)
limitOffsetSQL := mysql.ConstructLimitOffsetSQLString(limit, offset)
query := fmt.Sprintf(selectOpsProgressiveRolloutsSQL, whereSQL, orderBySQL, limitOffsetSQL)
rows, err := s.qe.QueryContext(ctx, query, whereArgs...)
if err != nil {
Expand Down
Loading
Loading