Skip to content

Commit

Permalink
Make user-friendly status filter multi-search (#43)
Browse files Browse the repository at this point in the history
* Upgrade Gorm version

* API changes for searching status_friendly using multiple values

* Make experiment status filter multiselect

* Refactor existing query with OR clause

* Add comments about the need for AfterFind hook

* Add inline comments

Co-authored-by: Krithika Sundararajan <krithika.sundararajan@go-jek.com>
  • Loading branch information
krithika369 and Krithika Sundararajan committed Oct 18, 2022
1 parent 48efa46 commit 87dd5d3
Show file tree
Hide file tree
Showing 34 changed files with 325 additions and 224 deletions.
4 changes: 3 additions & 1 deletion api/experiments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ paths:
is supplied, the status, start_time and end_time filters can also be set. However, the final
result would be an intersection of the application of each of these filters.
schema:
$ref: 'schema.yaml#/components/schemas/ExperimentStatusFriendly'
type: array
items:
$ref: 'schema.yaml#/components/schemas/ExperimentStatusFriendly'
- name: end_time
description: Used together with the start_time, to filter experiments that are at least partially running in the input range.
in: query
Expand Down
2 changes: 1 addition & 1 deletion clients/management/managementclient.go

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

58 changes: 29 additions & 29 deletions management-service/api/api.go

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

2 changes: 1 addition & 1 deletion management-service/appcontext/appcontext.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package appcontext

import (
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
"gorm.io/gorm"

"github.com/caraml-dev/xp/management-service/config"
mw "github.com/caraml-dev/xp/management-service/middleware"
Expand Down
2 changes: 1 addition & 1 deletion management-service/appcontext/appcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"testing"

"bou.ke/monkey"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm"

"github.com/caraml-dev/xp/management-service/config"
mw "github.com/caraml-dev/xp/management-service/middleware"
Expand Down
7 changes: 4 additions & 3 deletions management-service/controller/experiment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,11 @@ func (e ExperimentController) toListExperimentParams(params api.ListExperimentsP
val := models.ExperimentStatus(*params.Status)
status = &val
}
var statusFriendly *services.ExperimentStatusFriendly
statusFriendly := []services.ExperimentStatusFriendly{}
if params.StatusFriendly != nil {
val := services.ExperimentStatusFriendly(*params.StatusFriendly)
statusFriendly = &val
for _, val := range *params.StatusFriendly {
statusFriendly = append(statusFriendly, services.ExperimentStatusFriendly(val))
}
}
var expTier *models.ExperimentTier
if params.Tier != nil {
Expand Down
17 changes: 12 additions & 5 deletions management-service/controller/experiment_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,25 @@ func (s *ExperimentControllerTestSuite) SetupSuite() {
updatedBy := "test-user"
expSvc.
On("ListExperiments", int64(3), services.ListExperimentsParams{
Status: emptyStatus, Type: emptyType, Segment: models.ExperimentSegment{},
Status: emptyStatus,
StatusFriendly: []services.ExperimentStatusFriendly{},
Type: emptyType,
Segment: models.ExperimentSegment{},
}).Return(nil, nil, fmt.Errorf("unexpected error"))
expSvc.
On("ListExperiments", int64(2), services.ListExperimentsParams{
Status: emptyStatus, Type: emptyType, Segment: models.ExperimentSegment{"days_of_week": []string{"1"}},
Status: emptyStatus,
StatusFriendly: []services.ExperimentStatusFriendly{},
Type: emptyType,
Segment: models.ExperimentSegment{"days_of_week": []string{"1"}},
}).Return([]*models.Experiment{testExperiment}, nil, nil)
expSvc.
On("ListExperiments", int64(2),
services.ListExperimentsParams{
Status: emptyStatus,
Type: emptyType,
Segment: models.ExperimentSegment{}}).
Status: emptyStatus,
StatusFriendly: []services.ExperimentStatusFriendly{},
Type: emptyType,
Segment: models.ExperimentSegment{}}).
Return([]*models.Experiment{testExperiment}, nil, nil)
expSvc.
On("CreateExperiment",
Expand Down
2 changes: 1 addition & 1 deletion management-service/controller/segmenter_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"net/http/httptest"
"testing"

"github.com/jinzhu/gorm"
"github.com/stretchr/testify/suite"
"gorm.io/gorm"

"github.com/caraml-dev/xp/common/api/schema"
_segmenters "github.com/caraml-dev/xp/common/segmenters"
Expand Down
24 changes: 17 additions & 7 deletions management-service/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ package database
import (
"errors"
"fmt"
"time"

gomigrate "github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/jinzhu/gorm"

// Gorm requires this for interfacing with the postgres DB
_ "github.com/jinzhu/gorm/dialects/postgres"
pg "gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"

"github.com/caraml-dev/xp/management-service/config"
)

var UtcLoc, _ = time.LoadLocation("UTC")

func ConnectionString(cfg *config.DatabaseConfig) string {
return fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s sslmode=disable timezone=UTC",
return fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s sslmode=disable TimeZone=UTC",
cfg.Host,
cfg.Port,
cfg.User,
Expand All @@ -25,7 +27,10 @@ func ConnectionString(cfg *config.DatabaseConfig) string {
}

func Open(cfg *config.DatabaseConfig) (*gorm.DB, error) {
return gorm.Open("postgres", ConnectionString(cfg))
return gorm.Open(pg.Open(ConnectionString(cfg)),
&gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
}

func Migrate(cfg *config.DatabaseConfig) error {
Expand All @@ -34,7 +39,12 @@ func Migrate(cfg *config.DatabaseConfig) error {
return err
}

driver, err := postgres.WithInstance(db.DB(), &postgres.Config{})
sqlDB, err := db.DB()
if err != nil {
return err
}

driver, err := postgres.WithInstance(sqlDB, &postgres.Config{})
if err != nil {
return err
}
Expand Down
26 changes: 18 additions & 8 deletions management-service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ require (
github.com/golang/geo v0.0.0-20210211234256-740aa86cb551
github.com/google/go-cmp v0.5.6
github.com/heptiolabs/healthcheck v0.0.0-20180807145615-6ff867650f40
github.com/jinzhu/gorm v1.9.16
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/rs/cors v1.7.0
github.com/spf13/cobra v1.1.3
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.8.0
github.com/testcontainers/testcontainers-go v0.11.0
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
google.golang.org/api v0.30.0
google.golang.org/protobuf v1.26.0-rc.1
gorm.io/driver/postgres v1.4.4
gorm.io/gorm v1.24.0
)

require (
Expand Down Expand Up @@ -80,10 +81,19 @@ require (
github.com/huandu/xstrings v1.3.1 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jackc/pgx/v4 v4.17.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lib/pq v1.8.0 // indirect
github.com/lib/pq v1.10.2 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/mailru/easyjson v0.7.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
Expand Down Expand Up @@ -112,24 +122,24 @@ require (
github.com/spf13/jwalterweatherman v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.1 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/objx v0.4.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
go.mongodb.org/mongo-driver v1.1.2 // indirect
go.opencensus.io v0.22.4 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/crypto v0.0.0-20221010152910-d6f0a8c073c2 // indirect
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 // indirect
golang.org/x/mod v0.3.0 // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/genproto v0.0.0-20201110150050-8816d57aaa9a // indirect
google.golang.org/grpc v1.33.2 // indirect
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace (
Expand Down
Loading

0 comments on commit 87dd5d3

Please sign in to comment.